Subversion Repositories SmartDukaan

Rev

Rev 23059 | Rev 23061 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22980 ashik.ali 1
package com.spice.profitmandi.service.user;
2
 
23060 ashik.ali 3
import java.util.ArrayList;
22980 ashik.ali 4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.function.Function;
9
import java.util.stream.Collectors;
10
 
23043 ashik.ali 11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
22980 ashik.ali 13
import org.springframework.beans.factory.annotation.Autowired;
23043 ashik.ali 14
import org.springframework.beans.factory.annotation.Qualifier;
22980 ashik.ali 15
import org.springframework.stereotype.Component;
16
 
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23025 ashik.ali 18
import com.spice.profitmandi.common.model.CustomAddress;
19
import com.spice.profitmandi.common.model.CustomShop;
20
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
22980 ashik.ali 21
import com.spice.profitmandi.dao.entity.dtr.Retailer;
23043 ashik.ali 22
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
22980 ashik.ali 23
import com.spice.profitmandi.dao.entity.dtr.Shop;
24
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
25
import com.spice.profitmandi.dao.entity.dtr.User;
26
import com.spice.profitmandi.dao.entity.dtr.UserRole;
27
import com.spice.profitmandi.dao.entity.user.Address;
23043 ashik.ali 28
import com.spice.profitmandi.dao.entity.user.Cart;
22980 ashik.ali 29
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
30
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
31
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
32
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
33
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
34
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
35
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
36
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
37
import com.spice.profitmandi.dao.repository.user.AddressRepository;
23043 ashik.ali 38
import com.spice.profitmandi.dao.repository.user.CartRepository;
22980 ashik.ali 39
 
23043 ashik.ali 40
import in.shop2020.model.v1.user.CartStatus;
41
 
22980 ashik.ali 42
@Component
43
public class RetailerServiceImpl implements RetailerService {
23043 ashik.ali 44
 
45
	private static final Logger LOGGER = LoggerFactory.getLogger(RetailerServiceImpl.class);
46
 
22980 ashik.ali 47
	@Autowired
48
	private RetailerRepository retailerRepository;
49
 
50
	@Autowired
51
	private UserAccountRepository userAccountRepository;
52
 
53
	@Autowired
54
	private UserRepository userRepository;
55
 
56
	@Autowired
23043 ashik.ali 57
	private CartRepository cartRepository;
58
 
59
	@Autowired
22980 ashik.ali 60
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
61
 
62
	@Autowired
63
	private AddressRepository addressRepository;
64
 
65
	@Autowired
66
	private ShopRepository shopRepository;
67
 
68
	@Autowired
69
	private ShopAddressRepository shopAddressRepository;
70
 
71
	@Autowired
72
	private UserRoleRepository userRoleRepository;
73
 
74
	@Autowired
75
	private DocumentRepository documentRepository;
76
 
23043 ashik.ali 77
	@Autowired
78
	@Qualifier("userUserRepository")
79
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
80
 
22980 ashik.ali 81
	@Override
82
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
83
			throws ProfitMandiBusinessException {
84
		User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
85
		List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
86
		Map<String, Object> map = new HashMap<>();
87
		map.put("user", user);
23043 ashik.ali 88
		//map.put("retailer", retailer);
89
 
90
 
22980 ashik.ali 91
		map.put("userRoles", this.toString(userRoles));
23043 ashik.ali 92
		try{
93
			int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
94
			Retailer retailer = retailerRepository.selectById(retailerId);
95
 
96
			map.put("retailer", retailer);
97
			try{
98
				int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
99
				Address retailerAddress = addressRepository.selectById(retailerAddressId);
100
				map.put("retailerAddress", retailerAddress);
101
				List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
102
				map.put("shops", shops);
103
				this.addAddress(shops);
104
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
105
				LOGGER.error("Retailer Registered Address not found");
106
			}
107
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
108
			LOGGER.error("Retailer not found");
109
		}
110
 
22980 ashik.ali 111
		return map;
112
	}
113
 
114
	@SuppressWarnings("unchecked")
115
	@Override
23025 ashik.ali 116
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
117
			throws ProfitMandiBusinessException {
118
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
23043 ashik.ali 119
		User user = (User)map.get("user");
23025 ashik.ali 120
		Retailer retailer = (Retailer)map.get("retailer");
23059 ashik.ali 121
		retailer = this.updateRetailer(user.getEmailId(), retailer, updateRetailerRequest);
23025 ashik.ali 122
		Address retailerAddress = (Address)map.get("retailerAddress");
23059 ashik.ali 123
		retailerAddress = this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(), retailer.getId());
23025 ashik.ali 124
		List<Shop> shops = (List<Shop>)map.get("shops");
23060 ashik.ali 125
		if(shops == null){
126
			shops = new ArrayList<>();
127
		}
23043 ashik.ali 128
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId());
23025 ashik.ali 129
		return map;
130
	}
131
 
23043 ashik.ali 132
	private int createSaholicUser(String emailId, String retailerName){
133
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
134
		try {
135
			saholicUser = userUserRepository.selectByEmailId(emailId);
136
		}catch (ProfitMandiBusinessException e) {
137
			LOGGER.info("User doesnt exist in old system");
138
		}
139
		if(saholicUser == null){
140
			Cart cart = new Cart();
141
			cart.setCartStatus(CartStatus.ACTIVE);
142
			cartRepository.persist(cart);
143
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
144
			saholicUser.setEmailId(emailId);
145
			saholicUser.setName(retailerName);
146
			saholicUser.setActiveCartId(cart.getId());
147
			userUserRepository.persist(saholicUser);
148
		}
149
		return saholicUser.getId();
150
	}
151
 
23059 ashik.ali 152
	private Retailer updateRetailer(String emailId, Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
23043 ashik.ali 153
		if(retailer == null){
154
			int saholicUserId = this.createSaholicUser(emailId, updateRetailerRequest.getName());
155
			retailer = new Retailer();
156
			retailer.setId(saholicUserId);
157
		}
23025 ashik.ali 158
		retailer.setName(updateRetailerRequest.getName());
159
		retailer.setNumber(updateRetailerRequest.getNumber());
160
		if(updateRetailerRequest.getDocumentId() > 0){
161
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
162
		}
163
		retailerRepository.persist(retailer);
23059 ashik.ali 164
		return retailer;
23025 ashik.ali 165
	}
166
 
23059 ashik.ali 167
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{
23043 ashik.ali 168
		if(address == null){
169
			address = new Address();
170
			address.setRetaierId(retailerId);
171
			this.updateAddress(address, customAddress);
172
			//addressRepository.persist(addressRetailer);
173
 
174
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
175
			retailerRegisteredAddress.setRetailerId(retailerId);
176
			retailerRegisteredAddress.setAddressId(address.getId());
177
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
178
		}
23059 ashik.ali 179
		return address;
23043 ashik.ali 180
	}
181
 
23025 ashik.ali 182
	private void updateAddress(Address address, CustomAddress customAddress){
183
		address.setName(customAddress.getName());
184
		address.setLine1(customAddress.getLine1());
185
		address.setLine2(customAddress.getLine2());
186
		address.setCity(customAddress.getCity());
187
		address.setPinCode(customAddress.getPinCode());
188
		address.setState(customAddress.getState());
189
		addressRepository.persist(address);
190
	}
191
 
23043 ashik.ali 192
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId) throws ProfitMandiBusinessException{
193
		if(shops.isEmpty()){
23025 ashik.ali 194
			for(CustomShop customShop : customShops){
23043 ashik.ali 195
				Shop shop = new Shop();
196
				this.createOrUpdateShop(shop, customShop, retailerId);
197
				shops.add(shop);
198
			}
199
		}else{
200
			for(Shop shop : shops){
201
				for(CustomShop customShop : customShops){
202
					if(shop.getId() == customShop.getShopId()){
203
						this.createOrUpdateShop(shop, customShop, retailerId);
23025 ashik.ali 204
					}
205
				}
206
			}
23043 ashik.ali 207
			for(CustomShop customShop : customShops){
208
				if(customShop.getShopId() == 0){
209
					Shop shop = new Shop();
210
					this.createOrUpdateShop(shop, customShop, retailerId);
211
					shops.add(shop);
212
				}
213
			}
23025 ashik.ali 214
		}
215
	}
216
 
23043 ashik.ali 217
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId) throws ProfitMandiBusinessException{
218
		shop.setRetailerId(retailerId);
219
		shop.setName(customShop.getName());
220
		if(customShop.getDocumentId() > 0){
221
			shop.setDocumentId(customShop.getDocumentId());
222
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
223
		}
224
		if(shop.getAddressId() == null){
225
			//shop.setDocumentId(customShop.getDocumentId());
226
			Address address = new Address();
227
			this.updateAddress(address, customShop.getAddress());
228
			shop.setAddressId(address.getId());
229
			shop.setAddress(address);
230
			shopRepository.persist(shop);
231
			ShopAddress shopAddress = new ShopAddress();
232
			shopAddress.setAddressId(address.getId());
233
			shopAddress.setShopId(shop.getId());
234
			shopAddressRepository.persist(shopAddress);
235
		}else{
236
			Address address = addressRepository.selectById(shop.getAddressId());
237
			this.updateAddress(address, customShop.getAddress());
238
			shop.setAddress(address);
239
			shopRepository.persist(shop);
240
		}
241
 
242
	}
23025 ashik.ali 243
 
23043 ashik.ali 244
 
22980 ashik.ali 245
	private void addAddress(List<Shop> shops){
246
		Set<Integer> shopIds = this.toShopIds(shops);
247
		if(shopIds.isEmpty()){
248
			return;
249
		}
250
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
251
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
252
 
253
		for(Shop shop : shops){
254
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
255
		}
256
	}
257
 
258
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
259
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
260
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
261
		List<Address> addresses = addressRepository.selectByIds(addressIds);
262
		for(Address address : addresses){
263
			addressIdAddressMap.put(address.getId(), address);
264
		}
265
		return addressIdAddressMap;
266
	}
267
 
268
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
269
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
270
			@Override
271
			public Integer apply(ShopAddress shopAddress) {
272
				return shopAddress.getAddressId();
273
			}
274
		};
275
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
276
	}
277
 
278
	private Set<Integer> toShopIds(List<Shop> shops){
279
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
280
			@Override
281
			public Integer apply(Shop shop) {
282
				return shop.getId();
283
			}
284
		};
285
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
286
	}
287
 
288
	private String toString(List<UserRole> userRoles){
289
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
290
			public String apply(UserRole userRole) {
291
				return userRole.getRoleType().toString();
292
			};
293
		};
294
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
295
		return String.join(", ", userRoleStrings);
296
	}
297
 
298
}