Subversion Repositories SmartDukaan

Rev

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