Subversion Repositories SmartDukaan

Rev

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