Subversion Repositories SmartDukaan

Rev

Rev 23269 | Rev 23329 | 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
 
23300 amit.gupta 3
import java.time.LocalDateTime;
23060 ashik.ali 4
import java.util.ArrayList;
22980 ashik.ali 5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Set;
9
import java.util.function.Function;
10
import java.util.stream.Collectors;
11
 
23043 ashik.ali 12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
22980 ashik.ali 14
import org.springframework.beans.factory.annotation.Autowired;
23043 ashik.ali 15
import org.springframework.beans.factory.annotation.Qualifier;
22980 ashik.ali 16
import org.springframework.stereotype.Component;
17
 
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23025 ashik.ali 19
import com.spice.profitmandi.common.model.CustomAddress;
20
import com.spice.profitmandi.common.model.CustomShop;
21
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
22980 ashik.ali 22
import com.spice.profitmandi.dao.entity.dtr.Retailer;
23043 ashik.ali 23
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
22980 ashik.ali 24
import com.spice.profitmandi.dao.entity.dtr.Shop;
25
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
26
import com.spice.profitmandi.dao.entity.dtr.User;
23269 ashik.ali 27
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
22980 ashik.ali 28
import com.spice.profitmandi.dao.entity.dtr.UserRole;
29
import com.spice.profitmandi.dao.entity.user.Address;
23043 ashik.ali 30
import com.spice.profitmandi.dao.entity.user.Cart;
23063 ashik.ali 31
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
23106 ashik.ali 32
import com.spice.profitmandi.dao.enumuration.dtr.RetailerType;
23136 ashik.ali 33
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
22980 ashik.ali 34
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
35
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
36
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
37
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
38
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
39
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
40
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
41
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
42
import com.spice.profitmandi.dao.repository.user.AddressRepository;
23043 ashik.ali 43
import com.spice.profitmandi.dao.repository.user.CartRepository;
22980 ashik.ali 44
 
23043 ashik.ali 45
import in.shop2020.model.v1.user.CartStatus;
46
 
22980 ashik.ali 47
@Component
48
public class RetailerServiceImpl implements RetailerService {
23043 ashik.ali 49
 
50
	private static final Logger LOGGER = LoggerFactory.getLogger(RetailerServiceImpl.class);
51
 
22980 ashik.ali 52
	@Autowired
53
	private RetailerRepository retailerRepository;
54
 
55
	@Autowired
56
	private UserAccountRepository userAccountRepository;
57
 
58
	@Autowired
59
	private UserRepository userRepository;
60
 
61
	@Autowired
23043 ashik.ali 62
	private CartRepository cartRepository;
63
 
64
	@Autowired
22980 ashik.ali 65
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
66
 
67
	@Autowired
68
	private AddressRepository addressRepository;
69
 
70
	@Autowired
71
	private ShopRepository shopRepository;
72
 
73
	@Autowired
74
	private ShopAddressRepository shopAddressRepository;
75
 
76
	@Autowired
77
	private UserRoleRepository userRoleRepository;
78
 
79
	@Autowired
80
	private DocumentRepository documentRepository;
81
 
23043 ashik.ali 82
	@Autowired
83
	@Qualifier("userUserRepository")
84
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
85
 
22980 ashik.ali 86
	@Override
87
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
88
			throws ProfitMandiBusinessException {
23202 ashik.ali 89
		User user = null;
90
		try{
91
			user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
92
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
93
 
94
		}
95
		if(user == null){
96
			user = userRepository.selectBySecondryEmailId(emailIdOrMobileNumber);
97
		}
22980 ashik.ali 98
		List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
99
		Map<String, Object> map = new HashMap<>();
100
		map.put("user", user);
23043 ashik.ali 101
		//map.put("retailer", retailer);
102
 
103
 
22980 ashik.ali 104
		map.put("userRoles", this.toString(userRoles));
23043 ashik.ali 105
		try{
106
			int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
107
			Retailer retailer = retailerRepository.selectById(retailerId);
108
 
109
			map.put("retailer", retailer);
110
			try{
111
				int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
112
				Address retailerAddress = addressRepository.selectById(retailerAddressId);
113
				map.put("retailerAddress", retailerAddress);
114
				List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
115
				map.put("shops", shops);
116
				this.addAddress(shops);
117
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
118
				LOGGER.error("Retailer Registered Address not found");
119
			}
120
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
23062 ashik.ali 121
			LOGGER.error("Retailer not found in user_account");
23043 ashik.ali 122
		}
123
 
22980 ashik.ali 124
		return map;
125
	}
126
 
127
	@SuppressWarnings("unchecked")
128
	@Override
23025 ashik.ali 129
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
130
			throws ProfitMandiBusinessException {
131
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
23043 ashik.ali 132
		User user = (User)map.get("user");
23025 ashik.ali 133
		Retailer retailer = (Retailer)map.get("retailer");
23061 ashik.ali 134
		if(retailer == null){
23063 ashik.ali 135
			retailer = this.updateRetailer(user, retailer, updateRetailerRequest);
23061 ashik.ali 136
			map.put("retailer", retailer);
137
		}
23025 ashik.ali 138
		Address retailerAddress = (Address)map.get("retailerAddress");
23061 ashik.ali 139
		if(retailerAddress == null){
140
			retailerAddress = this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(), retailer.getId());
141
			map.put("retailerAddress", retailerAddress);
142
		}
23025 ashik.ali 143
		List<Shop> shops = (List<Shop>)map.get("shops");
23060 ashik.ali 144
		if(shops == null){
145
			shops = new ArrayList<>();
23061 ashik.ali 146
			map.put("shops", shops);
23060 ashik.ali 147
		}
23079 ashik.ali 148
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId(), retailerAddress);
23025 ashik.ali 149
		return map;
150
	}
151
 
23063 ashik.ali 152
	private int createSaholicUser(User user, String retailerName){
23043 ashik.ali 153
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
154
		try {
23063 ashik.ali 155
			saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
23043 ashik.ali 156
		}catch (ProfitMandiBusinessException e) {
157
			LOGGER.info("User doesnt exist in old system");
158
		}
159
		if(saholicUser == null){
160
			Cart cart = new Cart();
161
			cart.setCartStatus(CartStatus.ACTIVE);
162
			cartRepository.persist(cart);
163
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
23063 ashik.ali 164
			saholicUser.setEmailId(user.getEmailId());
23043 ashik.ali 165
			saholicUser.setName(retailerName);
166
			saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 167
			saholicUser.setCreateTimestamp(LocalDateTime.now());
23043 ashik.ali 168
			userUserRepository.persist(saholicUser);
23063 ashik.ali 169
 
23269 ashik.ali 170
			UserAccount ua = new UserAccount();
171
			ua.setAccountKey(saholicUser.getId());
172
			ua.setUserId(user.getId());
173
			ua.setType(AccountType.saholic);
23063 ashik.ali 174
			userAccountRepository.persist(ua);
175
 
23269 ashik.ali 176
			UserAccount ua2 = new UserAccount();
177
			ua2.setAccountKey(saholicUser.getActiveCartId());
178
			ua2.setUserId(user.getId());
179
			ua2.setType(AccountType.cartId);
23063 ashik.ali 180
			userAccountRepository.persist(ua2);
23043 ashik.ali 181
		}
182
		return saholicUser.getId();
183
	}
184
 
23063 ashik.ali 185
	private Retailer updateRetailer(User user, Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
23043 ashik.ali 186
		if(retailer == null){
23063 ashik.ali 187
			int saholicUserId = this.createSaholicUser(user, updateRetailerRequest.getName());
23043 ashik.ali 188
			retailer = new Retailer();
189
			retailer.setId(saholicUserId);
190
		}
23137 ashik.ali 191
		this.createRole(user.getId(), RoleType.RETAILER);
23025 ashik.ali 192
		retailer.setName(updateRetailerRequest.getName());
193
		retailer.setNumber(updateRetailerRequest.getNumber());
23107 ashik.ali 194
		if(updateRetailerRequest.getNumber() == null || updateRetailerRequest.getNumber().isEmpty()){
195
			retailer.setType(RetailerType.UNREGISTERED_SHOP);
196
		}else{
197
			retailer.setType(RetailerType.GSTIN);
198
		}
23025 ashik.ali 199
		if(updateRetailerRequest.getDocumentId() > 0){
200
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
201
		}
202
		retailerRepository.persist(retailer);
23059 ashik.ali 203
		return retailer;
23025 ashik.ali 204
	}
205
 
23137 ashik.ali 206
	private void createRole(int userId, RoleType roleType){
207
		if(!userRoleRepository.isExistByUserIdAndType(userId, roleType)){
208
			UserRole userRole = new UserRole();
209
			userRole.setUserId(userId);
210
			userRole.setRoleType(roleType);
211
			try{
212
				userRoleRepository.persist(userRole);
213
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
214
				LOGGER.error("Retailer role is already exist with userId {}", userId);
215
			}
216
		}else{
217
			LOGGER.error("Retailer role not found with userId {}", userId);
218
		}
219
 
220
	}
221
 
23059 ashik.ali 222
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{
23043 ashik.ali 223
		if(address == null){
224
			address = new Address();
225
			address.setRetaierId(retailerId);
226
			this.updateAddress(address, customAddress);
227
			//addressRepository.persist(addressRetailer);
228
 
229
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
230
			retailerRegisteredAddress.setRetailerId(retailerId);
231
			retailerRegisteredAddress.setAddressId(address.getId());
232
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
233
		}
23059 ashik.ali 234
		return address;
23043 ashik.ali 235
	}
236
 
23025 ashik.ali 237
	private void updateAddress(Address address, CustomAddress customAddress){
238
		address.setName(customAddress.getName());
239
		address.setLine1(customAddress.getLine1());
240
		address.setLine2(customAddress.getLine2());
241
		address.setCity(customAddress.getCity());
242
		address.setPinCode(customAddress.getPinCode());
243
		address.setState(customAddress.getState());
244
		addressRepository.persist(address);
245
	}
246
 
23079 ashik.ali 247
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 248
		if(shops.isEmpty()){
23025 ashik.ali 249
			for(CustomShop customShop : customShops){
23043 ashik.ali 250
				Shop shop = new Shop();
23079 ashik.ali 251
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 252
				shops.add(shop);
253
			}
254
		}else{
255
			for(Shop shop : shops){
256
				for(CustomShop customShop : customShops){
257
					if(shop.getId() == customShop.getShopId()){
23079 ashik.ali 258
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 259
					}
260
				}
261
			}
23043 ashik.ali 262
			for(CustomShop customShop : customShops){
263
				if(customShop.getShopId() == 0){
264
					Shop shop = new Shop();
23079 ashik.ali 265
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 266
					shops.add(shop);
267
				}
268
			}
23025 ashik.ali 269
		}
270
	}
271
 
23079 ashik.ali 272
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 273
		shop.setRetailerId(retailerId);
274
		shop.setName(customShop.getName());
275
		if(customShop.getDocumentId() > 0){
276
			shop.setDocumentId(customShop.getDocumentId());
277
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
278
		}
279
		if(shop.getAddressId() == null){
23079 ashik.ali 280
			Address address = null;
281
			if(customShop.isSameAsRetailerAddress()){
282
				address = sameAsRetailerAddressValue;
283
			}else{
284
				//shop.setDocumentId(customShop.getDocumentId());
285
				address = new Address();
286
				this.updateAddress(address, customShop.getAddress());
287
			}
23043 ashik.ali 288
			shop.setAddressId(address.getId());
289
			shop.setAddress(address);
290
			shopRepository.persist(shop);
23131 ashik.ali 291
			ShopAddress shopAddress = null;
292
			try{
293
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
294
				shopAddress.setAddressId(address.getId());
295
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
296
				shopAddress = new ShopAddress();
297
				shopAddress.setAddressId(address.getId());
298
				shopAddress.setShopId(shop.getId());
299
			}
23043 ashik.ali 300
			shopAddressRepository.persist(shopAddress);
301
		}else{
302
			Address address = addressRepository.selectById(shop.getAddressId());
23079 ashik.ali 303
			CustomAddress customAddress = customShop.getAddress();
304
			if(address.getName().equals(customAddress.getName()) &&
305
				address.getLine1().equals(customAddress.getLine1()) &&
306
				address.getLine2().equals(customAddress.getLine2())	&&
307
				address.getCity().equals(customAddress.getCity()) &&
308
				address.getPinCode().equals(customAddress.getPinCode()) &&
309
				address.getState().equals(customAddress.getState())){
310
				this.updateAddress(address, customShop.getAddress());
311
			}else{
312
				address = new Address();
313
				ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
314
				this.updateAddress(address, customAddress);
315
				shopAddress.setAddressId(address.getId());
316
				shopAddressRepository.persist(shopAddress);
317
			}
318
 
23043 ashik.ali 319
			shop.setAddress(address);
320
			shopRepository.persist(shop);
321
		}
322
 
323
	}
23025 ashik.ali 324
 
23043 ashik.ali 325
 
22980 ashik.ali 326
	private void addAddress(List<Shop> shops){
327
		Set<Integer> shopIds = this.toShopIds(shops);
328
		if(shopIds.isEmpty()){
329
			return;
330
		}
331
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
332
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
333
 
334
		for(Shop shop : shops){
335
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
336
		}
337
	}
338
 
339
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
340
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
341
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
342
		List<Address> addresses = addressRepository.selectByIds(addressIds);
343
		for(Address address : addresses){
344
			addressIdAddressMap.put(address.getId(), address);
345
		}
346
		return addressIdAddressMap;
347
	}
348
 
349
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
350
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
351
			@Override
352
			public Integer apply(ShopAddress shopAddress) {
353
				return shopAddress.getAddressId();
354
			}
355
		};
356
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
357
	}
358
 
359
	private Set<Integer> toShopIds(List<Shop> shops){
360
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
361
			@Override
362
			public Integer apply(Shop shop) {
363
				return shop.getId();
364
			}
365
		};
366
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
367
	}
368
 
369
	private String toString(List<UserRole> userRoles){
370
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
371
			public String apply(UserRole userRole) {
372
				return userRole.getRoleType().toString();
373
			};
374
		};
375
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
376
		return String.join(", ", userRoleStrings);
377
	}
378
 
379
}