Subversion Repositories SmartDukaan

Rev

Rev 23473 | Rev 23489 | 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
 
23063 ashik.ali 339
	private int createSaholicUser(User user, String retailerName){
23043 ashik.ali 340
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
341
		try {
23063 ashik.ali 342
			saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
23043 ashik.ali 343
		}catch (ProfitMandiBusinessException e) {
344
			LOGGER.info("User doesnt exist in old system");
345
		}
346
		if(saholicUser == null){
347
			Cart cart = new Cart();
348
			cart.setCartStatus(CartStatus.ACTIVE);
349
			cartRepository.persist(cart);
350
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
23063 ashik.ali 351
			saholicUser.setEmailId(user.getEmailId());
23043 ashik.ali 352
			saholicUser.setName(retailerName);
353
			saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 354
			saholicUser.setCreateTimestamp(LocalDateTime.now());
23043 ashik.ali 355
			userUserRepository.persist(saholicUser);
23063 ashik.ali 356
 
23269 ashik.ali 357
			UserAccount ua = new UserAccount();
358
			ua.setAccountKey(saholicUser.getId());
359
			ua.setUserId(user.getId());
360
			ua.setType(AccountType.saholic);
23063 ashik.ali 361
			userAccountRepository.persist(ua);
362
 
23269 ashik.ali 363
			UserAccount ua2 = new UserAccount();
364
			ua2.setAccountKey(saholicUser.getActiveCartId());
365
			ua2.setUserId(user.getId());
366
			ua2.setType(AccountType.cartId);
23063 ashik.ali 367
			userAccountRepository.persist(ua2);
23043 ashik.ali 368
		}
369
		return saholicUser.getId();
370
	}
371
 
23063 ashik.ali 372
	private Retailer updateRetailer(User user, Retailer retailer, UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException{
23043 ashik.ali 373
		if(retailer == null){
23063 ashik.ali 374
			int saholicUserId = this.createSaholicUser(user, updateRetailerRequest.getName());
23043 ashik.ali 375
			retailer = new Retailer();
376
			retailer.setId(saholicUserId);
377
		}
23329 ashik.ali 378
		retailer.setActive(updateRetailerRequest.isActive());
23487 ashik.ali 379
		//this.createRole(user.getId(), RoleType.RETAILER);
23025 ashik.ali 380
		retailer.setName(updateRetailerRequest.getName());
381
		retailer.setNumber(updateRetailerRequest.getNumber());
23107 ashik.ali 382
		if(updateRetailerRequest.getNumber() == null || updateRetailerRequest.getNumber().isEmpty()){
383
			retailer.setType(RetailerType.UNREGISTERED_SHOP);
384
		}else{
385
			retailer.setType(RetailerType.GSTIN);
386
		}
23025 ashik.ali 387
		if(updateRetailerRequest.getDocumentId() > 0){
388
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
389
		}
390
		retailerRepository.persist(retailer);
23059 ashik.ali 391
		return retailer;
23025 ashik.ali 392
	}
393
 
23329 ashik.ali 394
	private void updateSaholicUser(int retailerId, int retailerAddressId){
395
		try {
396
			com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectById(retailerId);
397
			user.setAddressId(retailerAddressId);
398
			userUserRepository.persist(user);
399
		} catch (ProfitMandiBusinessException e) {
400
 
401
		}
402
	}
403
 
404
	private void createPrivateDealUser(User user, boolean fofo, String gstNumber, Retailer retailer, int retailerAddressId){
405
		PrivateDealUser privateDealUser = null;
406
		try{
407
			privateDealUser = privateDealUserRepository.selectById(retailer.getId());
408
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
409
			LOGGER.error("PrivateDealUser not found");
410
		}
411
 
412
		 //= privateDealUserRepository.selectById(saholicUser.getId());
413
		if(privateDealUser == null){
414
			Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
23473 ashik.ali 415
			try {
416
				this.createPrivateDealUser(retailer.getId(), counterId, fofo);
417
			}catch (Exception e) {
418
				LOGGER.error("ERROR : ", e);
419
			}
23329 ashik.ali 420
		}else{
421
			if(privateDealUser.getCounterId() == null){
422
				Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
423
				privateDealUser.setCounterId(counterId);
424
				privateDealUser.setFofo(fofo);
425
				privateDealUserRepository.persist(privateDealUser);
426
			}else{
427
				Counter counter = null;
428
				try{
429
					counter = counterRepository.selectById(privateDealUser.getCounterId());
430
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
431
					LOGGER.error("Counter not found with id [{}]", privateDealUser.getCounterId());
432
				}
433
				if(counter == null){
434
					this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(), retailerAddressId);
435
				}else{
436
					counter.setGstin(gstNumber);
437
					counterRepository.persist(counter);
438
				}
439
				privateDealUser.setFofo(fofo);
440
				privateDealUserRepository.persist(privateDealUser);
441
			}
442
		}
443
 
444
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
445
		privateDealUserAddressMapping.setUserId(retailer.getId());
446
		privateDealUserAddressMapping.setAddressId(retailerAddressId);
447
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
448
	}
449
 
450
	//Specifically set isFofo to true that has to be used by old system
451
	private void createPrivateDealUser(int retailerId, int counterId, boolean fofo){
452
		PrivateDealUser privateDealUser = new PrivateDealUser();
453
		privateDealUser.setActive(true);
454
		privateDealUser.setBulkShipmentAmountLimit(fofo ? 1000000 : 50000);
455
		privateDealUser.setId(retailerId);
456
		privateDealUser.setCounterId(counterId);
457
		privateDealUser.setFofo(fofo);
458
		privateDealUserRepository.persist(privateDealUser);
459
	}
460
 
461
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
462
		if(gstNumber != null && !gstNumber.isEmpty()){
463
			Counter counter = new Counter();
464
			counter.setEmailId(emailId);
465
			counter.setGstin(gstNumber);
466
			counter.setMobileNumber(mobileNumber);
467
			counter.setName(name);
468
			counter.setAddressId(addressId);
469
			counterRepository.persist(counter);
470
			return counter.getId();
471
		}else{
472
			return null;
473
		}
474
	}
475
 
23059 ashik.ali 476
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId) throws ProfitMandiBusinessException{
23043 ashik.ali 477
		if(address == null){
478
			address = new Address();
479
			address.setRetaierId(retailerId);
480
			this.updateAddress(address, customAddress);
481
			//addressRepository.persist(addressRetailer);
482
 
483
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
484
			retailerRegisteredAddress.setRetailerId(retailerId);
485
			retailerRegisteredAddress.setAddressId(address.getId());
486
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
487
		}
23059 ashik.ali 488
		return address;
23043 ashik.ali 489
	}
490
 
23025 ashik.ali 491
	private void updateAddress(Address address, CustomAddress customAddress){
492
		address.setName(customAddress.getName());
493
		address.setLine1(customAddress.getLine1());
494
		address.setLine2(customAddress.getLine2());
495
		address.setCity(customAddress.getCity());
496
		address.setPinCode(customAddress.getPinCode());
497
		address.setState(customAddress.getState());
498
		addressRepository.persist(address);
499
	}
500
 
23079 ashik.ali 501
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 502
		if(shops.isEmpty()){
23025 ashik.ali 503
			for(CustomShop customShop : customShops){
23043 ashik.ali 504
				Shop shop = new Shop();
23079 ashik.ali 505
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 506
				shops.add(shop);
507
			}
508
		}else{
509
			for(Shop shop : shops){
510
				for(CustomShop customShop : customShops){
511
					if(shop.getId() == customShop.getShopId()){
23079 ashik.ali 512
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 513
					}
514
				}
515
			}
23043 ashik.ali 516
			for(CustomShop customShop : customShops){
517
				if(customShop.getShopId() == 0){
518
					Shop shop = new Shop();
23079 ashik.ali 519
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 520
					shops.add(shop);
521
				}
522
			}
23025 ashik.ali 523
		}
524
	}
525
 
23079 ashik.ali 526
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId, Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException{
23043 ashik.ali 527
		shop.setRetailerId(retailerId);
528
		shop.setName(customShop.getName());
529
		if(customShop.getDocumentId() > 0){
530
			shop.setDocumentId(customShop.getDocumentId());
531
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
532
		}
533
		if(shop.getAddressId() == null){
23079 ashik.ali 534
			Address address = null;
535
			if(customShop.isSameAsRetailerAddress()){
536
				address = sameAsRetailerAddressValue;
537
			}else{
538
				//shop.setDocumentId(customShop.getDocumentId());
539
				address = new Address();
540
				this.updateAddress(address, customShop.getAddress());
541
			}
23043 ashik.ali 542
			shop.setAddressId(address.getId());
543
			shop.setAddress(address);
544
			shopRepository.persist(shop);
23131 ashik.ali 545
			ShopAddress shopAddress = null;
546
			try{
547
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
548
				shopAddress.setAddressId(address.getId());
549
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
550
				shopAddress = new ShopAddress();
551
				shopAddress.setAddressId(address.getId());
552
				shopAddress.setShopId(shop.getId());
553
			}
23043 ashik.ali 554
			shopAddressRepository.persist(shopAddress);
555
		}else{
556
			Address address = addressRepository.selectById(shop.getAddressId());
23329 ashik.ali 557
			LOGGER.info("found address : {}", address);
23079 ashik.ali 558
			CustomAddress customAddress = customShop.getAddress();
23329 ashik.ali 559
			LOGGER.info("requested address : {}", customAddress);
560
			if(!(address.getName().equals(customAddress.getName()) &&
23079 ashik.ali 561
				address.getLine1().equals(customAddress.getLine1()) &&
562
				address.getLine2().equals(customAddress.getLine2())	&&
563
				address.getCity().equals(customAddress.getCity()) &&
564
				address.getPinCode().equals(customAddress.getPinCode()) &&
23329 ashik.ali 565
				address.getState().equals(customAddress.getState()))){
23079 ashik.ali 566
				this.updateAddress(address, customShop.getAddress());
567
			}else{
568
				address = new Address();
569
				ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
570
				this.updateAddress(address, customAddress);
571
				shopAddress.setAddressId(address.getId());
572
				shopAddressRepository.persist(shopAddress);
573
			}
574
 
23043 ashik.ali 575
			shop.setAddress(address);
576
			shopRepository.persist(shop);
577
		}
578
 
579
	}
23025 ashik.ali 580
 
23043 ashik.ali 581
 
22980 ashik.ali 582
	private void addAddress(List<Shop> shops){
583
		Set<Integer> shopIds = this.toShopIds(shops);
584
		if(shopIds.isEmpty()){
585
			return;
586
		}
587
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
588
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
589
 
590
		for(Shop shop : shops){
591
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
592
		}
593
	}
594
 
595
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses){
596
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
597
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
598
		List<Address> addresses = addressRepository.selectByIds(addressIds);
599
		for(Address address : addresses){
600
			addressIdAddressMap.put(address.getId(), address);
601
		}
602
		return addressIdAddressMap;
603
	}
604
 
605
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses){
606
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>(){
607
			@Override
608
			public Integer apply(ShopAddress shopAddress) {
609
				return shopAddress.getAddressId();
610
			}
611
		};
612
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
613
	}
614
 
615
	private Set<Integer> toShopIds(List<Shop> shops){
616
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>(){
617
			@Override
618
			public Integer apply(Shop shop) {
619
				return shop.getId();
620
			}
621
		};
622
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
623
	}
624
 
625
	private String toString(List<UserRole> userRoles){
626
		Function<UserRole, String> userRoleToRoleNameFunction = new Function<UserRole, String>(){
627
			public String apply(UserRole userRole) {
628
				return userRole.getRoleType().toString();
629
			};
630
		};
631
		Set<String> userRoleStrings = userRoles.stream().map(userRoleToRoleNameFunction).collect(Collectors.toSet());
632
		return String.join(", ", userRoleStrings);
633
	}
23329 ashik.ali 634
 
23378 ashik.ali 635
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName) throws ProfitMandiBusinessException{
636
		FofoStore fofoStore = null;
637
		try{
638
			fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
639
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
640
 
641
		}
642
 
643
		if(fofoStore != null){
644
			LOGGER.error(ResponseCodeHolder.getMessage("USR_1014"));
645
			//throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "USR_1014");
646
		}else {
647
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
648
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
649
 
650
			StateInfo stateInfo = null;
651
			try {
652
				stateInfo = Utils.getStateInfo(retailerAddress.getState());
653
			} catch (Exception e) {
654
				// TODO Auto-generated catch block
655
				e.printStackTrace();
656
				//throw new ProfitMandiBusinessException();
657
			}
658
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName, stateInfo.getShortName());
659
 
660
			FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
661
			boolean foundFofoStoreSequence = false;
662
			try{
663
				fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
664
				foundFofoStoreSequence = true;
665
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
666
				fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
667
				fofoStoreSequenceGeneration.setSequence(1);
668
				fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
669
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
670
			}
671
 
672
			fofoStore = new FofoStore();
673
			fofoStore.setId(retailerId);
674
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
675
			if(foundFofoStoreSequence){
676
				fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
677
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
678
			}
679
			fofoStore.setCode(fofoStoreCode);
680
			fofoStoreRepository.persist(fofoStore);
681
		}
682
 
683
		return fofoStore;
684
	}
685
 
23329 ashik.ali 686
	@Override
23378 ashik.ali 687
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName) throws ProfitMandiBusinessException {
23329 ashik.ali 688
		User user = userRepository.selectById(userId);
689
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
690
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
691
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
692
		try{
693
			userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.FOFO);
694
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
695
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
696
		}
23378 ashik.ali 697
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName);
23329 ashik.ali 698
 
699
	}
700
 
701
	@Override
702
	public List<DistrictMaster> getAllDistrictMaster(String stateName) {
703
		StateInfo stateInfo = null;
704
		try {
705
			stateInfo = Utils.getStateInfo(stateName);
706
		} catch (Exception e) {
707
			e.printStackTrace();
708
			//throw new ProfitMandiBusinessException();
709
		}
710
		return districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
711
	}
22980 ashik.ali 712
 
713
}