Subversion Repositories SmartDukaan

Rev

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