Subversion Repositories SmartDukaan

Rev

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