Subversion Repositories SmartDukaan

Rev

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