Subversion Repositories SmartDukaan

Rev

Rev 23378 | Rev 23487 | 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);
23473 ashik.ali 417
			try {
418
				this.createPrivateDealUser(retailer.getId(), counterId, fofo);
419
			}catch (Exception e) {
420
				LOGGER.error("ERROR : ", e);
421
			}
23329 ashik.ali 422
		}else{
423
			if(privateDealUser.getCounterId() == null){
424
				Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
425
				privateDealUser.setCounterId(counterId);
426
				privateDealUser.setFofo(fofo);
427
				privateDealUserRepository.persist(privateDealUser);
428
			}else{
429
				Counter counter = null;
430
				try{
431
					counter = counterRepository.selectById(privateDealUser.getCounterId());
432
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
433
					LOGGER.error("Counter not found with id [{}]", privateDealUser.getCounterId());
434
				}
435
				if(counter == null){
436
					this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
437
				}else{
438
					counter.setGstin(gstNumber);
439
					counterRepository.persist(counter);
440
				}
441
				privateDealUser.setFofo(fofo);
442
				privateDealUserRepository.persist(privateDealUser);
443
			}
444
		}
445
 
446
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
447
		privateDealUserAddressMapping.setUserId(retailer.getId());
448
		privateDealUserAddressMapping.setAddressId(retailerAddressId);
449
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
450
	}
451
 
452
	//Specifically set isFofo to true that has to be used by old system
453
	private void createPrivateDealUser(int retailerId, int counterId, boolean fofo){
454
		PrivateDealUser privateDealUser = new PrivateDealUser();
455
		privateDealUser.setActive(true);
456
		privateDealUser.setBulkShipmentAmountLimit(fofo ? 1000000 : 50000);
457
		privateDealUser.setId(retailerId);
458
		privateDealUser.setCounterId(counterId);
459
		privateDealUser.setFofo(fofo);
460
		privateDealUserRepository.persist(privateDealUser);
461
	}
462
 
463
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
464
		if(gstNumber != null && !gstNumber.isEmpty()){
465
			Counter counter = new Counter();
466
			counter.setEmailId(emailId);
467
			counter.setGstin(gstNumber);
468
			counter.setMobileNumber(mobileNumber);
469
			counter.setName(name);
470
			counter.setAddressId(addressId);
471
			counterRepository.persist(counter);
472
			return counter.getId();
473
		}else{
474
			return null;
475
		}
476
	}
477
 
23137 ashik.ali 478
	private void createRole(int userId, RoleType roleType){
479
		if(!userRoleRepository.isExistByUserIdAndType(userId, roleType)){
480
			UserRole userRole = new UserRole();
481
			userRole.setUserId(userId);
482
			userRole.setRoleType(roleType);
483
			try{
484
				userRoleRepository.persist(userRole);
485
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
486
				LOGGER.error("Retailer role is already exist with userId {}", userId);
487
			}
488
		}else{
489
			LOGGER.error("Retailer role not found with userId {}", userId);
490
		}
491
 
492
	}
493
 
23059 ashik.ali 494
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{
23043 ashik.ali 495
		if(address == null){
496
			address = new Address();
497
			address.setRetaierId(retailerId);
498
			this.updateAddress(address, customAddress);
499
			//addressRepository.persist(addressRetailer);
500
 
501
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
502
			retailerRegisteredAddress.setRetailerId(retailerId);
503
			retailerRegisteredAddress.setAddressId(address.getId());
504
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
505
		}
23059 ashik.ali 506
		return address;
23043 ashik.ali 507
	}
508
 
23025 ashik.ali 509
	private void updateAddress(Address address, CustomAddress customAddress){
510
		address.setName(customAddress.getName());
511
		address.setLine1(customAddress.getLine1());
512
		address.setLine2(customAddress.getLine2());
513
		address.setCity(customAddress.getCity());
514
		address.setPinCode(customAddress.getPinCode());
515
		address.setState(customAddress.getState());
516
		addressRepository.persist(address);
517
	}
518
 
23079 ashik.ali 519
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 520
		if(shops.isEmpty()){
23025 ashik.ali 521
			for(CustomShop customShop : customShops){
23043 ashik.ali 522
				Shop shop = new Shop();
23079 ashik.ali 523
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 524
				shops.add(shop);
525
			}
526
		}else{
527
			for(Shop shop : shops){
528
				for(CustomShop customShop : customShops){
529
					if(shop.getId() == customShop.getShopId()){
23079 ashik.ali 530
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 531
					}
532
				}
533
			}
23043 ashik.ali 534
			for(CustomShop customShop : customShops){
535
				if(customShop.getShopId() == 0){
536
					Shop shop = new Shop();
23079 ashik.ali 537
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 538
					shops.add(shop);
539
				}
540
			}
23025 ashik.ali 541
		}
542
	}
543
 
23079 ashik.ali 544
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 545
		shop.setRetailerId(retailerId);
546
		shop.setName(customShop.getName());
547
		if(customShop.getDocumentId() > 0){
548
			shop.setDocumentId(customShop.getDocumentId());
549
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
550
		}
551
		if(shop.getAddressId() == null){
23079 ashik.ali 552
			Address address = null;
553
			if(customShop.isSameAsRetailerAddress()){
554
				address = sameAsRetailerAddressValue;
555
			}else{
556
				//shop.setDocumentId(customShop.getDocumentId());
557
				address = new Address();
558
				this.updateAddress(address, customShop.getAddress());
559
			}
23043 ashik.ali 560
			shop.setAddressId(address.getId());
561
			shop.setAddress(address);
562
			shopRepository.persist(shop);
23131 ashik.ali 563
			ShopAddress shopAddress = null;
564
			try{
565
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
566
				shopAddress.setAddressId(address.getId());
567
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
568
				shopAddress = new ShopAddress();
569
				shopAddress.setAddressId(address.getId());
570
				shopAddress.setShopId(shop.getId());
571
			}
23043 ashik.ali 572
			shopAddressRepository.persist(shopAddress);
573
		}else{
574
			Address address = addressRepository.selectById(shop.getAddressId());
23329 ashik.ali 575
			LOGGER.info("found address : {}", address);
23079 ashik.ali 576
			CustomAddress customAddress = customShop.getAddress();
23329 ashik.ali 577
			LOGGER.info("requested address : {}", customAddress);
578
			if(!(address.getName().equals(customAddress.getName()) &&
23079 ashik.ali 579
				address.getLine1().equals(customAddress.getLine1()) &&
580
				address.getLine2().equals(customAddress.getLine2())	&&
581
				address.getCity().equals(customAddress.getCity()) &&
582
				address.getPinCode().equals(customAddress.getPinCode()) &&
23329 ashik.ali 583
				address.getState().equals(customAddress.getState()))){
23079 ashik.ali 584
				this.updateAddress(address, customShop.getAddress());
585
			}else{
586
				address = new Address();
587
				ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
588
				this.updateAddress(address, customAddress);
589
				shopAddress.setAddressId(address.getId());
590
				shopAddressRepository.persist(shopAddress);
591
			}
592
 
23043 ashik.ali 593
			shop.setAddress(address);
594
			shopRepository.persist(shop);
595
		}
596
 
597
	}
23025 ashik.ali 598
 
23043 ashik.ali 599
 
22980 ashik.ali 600
	private void addAddress(List<Shop> shops){
601
		Set<Integer> shopIds = this.toShopIds(shops);
602
		if(shopIds.isEmpty()){
603
			return;
604
		}
605
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
606
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
607
 
608
		for(Shop shop : shops){
609
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
610
		}
611
	}
612
 
613
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
614
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
615
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
616
		List<Address> addresses = addressRepository.selectByIds(addressIds);
617
		for(Address address : addresses){
618
			addressIdAddressMap.put(address.getId(), address);
619
		}
620
		return addressIdAddressMap;
621
	}
622
 
623
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
624
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
625
			@Override
626
			public Integer apply(ShopAddress shopAddress) {
627
				return shopAddress.getAddressId();
628
			}
629
		};
630
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
631
	}
632
 
633
	private Set<Integer> toShopIds(List<Shop> shops){
634
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
635
			@Override
636
			public Integer apply(Shop shop) {
637
				return shop.getId();
638
			}
639
		};
640
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
641
	}
642
 
643
	private String toString(List<UserRole> userRoles){
644
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
645
			public String apply(UserRole userRole) {
646
				return userRole.getRoleType().toString();
647
			};
648
		};
649
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
650
		return String.join(", ", userRoleStrings);
651
	}
23329 ashik.ali 652
 
23378 ashik.ali 653
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName) throws ProfitMandiBusinessException{
654
		FofoStore fofoStore = null;
655
		try{
656
			fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
657
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
658
 
659
		}
660
 
661
		if(fofoStore != null){
662
			LOGGER.error(ResponseCodeHolder.getMessage("USR_1014"));
663
			//throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "USR_1014");
664
		}else {
665
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
666
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
667
 
668
			StateInfo stateInfo = null;
669
			try {
670
				stateInfo = Utils.getStateInfo(retailerAddress.getState());
671
			} catch (Exception e) {
672
				// TODO Auto-generated catch block
673
				e.printStackTrace();
674
				//throw new ProfitMandiBusinessException();
675
			}
676
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName, stateInfo.getShortName());
677
 
678
			FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
679
			boolean foundFofoStoreSequence = false;
680
			try{
681
				fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
682
				foundFofoStoreSequence = true;
683
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
684
				fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
685
				fofoStoreSequenceGeneration.setSequence(1);
686
				fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
687
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
688
			}
689
 
690
			fofoStore = new FofoStore();
691
			fofoStore.setId(retailerId);
692
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
693
			if(foundFofoStoreSequence){
694
				fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
695
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
696
			}
697
			fofoStore.setCode(fofoStoreCode);
698
			fofoStoreRepository.persist(fofoStore);
699
		}
700
 
701
		return fofoStore;
702
	}
703
 
23329 ashik.ali 704
	@Override
23378 ashik.ali 705
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName) throws ProfitMandiBusinessException {
23329 ashik.ali 706
		User user = userRepository.selectById(userId);
707
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
708
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
709
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
710
		try{
711
			userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.FOFO);
712
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
713
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
714
		}
23378 ashik.ali 715
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName);
23329 ashik.ali 716
 
717
	}
718
 
719
	@Override
720
	public List<DistrictMaster> getAllDistrictMaster(String stateName) {
721
		StateInfo stateInfo = null;
722
		try {
723
			stateInfo = Utils.getStateInfo(stateName);
724
		} catch (Exception e) {
725
			e.printStackTrace();
726
			//throw new ProfitMandiBusinessException();
727
		}
728
		return districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
729
	}
22980 ashik.ali 730
 
731
}