Subversion Repositories SmartDukaan

Rev

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