Subversion Repositories SmartDukaan

Rev

Rev 24704 | Rev 25295 | 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;
24349 amit.gupta 117
 
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<>();
24349 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
 
22980 ashik.ali 252
	@SuppressWarnings("unchecked")
253
	@Override
23025 ashik.ali 254
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
255
			throws ProfitMandiBusinessException {
256
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
23955 govind 257
		User user = (User) map.get("user");
23329 ashik.ali 258
		user = this.createUser(user, updateRetailerRequest);
259
		map.put("user", user);
23955 govind 260
		List<UserRole> userRoles = (List<UserRole>) map.get("userRoles");
23781 ashik.ali 261
		Role roleUser = roleRepository.selectByName(RoleType.USER.toString());
262
		userRoles = this.addRole(userRoles, user.getId(), roleUser.getId());
23955 govind 263
		Retailer retailer = (Retailer) map.get("retailer");
23329 ashik.ali 264
		retailer = this.updateRetailer(user, retailer, updateRetailerRequest);
265
		map.put("retailer", retailer);
23955 govind 266
 
23781 ashik.ali 267
		Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
23955 govind 268
 
23781 ashik.ali 269
		userRoles = this.addRole(userRoles, user.getId(), roleRetailer.getId());
23955 govind 270
 
23781 ashik.ali 271
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 272
 
273
		if (updateRetailerRequest.isFofo()) {
274
 
23781 ashik.ali 275
			userRoles = this.addRole(userRoles, user.getId(), roleFofo.getId());
276
			map.put("fofoRole", this.containsRoleType(userRoles, roleFofo.getId()));
23061 ashik.ali 277
		}
23955 govind 278
 
279
		if (this.containsRoleType(userRoles, roleFofo.getId())) {
23365 ashik.ali 280
			this.createDefaultPaymentOption(retailer.getId());
23329 ashik.ali 281
		}
23955 govind 282
 
23329 ashik.ali 283
		map.put("userRoles", userRoles);
284
		map.put("userRoleNames", this.toString(userRoles));
23955 govind 285
 
286
		Address retailerAddress = (Address) map.get("retailerAddress");
287
		retailerAddress = this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(),
288
				retailer.getId());
23329 ashik.ali 289
		map.put("retailerAddress", retailerAddress);
23955 govind 290
 
291
		if (updateRetailerRequest.isFofo()) {
292
			FofoStore fofoStore = this.createFofoStoreCodeByRetailerId(retailer.getId(),
293
					updateRetailerRequest.getDistrictName(), retailerAddress.getState(), updateRetailerRequest);
23378 ashik.ali 294
			map.put("fofoStore", fofoStore);
23061 ashik.ali 295
		}
23955 govind 296
 
297
		this.createPrivateDealUser(user, updateRetailerRequest.isFofo(), updateRetailerRequest.getGstNumber(), retailer,
298
				retailerAddress.getId());
23329 ashik.ali 299
		map.put("gstNumber", updateRetailerRequest.getGstNumber());
300
		this.updateSaholicUser(retailer.getId(), retailerAddress.getId());
23955 govind 301
 
302
		List<Shop> shops = (List<Shop>) map.get("shops");
303
		if (shops == null) {
23060 ashik.ali 304
			shops = new ArrayList<>();
23061 ashik.ali 305
			map.put("shops", shops);
23060 ashik.ali 306
		}
23079 ashik.ali 307
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId(), retailerAddress);
23955 govind 308
 
23025 ashik.ali 309
		return map;
23955 govind 310
 
23025 ashik.ali 311
	}
23955 govind 312
 
313
	private void createDefaultPaymentOption(int fofoId) {
23365 ashik.ali 314
		List<Integer> paymentOptionIds = fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(fofoId);
23955 govind 315
		if (paymentOptionIds.isEmpty()) {
23365 ashik.ali 316
			PaymentOption paymentOption = null;
23955 govind 317
			try {
23365 ashik.ali 318
				paymentOption = paymentOptionRepository.selectByName(PaymentOptionType.CASH.toString());
23955 govind 319
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23365 ashik.ali 320
				paymentOption = new PaymentOption();
321
				paymentOption.setName(PaymentOptionType.CASH.toString());
322
				paymentOptionRepository.persist(paymentOption);
323
			}
324
			FofoPartnerPaymentOption fofoPartnerPaymentOption = new FofoPartnerPaymentOption();
325
			fofoPartnerPaymentOption.setFofoId(fofoId);
326
			fofoPartnerPaymentOption.setPaymentOptionId(paymentOption.getId());
327
			fofoPartnerPaymentOptionRepository.persist(fofoPartnerPaymentOption);
328
		}
23955 govind 329
 
23365 ashik.ali 330
	}
23955 govind 331
 
332
	private User createUser(User user, UpdateRetailerRequest updateRetailerRequest) {
333
		if (user == null) {
23329 ashik.ali 334
			user = new User();
335
			user.setCity("");
336
			user.setPinCode(0);
337
			user.setState("");
338
			user.setPassword("");
339
			user.setMobile_verified(false);
340
			user.setReferral_url("");
341
			user.setGroup_id(1);
342
			user.setStatus(1);
343
			user.setActivated(true);
344
			user.setCreateTimestamp(LocalDateTime.now());
345
		}
24705 amit.gupta 346
		user.setActivated(updateRetailerRequest.isActive());
23329 ashik.ali 347
		user.setFirstName(updateRetailerRequest.getUserFirstName());
348
		user.setLastName(updateRetailerRequest.getUserLastName());
349
		user.setMobileNumber(updateRetailerRequest.getUserMobileNumber());
350
		user.setEmailId(updateRetailerRequest.getUserEmailId());
351
		user.setUsername(updateRetailerRequest.getUserEmailId());
23509 amit.gupta 352
		user.setUpdateTimestamp(LocalDateTime.now());
353
		userRepository.persist(user);
23329 ashik.ali 354
		return user;
23955 govind 355
 
23329 ashik.ali 356
	}
23955 govind 357
 
358
	private int createSaholicUser(User user, String retailerName) {
23043 ashik.ali 359
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
360
		try {
23063 ashik.ali 361
			saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
23955 govind 362
		} catch (ProfitMandiBusinessException e) {
23043 ashik.ali 363
			LOGGER.info("User doesnt exist in old system");
364
		}
23955 govind 365
		if (saholicUser == null) {
23043 ashik.ali 366
			Cart cart = new Cart();
367
			cart.setCartStatus(CartStatus.ACTIVE);
368
			cartRepository.persist(cart);
369
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
23063 ashik.ali 370
			saholicUser.setEmailId(user.getEmailId());
23043 ashik.ali 371
			saholicUser.setName(retailerName);
372
			saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 373
			saholicUser.setCreateTimestamp(LocalDateTime.now());
23043 ashik.ali 374
			userUserRepository.persist(saholicUser);
23955 govind 375
 
23269 ashik.ali 376
			UserAccount ua = new UserAccount();
377
			ua.setAccountKey(saholicUser.getId());
378
			ua.setUserId(user.getId());
379
			ua.setType(AccountType.saholic);
23063 ashik.ali 380
			userAccountRepository.persist(ua);
381
 
23269 ashik.ali 382
			UserAccount ua2 = new UserAccount();
383
			ua2.setAccountKey(saholicUser.getActiveCartId());
384
			ua2.setUserId(user.getId());
385
			ua2.setType(AccountType.cartId);
23063 ashik.ali 386
			userAccountRepository.persist(ua2);
23955 govind 387
			LOGGER.info("created....");
23043 ashik.ali 388
		}
389
		return saholicUser.getId();
390
	}
23955 govind 391
 
392
	private Retailer updateRetailer(User user, Retailer retailer, UpdateRetailerRequest updateRetailerRequest)
393
			throws ProfitMandiBusinessException {
394
		if (retailer == null) {
395
			LOGGER.info("createSaholicUser.....");
23063 ashik.ali 396
			int saholicUserId = this.createSaholicUser(user, updateRetailerRequest.getName());
23043 ashik.ali 397
			retailer = new Retailer();
398
			retailer.setId(saholicUserId);
399
		}
23329 ashik.ali 400
		retailer.setActive(updateRetailerRequest.isActive());
24704 amit.gupta 401
		user.setActivated(updateRetailerRequest.isActive());//
23955 govind 402
		// this.createRole(user.getId(), RoleType.RETAILER);
23025 ashik.ali 403
		retailer.setName(updateRetailerRequest.getName());
404
		retailer.setNumber(updateRetailerRequest.getNumber());
23955 govind 405
		if (updateRetailerRequest.getNumber() == null || updateRetailerRequest.getNumber().isEmpty()) {
23107 ashik.ali 406
			retailer.setType(RetailerType.UNREGISTERED_SHOP);
23955 govind 407
		} else {
23107 ashik.ali 408
			retailer.setType(RetailerType.GSTIN);
409
		}
23955 govind 410
		if (updateRetailerRequest.getDocumentId() > 0) {
411
			if (retailer.getDocumentId() != null && retailer.getDocumentId() != updateRetailerRequest.getDocumentId()) {
24374 amit.gupta 412
				try {
413
					documentRepository.deleteById(retailer.getDocumentId());
414
				} catch(Exception e) {
415
					e.printStackTrace();
416
				}
23489 ashik.ali 417
			}
23497 ashik.ali 418
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
419
			documentRepository.markDocumentAsPersisted(updateRetailerRequest.getDocumentId());
23025 ashik.ali 420
		}
421
		retailerRepository.persist(retailer);
23059 ashik.ali 422
		return retailer;
23025 ashik.ali 423
	}
23955 govind 424
 
425
	private void updateSaholicUser(int retailerId, int retailerAddressId) {
23329 ashik.ali 426
		try {
427
			com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectById(retailerId);
428
			user.setAddressId(retailerAddressId);
429
			userUserRepository.persist(user);
430
		} catch (ProfitMandiBusinessException e) {
23955 govind 431
 
23329 ashik.ali 432
		}
433
	}
23955 govind 434
 
435
	private void createPrivateDealUser(User user, boolean fofo, String gstNumber, Retailer retailer,
436
			int retailerAddressId) {
23329 ashik.ali 437
		PrivateDealUser privateDealUser = null;
23955 govind 438
		try {
23329 ashik.ali 439
			privateDealUser = privateDealUserRepository.selectById(retailer.getId());
23955 govind 440
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 441
			LOGGER.error("PrivateDealUser not found");
442
		}
23955 govind 443
 
444
		// = privateDealUserRepository.selectById(saholicUser.getId());
445
		if (privateDealUser == null) {
446
			Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(),
447
					retailer.getName(), retailerAddressId);
23473 ashik.ali 448
			try {
449
				this.createPrivateDealUser(retailer.getId(), counterId, fofo);
23955 govind 450
			} catch (Exception e) {
23473 ashik.ali 451
				LOGGER.error("ERROR : ", e);
452
			}
23955 govind 453
		} else {
454
			if (privateDealUser.getCounterId() == null) {
455
				Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(),
456
						retailer.getName(), retailerAddressId);
23329 ashik.ali 457
				privateDealUser.setCounterId(counterId);
458
				privateDealUser.setFofo(fofo);
459
				privateDealUserRepository.persist(privateDealUser);
23955 govind 460
			} else {
23329 ashik.ali 461
				Counter counter = null;
23955 govind 462
				try {
23329 ashik.ali 463
					counter = counterRepository.selectById(privateDealUser.getCounterId());
23955 govind 464
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 465
					LOGGER.error("Counter not found with id [{}]", privateDealUser.getCounterId());
466
				}
23955 govind 467
				if (counter == null) {
468
					this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(),
469
							retailerAddressId);
470
				} else {
23329 ashik.ali 471
					counter.setGstin(gstNumber);
472
					counterRepository.persist(counter);
473
				}
474
				privateDealUser.setFofo(fofo);
475
				privateDealUserRepository.persist(privateDealUser);
476
			}
477
		}
23955 govind 478
 
23329 ashik.ali 479
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
480
		privateDealUserAddressMapping.setUserId(retailer.getId());
481
		privateDealUserAddressMapping.setAddressId(retailerAddressId);
482
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
483
	}
23955 govind 484
 
485
	// Specifically set isFofo to true that has to be used by old system
486
	private void createPrivateDealUser(int retailerId, int counterId, boolean fofo) {
23329 ashik.ali 487
		PrivateDealUser privateDealUser = new PrivateDealUser();
488
		privateDealUser.setActive(true);
489
		privateDealUser.setBulkShipmentAmountLimit(fofo ? 1000000 : 50000);
490
		privateDealUser.setId(retailerId);
491
		privateDealUser.setCounterId(counterId);
492
		privateDealUser.setFofo(fofo);
493
		privateDealUserRepository.persist(privateDealUser);
494
	}
23955 govind 495
 
496
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId) {
497
		if (gstNumber != null && !gstNumber.isEmpty()) {
23329 ashik.ali 498
			Counter counter = new Counter();
499
			counter.setEmailId(emailId);
500
			counter.setGstin(gstNumber);
501
			counter.setMobileNumber(mobileNumber);
502
			counter.setName(name);
503
			counter.setAddressId(addressId);
504
			counterRepository.persist(counter);
505
			return counter.getId();
23955 govind 506
		} else {
23329 ashik.ali 507
			return null;
508
		}
509
	}
23955 govind 510
 
511
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId)
512
			throws ProfitMandiBusinessException {
513
		if (address == null) {
23043 ashik.ali 514
			address = new Address();
515
			address.setRetaierId(retailerId);
516
			this.updateAddress(address, customAddress);
23955 govind 517
			// addressRepository.persist(addressRetailer);
518
 
23043 ashik.ali 519
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
520
			retailerRegisteredAddress.setRetailerId(retailerId);
521
			retailerRegisteredAddress.setAddressId(address.getId());
522
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
23955 govind 523
		} else {
524
			this.updateAddress(address, customAddress);
525
			RetailerRegisteredAddress retailerRegisteredAddress = retailerRegisteredAddressRepository
526
					.selectByRetailerId(retailerId);
527
			retailerRegisteredAddress.setAddressId(address.getId());
528
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
23043 ashik.ali 529
		}
23059 ashik.ali 530
		return address;
23043 ashik.ali 531
	}
23955 govind 532
 
533
	private void updateAddress(Address address, CustomAddress customAddress) {
23025 ashik.ali 534
		address.setName(customAddress.getName());
23829 amit.gupta 535
		address.setPhoneNumber(customAddress.getPhoneNumber());
23025 ashik.ali 536
		address.setLine1(customAddress.getLine1());
537
		address.setLine2(customAddress.getLine2());
538
		address.setCity(customAddress.getCity());
539
		address.setPinCode(customAddress.getPinCode());
540
		address.setState(customAddress.getState());
541
		addressRepository.persist(address);
23955 govind 542
		LOGGER.info("Address Updated" + address);
23025 ashik.ali 543
	}
23955 govind 544
 
545
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId,
546
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
547
		if (shops.isEmpty()) {
548
			for (CustomShop customShop : customShops) {
23043 ashik.ali 549
				Shop shop = new Shop();
23079 ashik.ali 550
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 551
				shops.add(shop);
552
			}
23955 govind 553
		} else {
554
			for (Shop shop : shops) {
555
				for (CustomShop customShop : customShops) {
556
					if (shop.getId() == customShop.getShopId()) {
23079 ashik.ali 557
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 558
					}
559
				}
560
			}
23955 govind 561
			for (CustomShop customShop : customShops) {
562
				if (customShop.getShopId() == 0) {
23043 ashik.ali 563
					Shop shop = new Shop();
23079 ashik.ali 564
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 565
					shops.add(shop);
566
				}
567
			}
23025 ashik.ali 568
		}
569
	}
23955 govind 570
 
571
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId,
572
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
23043 ashik.ali 573
		shop.setRetailerId(retailerId);
574
		shop.setName(customShop.getName());
23955 govind 575
		if (customShop.getDocumentId() > 0) {
576
			if (shop.getDocumentId() != null && shop.getDocumentId() != customShop.getDocumentId()) {
24374 amit.gupta 577
				try {
578
					documentRepository.deleteById(shop.getDocumentId());
579
				} catch(Exception e) {
580
					e.printStackTrace();
581
				}
23489 ashik.ali 582
			}
23497 ashik.ali 583
			shop.setDocumentId(customShop.getDocumentId());
584
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
23043 ashik.ali 585
		}
23955 govind 586
		if (shop.getAddressId() == null) {
23079 ashik.ali 587
			Address address = null;
23955 govind 588
			if (customShop.isSameAsRetailerAddress()) {
23079 ashik.ali 589
				address = sameAsRetailerAddressValue;
23955 govind 590
			} else {
591
				// shop.setDocumentId(customShop.getDocumentId());
23079 ashik.ali 592
				address = new Address();
593
				this.updateAddress(address, customShop.getAddress());
594
			}
23043 ashik.ali 595
			shop.setAddressId(address.getId());
596
			shop.setAddress(address);
597
			shopRepository.persist(shop);
23131 ashik.ali 598
			ShopAddress shopAddress = null;
23955 govind 599
			try {
23131 ashik.ali 600
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
601
				shopAddress.setAddressId(address.getId());
23955 govind 602
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23131 ashik.ali 603
				shopAddress = new ShopAddress();
604
				shopAddress.setAddressId(address.getId());
605
				shopAddress.setShopId(shop.getId());
606
			}
23043 ashik.ali 607
			shopAddressRepository.persist(shopAddress);
23955 govind 608
		} else {
24375 amit.gupta 609
			Address address = null;
610
			try {
611
				address = addressRepository.selectById(shop.getAddressId());
612
				CustomAddress customAddress = customShop.getAddress();
613
				if (!(address.getName().equals(customAddress.getName())
614
						&& address.getLine1().equals(customAddress.getLine1())
615
						&& address.getLine2().equals(customAddress.getLine2())
616
						&& address.getCity().equals(customAddress.getCity())
617
						&& address.getPinCode().equals(customAddress.getPinCode())
618
						&& address.getState().equals(customAddress.getState()))) {
619
					address = new Address();
620
					ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
621
					this.updateAddress(address, customAddress);
622
					shopAddress.setAddressId(address.getId());
623
					shopAddressRepository.persist(shopAddress);
624
					shop.setAddress(address);
625
					shopRepository.persist(shop);
626
				}
627
			} catch(Exception e) {
628
				if (customShop.isSameAsRetailerAddress()) {
629
					address = sameAsRetailerAddressValue;
630
				} else {
631
					// shop.setDocumentId(customShop.getDocumentId());
632
					address = new Address();
633
					this.updateAddress(address, customShop.getAddress());
634
				}
635
				shop.setAddressId(address.getId());
636
				shop.setAddress(address);
637
				shopRepository.persist(shop);
638
				ShopAddress shopAddress = null;
639
				try {
640
					shopAddress = shopAddressRepository.selectByShopId(shop.getId());
641
					shopAddress.setAddressId(address.getId());
642
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
643
					shopAddress = new ShopAddress();
644
					shopAddress.setAddressId(address.getId());
645
					shopAddress.setShopId(shop.getId());
646
				}
647
				shopAddressRepository.persist(shopAddress);			}
23955 govind 648
 
23043 ashik.ali 649
		}
23955 govind 650
 
23043 ashik.ali 651
	}
23955 govind 652
 
653
	private void addAddress(List<Shop> shops) {
22980 ashik.ali 654
		Set<Integer> shopIds = this.toShopIds(shops);
23955 govind 655
		if (shopIds.isEmpty()) {
22980 ashik.ali 656
			return;
657
		}
658
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
659
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
23955 govind 660
 
661
		for (Shop shop : shops) {
22980 ashik.ali 662
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
663
		}
664
	}
665
 
23955 govind 666
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses) {
22980 ashik.ali 667
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
668
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
669
		List<Address> addresses = addressRepository.selectByIds(addressIds);
23955 govind 670
		for (Address address : addresses) {
22980 ashik.ali 671
			addressIdAddressMap.put(address.getId(), address);
672
		}
673
		return addressIdAddressMap;
674
	}
23955 govind 675
 
676
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses) {
677
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>() {
22980 ashik.ali 678
			@Override
679
			public Integer apply(ShopAddress shopAddress) {
680
				return shopAddress.getAddressId();
681
			}
682
		};
683
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
684
	}
23955 govind 685
 
686
	private Set<Integer> toShopIds(List<Shop> shops) {
687
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>() {
22980 ashik.ali 688
			@Override
689
			public Integer apply(Shop shop) {
690
				return shop.getId();
691
			}
692
		};
693
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
694
	}
23955 govind 695
 
696
	private String toString(List<UserRole> userRoles) {
23781 ashik.ali 697
		Set<Integer> roleIds = new HashSet<>();
23955 govind 698
		for (UserRole userRole : userRoles) {
23781 ashik.ali 699
			roleIds.add(userRole.getRoleId());
700
		}
701
		List<Role> roles = roleRepository.selectByIds(roleIds);
23955 govind 702
		Function<Role, String> roleToNameFunction = new Function<Role, String>() {
23781 ashik.ali 703
			public String apply(Role role) {
704
				return String.valueOf(role.getName());
22980 ashik.ali 705
			};
706
		};
23781 ashik.ali 707
		Set<String> userRoleStrings = roles.stream().map(roleToNameFunction).collect(Collectors.toSet());
22980 ashik.ali 708
		return String.join(", ", userRoleStrings);
709
	}
23955 govind 710
 
711
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName,
712
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23378 ashik.ali 713
		FofoStore fofoStore = null;
23955 govind 714
		try {
23378 ashik.ali 715
			fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
23955 govind 716
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
717
 
23378 ashik.ali 718
		}
23955 govind 719
 
720
		if (fofoStore != null) {
721
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
722
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
723
			fofoStoreRepository.persist(fofoStore);
724
 
725
		} else {
23378 ashik.ali 726
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
727
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
23955 govind 728
 
23378 ashik.ali 729
			StateInfo stateInfo = null;
730
			try {
731
				stateInfo = Utils.getStateInfo(retailerAddress.getState());
732
			} catch (Exception e) {
733
				// TODO Auto-generated catch block
734
				e.printStackTrace();
23955 govind 735
				// throw new ProfitMandiBusinessException();
23378 ashik.ali 736
			}
23955 govind 737
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName,
738
					stateInfo.getShortName());
739
 
23378 ashik.ali 740
			fofoStore = new FofoStore();
741
			fofoStore.setId(retailerId);
24023 amit.gupta 742
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
743
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
23786 amit.gupta 744
			String latestStoreCode = fofoStoreRepository.selectLatestStore().getCode();
745
			int latestCodeCounter = Integer.parseInt(latestStoreCode.replaceAll("[A-Z]", ""));
23955 govind 746
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(
747
					districtMaster.getStateShortName() + districtMaster.getShortName(), latestCodeCounter + 1);
23378 ashik.ali 748
			fofoStore.setCode(fofoStoreCode);
749
			fofoStoreRepository.persist(fofoStore);
750
		}
23955 govind 751
 
23378 ashik.ali 752
		return fofoStore;
753
	}
23955 govind 754
 
23329 ashik.ali 755
	@Override
23955 govind 756
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName,
757
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23329 ashik.ali 758
		User user = userRepository.selectById(userId);
759
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
760
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
761
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
23781 ashik.ali 762
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 763
		try {
23781 ashik.ali 764
			userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleFofo.getId());
23955 govind 765
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 766
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
767
		}
23955 govind 768
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName, updateRetailerRequest);
769
 
23329 ashik.ali 770
	}
24248 govind 771
 
23955 govind 772
 
23329 ashik.ali 773
	@Override
774
	public List<DistrictMaster> getAllDistrictMaster(String stateName) {
775
		StateInfo stateInfo = null;
776
		try {
777
			stateInfo = Utils.getStateInfo(stateName);
778
		} catch (Exception e) {
779
			e.printStackTrace();
23955 govind 780
			// throw new ProfitMandiBusinessException();
23329 ashik.ali 781
		}
782
		return districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
783
	}
22980 ashik.ali 784
 
23509 amit.gupta 785
	@Override
786
	public Map<Integer, CustomRetailer> getFofoRetailers(List<Integer> fofoIds) {
787
		List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
23957 tejbeer 788
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream().filter(x->x.getAddressId()!=null).collect(Collectors.toMap(x->x.getAddressId(), x->x));
23903 amit.gupta 789
		List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
23509 amit.gupta 790
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
23957 tejbeer 791
		for(Address address: addresses) {
23903 amit.gupta 792
			com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
23509 amit.gupta 793
			CustomRetailer customRetailer = new CustomRetailer();
23903 amit.gupta 794
			customRetailer.setEmail(user.getEmailId());
23509 amit.gupta 795
			customRetailer.setBusinessName(address.getName());
796
			customRetailer.setMobileNumber(address.getPhoneNumber());
23532 amit.gupta 797
			try {
23796 amit.gupta 798
				customRetailer.setCartId(user.getActiveCartId());
23532 amit.gupta 799
				PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
800
				Counter counter = counterRepository.selectById(pdu.getCounterId());
801
				customRetailer.setGstNumber(counter.getGstin());
23955 govind 802
			} catch (Exception e) {
23532 amit.gupta 803
				customRetailer.setGstNumber(null);
804
			}
805
			CustomAddress address1 = new CustomAddress();
806
			address1.setCity(address.getCity());
807
			address1.setState(address.getState());
808
			address1.setLine1(address.getLine1());
809
			address1.setLine2(address.getLine2());
810
			address1.setPinCode(address.getPinCode());
811
			address1.setName(address.getName());
812
			customRetailer.setAddress(address1);
23786 amit.gupta 813
			customRetailer.setPartnerId(address.getRetaierId());
24349 amit.gupta 814
			customRetailer.setDisplayName(customRetailer.getBusinessName() + "-" + address1.getCity());
23509 amit.gupta 815
			customRetailersMap.put(address.getRetaierId(), customRetailer);
816
		}
817
		return customRetailersMap;
818
	}
23957 tejbeer 819
 
23781 ashik.ali 820
	@Override
23957 tejbeer 821
	public  CustomRetailer getFofoRetailer(int fofoId) throws ProfitMandiBusinessException {
822
		com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(fofoId);
823
		Address address = null;
824
		try {
825
			address = addressRepository.selectById(saholicUser.getAddressId());
826
		} catch (ProfitMandiBusinessException e1) {
827
			// TODO Auto-generated catch block
828
			e1.printStackTrace();
829
		}
830
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
831
			CustomRetailer customRetailer = new CustomRetailer();
832
			customRetailer.setEmail(saholicUser.getEmailId());
833
			customRetailer.setBusinessName(address.getName());
834
			customRetailer.setMobileNumber(address.getPhoneNumber());
835
			try {
836
				customRetailer.setCartId(saholicUser.getActiveCartId());
837
				PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
838
				Counter counter = counterRepository.selectById(pdu.getCounterId());
839
				customRetailer.setGstNumber(counter.getGstin());
840
			} catch(Exception e) {
841
				customRetailer.setGstNumber(null);
842
			}
843
			CustomAddress address1 = new CustomAddress();
844
			address1.setCity(address.getCity());
845
			address1.setState(address.getState());
846
			address1.setLine1(address.getLine1());
847
			address1.setLine2(address.getLine2());
848
			address1.setPinCode(address.getPinCode());
849
			address1.setName(address.getName());
850
			customRetailer.setAddress(address1);
851
			customRetailer.setPartnerId(address.getRetaierId());
852
			customRetailersMap.put(address.getRetaierId(), customRetailer);
853
		return customRetailer;
854
 
855
	}
856
 
857
	@Override
23781 ashik.ali 858
	public Map<Integer, String> getAllFofoRetailerIdEmailIdMap() {
859
		Role roleFofo = null;
860
		try {
861
			roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
862
		} catch (ProfitMandiBusinessException e) {
863
			// TODO Auto-generated catch block
864
			e.printStackTrace();
865
		}
866
		Role roleRetailer = null;
867
		try {
868
			roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
869
		} catch (ProfitMandiBusinessException e) {
870
			// TODO Auto-generated catch block
871
			e.printStackTrace();
872
		}
873
		Set<Integer> roleIds = new HashSet<>();
874
		roleIds.add(roleFofo.getId());
875
		roleIds.add(roleRetailer.getId());
876
		List<Integer> userIds = userRoleRepository.selectUserIdsByRoleIds(roleIds);
877
		List<User> users = userRepository.selectAllByIds(new HashSet<>(userIds));
878
		Map<Integer, Integer> userIdRetailerIdMap = this.getUserIdRetailerIdMap(userIds);
879
		Map<Integer, String> retailerIdEmailIdMap = new HashMap<>();
23955 govind 880
		for (User user : users) {
23781 ashik.ali 881
			retailerIdEmailIdMap.put(userIdRetailerIdMap.get(user.getId()), user.getEmailId());
882
		}
883
		return retailerIdEmailIdMap;
884
	}
23955 govind 885
 
886
	private Map<Integer, Integer> getUserIdRetailerIdMap(List<Integer> userIds) {
23781 ashik.ali 887
		List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userIds));
888
		Map<Integer, Integer> userIdRetailerIdMap = new HashMap<>();
23955 govind 889
		for (UserAccount userAccount : userAccounts) {
23781 ashik.ali 890
			userIdRetailerIdMap.put(userAccount.getUserId(), userAccount.getAccountKey());
891
		}
892
		return userIdRetailerIdMap;
893
	}
23957 tejbeer 894
 
895
	private Map<Integer, String> getUserIdEmailIdMap(Set<Integer> userIds){
23880 ashik.ali 896
		List<User> users = userRepository.selectAllByIds(userIds);
897
		Map<Integer, String> userIdEmailIdMap = new HashMap<>();
23955 govind 898
		for (User user : users) {
23880 ashik.ali 899
			userIdEmailIdMap.put(user.getId(), user.getEmailId());
900
		}
901
		return userIdEmailIdMap;
902
	}
23509 amit.gupta 903
 
23880 ashik.ali 904
	@Override
905
	public Map<Integer, String> getAllFofoRetailerIdEmailIdMap(Set<Integer> retailerIds) {
906
		List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByRetailerIds(retailerIds);
907
		Set<Integer> userIds = new HashSet<>();
23955 govind 908
		for (UserAccount userAccount : userAccounts) {
23880 ashik.ali 909
			userIds.add(userAccount.getUserId());
910
		}
911
		Map<Integer, String> retailerIdEmailIdMap = new HashMap<>();
912
		Map<Integer, String> userIdEmailIdMap = this.getUserIdEmailIdMap(userIds);
23955 govind 913
		for (UserAccount userAccount : userAccounts) {
23880 ashik.ali 914
			retailerIdEmailIdMap.put(userAccount.getAccountKey(), userIdEmailIdMap.get(userAccount.getUserId()));
915
		}
916
		return retailerIdEmailIdMap;
917
	}
24168 amit.gupta 918
	@Override
24349 amit.gupta 919
	public Map<Integer, String> getAllFofoRetailerIdNameMap(List<Integer> storeIds) {
24168 amit.gupta 920
		Map<Integer, CustomRetailer> retailersMap = this.getFofoRetailers(storeIds);
921
		Map<Integer, String> retailerIdNameMap = new HashMap<>();
922
		for (Map.Entry<Integer, CustomRetailer> entry: retailersMap.entrySet()) {
24349 amit.gupta 923
			retailerIdNameMap.put(entry.getKey(), entry.getValue().getBusinessName() + "-" + entry.getValue().getAddress().getCity());
24168 amit.gupta 924
		}
925
		return retailerIdNameMap;
926
	}
927
 
928
	@Override
929
	public Map<Integer, String> getAllFofoRetailerIdNameMap() {
24349 amit.gupta 930
		List<FofoStore> stores = fofoStoreRepository.selectAll();
931
		List<Integer> storeIds = stores.stream().map(x->x.getId()).collect(Collectors.toList());
932
		return this.getAllFofoRetailerIdNameMap(storeIds);
933
	}
934
 
935
	@Override
936
	public List<MapWrapper<Integer, String>> getAllFofoRetailerIdNameList() {
937
		List<MapWrapper<Integer, String>> mapWrappers = new ArrayList<>();
24168 amit.gupta 938
		// TODO Auto-generated method stub
24349 amit.gupta 939
		Map<Integer, String> fofoIdNameMap = this.getAllFofoRetailerIdNameMap();
940
		fofoIdNameMap .forEach((fofoId,name)->{
941
			MapWrapper<Integer, String> idWrapper = new MapWrapper<>();
942
			idWrapper.setKey(fofoId);
943
			idWrapper.setValue(name);
944
			mapWrappers.add(idWrapper);
945
		});
946
		return mapWrappers;
947
	}
948
 
949
 
950
	@Override
951
	@Cacheable(value="retailerNames", cacheManager="cacheManager")
952
	public Map<Integer, CustomRetailer> getFofoRetailers() {
953
		// TODO Auto-generated method stub
24168 amit.gupta 954
		List<FofoStore> stores = fofoStoreRepository.selectAll();
955
		List<Integer> storeIds = stores.stream().map(x->x.getId()).collect(Collectors.toList());
24349 amit.gupta 956
		return this.getFofoRetailers(storeIds);
24168 amit.gupta 957
	}
24124 govind 958
 
22980 ashik.ali 959
}