Subversion Repositories SmartDukaan

Rev

Rev 26772 | Rev 26977 | 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
 
576
	private void updateAddress(Address address, CustomAddress customAddress) {
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());
583
		address.setState(customAddress.getState());
584
		addressRepository.persist(address);
23955 govind 585
		LOGGER.info("Address Updated" + address);
23025 ashik.ali 586
	}
23955 govind 587
 
588
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId,
589
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
590
		if (shops.isEmpty()) {
591
			for (CustomShop customShop : customShops) {
23043 ashik.ali 592
				Shop shop = new Shop();
23079 ashik.ali 593
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 594
				shops.add(shop);
595
			}
23955 govind 596
		} else {
597
			for (Shop shop : shops) {
598
				for (CustomShop customShop : customShops) {
599
					if (shop.getId() == customShop.getShopId()) {
23079 ashik.ali 600
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 601
					}
602
				}
603
			}
23955 govind 604
			for (CustomShop customShop : customShops) {
605
				if (customShop.getShopId() == 0) {
23043 ashik.ali 606
					Shop shop = new Shop();
23079 ashik.ali 607
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 608
					shops.add(shop);
609
				}
610
			}
23025 ashik.ali 611
		}
612
	}
23955 govind 613
 
614
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId,
615
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
23043 ashik.ali 616
		shop.setRetailerId(retailerId);
617
		shop.setName(customShop.getName());
23955 govind 618
		if (customShop.getDocumentId() > 0) {
619
			if (shop.getDocumentId() != null && shop.getDocumentId() != customShop.getDocumentId()) {
24374 amit.gupta 620
				try {
621
					documentRepository.deleteById(shop.getDocumentId());
25295 amit.gupta 622
				} catch (Exception e) {
24374 amit.gupta 623
					e.printStackTrace();
624
				}
23489 ashik.ali 625
			}
23497 ashik.ali 626
			shop.setDocumentId(customShop.getDocumentId());
627
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
23043 ashik.ali 628
		}
23955 govind 629
		if (shop.getAddressId() == null) {
23079 ashik.ali 630
			Address address = null;
23955 govind 631
			if (customShop.isSameAsRetailerAddress()) {
23079 ashik.ali 632
				address = sameAsRetailerAddressValue;
23955 govind 633
			} else {
634
				// shop.setDocumentId(customShop.getDocumentId());
23079 ashik.ali 635
				address = new Address();
636
				this.updateAddress(address, customShop.getAddress());
637
			}
23043 ashik.ali 638
			shop.setAddressId(address.getId());
639
			shop.setAddress(address);
640
			shopRepository.persist(shop);
23131 ashik.ali 641
			ShopAddress shopAddress = null;
23955 govind 642
			try {
23131 ashik.ali 643
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
644
				shopAddress.setAddressId(address.getId());
23955 govind 645
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23131 ashik.ali 646
				shopAddress = new ShopAddress();
647
				shopAddress.setAddressId(address.getId());
648
				shopAddress.setShopId(shop.getId());
649
			}
23043 ashik.ali 650
			shopAddressRepository.persist(shopAddress);
23955 govind 651
		} else {
24375 amit.gupta 652
			Address address = null;
653
			try {
654
				address = addressRepository.selectById(shop.getAddressId());
655
				CustomAddress customAddress = customShop.getAddress();
656
				if (!(address.getName().equals(customAddress.getName())
657
						&& address.getLine1().equals(customAddress.getLine1())
658
						&& address.getLine2().equals(customAddress.getLine2())
659
						&& address.getCity().equals(customAddress.getCity())
660
						&& address.getPinCode().equals(customAddress.getPinCode())
661
						&& address.getState().equals(customAddress.getState()))) {
662
					address = new Address();
663
					ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
664
					this.updateAddress(address, customAddress);
665
					shopAddress.setAddressId(address.getId());
666
					shopAddressRepository.persist(shopAddress);
667
					shop.setAddress(address);
668
					shopRepository.persist(shop);
669
				}
25295 amit.gupta 670
			} catch (Exception e) {
24375 amit.gupta 671
				if (customShop.isSameAsRetailerAddress()) {
672
					address = sameAsRetailerAddressValue;
673
				} else {
674
					// shop.setDocumentId(customShop.getDocumentId());
675
					address = new Address();
676
					this.updateAddress(address, customShop.getAddress());
677
				}
678
				shop.setAddressId(address.getId());
679
				shop.setAddress(address);
680
				shopRepository.persist(shop);
681
				ShopAddress shopAddress = null;
682
				try {
683
					shopAddress = shopAddressRepository.selectByShopId(shop.getId());
684
					shopAddress.setAddressId(address.getId());
685
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
686
					shopAddress = new ShopAddress();
687
					shopAddress.setAddressId(address.getId());
688
					shopAddress.setShopId(shop.getId());
689
				}
25295 amit.gupta 690
				shopAddressRepository.persist(shopAddress);
691
			}
23955 govind 692
 
23043 ashik.ali 693
		}
23955 govind 694
 
23043 ashik.ali 695
	}
23955 govind 696
 
697
	private void addAddress(List<Shop> shops) {
22980 ashik.ali 698
		Set<Integer> shopIds = this.toShopIds(shops);
23955 govind 699
		if (shopIds.isEmpty()) {
22980 ashik.ali 700
			return;
701
		}
702
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
703
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
23955 govind 704
 
705
		for (Shop shop : shops) {
22980 ashik.ali 706
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
707
		}
708
	}
709
 
23955 govind 710
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses) {
22980 ashik.ali 711
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
712
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
713
		List<Address> addresses = addressRepository.selectByIds(addressIds);
23955 govind 714
		for (Address address : addresses) {
22980 ashik.ali 715
			addressIdAddressMap.put(address.getId(), address);
716
		}
717
		return addressIdAddressMap;
718
	}
23955 govind 719
 
720
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses) {
721
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>() {
22980 ashik.ali 722
			@Override
723
			public Integer apply(ShopAddress shopAddress) {
724
				return shopAddress.getAddressId();
725
			}
726
		};
727
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
728
	}
23955 govind 729
 
730
	private Set<Integer> toShopIds(List<Shop> shops) {
731
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>() {
22980 ashik.ali 732
			@Override
733
			public Integer apply(Shop shop) {
734
				return shop.getId();
735
			}
736
		};
737
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
738
	}
23955 govind 739
 
740
	private String toString(List<UserRole> userRoles) {
23781 ashik.ali 741
		Set<Integer> roleIds = new HashSet<>();
23955 govind 742
		for (UserRole userRole : userRoles) {
23781 ashik.ali 743
			roleIds.add(userRole.getRoleId());
744
		}
745
		List<Role> roles = roleRepository.selectByIds(roleIds);
23955 govind 746
		Function<Role, String> roleToNameFunction = new Function<Role, String>() {
23781 ashik.ali 747
			public String apply(Role role) {
748
				return String.valueOf(role.getName());
22980 ashik.ali 749
			};
750
		};
23781 ashik.ali 751
		Set<String> userRoleStrings = roles.stream().map(roleToNameFunction).collect(Collectors.toSet());
22980 ashik.ali 752
		return String.join(", ", userRoleStrings);
753
	}
23955 govind 754
 
755
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName,
756
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23378 ashik.ali 757
		FofoStore fofoStore = null;
23955 govind 758
		try {
23378 ashik.ali 759
			fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
23955 govind 760
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
761
 
23378 ashik.ali 762
		}
23955 govind 763
 
764
		if (fofoStore != null) {
765
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
766
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
26209 tejbeer 767
			fofoStore.setWarehouseId(updateRetailerRequest.getWarehouseId());
23955 govind 768
			fofoStoreRepository.persist(fofoStore);
769
 
770
		} else {
23378 ashik.ali 771
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
772
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
23955 govind 773
 
23378 ashik.ali 774
			StateInfo stateInfo = null;
775
			try {
776
				stateInfo = Utils.getStateInfo(retailerAddress.getState());
777
			} catch (Exception e) {
778
				// TODO Auto-generated catch block
779
				e.printStackTrace();
23955 govind 780
				// throw new ProfitMandiBusinessException();
23378 ashik.ali 781
			}
23955 govind 782
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName,
783
					stateInfo.getShortName());
784
 
23378 ashik.ali 785
			fofoStore = new FofoStore();
786
			fofoStore.setId(retailerId);
24023 amit.gupta 787
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
788
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
26209 tejbeer 789
			fofoStore.setWarehouseId(updateRetailerRequest.getWarehouseId());
23786 amit.gupta 790
			String latestStoreCode = fofoStoreRepository.selectLatestStore().getCode();
791
			int latestCodeCounter = Integer.parseInt(latestStoreCode.replaceAll("[A-Z]", ""));
23955 govind 792
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(
793
					districtMaster.getStateShortName() + districtMaster.getShortName(), latestCodeCounter + 1);
23378 ashik.ali 794
			fofoStore.setCode(fofoStoreCode);
795
			fofoStoreRepository.persist(fofoStore);
796
		}
23955 govind 797
 
23378 ashik.ali 798
		return fofoStore;
799
	}
23955 govind 800
 
23329 ashik.ali 801
	@Override
23955 govind 802
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName,
803
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23329 ashik.ali 804
		User user = userRepository.selectById(userId);
805
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
806
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
807
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
23781 ashik.ali 808
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 809
		try {
23781 ashik.ali 810
			userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleFofo.getId());
23955 govind 811
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 812
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
813
		}
23955 govind 814
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName, updateRetailerRequest);
815
 
23329 ashik.ali 816
	}
23955 govind 817
 
23329 ashik.ali 818
	@Override
819
	public List<DistrictMaster> getAllDistrictMaster(String stateName) {
820
		StateInfo stateInfo = null;
821
		try {
822
			stateInfo = Utils.getStateInfo(stateName);
823
		} catch (Exception e) {
824
			e.printStackTrace();
23955 govind 825
			// throw new ProfitMandiBusinessException();
23329 ashik.ali 826
		}
827
		return districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
828
	}
22980 ashik.ali 829
 
23509 amit.gupta 830
	@Override
831
	public Map<Integer, CustomRetailer> getFofoRetailers(List<Integer> fofoIds) {
832
		List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
25295 amit.gupta 833
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream()
834
				.filter(x -> x.getAddressId() != null).collect(Collectors.toMap(x -> x.getAddressId(), x -> x));
23903 amit.gupta 835
		List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
23509 amit.gupta 836
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
25295 amit.gupta 837
		for (Address address : addresses) {
23903 amit.gupta 838
			com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
23509 amit.gupta 839
			CustomRetailer customRetailer = new CustomRetailer();
23903 amit.gupta 840
			customRetailer.setEmail(user.getEmailId());
23509 amit.gupta 841
			customRetailer.setBusinessName(address.getName());
842
			customRetailer.setMobileNumber(address.getPhoneNumber());
23532 amit.gupta 843
			try {
26151 amit.gupta 844
				FofoStore fs = fofoStoreRepository.selectByRetailerId(address.getRetaierId());
845
				customRetailer.setCode(fs.getCode());
846
				customRetailer.setWarehouseId(fs.getWarehouseId());
26125 amit.gupta 847
			} catch (Exception e) {
848
				continue;
849
			}
850
			try {
23796 amit.gupta 851
				customRetailer.setCartId(user.getActiveCartId());
23532 amit.gupta 852
				PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
853
				Counter counter = counterRepository.selectById(pdu.getCounterId());
854
				customRetailer.setGstNumber(counter.getGstin());
23955 govind 855
			} catch (Exception e) {
23532 amit.gupta 856
				customRetailer.setGstNumber(null);
857
			}
858
			CustomAddress address1 = new CustomAddress();
859
			address1.setCity(address.getCity());
860
			address1.setState(address.getState());
861
			address1.setLine1(address.getLine1());
862
			address1.setLine2(address.getLine2());
863
			address1.setPinCode(address.getPinCode());
864
			address1.setName(address.getName());
865
			customRetailer.setAddress(address1);
23786 amit.gupta 866
			customRetailer.setPartnerId(address.getRetaierId());
26522 amit.gupta 867
			customRetailer.setDisplayName(
868
					customRetailer.getBusinessName() + "-" + customRetailer.getCode() + "- " + address1.getCity());
23509 amit.gupta 869
			customRetailersMap.put(address.getRetaierId(), customRetailer);
870
		}
871
		return customRetailersMap;
872
	}
25295 amit.gupta 873
 
23781 ashik.ali 874
	@Override
25295 amit.gupta 875
	public CustomRetailer getFofoRetailer(int fofoId) throws ProfitMandiBusinessException {
23957 tejbeer 876
		com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(fofoId);
26089 amit.gupta 877
		FofoStore store = fofoStoreRepository.selectByRetailerId(fofoId);
23957 tejbeer 878
		Address address = null;
879
		try {
880
			address = addressRepository.selectById(saholicUser.getAddressId());
881
		} catch (ProfitMandiBusinessException e1) {
882
			// TODO Auto-generated catch block
883
			e1.printStackTrace();
884
		}
885
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
25295 amit.gupta 886
		CustomRetailer customRetailer = new CustomRetailer();
887
		customRetailer.setEmail(saholicUser.getEmailId());
888
		customRetailer.setBusinessName(address.getName());
889
		customRetailer.setMobileNumber(address.getPhoneNumber());
890
		try {
891
			customRetailer.setCartId(saholicUser.getActiveCartId());
892
			PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
893
			Counter counter = counterRepository.selectById(pdu.getCounterId());
894
			customRetailer.setGstNumber(counter.getGstin());
895
		} catch (Exception e) {
896
			customRetailer.setGstNumber(null);
897
		}
898
		CustomAddress address1 = new CustomAddress();
899
		address1.setCity(address.getCity());
900
		address1.setState(address.getState());
901
		address1.setLine1(address.getLine1());
902
		address1.setLine2(address.getLine2());
903
		address1.setPinCode(address.getPinCode());
26653 amit.gupta 904
		address1.setPhoneNumber(address.getPhoneNumber());
25295 amit.gupta 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")
26962 amit.gupta 1052
	public Map<Integer, CustomRetailer> getFofoRetailers(boolean activeOnly) {
24349 amit.gupta 1053
		// TODO Auto-generated method stub
26962 amit.gupta 1054
		Stream<FofoStore> storeStream = fofoStoreRepository.selectAll().stream();
1055
		if(activeOnly) {
1056
			storeStream = storeStream.filter(x->x.isActive());
1057
		}
1058
		List<Integer> storeIds = storeStream.map(x -> x.getId()).collect(Collectors.toList());
24349 amit.gupta 1059
		return this.getFofoRetailers(storeIds);
24168 amit.gupta 1060
	}
25295 amit.gupta 1061
 
26522 amit.gupta 1062
	@Override
1063
	public LoginRequestResponseModel registerWebUser(LoginRequestResponseModel loginRequestModel)
1064
			throws ProfitMandiBusinessException {
1065
 
1066
		com.spice.profitmandi.dao.entity.user.User saholicUser = this.createSaholicUser(loginRequestModel);
1067
		loginRequestModel.setUserId(saholicUser.getId());
1068
 
1069
		this.createRetailerAddress(loginRequestModel);
1070
 
1071
		this.createPrivateDealUser(loginRequestModel);
1072
		this.updateSaholicUser(saholicUser.getId(), loginRequestModel.getBusinessAddress().getId());
1073
 
1074
		return loginRequestModel;
1075
 
1076
	}
1077
 
1078
	private void createPrivateDealUser(LoginRequestResponseModel loginRequestModel) {
1079
 
1080
		Integer counterId = this.createCounter(loginRequestModel.getEmail(), loginRequestModel.getGstNumber(),
1081
				loginRequestModel.getBusinessAddress().getPhoneNumber(),
1082
				loginRequestModel.getBusinessAddress().getName(), loginRequestModel.getBusinessAddress().getId());
1083
		try {
1084
			this.createPrivateDealUser(loginRequestModel.getUserId(), counterId, false);
1085
		} catch (Exception e) {
1086
			LOGGER.error("ERROR : ", e);
1087
		}
1088
 
1089
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
1090
		privateDealUserAddressMapping.setUserId(loginRequestModel.getUserId());
1091
		privateDealUserAddressMapping.setAddressId(loginRequestModel.getBusinessAddress().getId());
1092
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
1093
 
1094
	}
1095
 
1096
	private void createRetailerAddress(LoginRequestResponseModel loginRequestResponseModel)
1097
			throws ProfitMandiBusinessException {
1098
		Address businessAddress = loginRequestResponseModel.getBusinessAddress();
1099
		businessAddress.setRetaierId(loginRequestResponseModel.getUserId());
1100
		addressRepository.persist(businessAddress);
1101
	}
26536 amit.gupta 1102
 
26528 amit.gupta 1103
	private String getHash256(String originalString) {
26533 amit.gupta 1104
		String hashString = Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
1105
		LOGGER.info("Hash String {}", hashString);
1106
		return hashString;
26528 amit.gupta 1107
	}
26533 amit.gupta 1108
 
1109
	@Override
26536 amit.gupta 1110
	@Cacheable(value = "fofoIdUrl", cacheManager = "oneDayCacheManager")
26533 amit.gupta 1111
	public Map<Integer, String> getAllFofoRetailerIdUrlMap() {
26536 amit.gupta 1112
		Map<Integer, String> fofoRetailerUrlMap = new HashMap<>();
1113
		List<FofoStore> stores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive())
1114
				.collect(Collectors.toList());
1115
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userMap = userUserRepository
1116
				.selectByIds(stores.stream().map(x -> x.getId()).collect(Collectors.toList())).stream()
1117
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
1118
		for (FofoStore store : stores) {
26537 amit.gupta 1119
			LOGGER.info("Store is {}", store);
26544 amit.gupta 1120
			String districtShortName = store.getCode().replaceAll("\\d+", "").substring(2);
26593 amit.gupta 1121
			String stateShortName = store.getCode().replaceAll("\\d+", "").substring(0, 2);
26539 amit.gupta 1122
			DistrictMaster districtMaster = null;
26544 amit.gupta 1123
			LOGGER.info("Store shortname is {}", districtShortName);
26593 amit.gupta 1124
			districtMaster = districtMasterRepository.selectByShortnameNameAndStateShortName(districtShortName,
1125
					stateShortName);
26543 amit.gupta 1126
			if (districtMaster == null) {
26544 amit.gupta 1127
				districtMaster = districtMasterRepository.selectByShortnameNameAndStateShortName("FB", "HR");
26539 amit.gupta 1128
			}
26542 amit.gupta 1129
			LOGGER.info("Store id - {}", store.getId());
26536 amit.gupta 1130
			com.spice.profitmandi.dao.entity.user.User user = userMap.get(store.getId());
1131
			String urlString = districtMaster.getStateShortName() + "/"
1132
					+ Utils.getHyphenatedString(districtMaster.getName()) + "/"
26595 amit.gupta 1133
					+ store.getCode();
26536 amit.gupta 1134
			fofoRetailerUrlMap.put(store.getId(), urlString);
26541 amit.gupta 1135
			LOGGER.info("Store is {} end", store);
26536 amit.gupta 1136
 
1137
		}
1138
		return fofoRetailerUrlMap;
26533 amit.gupta 1139
	}
1140
 
1141
	@Override
26590 amit.gupta 1142
	@Cacheable(value = "storeCodeRetailerMap", cacheManager = "oneDayCacheManager")
1143
	public Map<String, Integer> getStoreCodeRetailerMap() {
26533 amit.gupta 1144
		Map<Integer, String> map = this.getAllFofoRetailerIdUrlMap();
26596 amit.gupta 1145
		Map<String, Integer> returnMap = map.entrySet().stream().collect(Collectors.toMap(x -> {
26593 amit.gupta 1146
			String[] splitString = x.getValue().split("/");
1147
			return splitString[splitString.length-1];
1148
		}, x -> x.getKey()));
26596 amit.gupta 1149
		LOGGER.info("returnMap {}", returnMap);
1150
		return returnMap;
26533 amit.gupta 1151
	}
22980 ashik.ali 1152
}