Subversion Repositories SmartDukaan

Rev

Rev 26264 | Rev 26527 | 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;
24349 amit.gupta 17
import org.springframework.cache.annotation.Cacheable;
22980 ashik.ali 18
import org.springframework.stereotype.Component;
19
 
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;
24349 amit.gupta 41
import com.spice.profitmandi.dao.entity.inventory.State;
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;
26522 amit.gupta 45
import com.spice.profitmandi.dao.entity.user.LoginRequestResponseModel;
23329 ashik.ali 46
import com.spice.profitmandi.dao.entity.user.PrivateDealUser;
47
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressMapping;
23063 ashik.ali 48
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
23106 ashik.ali 49
import com.spice.profitmandi.dao.enumuration.dtr.RetailerType;
23136 ashik.ali 50
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
23365 ashik.ali 51
import com.spice.profitmandi.dao.enumuration.fofo.PaymentOptionType;
23329 ashik.ali 52
import com.spice.profitmandi.dao.repository.dtr.DistrictMasterRepository;
22980 ashik.ali 53
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
23329 ashik.ali 54
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
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;
24349 amit.gupta 65
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
22980 ashik.ali 66
import com.spice.profitmandi.dao.repository.user.AddressRepository;
23043 ashik.ali 67
import com.spice.profitmandi.dao.repository.user.CartRepository;
23329 ashik.ali 68
import com.spice.profitmandi.dao.repository.user.CounterRepository;
69
import com.spice.profitmandi.dao.repository.user.PrivateDealUserAddressMappingRepository;
70
import com.spice.profitmandi.dao.repository.user.PrivateDealUserRepository;
22980 ashik.ali 71
 
23329 ashik.ali 72
import in.shop2020.model.v1.inventory.StateInfo;
23043 ashik.ali 73
import in.shop2020.model.v1.user.CartStatus;
74
 
22980 ashik.ali 75
@Component
76
public class RetailerServiceImpl implements RetailerService {
23955 govind 77
 
23568 govind 78
	private static final Logger LOGGER = LogManager.getLogger(RetailerServiceImpl.class);
23955 govind 79
 
22980 ashik.ali 80
	@Autowired
81
	private RetailerRepository retailerRepository;
23955 govind 82
 
22980 ashik.ali 83
	@Autowired
84
	private UserAccountRepository userAccountRepository;
23955 govind 85
 
22980 ashik.ali 86
	@Autowired
87
	private UserRepository userRepository;
23955 govind 88
 
22980 ashik.ali 89
	@Autowired
23043 ashik.ali 90
	private CartRepository cartRepository;
23955 govind 91
 
23043 ashik.ali 92
	@Autowired
22980 ashik.ali 93
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
23955 govind 94
 
22980 ashik.ali 95
	@Autowired
96
	private AddressRepository addressRepository;
23955 govind 97
 
22980 ashik.ali 98
	@Autowired
99
	private ShopRepository shopRepository;
23955 govind 100
 
22980 ashik.ali 101
	@Autowired
102
	private ShopAddressRepository shopAddressRepository;
23955 govind 103
 
22980 ashik.ali 104
	@Autowired
105
	private UserRoleRepository userRoleRepository;
23955 govind 106
 
22980 ashik.ali 107
	@Autowired
108
	private DocumentRepository documentRepository;
23955 govind 109
 
23043 ashik.ali 110
	@Autowired
23329 ashik.ali 111
	private PrivateDealUserRepository privateDealUserRepository;
23955 govind 112
 
23329 ashik.ali 113
	@Autowired
114
	private PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
23955 govind 115
 
23329 ashik.ali 116
	@Autowired
117
	private CounterRepository counterRepository;
25295 amit.gupta 118
 
24349 amit.gupta 119
	@Autowired
120
	private StateRepository stateRepository;
23955 govind 121
 
23329 ashik.ali 122
	@Autowired
23043 ashik.ali 123
	@Qualifier("userUserRepository")
124
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
23955 govind 125
 
23329 ashik.ali 126
	@Autowired
127
	private DistrictMasterRepository districtMasterRepository;
23955 govind 128
 
23329 ashik.ali 129
	@Autowired
130
	private FofoStoreRepository fofoStoreRepository;
23955 govind 131
 
23329 ashik.ali 132
	@Autowired
23365 ashik.ali 133
	private PaymentOptionRepository paymentOptionRepository;
23955 govind 134
 
23365 ashik.ali 135
	@Autowired
136
	private FofoPartnerPaymentOptionRepository fofoPartnerPaymentOptionRepository;
23955 govind 137
 
23842 ashik.ali 138
	@Autowired
139
	private RoleRepository roleRepository;
23955 govind 140
 
22980 ashik.ali 141
	@Override
142
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
143
			throws ProfitMandiBusinessException {
23202 ashik.ali 144
		User user = null;
23955 govind 145
		try {
23202 ashik.ali 146
			user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
23955 govind 147
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
148
 
23202 ashik.ali 149
		}
23955 govind 150
		if (user == null) {
151
			try {
23329 ashik.ali 152
				user = userRepository.selectBySecondryEmailId(emailIdOrMobileNumber);
23955 govind 153
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
154
 
23329 ashik.ali 155
			}
23202 ashik.ali 156
		}
22980 ashik.ali 157
		Map<String, Object> map = new HashMap<>();
25295 amit.gupta 158
		map.put("stateNames", stateRepository.selectAll().stream().map(x -> x.getName()).collect(Collectors.toList()));
23955 govind 159
 
160
		if (user != null) {
161
 
23329 ashik.ali 162
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
23955 govind 163
 
23781 ashik.ali 164
			Role role = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 165
 
23329 ashik.ali 166
			map.put("userRoles", userRoles);
167
			map.put("user", user);
23955 govind 168
			// map.put("retailer", retailer);
23781 ashik.ali 169
			map.put("fofoRole", this.containsRoleType(userRoles, role.getId()));
23955 govind 170
 
23329 ashik.ali 171
			map.put("userRoleNames", this.toString(userRoles));
23955 govind 172
			try {
23329 ashik.ali 173
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
174
				Retailer retailer = retailerRepository.selectById(retailerId);
23955 govind 175
 
23329 ashik.ali 176
				map.put("retailer", retailer);
23955 govind 177
 
178
				if (this.containsRoleType(userRoles, role.getId())) {
179
					try {
23329 ashik.ali 180
						FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
181
						map.put("fofoStore", fofoStore);
23955 govind 182
						// map.put("counterSize", fofoStore.getCounterSize().toString());
183
						// LOGGER.info("fofoStore" + fofoStore);
184
					} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 185
						LOGGER.error("FofoStore code not found");
186
					}
187
				}
23955 govind 188
 
189
				try {
23329 ashik.ali 190
					PrivateDealUser privateDealUser = privateDealUserRepository.selectById(retailer.getId());
23955 govind 191
					if (privateDealUser.getCounterId() != null) {
23329 ashik.ali 192
						Counter counter = counterRepository.selectById(privateDealUser.getCounterId());
193
						map.put("gstNumber", counter.getGstin());
23955 govind 194
						// LOGGER.info("gstNumber" + counter.getGstin());
23329 ashik.ali 195
					}
23955 govind 196
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 197
					LOGGER.error("PrivateDealUser not found");
198
				}
23955 govind 199
				try {
200
					int retailerAddressId = retailerRegisteredAddressRepository
201
							.selectAddressIdByRetailerId(retailer.getId());
23329 ashik.ali 202
					Address retailerAddress = addressRepository.selectById(retailerAddressId);
203
					map.put("retailerAddress", retailerAddress);
24349 amit.gupta 204
					State state = stateRepository.selectByName(retailerAddress.getState());
23955 govind 205
					// LOGGER.info("retailerAddress.." + retailerAddress);
206
					List<DistrictMaster> districtMasters = districtMasterRepository
24349 amit.gupta 207
							.selectByStateShortName(state.getShortName());
23329 ashik.ali 208
					map.put("districtMasters", districtMasters);
23955 govind 209
					// LOGGER.info("districtMasters" + districtMasters);
23329 ashik.ali 210
					List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
23955 govind 211
					map.put("shops", shops);
23329 ashik.ali 212
					this.addAddress(shops);
23955 govind 213
					// LOGGER.info("shops" + shops);
214
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 215
					LOGGER.error("Retailer Registered Address not found");
216
				}
23955 govind 217
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 218
				LOGGER.error("Retailer not found in user_account");
23043 ashik.ali 219
			}
220
		}
23955 govind 221
 
22980 ashik.ali 222
		return map;
223
	}
23955 govind 224
 
225
	private boolean containsRoleType(List<UserRole> userRoles, int roleId) {
226
		for (UserRole userRole : userRoles) {
227
			if (userRole.getRoleId() == roleId) {
23329 ashik.ali 228
				return true;
229
			}
230
		}
231
		return false;
232
	}
23955 govind 233
 
234
	private List<UserRole> addRole(List<UserRole> userRoles, int userId, int roleId) {
235
		if (userRoles == null) {
23329 ashik.ali 236
			userRoles = new ArrayList<>();
237
		}
23955 govind 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;
23955 govind 250
 
23329 ashik.ali 251
	}
23955 govind 252
 
25295 amit.gupta 253
	private List<UserRole> removeRole(List<UserRole> userRoles, int userId, int roleId) {
254
		if (userRoles == null) {
255
			userRoles = new ArrayList<>();
256
			return userRoles;
257
		}
258
		try {
259
			userRoles.remove(userRoleRepository.deleteByRoleUserId(userId, roleId));
260
		} catch (Exception e) {
261
 
262
		}
263
		return userRoles;
264
 
265
	}
266
 
22980 ashik.ali 267
	@SuppressWarnings("unchecked")
268
	@Override
23025 ashik.ali 269
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
270
			throws ProfitMandiBusinessException {
271
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
23955 govind 272
		User user = (User) map.get("user");
23329 ashik.ali 273
		user = this.createUser(user, updateRetailerRequest);
274
		map.put("user", user);
23955 govind 275
		List<UserRole> userRoles = (List<UserRole>) map.get("userRoles");
23781 ashik.ali 276
		Role roleUser = roleRepository.selectByName(RoleType.USER.toString());
277
		userRoles = this.addRole(userRoles, user.getId(), roleUser.getId());
23955 govind 278
		Retailer retailer = (Retailer) map.get("retailer");
23329 ashik.ali 279
		retailer = this.updateRetailer(user, retailer, updateRetailerRequest);
280
		map.put("retailer", retailer);
23955 govind 281
 
23781 ashik.ali 282
		Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
23955 govind 283
 
23781 ashik.ali 284
		userRoles = this.addRole(userRoles, user.getId(), roleRetailer.getId());
23955 govind 285
 
23781 ashik.ali 286
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 287
 
288
		if (updateRetailerRequest.isFofo()) {
23781 ashik.ali 289
			userRoles = this.addRole(userRoles, user.getId(), roleFofo.getId());
25295 amit.gupta 290
		} else {
291
			userRoles = this.removeRole(userRoles, user.getId(), roleFofo.getId());
23061 ashik.ali 292
		}
25295 amit.gupta 293
		map.put("fofoRole", this.containsRoleType(userRoles, roleFofo.getId()));
23955 govind 294
 
295
		if (this.containsRoleType(userRoles, roleFofo.getId())) {
23365 ashik.ali 296
			this.createDefaultPaymentOption(retailer.getId());
23329 ashik.ali 297
		}
23955 govind 298
 
23329 ashik.ali 299
		map.put("userRoles", userRoles);
300
		map.put("userRoleNames", this.toString(userRoles));
23955 govind 301
 
302
		Address retailerAddress = (Address) map.get("retailerAddress");
303
		retailerAddress = this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(),
304
				retailer.getId());
23329 ashik.ali 305
		map.put("retailerAddress", retailerAddress);
23955 govind 306
 
307
		if (updateRetailerRequest.isFofo()) {
308
			FofoStore fofoStore = this.createFofoStoreCodeByRetailerId(retailer.getId(),
309
					updateRetailerRequest.getDistrictName(), retailerAddress.getState(), updateRetailerRequest);
23378 ashik.ali 310
			map.put("fofoStore", fofoStore);
25297 amit.gupta 311
			fofoStore.setActive(updateRetailerRequest.isActive());
23061 ashik.ali 312
		}
23955 govind 313
 
314
		this.createPrivateDealUser(user, updateRetailerRequest.isFofo(), updateRetailerRequest.getGstNumber(), retailer,
315
				retailerAddress.getId());
23329 ashik.ali 316
		map.put("gstNumber", updateRetailerRequest.getGstNumber());
317
		this.updateSaholicUser(retailer.getId(), retailerAddress.getId());
23955 govind 318
 
319
		List<Shop> shops = (List<Shop>) map.get("shops");
320
		if (shops == null) {
23060 ashik.ali 321
			shops = new ArrayList<>();
23061 ashik.ali 322
			map.put("shops", shops);
23060 ashik.ali 323
		}
23079 ashik.ali 324
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId(), retailerAddress);
23955 govind 325
 
23025 ashik.ali 326
		return map;
23955 govind 327
 
23025 ashik.ali 328
	}
23955 govind 329
 
330
	private void createDefaultPaymentOption(int fofoId) {
23365 ashik.ali 331
		List<Integer> paymentOptionIds = fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(fofoId);
23955 govind 332
		if (paymentOptionIds.isEmpty()) {
23365 ashik.ali 333
			PaymentOption paymentOption = null;
23955 govind 334
			try {
23365 ashik.ali 335
				paymentOption = paymentOptionRepository.selectByName(PaymentOptionType.CASH.toString());
23955 govind 336
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23365 ashik.ali 337
				paymentOption = new PaymentOption();
338
				paymentOption.setName(PaymentOptionType.CASH.toString());
339
				paymentOptionRepository.persist(paymentOption);
340
			}
341
			FofoPartnerPaymentOption fofoPartnerPaymentOption = new FofoPartnerPaymentOption();
342
			fofoPartnerPaymentOption.setFofoId(fofoId);
343
			fofoPartnerPaymentOption.setPaymentOptionId(paymentOption.getId());
344
			fofoPartnerPaymentOptionRepository.persist(fofoPartnerPaymentOption);
345
		}
23955 govind 346
 
23365 ashik.ali 347
	}
23955 govind 348
 
349
	private User createUser(User user, UpdateRetailerRequest updateRetailerRequest) {
350
		if (user == null) {
23329 ashik.ali 351
			user = new User();
352
			user.setCity("");
353
			user.setPinCode(0);
354
			user.setState("");
355
			user.setPassword("");
356
			user.setMobile_verified(false);
357
			user.setReferral_url("");
358
			user.setGroup_id(1);
359
			user.setStatus(1);
360
			user.setActivated(true);
361
			user.setCreateTimestamp(LocalDateTime.now());
362
		}
24705 amit.gupta 363
		user.setActivated(updateRetailerRequest.isActive());
23329 ashik.ali 364
		user.setFirstName(updateRetailerRequest.getUserFirstName());
365
		user.setLastName(updateRetailerRequest.getUserLastName());
366
		user.setMobileNumber(updateRetailerRequest.getUserMobileNumber());
367
		user.setEmailId(updateRetailerRequest.getUserEmailId());
368
		user.setUsername(updateRetailerRequest.getUserEmailId());
23509 amit.gupta 369
		user.setUpdateTimestamp(LocalDateTime.now());
370
		userRepository.persist(user);
23329 ashik.ali 371
		return user;
23955 govind 372
 
23329 ashik.ali 373
	}
23955 govind 374
 
375
	private int createSaholicUser(User user, String retailerName) {
23043 ashik.ali 376
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
377
		try {
23063 ashik.ali 378
			saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
23955 govind 379
		} catch (ProfitMandiBusinessException e) {
23043 ashik.ali 380
			LOGGER.info("User doesnt exist in old system");
381
		}
23955 govind 382
		if (saholicUser == null) {
23043 ashik.ali 383
			Cart cart = new Cart();
384
			cart.setCartStatus(CartStatus.ACTIVE);
385
			cartRepository.persist(cart);
386
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
23063 ashik.ali 387
			saholicUser.setEmailId(user.getEmailId());
23043 ashik.ali 388
			saholicUser.setName(retailerName);
389
			saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 390
			saholicUser.setCreateTimestamp(LocalDateTime.now());
23043 ashik.ali 391
			userUserRepository.persist(saholicUser);
23955 govind 392
 
23269 ashik.ali 393
			UserAccount ua = new UserAccount();
394
			ua.setAccountKey(saholicUser.getId());
395
			ua.setUserId(user.getId());
396
			ua.setType(AccountType.saholic);
23063 ashik.ali 397
			userAccountRepository.persist(ua);
398
 
23269 ashik.ali 399
			UserAccount ua2 = new UserAccount();
400
			ua2.setAccountKey(saholicUser.getActiveCartId());
401
			ua2.setUserId(user.getId());
402
			ua2.setType(AccountType.cartId);
23063 ashik.ali 403
			userAccountRepository.persist(ua2);
23955 govind 404
			LOGGER.info("created....");
23043 ashik.ali 405
		}
406
		return saholicUser.getId();
407
	}
23955 govind 408
 
26522 amit.gupta 409
	private com.spice.profitmandi.dao.entity.user.User createSaholicUser(
410
			LoginRequestResponseModel loginRequestResponseModel) {
411
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
412
		try {
413
			saholicUser = userUserRepository.selectByEmailId(loginRequestResponseModel.getEmail());
414
		} catch (ProfitMandiBusinessException e) {
415
			LOGGER.info("User doesnt exist in old system");
416
		}
417
		if (saholicUser == null) {
418
			Cart cart = new Cart();
419
			cart.setCartStatus(CartStatus.ACTIVE);
420
			cartRepository.persist(cart);
421
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
422
			saholicUser.setPassword(loginRequestResponseModel.getPassword());
423
			saholicUser.setEmailId(loginRequestResponseModel.getEmail());
424
			saholicUser.setName(loginRequestResponseModel.getCustomerName());
425
			saholicUser.setActiveCartId(cart.getId());
426
			saholicUser.setCreateTimestamp(LocalDateTime.now());
427
			loginRequestResponseModel.setPassword(null);
428
			userUserRepository.persist(saholicUser);
429
		}
430
		return saholicUser;
431
	}
432
 
23955 govind 433
	private Retailer updateRetailer(User user, Retailer retailer, UpdateRetailerRequest updateRetailerRequest)
434
			throws ProfitMandiBusinessException {
435
		if (retailer == null) {
436
			LOGGER.info("createSaholicUser.....");
23063 ashik.ali 437
			int saholicUserId = this.createSaholicUser(user, updateRetailerRequest.getName());
23043 ashik.ali 438
			retailer = new Retailer();
439
			retailer.setId(saholicUserId);
440
		}
23329 ashik.ali 441
		retailer.setActive(updateRetailerRequest.isActive());
24704 amit.gupta 442
		user.setActivated(updateRetailerRequest.isActive());//
23955 govind 443
		// this.createRole(user.getId(), RoleType.RETAILER);
23025 ashik.ali 444
		retailer.setName(updateRetailerRequest.getName());
445
		retailer.setNumber(updateRetailerRequest.getNumber());
23955 govind 446
		if (updateRetailerRequest.getNumber() == null || updateRetailerRequest.getNumber().isEmpty()) {
23107 ashik.ali 447
			retailer.setType(RetailerType.UNREGISTERED_SHOP);
23955 govind 448
		} else {
23107 ashik.ali 449
			retailer.setType(RetailerType.GSTIN);
450
		}
23955 govind 451
		if (updateRetailerRequest.getDocumentId() > 0) {
452
			if (retailer.getDocumentId() != null && retailer.getDocumentId() != updateRetailerRequest.getDocumentId()) {
24374 amit.gupta 453
				try {
454
					documentRepository.deleteById(retailer.getDocumentId());
25295 amit.gupta 455
				} catch (Exception e) {
24374 amit.gupta 456
					e.printStackTrace();
457
				}
23489 ashik.ali 458
			}
23497 ashik.ali 459
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
460
			documentRepository.markDocumentAsPersisted(updateRetailerRequest.getDocumentId());
23025 ashik.ali 461
		}
462
		retailerRepository.persist(retailer);
23059 ashik.ali 463
		return retailer;
23025 ashik.ali 464
	}
23955 govind 465
 
466
	private void updateSaholicUser(int retailerId, int retailerAddressId) {
23329 ashik.ali 467
		try {
468
			com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectById(retailerId);
469
			user.setAddressId(retailerAddressId);
470
		} catch (ProfitMandiBusinessException e) {
23955 govind 471
 
23329 ashik.ali 472
		}
473
	}
23955 govind 474
 
475
	private void createPrivateDealUser(User user, boolean fofo, String gstNumber, Retailer retailer,
476
			int retailerAddressId) {
23329 ashik.ali 477
		PrivateDealUser privateDealUser = null;
23955 govind 478
		try {
23329 ashik.ali 479
			privateDealUser = privateDealUserRepository.selectById(retailer.getId());
23955 govind 480
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 481
			LOGGER.error("PrivateDealUser not found");
482
		}
23955 govind 483
 
484
		// = privateDealUserRepository.selectById(saholicUser.getId());
485
		if (privateDealUser == null) {
486
			Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(),
487
					retailer.getName(), retailerAddressId);
23473 ashik.ali 488
			try {
489
				this.createPrivateDealUser(retailer.getId(), counterId, fofo);
23955 govind 490
			} catch (Exception e) {
23473 ashik.ali 491
				LOGGER.error("ERROR : ", e);
492
			}
23955 govind 493
		} else {
494
			if (privateDealUser.getCounterId() == null) {
495
				Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(),
496
						retailer.getName(), retailerAddressId);
23329 ashik.ali 497
				privateDealUser.setCounterId(counterId);
498
				privateDealUser.setFofo(fofo);
499
				privateDealUserRepository.persist(privateDealUser);
23955 govind 500
			} else {
23329 ashik.ali 501
				Counter counter = null;
23955 govind 502
				try {
23329 ashik.ali 503
					counter = counterRepository.selectById(privateDealUser.getCounterId());
23955 govind 504
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 505
					LOGGER.error("Counter not found with id [{}]", privateDealUser.getCounterId());
506
				}
23955 govind 507
				if (counter == null) {
508
					this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(),
509
							retailerAddressId);
510
				} else {
23329 ashik.ali 511
					counter.setGstin(gstNumber);
512
					counterRepository.persist(counter);
513
				}
514
				privateDealUser.setFofo(fofo);
515
				privateDealUserRepository.persist(privateDealUser);
516
			}
517
		}
23955 govind 518
 
23329 ashik.ali 519
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
520
		privateDealUserAddressMapping.setUserId(retailer.getId());
521
		privateDealUserAddressMapping.setAddressId(retailerAddressId);
522
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
523
	}
23955 govind 524
 
525
	// Specifically set isFofo to true that has to be used by old system
526
	private void createPrivateDealUser(int retailerId, int counterId, boolean fofo) {
23329 ashik.ali 527
		PrivateDealUser privateDealUser = new PrivateDealUser();
528
		privateDealUser.setActive(true);
529
		privateDealUser.setBulkShipmentAmountLimit(fofo ? 1000000 : 50000);
530
		privateDealUser.setId(retailerId);
531
		privateDealUser.setCounterId(counterId);
532
		privateDealUser.setFofo(fofo);
533
		privateDealUserRepository.persist(privateDealUser);
534
	}
23955 govind 535
 
536
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId) {
537
		if (gstNumber != null && !gstNumber.isEmpty()) {
23329 ashik.ali 538
			Counter counter = new Counter();
539
			counter.setEmailId(emailId);
540
			counter.setGstin(gstNumber);
541
			counter.setMobileNumber(mobileNumber);
542
			counter.setName(name);
543
			counter.setAddressId(addressId);
544
			counterRepository.persist(counter);
545
			return counter.getId();
23955 govind 546
		} else {
23329 ashik.ali 547
			return null;
548
		}
549
	}
23955 govind 550
 
551
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId)
552
			throws ProfitMandiBusinessException {
553
		if (address == null) {
23043 ashik.ali 554
			address = new Address();
555
			address.setRetaierId(retailerId);
556
			this.updateAddress(address, customAddress);
23955 govind 557
			// addressRepository.persist(addressRetailer);
558
 
23043 ashik.ali 559
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
560
			retailerRegisteredAddress.setRetailerId(retailerId);
561
			retailerRegisteredAddress.setAddressId(address.getId());
562
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
23955 govind 563
		} else {
564
			this.updateAddress(address, customAddress);
565
			RetailerRegisteredAddress retailerRegisteredAddress = retailerRegisteredAddressRepository
566
					.selectByRetailerId(retailerId);
567
			retailerRegisteredAddress.setAddressId(address.getId());
568
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
23043 ashik.ali 569
		}
23059 ashik.ali 570
		return address;
23043 ashik.ali 571
	}
23955 govind 572
 
573
	private void updateAddress(Address address, CustomAddress customAddress) {
23025 ashik.ali 574
		address.setName(customAddress.getName());
23829 amit.gupta 575
		address.setPhoneNumber(customAddress.getPhoneNumber());
23025 ashik.ali 576
		address.setLine1(customAddress.getLine1());
577
		address.setLine2(customAddress.getLine2());
578
		address.setCity(customAddress.getCity());
579
		address.setPinCode(customAddress.getPinCode());
580
		address.setState(customAddress.getState());
581
		addressRepository.persist(address);
23955 govind 582
		LOGGER.info("Address Updated" + address);
23025 ashik.ali 583
	}
23955 govind 584
 
585
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId,
586
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
587
		if (shops.isEmpty()) {
588
			for (CustomShop customShop : customShops) {
23043 ashik.ali 589
				Shop shop = new Shop();
23079 ashik.ali 590
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 591
				shops.add(shop);
592
			}
23955 govind 593
		} else {
594
			for (Shop shop : shops) {
595
				for (CustomShop customShop : customShops) {
596
					if (shop.getId() == customShop.getShopId()) {
23079 ashik.ali 597
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 598
					}
599
				}
600
			}
23955 govind 601
			for (CustomShop customShop : customShops) {
602
				if (customShop.getShopId() == 0) {
23043 ashik.ali 603
					Shop shop = new Shop();
23079 ashik.ali 604
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 605
					shops.add(shop);
606
				}
607
			}
23025 ashik.ali 608
		}
609
	}
23955 govind 610
 
611
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId,
612
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
23043 ashik.ali 613
		shop.setRetailerId(retailerId);
614
		shop.setName(customShop.getName());
23955 govind 615
		if (customShop.getDocumentId() > 0) {
616
			if (shop.getDocumentId() != null && shop.getDocumentId() != customShop.getDocumentId()) {
24374 amit.gupta 617
				try {
618
					documentRepository.deleteById(shop.getDocumentId());
25295 amit.gupta 619
				} catch (Exception e) {
24374 amit.gupta 620
					e.printStackTrace();
621
				}
23489 ashik.ali 622
			}
23497 ashik.ali 623
			shop.setDocumentId(customShop.getDocumentId());
624
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
23043 ashik.ali 625
		}
23955 govind 626
		if (shop.getAddressId() == null) {
23079 ashik.ali 627
			Address address = null;
23955 govind 628
			if (customShop.isSameAsRetailerAddress()) {
23079 ashik.ali 629
				address = sameAsRetailerAddressValue;
23955 govind 630
			} else {
631
				// shop.setDocumentId(customShop.getDocumentId());
23079 ashik.ali 632
				address = new Address();
633
				this.updateAddress(address, customShop.getAddress());
634
			}
23043 ashik.ali 635
			shop.setAddressId(address.getId());
636
			shop.setAddress(address);
637
			shopRepository.persist(shop);
23131 ashik.ali 638
			ShopAddress shopAddress = null;
23955 govind 639
			try {
23131 ashik.ali 640
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
641
				shopAddress.setAddressId(address.getId());
23955 govind 642
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23131 ashik.ali 643
				shopAddress = new ShopAddress();
644
				shopAddress.setAddressId(address.getId());
645
				shopAddress.setShopId(shop.getId());
646
			}
23043 ashik.ali 647
			shopAddressRepository.persist(shopAddress);
23955 govind 648
		} else {
24375 amit.gupta 649
			Address address = null;
650
			try {
651
				address = addressRepository.selectById(shop.getAddressId());
652
				CustomAddress customAddress = customShop.getAddress();
653
				if (!(address.getName().equals(customAddress.getName())
654
						&& address.getLine1().equals(customAddress.getLine1())
655
						&& address.getLine2().equals(customAddress.getLine2())
656
						&& address.getCity().equals(customAddress.getCity())
657
						&& address.getPinCode().equals(customAddress.getPinCode())
658
						&& address.getState().equals(customAddress.getState()))) {
659
					address = new Address();
660
					ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
661
					this.updateAddress(address, customAddress);
662
					shopAddress.setAddressId(address.getId());
663
					shopAddressRepository.persist(shopAddress);
664
					shop.setAddress(address);
665
					shopRepository.persist(shop);
666
				}
25295 amit.gupta 667
			} catch (Exception e) {
24375 amit.gupta 668
				if (customShop.isSameAsRetailerAddress()) {
669
					address = sameAsRetailerAddressValue;
670
				} else {
671
					// shop.setDocumentId(customShop.getDocumentId());
672
					address = new Address();
673
					this.updateAddress(address, customShop.getAddress());
674
				}
675
				shop.setAddressId(address.getId());
676
				shop.setAddress(address);
677
				shopRepository.persist(shop);
678
				ShopAddress shopAddress = null;
679
				try {
680
					shopAddress = shopAddressRepository.selectByShopId(shop.getId());
681
					shopAddress.setAddressId(address.getId());
682
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
683
					shopAddress = new ShopAddress();
684
					shopAddress.setAddressId(address.getId());
685
					shopAddress.setShopId(shop.getId());
686
				}
25295 amit.gupta 687
				shopAddressRepository.persist(shopAddress);
688
			}
23955 govind 689
 
23043 ashik.ali 690
		}
23955 govind 691
 
23043 ashik.ali 692
	}
23955 govind 693
 
694
	private void addAddress(List<Shop> shops) {
22980 ashik.ali 695
		Set<Integer> shopIds = this.toShopIds(shops);
23955 govind 696
		if (shopIds.isEmpty()) {
22980 ashik.ali 697
			return;
698
		}
699
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
700
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
23955 govind 701
 
702
		for (Shop shop : shops) {
22980 ashik.ali 703
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
704
		}
705
	}
706
 
23955 govind 707
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses) {
22980 ashik.ali 708
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
709
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
710
		List<Address> addresses = addressRepository.selectByIds(addressIds);
23955 govind 711
		for (Address address : addresses) {
22980 ashik.ali 712
			addressIdAddressMap.put(address.getId(), address);
713
		}
714
		return addressIdAddressMap;
715
	}
23955 govind 716
 
717
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses) {
718
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>() {
22980 ashik.ali 719
			@Override
720
			public Integer apply(ShopAddress shopAddress) {
721
				return shopAddress.getAddressId();
722
			}
723
		};
724
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
725
	}
23955 govind 726
 
727
	private Set<Integer> toShopIds(List<Shop> shops) {
728
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>() {
22980 ashik.ali 729
			@Override
730
			public Integer apply(Shop shop) {
731
				return shop.getId();
732
			}
733
		};
734
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
735
	}
23955 govind 736
 
737
	private String toString(List<UserRole> userRoles) {
23781 ashik.ali 738
		Set<Integer> roleIds = new HashSet<>();
23955 govind 739
		for (UserRole userRole : userRoles) {
23781 ashik.ali 740
			roleIds.add(userRole.getRoleId());
741
		}
742
		List<Role> roles = roleRepository.selectByIds(roleIds);
23955 govind 743
		Function<Role, String> roleToNameFunction = new Function<Role, String>() {
23781 ashik.ali 744
			public String apply(Role role) {
745
				return String.valueOf(role.getName());
22980 ashik.ali 746
			};
747
		};
23781 ashik.ali 748
		Set<String> userRoleStrings = roles.stream().map(roleToNameFunction).collect(Collectors.toSet());
22980 ashik.ali 749
		return String.join(", ", userRoleStrings);
750
	}
23955 govind 751
 
752
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName,
753
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23378 ashik.ali 754
		FofoStore fofoStore = null;
23955 govind 755
		try {
23378 ashik.ali 756
			fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
23955 govind 757
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
758
 
23378 ashik.ali 759
		}
23955 govind 760
 
761
		if (fofoStore != null) {
762
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
763
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
26209 tejbeer 764
			fofoStore.setWarehouseId(updateRetailerRequest.getWarehouseId());
23955 govind 765
			fofoStoreRepository.persist(fofoStore);
766
 
767
		} else {
23378 ashik.ali 768
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
769
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
23955 govind 770
 
23378 ashik.ali 771
			StateInfo stateInfo = null;
772
			try {
773
				stateInfo = Utils.getStateInfo(retailerAddress.getState());
774
			} catch (Exception e) {
775
				// TODO Auto-generated catch block
776
				e.printStackTrace();
23955 govind 777
				// throw new ProfitMandiBusinessException();
23378 ashik.ali 778
			}
23955 govind 779
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName,
780
					stateInfo.getShortName());
781
 
23378 ashik.ali 782
			fofoStore = new FofoStore();
783
			fofoStore.setId(retailerId);
24023 amit.gupta 784
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
785
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
26209 tejbeer 786
			fofoStore.setWarehouseId(updateRetailerRequest.getWarehouseId());
23786 amit.gupta 787
			String latestStoreCode = fofoStoreRepository.selectLatestStore().getCode();
788
			int latestCodeCounter = Integer.parseInt(latestStoreCode.replaceAll("[A-Z]", ""));
23955 govind 789
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(
790
					districtMaster.getStateShortName() + districtMaster.getShortName(), latestCodeCounter + 1);
23378 ashik.ali 791
			fofoStore.setCode(fofoStoreCode);
792
			fofoStoreRepository.persist(fofoStore);
793
		}
23955 govind 794
 
23378 ashik.ali 795
		return fofoStore;
796
	}
23955 govind 797
 
23329 ashik.ali 798
	@Override
23955 govind 799
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName,
800
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23329 ashik.ali 801
		User user = userRepository.selectById(userId);
802
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
803
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
804
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
23781 ashik.ali 805
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 806
		try {
23781 ashik.ali 807
			userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleFofo.getId());
23955 govind 808
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 809
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
810
		}
23955 govind 811
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName, updateRetailerRequest);
812
 
23329 ashik.ali 813
	}
23955 govind 814
 
23329 ashik.ali 815
	@Override
816
	public List<DistrictMaster> getAllDistrictMaster(String stateName) {
817
		StateInfo stateInfo = null;
818
		try {
819
			stateInfo = Utils.getStateInfo(stateName);
820
		} catch (Exception e) {
821
			e.printStackTrace();
23955 govind 822
			// throw new ProfitMandiBusinessException();
23329 ashik.ali 823
		}
824
		return districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
825
	}
22980 ashik.ali 826
 
23509 amit.gupta 827
	@Override
828
	public Map<Integer, CustomRetailer> getFofoRetailers(List<Integer> fofoIds) {
829
		List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
25295 amit.gupta 830
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream()
831
				.filter(x -> x.getAddressId() != null).collect(Collectors.toMap(x -> x.getAddressId(), x -> x));
23903 amit.gupta 832
		List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
23509 amit.gupta 833
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
25295 amit.gupta 834
		for (Address address : addresses) {
23903 amit.gupta 835
			com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
23509 amit.gupta 836
			CustomRetailer customRetailer = new CustomRetailer();
23903 amit.gupta 837
			customRetailer.setEmail(user.getEmailId());
23509 amit.gupta 838
			customRetailer.setBusinessName(address.getName());
839
			customRetailer.setMobileNumber(address.getPhoneNumber());
23532 amit.gupta 840
			try {
26151 amit.gupta 841
				FofoStore fs = fofoStoreRepository.selectByRetailerId(address.getRetaierId());
842
				customRetailer.setCode(fs.getCode());
843
				customRetailer.setWarehouseId(fs.getWarehouseId());
26125 amit.gupta 844
			} catch (Exception e) {
845
				continue;
846
			}
847
			try {
23796 amit.gupta 848
				customRetailer.setCartId(user.getActiveCartId());
23532 amit.gupta 849
				PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
850
				Counter counter = counterRepository.selectById(pdu.getCounterId());
851
				customRetailer.setGstNumber(counter.getGstin());
23955 govind 852
			} catch (Exception e) {
23532 amit.gupta 853
				customRetailer.setGstNumber(null);
854
			}
855
			CustomAddress address1 = new CustomAddress();
856
			address1.setCity(address.getCity());
857
			address1.setState(address.getState());
858
			address1.setLine1(address.getLine1());
859
			address1.setLine2(address.getLine2());
860
			address1.setPinCode(address.getPinCode());
861
			address1.setName(address.getName());
862
			customRetailer.setAddress(address1);
23786 amit.gupta 863
			customRetailer.setPartnerId(address.getRetaierId());
26522 amit.gupta 864
			customRetailer.setDisplayName(
865
					customRetailer.getBusinessName() + "-" + customRetailer.getCode() + "- " + address1.getCity());
23509 amit.gupta 866
			customRetailersMap.put(address.getRetaierId(), customRetailer);
867
		}
868
		return customRetailersMap;
869
	}
25295 amit.gupta 870
 
23781 ashik.ali 871
	@Override
25295 amit.gupta 872
	public CustomRetailer getFofoRetailer(int fofoId) throws ProfitMandiBusinessException {
23957 tejbeer 873
		com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(fofoId);
26089 amit.gupta 874
		FofoStore store = fofoStoreRepository.selectByRetailerId(fofoId);
23957 tejbeer 875
		Address address = null;
876
		try {
877
			address = addressRepository.selectById(saholicUser.getAddressId());
878
		} catch (ProfitMandiBusinessException e1) {
879
			// TODO Auto-generated catch block
880
			e1.printStackTrace();
881
		}
882
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
25295 amit.gupta 883
		CustomRetailer customRetailer = new CustomRetailer();
884
		customRetailer.setEmail(saholicUser.getEmailId());
885
		customRetailer.setBusinessName(address.getName());
886
		customRetailer.setMobileNumber(address.getPhoneNumber());
887
		try {
888
			customRetailer.setCartId(saholicUser.getActiveCartId());
889
			PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
890
			Counter counter = counterRepository.selectById(pdu.getCounterId());
891
			customRetailer.setGstNumber(counter.getGstin());
892
		} catch (Exception e) {
893
			customRetailer.setGstNumber(null);
894
		}
895
		CustomAddress address1 = new CustomAddress();
896
		address1.setCity(address.getCity());
897
		address1.setState(address.getState());
898
		address1.setLine1(address.getLine1());
899
		address1.setLine2(address.getLine2());
900
		address1.setPinCode(address.getPinCode());
901
		address1.setName(address.getName());
26125 amit.gupta 902
		customRetailer.setDisplayName(address.getName() + " - " + address.getCity());
25295 amit.gupta 903
		customRetailer.setAddress(address1);
904
		customRetailer.setPartnerId(address.getRetaierId());
26089 amit.gupta 905
		customRetailer.setCode(store.getCode());
26112 amit.gupta 906
		customRetailer.setWarehouseId(store.getWarehouseId());
25295 amit.gupta 907
		customRetailersMap.put(address.getRetaierId(), customRetailer);
23957 tejbeer 908
		return customRetailer;
25295 amit.gupta 909
 
23957 tejbeer 910
	}
26125 amit.gupta 911
 
25383 tejbeer 912
	@Override
26125 amit.gupta 913
	public Map<Integer, CustomRetailer> getFofoRetailerUserId(List<Integer> fofoIds)
914
			throws ProfitMandiBusinessException {
25383 tejbeer 915
		List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
916
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream()
917
				.filter(x -> x.getAddressId() != null).collect(Collectors.toMap(x -> x.getAddressId(), x -> x));
918
		List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
919
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
920
		for (Address address : addresses) {
921
			com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
922
			CustomRetailer customRetailer = new CustomRetailer();
923
			customRetailer.setEmail(user.getEmailId());
924
			customRetailer.setBusinessName(address.getName());
925
			customRetailer.setMobileNumber(address.getPhoneNumber());
26125 amit.gupta 926
 
927
			int userId = userAccountRepository.selectUserIdByRetailerId(user.getId());
928
 
25383 tejbeer 929
			try {
930
				customRetailer.setCartId(user.getActiveCartId());
931
				PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
932
				Counter counter = counterRepository.selectById(pdu.getCounterId());
933
				customRetailer.setGstNumber(counter.getGstin());
934
			} catch (Exception e) {
935
				customRetailer.setGstNumber(null);
936
			}
937
			CustomAddress address1 = new CustomAddress();
938
			address1.setCity(address.getCity());
939
			address1.setState(address.getState());
940
			address1.setLine1(address.getLine1());
941
			address1.setLine2(address.getLine2());
942
			address1.setPinCode(address.getPinCode());
943
			address1.setName(address.getName());
944
			customRetailer.setAddress(address1);
945
			customRetailer.setPartnerId(address.getRetaierId());
946
			customRetailer.setDisplayName(customRetailer.getBusinessName() + "-" + address1.getCity());
947
			customRetailersMap.put(userId, customRetailer);
948
		}
949
		return customRetailersMap;
950
	}
25295 amit.gupta 951
 
23957 tejbeer 952
	@Override
23781 ashik.ali 953
	public Map<Integer, String> getAllFofoRetailerIdEmailIdMap() {
954
		Role roleFofo = null;
955
		try {
956
			roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
957
		} catch (ProfitMandiBusinessException e) {
958
			// TODO Auto-generated catch block
959
			e.printStackTrace();
960
		}
961
		Role roleRetailer = null;
962
		try {
963
			roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
964
		} catch (ProfitMandiBusinessException e) {
965
			// TODO Auto-generated catch block
966
			e.printStackTrace();
967
		}
968
		Set<Integer> roleIds = new HashSet<>();
969
		roleIds.add(roleFofo.getId());
970
		roleIds.add(roleRetailer.getId());
971
		List<Integer> userIds = userRoleRepository.selectUserIdsByRoleIds(roleIds);
972
		List<User> users = userRepository.selectAllByIds(new HashSet<>(userIds));
973
		Map<Integer, Integer> userIdRetailerIdMap = this.getUserIdRetailerIdMap(userIds);
974
		Map<Integer, String> retailerIdEmailIdMap = new HashMap<>();
23955 govind 975
		for (User user : users) {
23781 ashik.ali 976
			retailerIdEmailIdMap.put(userIdRetailerIdMap.get(user.getId()), user.getEmailId());
977
		}
978
		return retailerIdEmailIdMap;
979
	}
23955 govind 980
 
981
	private Map<Integer, Integer> getUserIdRetailerIdMap(List<Integer> userIds) {
23781 ashik.ali 982
		List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userIds));
983
		Map<Integer, Integer> userIdRetailerIdMap = new HashMap<>();
23955 govind 984
		for (UserAccount userAccount : userAccounts) {
23781 ashik.ali 985
			userIdRetailerIdMap.put(userAccount.getUserId(), userAccount.getAccountKey());
986
		}
987
		return userIdRetailerIdMap;
988
	}
25295 amit.gupta 989
 
990
	private Map<Integer, String> getUserIdEmailIdMap(Set<Integer> userIds) {
23880 ashik.ali 991
		List<User> users = userRepository.selectAllByIds(userIds);
992
		Map<Integer, String> userIdEmailIdMap = new HashMap<>();
23955 govind 993
		for (User user : users) {
23880 ashik.ali 994
			userIdEmailIdMap.put(user.getId(), user.getEmailId());
995
		}
996
		return userIdEmailIdMap;
997
	}
23509 amit.gupta 998
 
23880 ashik.ali 999
	@Override
1000
	public Map<Integer, String> getAllFofoRetailerIdEmailIdMap(Set<Integer> retailerIds) {
1001
		List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByRetailerIds(retailerIds);
1002
		Set<Integer> userIds = new HashSet<>();
23955 govind 1003
		for (UserAccount userAccount : userAccounts) {
23880 ashik.ali 1004
			userIds.add(userAccount.getUserId());
1005
		}
1006
		Map<Integer, String> retailerIdEmailIdMap = new HashMap<>();
1007
		Map<Integer, String> userIdEmailIdMap = this.getUserIdEmailIdMap(userIds);
23955 govind 1008
		for (UserAccount userAccount : userAccounts) {
23880 ashik.ali 1009
			retailerIdEmailIdMap.put(userAccount.getAccountKey(), userIdEmailIdMap.get(userAccount.getUserId()));
1010
		}
1011
		return retailerIdEmailIdMap;
1012
	}
25295 amit.gupta 1013
 
24168 amit.gupta 1014
	@Override
24349 amit.gupta 1015
	public Map<Integer, String> getAllFofoRetailerIdNameMap(List<Integer> storeIds) {
24168 amit.gupta 1016
		Map<Integer, CustomRetailer> retailersMap = this.getFofoRetailers(storeIds);
1017
		Map<Integer, String> retailerIdNameMap = new HashMap<>();
25295 amit.gupta 1018
		for (Map.Entry<Integer, CustomRetailer> entry : retailersMap.entrySet()) {
1019
			retailerIdNameMap.put(entry.getKey(),
1020
					entry.getValue().getBusinessName() + "-" + entry.getValue().getAddress().getCity());
24168 amit.gupta 1021
		}
1022
		return retailerIdNameMap;
1023
	}
1024
 
1025
	@Override
1026
	public Map<Integer, String> getAllFofoRetailerIdNameMap() {
24349 amit.gupta 1027
		List<FofoStore> stores = fofoStoreRepository.selectAll();
25295 amit.gupta 1028
		List<Integer> storeIds = stores.stream().map(x -> x.getId()).collect(Collectors.toList());
24349 amit.gupta 1029
		return this.getAllFofoRetailerIdNameMap(storeIds);
1030
	}
1031
 
1032
	@Override
1033
	public List<MapWrapper<Integer, String>> getAllFofoRetailerIdNameList() {
1034
		List<MapWrapper<Integer, String>> mapWrappers = new ArrayList<>();
24168 amit.gupta 1035
		// TODO Auto-generated method stub
24349 amit.gupta 1036
		Map<Integer, String> fofoIdNameMap = this.getAllFofoRetailerIdNameMap();
25295 amit.gupta 1037
		fofoIdNameMap.forEach((fofoId, name) -> {
24349 amit.gupta 1038
			MapWrapper<Integer, String> idWrapper = new MapWrapper<>();
1039
			idWrapper.setKey(fofoId);
1040
			idWrapper.setValue(name);
1041
			mapWrappers.add(idWrapper);
1042
		});
1043
		return mapWrappers;
1044
	}
1045
 
1046
	@Override
25295 amit.gupta 1047
	@Cacheable(value = "retailerNames", cacheManager = "cacheManager")
24349 amit.gupta 1048
	public Map<Integer, CustomRetailer> getFofoRetailers() {
1049
		// TODO Auto-generated method stub
24168 amit.gupta 1050
		List<FofoStore> stores = fofoStoreRepository.selectAll();
25295 amit.gupta 1051
		List<Integer> storeIds = stores.stream().map(x -> x.getId()).collect(Collectors.toList());
24349 amit.gupta 1052
		return this.getFofoRetailers(storeIds);
24168 amit.gupta 1053
	}
25295 amit.gupta 1054
 
26522 amit.gupta 1055
	@Override
1056
	public LoginRequestResponseModel registerWebUser(LoginRequestResponseModel loginRequestModel)
1057
			throws ProfitMandiBusinessException {
1058
 
1059
		com.spice.profitmandi.dao.entity.user.User saholicUser = this.createSaholicUser(loginRequestModel);
1060
		loginRequestModel.setUserId(saholicUser.getId());
1061
 
1062
		this.createRetailerAddress(loginRequestModel);
1063
 
1064
		this.createPrivateDealUser(loginRequestModel);
1065
		this.updateSaholicUser(saholicUser.getId(), loginRequestModel.getBusinessAddress().getId());
1066
 
1067
		return loginRequestModel;
1068
 
1069
	}
1070
 
1071
	private void createPrivateDealUser(LoginRequestResponseModel loginRequestModel) {
1072
 
1073
		Integer counterId = this.createCounter(loginRequestModel.getEmail(), loginRequestModel.getGstNumber(),
1074
				loginRequestModel.getBusinessAddress().getPhoneNumber(),
1075
				loginRequestModel.getBusinessAddress().getName(), loginRequestModel.getBusinessAddress().getId());
1076
		try {
1077
			this.createPrivateDealUser(loginRequestModel.getUserId(), counterId, false);
1078
		} catch (Exception e) {
1079
			LOGGER.error("ERROR : ", e);
1080
		}
1081
 
1082
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
1083
		privateDealUserAddressMapping.setUserId(loginRequestModel.getUserId());
1084
		privateDealUserAddressMapping.setAddressId(loginRequestModel.getBusinessAddress().getId());
1085
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
1086
 
1087
	}
1088
 
1089
	private void createRetailerAddress(LoginRequestResponseModel loginRequestResponseModel)
1090
			throws ProfitMandiBusinessException {
1091
		Address businessAddress = loginRequestResponseModel.getBusinessAddress();
1092
		businessAddress.setRetaierId(loginRequestResponseModel.getUserId());
1093
		addressRepository.persist(businessAddress);
1094
	}
22980 ashik.ali 1095
}