Subversion Repositories SmartDukaan

Rev

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