Subversion Repositories SmartDukaan

Rev

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