Subversion Repositories SmartDukaan

Rev

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