Subversion Repositories SmartDukaan

Rev

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