Subversion Repositories SmartDukaan

Rev

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