Subversion Repositories SmartDukaan

Rev

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