Subversion Repositories SmartDukaan

Rev

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