Subversion Repositories SmartDukaan

Rev

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