Subversion Repositories SmartDukaan

Rev

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