Subversion Repositories SmartDukaan

Rev

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