Subversion Repositories SmartDukaan

Rev

Rev 23300 | Rev 23365 | 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
 
23300 amit.gupta 3
import java.time.LocalDateTime;
23060 ashik.ali 4
import java.util.ArrayList;
22980 ashik.ali 5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.Set;
9
import java.util.function.Function;
10
import java.util.stream.Collectors;
11
 
23043 ashik.ali 12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
22980 ashik.ali 14
import org.springframework.beans.factory.annotation.Autowired;
23043 ashik.ali 15
import org.springframework.beans.factory.annotation.Qualifier;
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;
23329 ashik.ali 21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23025 ashik.ali 22
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
23329 ashik.ali 23
import com.spice.profitmandi.common.util.StringUtils;
24
import com.spice.profitmandi.common.util.Utils;
25
import com.spice.profitmandi.dao.entity.dtr.DistrictMaster;
22980 ashik.ali 26
import com.spice.profitmandi.dao.entity.dtr.Retailer;
23043 ashik.ali 27
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
22980 ashik.ali 28
import com.spice.profitmandi.dao.entity.dtr.Shop;
29
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
30
import com.spice.profitmandi.dao.entity.dtr.User;
23269 ashik.ali 31
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
22980 ashik.ali 32
import com.spice.profitmandi.dao.entity.dtr.UserRole;
23329 ashik.ali 33
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
34
import com.spice.profitmandi.dao.entity.fofo.FofoStoreSequenceGeneration;
22980 ashik.ali 35
import com.spice.profitmandi.dao.entity.user.Address;
23043 ashik.ali 36
import com.spice.profitmandi.dao.entity.user.Cart;
23329 ashik.ali 37
import com.spice.profitmandi.dao.entity.user.Counter;
38
import com.spice.profitmandi.dao.entity.user.PrivateDealUser;
39
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressMapping;
23063 ashik.ali 40
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
23106 ashik.ali 41
import com.spice.profitmandi.dao.enumuration.dtr.RetailerType;
23136 ashik.ali 42
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
23329 ashik.ali 43
import com.spice.profitmandi.dao.repository.dtr.DistrictMasterRepository;
22980 ashik.ali 44
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
23329 ashik.ali 45
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
46
import com.spice.profitmandi.dao.repository.dtr.FofoStoreSequenceGenerationRepository;
22980 ashik.ali 47
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
48
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
49
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
50
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
51
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
52
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
53
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
54
import com.spice.profitmandi.dao.repository.user.AddressRepository;
23043 ashik.ali 55
import com.spice.profitmandi.dao.repository.user.CartRepository;
23329 ashik.ali 56
import com.spice.profitmandi.dao.repository.user.CounterRepository;
57
import com.spice.profitmandi.dao.repository.user.PrivateDealUserAddressMappingRepository;
58
import com.spice.profitmandi.dao.repository.user.PrivateDealUserRepository;
22980 ashik.ali 59
 
23329 ashik.ali 60
import in.shop2020.model.v1.inventory.StateInfo;
23043 ashik.ali 61
import in.shop2020.model.v1.user.CartStatus;
62
 
22980 ashik.ali 63
@Component
64
public class RetailerServiceImpl implements RetailerService {
23043 ashik.ali 65
 
66
	private static final Logger LOGGER = LoggerFactory.getLogger(RetailerServiceImpl.class);
67
 
22980 ashik.ali 68
	@Autowired
69
	private RetailerRepository retailerRepository;
70
 
71
	@Autowired
72
	private UserAccountRepository userAccountRepository;
73
 
74
	@Autowired
75
	private UserRepository userRepository;
76
 
77
	@Autowired
23043 ashik.ali 78
	private CartRepository cartRepository;
79
 
80
	@Autowired
22980 ashik.ali 81
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
82
 
83
	@Autowired
84
	private AddressRepository addressRepository;
85
 
86
	@Autowired
87
	private ShopRepository shopRepository;
88
 
89
	@Autowired
90
	private ShopAddressRepository shopAddressRepository;
91
 
92
	@Autowired
93
	private UserRoleRepository userRoleRepository;
94
 
95
	@Autowired
96
	private DocumentRepository documentRepository;
97
 
23043 ashik.ali 98
	@Autowired
23329 ashik.ali 99
	private PrivateDealUserRepository privateDealUserRepository;
100
 
101
	@Autowired
102
	private PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
103
 
104
	@Autowired
105
	private CounterRepository counterRepository;
106
 
107
	@Autowired
23043 ashik.ali 108
	@Qualifier("userUserRepository")
109
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
110
 
23329 ashik.ali 111
	@Autowired
112
	private DistrictMasterRepository districtMasterRepository;
113
 
114
	@Autowired
115
	private FofoStoreRepository fofoStoreRepository;
116
 
117
	@Autowired
118
	private FofoStoreSequenceGenerationRepository fofoStoreSequenceGenerationRepository;
119
 
22980 ashik.ali 120
	@Override
121
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
122
			throws ProfitMandiBusinessException {
23202 ashik.ali 123
		User user = null;
124
		try{
125
			user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
126
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
127
 
128
		}
129
		if(user == null){
23329 ashik.ali 130
			try{
131
				user = userRepository.selectBySecondryEmailId(emailIdOrMobileNumber);
132
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
133
 
134
			}
23202 ashik.ali 135
		}
22980 ashik.ali 136
		Map<String, Object> map = new HashMap<>();
23329 ashik.ali 137
		map.put("stateNames", Utils.getAllStateNames());
23043 ashik.ali 138
 
23329 ashik.ali 139
		if(user != null){
23043 ashik.ali 140
 
23329 ashik.ali 141
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
23043 ashik.ali 142
 
23329 ashik.ali 143
			map.put("userRoles", userRoles);
144
			map.put("user", user);
145
			//map.put("retailer", retailer);
146
			map.put("fofoRole", this.containsRoleType(userRoles, RoleType.FOFO));
147
 
148
			map.put("userRoleNames", this.toString(userRoles));
23043 ashik.ali 149
			try{
23329 ashik.ali 150
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
151
				Retailer retailer = retailerRepository.selectById(retailerId);
152
 
153
				map.put("retailer", retailer);
154
 
155
				if(this.containsRoleType(userRoles, RoleType.FOFO)){
156
					try{
157
						FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
158
						map.put("fofoStore", fofoStore);
159
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
160
						LOGGER.error("FofoStore code not found");
161
					}
162
				}
163
 
164
 
165
				try{
166
					PrivateDealUser privateDealUser = privateDealUserRepository.selectById(retailer.getId());
167
					if(privateDealUser.getCounterId() != null){
168
						Counter counter = counterRepository.selectById(privateDealUser.getCounterId());
169
						map.put("gstNumber", counter.getGstin());
170
					}
171
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
172
					LOGGER.error("PrivateDealUser not found");
173
				}
174
				try{
175
					int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
176
					Address retailerAddress = addressRepository.selectById(retailerAddressId);
177
					map.put("retailerAddress", retailerAddress);
178
					StateInfo stateInfo = null;
179
					try {
180
						stateInfo = Utils.getStateInfo(retailerAddress.getState());
181
					} catch (Exception e) {
182
						e.printStackTrace();
183
						//throw new ProfitMandiBusinessException();
184
					}
185
					List<DistrictMaster> districtMasters = districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
186
					map.put("districtMasters", districtMasters);
187
 
188
					List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
189
		     		map.put("shops", shops);
190
					this.addAddress(shops);
191
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
192
					LOGGER.error("Retailer Registered Address not found");
193
				}
23043 ashik.ali 194
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
23329 ashik.ali 195
				LOGGER.error("Retailer not found in user_account");
23043 ashik.ali 196
			}
197
		}
198
 
22980 ashik.ali 199
		return map;
200
	}
201
 
23329 ashik.ali 202
	private boolean containsRoleType(List<UserRole> userRoles, RoleType roleType){
203
		for(UserRole userRole : userRoles){
204
			if(userRole.getRoleType() == roleType){
205
				return true;
206
			}
207
		}
208
		return false;
209
	}
210
 
211
	private List<UserRole> addRole(List<UserRole> userRoles, int userId, RoleType roleType){
212
		if(userRoles == null){
213
			userRoles = new ArrayList<>();
214
		}
215
		if(!this.containsRoleType(userRoles, roleType)){
216
			UserRole userRole = new UserRole();
217
			userRole.setUserId(userId);
218
			userRole.setRoleType(roleType);
219
			try {
220
				userRoleRepository.persist(userRole);
221
			} catch (ProfitMandiBusinessException e) {
222
				LOGGER.error("UserRole is already exist");
223
			}
224
			userRoles.add(userRole);
225
		}
226
		return userRoles;
227
 
228
	}
229
 
22980 ashik.ali 230
	@SuppressWarnings("unchecked")
231
	@Override
23025 ashik.ali 232
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
233
			throws ProfitMandiBusinessException {
234
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
23329 ashik.ali 235
 
23043 ashik.ali 236
		User user = (User)map.get("user");
23329 ashik.ali 237
		user = this.createUser(user, updateRetailerRequest);
238
		map.put("user", user);
239
 
240
		List<UserRole> userRoles = (List<UserRole>)map.get("userRoles");
241
 
242
		userRoles = this.addRole(userRoles, user.getId(), RoleType.USER);
243
 
23025 ashik.ali 244
		Retailer retailer = (Retailer)map.get("retailer");
23329 ashik.ali 245
		retailer = this.updateRetailer(user, retailer, updateRetailerRequest);
246
		map.put("retailer", retailer);
247
 
248
		userRoles = this.addRole(userRoles, user.getId(), RoleType.RETAILER);
249
 
250
		if(updateRetailerRequest.isFofo()){
251
			userRoles = this.addRole(userRoles, user.getId(), RoleType.FOFO);
23061 ashik.ali 252
		}
23329 ashik.ali 253
 
254
		if(this.containsRoleType(userRoles, RoleType.FOFO)){
255
 
256
		}
257
 
258
		map.put("userRoles", userRoles);
259
		map.put("userRoleNames", this.toString(userRoles));
260
 
23025 ashik.ali 261
		Address retailerAddress = (Address)map.get("retailerAddress");
23329 ashik.ali 262
		retailerAddress = this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(), retailer.getId());
263
		map.put("retailerAddress", retailerAddress);
264
 
265
		if(updateRetailerRequest.isFofo()){
266
			this.createFofoStoreCodeByUserId(user.getId(), updateRetailerRequest.getDistrictName(), retailerAddress.getState());
23061 ashik.ali 267
		}
23329 ashik.ali 268
 
269
		this.createPrivateDealUser(user, updateRetailerRequest.isFofo(), updateRetailerRequest.getGstNumber(), retailer, retailerAddress.getId());
270
		map.put("gstNumber", updateRetailerRequest.getGstNumber());
271
		this.updateSaholicUser(retailer.getId(), retailerAddress.getId());
272
 
23025 ashik.ali 273
		List<Shop> shops = (List<Shop>)map.get("shops");
23060 ashik.ali 274
		if(shops == null){
275
			shops = new ArrayList<>();
23061 ashik.ali 276
			map.put("shops", shops);
23060 ashik.ali 277
		}
23079 ashik.ali 278
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId(), retailerAddress);
23025 ashik.ali 279
		return map;
280
	}
281
 
23329 ashik.ali 282
	private User createUser(User user, UpdateRetailerRequest updateRetailerRequest){
283
		if(user == null){
284
			user = new User();
285
			user.setCity("");
286
			user.setPinCode(0);
287
			user.setState("");
288
			user.setPassword("");
289
			user.setMobile_verified(false);
290
			user.setReferral_url("");
291
			user.setGroup_id(1);
292
			user.setStatus(1);
293
			user.setActivated(true);
294
			user.setCreateTimestamp(LocalDateTime.now());
295
			user.setUpdateTimestamp(LocalDateTime.now());
296
			userRepository.persist(user);
297
		}
298
		user.setFirstName(updateRetailerRequest.getUserFirstName());
299
		user.setLastName(updateRetailerRequest.getUserLastName());
300
		user.setMobileNumber(updateRetailerRequest.getUserMobileNumber());
301
		user.setEmailId(updateRetailerRequest.getUserEmailId());
302
		user.setUsername(updateRetailerRequest.getUserEmailId());
303
		return user;
304
	}
305
 
306
 
307
 
23063 ashik.ali 308
	private int createSaholicUser(User user, String retailerName){
23043 ashik.ali 309
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
310
		try {
23063 ashik.ali 311
			saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
23043 ashik.ali 312
		}catch (ProfitMandiBusinessException e) {
313
			LOGGER.info("User doesnt exist in old system");
314
		}
315
		if(saholicUser == null){
316
			Cart cart = new Cart();
317
			cart.setCartStatus(CartStatus.ACTIVE);
318
			cartRepository.persist(cart);
319
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
23063 ashik.ali 320
			saholicUser.setEmailId(user.getEmailId());
23043 ashik.ali 321
			saholicUser.setName(retailerName);
322
			saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 323
			saholicUser.setCreateTimestamp(LocalDateTime.now());
23043 ashik.ali 324
			userUserRepository.persist(saholicUser);
23063 ashik.ali 325
 
23269 ashik.ali 326
			UserAccount ua = new UserAccount();
327
			ua.setAccountKey(saholicUser.getId());
328
			ua.setUserId(user.getId());
329
			ua.setType(AccountType.saholic);
23063 ashik.ali 330
			userAccountRepository.persist(ua);
331
 
23269 ashik.ali 332
			UserAccount ua2 = new UserAccount();
333
			ua2.setAccountKey(saholicUser.getActiveCartId());
334
			ua2.setUserId(user.getId());
335
			ua2.setType(AccountType.cartId);
23063 ashik.ali 336
			userAccountRepository.persist(ua2);
23043 ashik.ali 337
		}
338
		return saholicUser.getId();
339
	}
340
 
23063 ashik.ali 341
	private Retailer updateRetailer(User user, Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
23043 ashik.ali 342
		if(retailer == null){
23063 ashik.ali 343
			int saholicUserId = this.createSaholicUser(user, updateRetailerRequest.getName());
23043 ashik.ali 344
			retailer = new Retailer();
345
			retailer.setId(saholicUserId);
346
		}
23329 ashik.ali 347
		retailer.setActive(updateRetailerRequest.isActive());
23137 ashik.ali 348
		this.createRole(user.getId(), RoleType.RETAILER);
23025 ashik.ali 349
		retailer.setName(updateRetailerRequest.getName());
350
		retailer.setNumber(updateRetailerRequest.getNumber());
23107 ashik.ali 351
		if(updateRetailerRequest.getNumber() == null || updateRetailerRequest.getNumber().isEmpty()){
352
			retailer.setType(RetailerType.UNREGISTERED_SHOP);
353
		}else{
354
			retailer.setType(RetailerType.GSTIN);
355
		}
23025 ashik.ali 356
		if(updateRetailerRequest.getDocumentId() > 0){
357
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
358
		}
359
		retailerRepository.persist(retailer);
23059 ashik.ali 360
		return retailer;
23025 ashik.ali 361
	}
362
 
23329 ashik.ali 363
	private void updateSaholicUser(int retailerId, int retailerAddressId){
364
		try {
365
			com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectById(retailerId);
366
			user.setAddressId(retailerAddressId);
367
			userUserRepository.persist(user);
368
		} catch (ProfitMandiBusinessException e) {
369
 
370
		}
371
	}
372
 
373
	private void createPrivateDealUser(User user, boolean fofo, String gstNumber, Retailer retailer, int retailerAddressId){
374
		PrivateDealUser privateDealUser = null;
375
		try{
376
			privateDealUser = privateDealUserRepository.selectById(retailer.getId());
377
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
378
			LOGGER.error("PrivateDealUser not found");
379
		}
380
 
381
		 //= privateDealUserRepository.selectById(saholicUser.getId());
382
		if(privateDealUser == null){
383
			Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
384
			this.createPrivateDealUser(retailer.getId(), counterId, fofo);
385
		}else{
386
			if(privateDealUser.getCounterId() == null){
387
				Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
388
				privateDealUser.setCounterId(counterId);
389
				privateDealUser.setFofo(fofo);
390
				privateDealUserRepository.persist(privateDealUser);
391
			}else{
392
				Counter counter = null;
393
				try{
394
					counter = counterRepository.selectById(privateDealUser.getCounterId());
395
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
396
					LOGGER.error("Counter not found with id [{}]", privateDealUser.getCounterId());
397
				}
398
				if(counter == null){
399
					this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
400
				}else{
401
					counter.setGstin(gstNumber);
402
					counterRepository.persist(counter);
403
				}
404
				privateDealUser.setFofo(fofo);
405
				privateDealUserRepository.persist(privateDealUser);
406
			}
407
		}
408
 
409
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
410
		privateDealUserAddressMapping.setUserId(retailer.getId());
411
		privateDealUserAddressMapping.setAddressId(retailerAddressId);
412
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
413
	}
414
 
415
	//Specifically set isFofo to true that has to be used by old system
416
	private void createPrivateDealUser(int retailerId, int counterId, boolean fofo){
417
		PrivateDealUser privateDealUser = new PrivateDealUser();
418
		privateDealUser.setActive(true);
419
		privateDealUser.setBulkShipmentAmountLimit(fofo ? 1000000 : 50000);
420
		privateDealUser.setId(retailerId);
421
		privateDealUser.setCounterId(counterId);
422
		privateDealUser.setFofo(fofo);
423
		privateDealUserRepository.persist(privateDealUser);
424
	}
425
 
426
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
427
		if(gstNumber != null && !gstNumber.isEmpty()){
428
			Counter counter = new Counter();
429
			counter.setEmailId(emailId);
430
			counter.setGstin(gstNumber);
431
			counter.setMobileNumber(mobileNumber);
432
			counter.setName(name);
433
			counter.setAddressId(addressId);
434
			counterRepository.persist(counter);
435
			return counter.getId();
436
		}else{
437
			return null;
438
		}
439
	}
440
 
23137 ashik.ali 441
	private void createRole(int userId, RoleType roleType){
442
		if(!userRoleRepository.isExistByUserIdAndType(userId, roleType)){
443
			UserRole userRole = new UserRole();
444
			userRole.setUserId(userId);
445
			userRole.setRoleType(roleType);
446
			try{
447
				userRoleRepository.persist(userRole);
448
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
449
				LOGGER.error("Retailer role is already exist with userId {}", userId);
450
			}
451
		}else{
452
			LOGGER.error("Retailer role not found with userId {}", userId);
453
		}
454
 
455
	}
456
 
23059 ashik.ali 457
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{
23043 ashik.ali 458
		if(address == null){
459
			address = new Address();
460
			address.setRetaierId(retailerId);
461
			this.updateAddress(address, customAddress);
462
			//addressRepository.persist(addressRetailer);
463
 
464
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
465
			retailerRegisteredAddress.setRetailerId(retailerId);
466
			retailerRegisteredAddress.setAddressId(address.getId());
467
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
468
		}
23059 ashik.ali 469
		return address;
23043 ashik.ali 470
	}
471
 
23025 ashik.ali 472
	private void updateAddress(Address address, CustomAddress customAddress){
473
		address.setName(customAddress.getName());
474
		address.setLine1(customAddress.getLine1());
475
		address.setLine2(customAddress.getLine2());
476
		address.setCity(customAddress.getCity());
477
		address.setPinCode(customAddress.getPinCode());
478
		address.setState(customAddress.getState());
479
		addressRepository.persist(address);
480
	}
481
 
23079 ashik.ali 482
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 483
		if(shops.isEmpty()){
23025 ashik.ali 484
			for(CustomShop customShop : customShops){
23043 ashik.ali 485
				Shop shop = new Shop();
23079 ashik.ali 486
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 487
				shops.add(shop);
488
			}
489
		}else{
490
			for(Shop shop : shops){
491
				for(CustomShop customShop : customShops){
492
					if(shop.getId() == customShop.getShopId()){
23079 ashik.ali 493
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 494
					}
495
				}
496
			}
23043 ashik.ali 497
			for(CustomShop customShop : customShops){
498
				if(customShop.getShopId() == 0){
499
					Shop shop = new Shop();
23079 ashik.ali 500
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 501
					shops.add(shop);
502
				}
503
			}
23025 ashik.ali 504
		}
505
	}
506
 
23079 ashik.ali 507
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 508
		shop.setRetailerId(retailerId);
509
		shop.setName(customShop.getName());
510
		if(customShop.getDocumentId() > 0){
511
			shop.setDocumentId(customShop.getDocumentId());
512
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
513
		}
514
		if(shop.getAddressId() == null){
23079 ashik.ali 515
			Address address = null;
516
			if(customShop.isSameAsRetailerAddress()){
517
				address = sameAsRetailerAddressValue;
518
			}else{
519
				//shop.setDocumentId(customShop.getDocumentId());
520
				address = new Address();
521
				this.updateAddress(address, customShop.getAddress());
522
			}
23043 ashik.ali 523
			shop.setAddressId(address.getId());
524
			shop.setAddress(address);
525
			shopRepository.persist(shop);
23131 ashik.ali 526
			ShopAddress shopAddress = null;
527
			try{
528
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
529
				shopAddress.setAddressId(address.getId());
530
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
531
				shopAddress = new ShopAddress();
532
				shopAddress.setAddressId(address.getId());
533
				shopAddress.setShopId(shop.getId());
534
			}
23043 ashik.ali 535
			shopAddressRepository.persist(shopAddress);
536
		}else{
537
			Address address = addressRepository.selectById(shop.getAddressId());
23329 ashik.ali 538
			LOGGER.info("found address : {}", address);
23079 ashik.ali 539
			CustomAddress customAddress = customShop.getAddress();
23329 ashik.ali 540
			LOGGER.info("requested address : {}", customAddress);
541
			if(!(address.getName().equals(customAddress.getName()) &&
23079 ashik.ali 542
				address.getLine1().equals(customAddress.getLine1()) &&
543
				address.getLine2().equals(customAddress.getLine2())	&&
544
				address.getCity().equals(customAddress.getCity()) &&
545
				address.getPinCode().equals(customAddress.getPinCode()) &&
23329 ashik.ali 546
				address.getState().equals(customAddress.getState()))){
23079 ashik.ali 547
				this.updateAddress(address, customShop.getAddress());
548
			}else{
549
				address = new Address();
550
				ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
551
				this.updateAddress(address, customAddress);
552
				shopAddress.setAddressId(address.getId());
553
				shopAddressRepository.persist(shopAddress);
554
			}
555
 
23043 ashik.ali 556
			shop.setAddress(address);
557
			shopRepository.persist(shop);
558
		}
559
 
560
	}
23025 ashik.ali 561
 
23043 ashik.ali 562
 
22980 ashik.ali 563
	private void addAddress(List<Shop> shops){
564
		Set<Integer> shopIds = this.toShopIds(shops);
565
		if(shopIds.isEmpty()){
566
			return;
567
		}
568
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
569
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
570
 
571
		for(Shop shop : shops){
572
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
573
		}
574
	}
575
 
576
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
577
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
578
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
579
		List<Address> addresses = addressRepository.selectByIds(addressIds);
580
		for(Address address : addresses){
581
			addressIdAddressMap.put(address.getId(), address);
582
		}
583
		return addressIdAddressMap;
584
	}
585
 
586
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
587
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
588
			@Override
589
			public Integer apply(ShopAddress shopAddress) {
590
				return shopAddress.getAddressId();
591
			}
592
		};
593
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
594
	}
595
 
596
	private Set<Integer> toShopIds(List<Shop> shops){
597
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
598
			@Override
599
			public Integer apply(Shop shop) {
600
				return shop.getId();
601
			}
602
		};
603
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
604
	}
605
 
606
	private String toString(List<UserRole> userRoles){
607
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
608
			public String apply(UserRole userRole) {
609
				return userRole.getRoleType().toString();
610
			};
611
		};
612
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
613
		return String.join(", ", userRoleStrings);
614
	}
23329 ashik.ali 615
 
616
	@Override
617
	public void createFofoStoreCodeByUserId(int userId, String districtName, String stateName) throws ProfitMandiBusinessException {
618
		User user = userRepository.selectById(userId);
619
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
620
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
621
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
622
		try{
623
			userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.FOFO);
624
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
625
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
626
		}
627
 
628
		boolean foundFofoStore = false;
629
		try{
630
			fofoStoreRepository.selectByRetailerId(retailer.getId());
631
			foundFofoStore = true;
632
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
633
 
634
		}
635
 
636
		if(foundFofoStore){
637
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "USR_1014");
638
		}			
639
 
640
		int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
641
		Address retailerAddress = addressRepository.selectById(retailerAddressId);
642
 
643
		StateInfo stateInfo = null;
644
		try {
645
			stateInfo = Utils.getStateInfo(retailerAddress.getState());
646
		} catch (Exception e) {
647
			// TODO Auto-generated catch block
648
			e.printStackTrace();
649
			//throw new ProfitMandiBusinessException();
650
		}
651
		DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName, stateInfo.getShortName());
652
 
653
		FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
654
		boolean foundFofoStoreSequence = false;
655
		try{
656
			fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
657
			foundFofoStoreSequence = true;
658
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
659
			fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
660
			fofoStoreSequenceGeneration.setSequence(1);
661
			fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
662
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
663
		}
664
 
665
		FofoStore fofoStore = new FofoStore();
666
		fofoStore.setId(retailer.getId());
667
		String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
668
		if(foundFofoStoreSequence){
669
			fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
670
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
671
		}
672
		fofoStore.setCode(fofoStoreCode);
673
		fofoStoreRepository.persist(fofoStore);
674
	}
675
 
676
	@Override
677
	public List<DistrictMaster> getAllDistrictMaster(String stateName) {
678
		StateInfo stateInfo = null;
679
		try {
680
			stateInfo = Utils.getStateInfo(stateName);
681
		} catch (Exception e) {
682
			e.printStackTrace();
683
			//throw new ProfitMandiBusinessException();
684
		}
685
		return districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
686
	}
22980 ashik.ali 687
 
688
}