Subversion Repositories SmartDukaan

Rev

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