Subversion Repositories SmartDukaan

Rev

Rev 23043 | Rev 23059 | 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
	@SuppressWarnings("unchecked")
114
	@Override
23025 ashik.ali 115
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
116
			throws ProfitMandiBusinessException {
117
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
23043 ashik.ali 118
		User user = (User)map.get("user");
23025 ashik.ali 119
		Retailer retailer = (Retailer)map.get("retailer");
23043 ashik.ali 120
		this.updateRetailer(user.getEmailId(), retailer, updateRetailerRequest);
23025 ashik.ali 121
		Address retailerAddress = (Address)map.get("retailerAddress");
23043 ashik.ali 122
		this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(), retailer.getId());
23025 ashik.ali 123
		List<Shop> shops = (List<Shop>)map.get("shops");
23043 ashik.ali 124
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId());
23025 ashik.ali 125
		return map;
126
	}
127
 
23043 ashik.ali 128
	private int createSaholicUser(String emailId, String retailerName){
129
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
130
		try {
131
			saholicUser = userUserRepository.selectByEmailId(emailId);
132
		}catch (ProfitMandiBusinessException e) {
133
			LOGGER.info("User doesnt exist in old system");
134
		}
135
		if(saholicUser == null){
136
			Cart cart = new Cart();
137
			cart.setCartStatus(CartStatus.ACTIVE);
138
			cartRepository.persist(cart);
139
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
140
			saholicUser.setEmailId(emailId);
141
			saholicUser.setName(retailerName);
142
			saholicUser.setActiveCartId(cart.getId());
143
			userUserRepository.persist(saholicUser);
144
		}
145
		return saholicUser.getId();
146
	}
147
 
148
	private void updateRetailer(String emailId, Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
149
		if(retailer == null){
150
			int saholicUserId = this.createSaholicUser(emailId, updateRetailerRequest.getName());
151
			retailer = new Retailer();
152
			retailer.setId(saholicUserId);
153
		}
23025 ashik.ali 154
		retailer.setName(updateRetailerRequest.getName());
155
		retailer.setNumber(updateRetailerRequest.getNumber());
156
		if(updateRetailerRequest.getDocumentId() > 0){
157
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
158
		}
159
		retailerRepository.persist(retailer);
160
	}
161
 
23043 ashik.ali 162
	private void updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{
163
		if(address == null){
164
			address = new Address();
165
			address.setRetaierId(retailerId);
166
			this.updateAddress(address, customAddress);
167
			//addressRepository.persist(addressRetailer);
168
 
169
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
170
			retailerRegisteredAddress.setRetailerId(retailerId);
171
			retailerRegisteredAddress.setAddressId(address.getId());
172
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
173
		}
174
	}
175
 
23025 ashik.ali 176
	private void updateAddress(Address address, CustomAddress customAddress){
177
		address.setName(customAddress.getName());
178
		address.setLine1(customAddress.getLine1());
179
		address.setLine2(customAddress.getLine2());
180
		address.setCity(customAddress.getCity());
181
		address.setPinCode(customAddress.getPinCode());
182
		address.setState(customAddress.getState());
183
		addressRepository.persist(address);
184
	}
185
 
23043 ashik.ali 186
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId) throws ProfitMandiBusinessException{
187
		if(shops.isEmpty()){
23025 ashik.ali 188
			for(CustomShop customShop : customShops){
23043 ashik.ali 189
				Shop shop = new Shop();
190
				this.createOrUpdateShop(shop, customShop, retailerId);
191
				shops.add(shop);
192
			}
193
		}else{
194
			for(Shop shop : shops){
195
				for(CustomShop customShop : customShops){
196
					if(shop.getId() == customShop.getShopId()){
197
						this.createOrUpdateShop(shop, customShop, retailerId);
23025 ashik.ali 198
					}
199
				}
200
			}
23043 ashik.ali 201
			for(CustomShop customShop : customShops){
202
				if(customShop.getShopId() == 0){
203
					Shop shop = new Shop();
204
					this.createOrUpdateShop(shop, customShop, retailerId);
205
					shops.add(shop);
206
				}
207
			}
23025 ashik.ali 208
		}
209
	}
210
 
23043 ashik.ali 211
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId) throws ProfitMandiBusinessException{
212
		shop.setRetailerId(retailerId);
213
		shop.setName(customShop.getName());
214
		if(customShop.getDocumentId() > 0){
215
			shop.setDocumentId(customShop.getDocumentId());
216
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
217
		}
218
		if(shop.getAddressId() == null){
219
			//shop.setDocumentId(customShop.getDocumentId());
220
			Address address = new Address();
221
			this.updateAddress(address, customShop.getAddress());
222
			shop.setAddressId(address.getId());
223
			shop.setAddress(address);
224
			shopRepository.persist(shop);
225
			ShopAddress shopAddress = new ShopAddress();
226
			shopAddress.setAddressId(address.getId());
227
			shopAddress.setShopId(shop.getId());
228
			shopAddressRepository.persist(shopAddress);
229
		}else{
230
			Address address = addressRepository.selectById(shop.getAddressId());
231
			this.updateAddress(address, customShop.getAddress());
232
			shop.setAddress(address);
233
			shopRepository.persist(shop);
234
		}
235
 
236
	}
23025 ashik.ali 237
 
23043 ashik.ali 238
 
22980 ashik.ali 239
	private void addAddress(List<Shop> shops){
240
		Set<Integer> shopIds = this.toShopIds(shops);
241
		if(shopIds.isEmpty()){
242
			return;
243
		}
244
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
245
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
246
 
247
		for(Shop shop : shops){
248
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
249
		}
250
	}
251
 
252
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
253
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
254
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
255
		List<Address> addresses = addressRepository.selectByIds(addressIds);
256
		for(Address address : addresses){
257
			addressIdAddressMap.put(address.getId(), address);
258
		}
259
		return addressIdAddressMap;
260
	}
261
 
262
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
263
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
264
			@Override
265
			public Integer apply(ShopAddress shopAddress) {
266
				return shopAddress.getAddressId();
267
			}
268
		};
269
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
270
	}
271
 
272
	private Set<Integer> toShopIds(List<Shop> shops){
273
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
274
			@Override
275
			public Integer apply(Shop shop) {
276
				return shop.getId();
277
			}
278
		};
279
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
280
	}
281
 
282
	private String toString(List<UserRole> userRoles){
283
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
284
			public String apply(UserRole userRole) {
285
				return userRole.getRoleType().toString();
286
			};
287
		};
288
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
289
		return String.join(", ", userRoleStrings);
290
	}
291
 
292
}