Subversion Repositories SmartDukaan

Rev

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