Subversion Repositories SmartDukaan

Rev

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