Subversion Repositories SmartDukaan

Rev

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