Subversion Repositories SmartDukaan

Rev

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