Subversion Repositories SmartDukaan

Rev

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