Subversion Repositories SmartDukaan

Rev

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