Subversion Repositories SmartDukaan

Rev

Rev 28908 | Rev 29605 | 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
 
26528 amit.gupta 3
import java.nio.charset.StandardCharsets;
23300 amit.gupta 4
import java.time.LocalDateTime;
23060 ashik.ali 5
import java.util.ArrayList;
22980 ashik.ali 6
import java.util.HashMap;
23781 ashik.ali 7
import java.util.HashSet;
22980 ashik.ali 8
import java.util.List;
9
import java.util.Map;
10
import java.util.Set;
11
import java.util.function.Function;
12
import java.util.stream.Collectors;
26962 amit.gupta 13
import java.util.stream.Stream;
22980 ashik.ali 14
 
23781 ashik.ali 15
import org.apache.logging.log4j.LogManager;
23568 govind 16
import org.apache.logging.log4j.Logger;
22980 ashik.ali 17
import org.springframework.beans.factory.annotation.Autowired;
23043 ashik.ali 18
import org.springframework.beans.factory.annotation.Qualifier;
24349 amit.gupta 19
import org.springframework.cache.annotation.Cacheable;
22980 ashik.ali 20
import org.springframework.stereotype.Component;
21
 
26528 amit.gupta 22
import com.google.common.hash.Hashing;
22980 ashik.ali 23
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23025 ashik.ali 24
import com.spice.profitmandi.common.model.CustomAddress;
23509 amit.gupta 25
import com.spice.profitmandi.common.model.CustomRetailer;
23025 ashik.ali 26
import com.spice.profitmandi.common.model.CustomShop;
23781 ashik.ali 27
import com.spice.profitmandi.common.model.MapWrapper;
23329 ashik.ali 28
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23025 ashik.ali 29
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
23329 ashik.ali 30
import com.spice.profitmandi.common.util.StringUtils;
31
import com.spice.profitmandi.common.util.Utils;
32
import com.spice.profitmandi.dao.entity.dtr.DistrictMaster;
22980 ashik.ali 33
import com.spice.profitmandi.dao.entity.dtr.Retailer;
28024 tejbeer 34
import com.spice.profitmandi.dao.entity.dtr.RetailerBlockBrands;
23043 ashik.ali 35
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
23781 ashik.ali 36
import com.spice.profitmandi.dao.entity.dtr.Role;
22980 ashik.ali 37
import com.spice.profitmandi.dao.entity.dtr.Shop;
38
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
39
import com.spice.profitmandi.dao.entity.dtr.User;
23269 ashik.ali 40
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
22980 ashik.ali 41
import com.spice.profitmandi.dao.entity.dtr.UserRole;
23365 ashik.ali 42
import com.spice.profitmandi.dao.entity.fofo.FofoPartnerPaymentOption;
23329 ashik.ali 43
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
28908 tejbeer 44
import com.spice.profitmandi.dao.entity.fofo.PartnerOnBoardingPanel;
29042 tejbeer 45
import com.spice.profitmandi.dao.entity.fofo.PartnerVerificationpanel;
23365 ashik.ali 46
import com.spice.profitmandi.dao.entity.fofo.PaymentOption;
24349 amit.gupta 47
import com.spice.profitmandi.dao.entity.inventory.State;
22980 ashik.ali 48
import com.spice.profitmandi.dao.entity.user.Address;
23043 ashik.ali 49
import com.spice.profitmandi.dao.entity.user.Cart;
23329 ashik.ali 50
import com.spice.profitmandi.dao.entity.user.Counter;
26522 amit.gupta 51
import com.spice.profitmandi.dao.entity.user.LoginRequestResponseModel;
23329 ashik.ali 52
import com.spice.profitmandi.dao.entity.user.PrivateDealUser;
53
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressMapping;
23063 ashik.ali 54
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
23106 ashik.ali 55
import com.spice.profitmandi.dao.enumuration.dtr.RetailerType;
23136 ashik.ali 56
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
23365 ashik.ali 57
import com.spice.profitmandi.dao.enumuration.fofo.PaymentOptionType;
29042 tejbeer 58
import com.spice.profitmandi.dao.enumuration.transaction.PartnerVerificationApprovalStatus;
23329 ashik.ali 59
import com.spice.profitmandi.dao.repository.dtr.DistrictMasterRepository;
22980 ashik.ali 60
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
23329 ashik.ali 61
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
28024 tejbeer 62
import com.spice.profitmandi.dao.repository.dtr.Mongo;
28908 tejbeer 63
import com.spice.profitmandi.dao.repository.dtr.PartnerOnBoardingPanelRepository;
29042 tejbeer 64
import com.spice.profitmandi.dao.repository.dtr.PartnerOnboardingVerificationRepository;
28024 tejbeer 65
import com.spice.profitmandi.dao.repository.dtr.RetailerBlockBrandsRepository;
22980 ashik.ali 66
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
67
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
23781 ashik.ali 68
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
22980 ashik.ali 69
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
70
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
71
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
72
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
73
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
23365 ashik.ali 74
import com.spice.profitmandi.dao.repository.fofo.FofoPartnerPaymentOptionRepository;
75
import com.spice.profitmandi.dao.repository.fofo.PaymentOptionRepository;
24349 amit.gupta 76
import com.spice.profitmandi.dao.repository.inventory.StateRepository;
22980 ashik.ali 77
import com.spice.profitmandi.dao.repository.user.AddressRepository;
23043 ashik.ali 78
import com.spice.profitmandi.dao.repository.user.CartRepository;
23329 ashik.ali 79
import com.spice.profitmandi.dao.repository.user.CounterRepository;
80
import com.spice.profitmandi.dao.repository.user.PrivateDealUserAddressMappingRepository;
81
import com.spice.profitmandi.dao.repository.user.PrivateDealUserRepository;
27877 amit.gupta 82
import com.spice.profitmandi.service.offers.PartnerCriteria;
22980 ashik.ali 83
 
23329 ashik.ali 84
import in.shop2020.model.v1.inventory.StateInfo;
23043 ashik.ali 85
import in.shop2020.model.v1.user.CartStatus;
86
 
22980 ashik.ali 87
@Component
88
public class RetailerServiceImpl implements RetailerService {
23955 govind 89
 
23568 govind 90
	private static final Logger LOGGER = LogManager.getLogger(RetailerServiceImpl.class);
23955 govind 91
 
22980 ashik.ali 92
	@Autowired
93
	private RetailerRepository retailerRepository;
23955 govind 94
 
22980 ashik.ali 95
	@Autowired
28024 tejbeer 96
	private RetailerBlockBrandsRepository retailerBlockBrandsRepository;
97
 
98
	@Autowired
22980 ashik.ali 99
	private UserAccountRepository userAccountRepository;
23955 govind 100
 
22980 ashik.ali 101
	@Autowired
28024 tejbeer 102
	private Mongo mongoClient;
103
 
104
	@Autowired
22980 ashik.ali 105
	private UserRepository userRepository;
23955 govind 106
 
22980 ashik.ali 107
	@Autowired
23043 ashik.ali 108
	private CartRepository cartRepository;
23955 govind 109
 
23043 ashik.ali 110
	@Autowired
22980 ashik.ali 111
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
23955 govind 112
 
22980 ashik.ali 113
	@Autowired
114
	private AddressRepository addressRepository;
23955 govind 115
 
22980 ashik.ali 116
	@Autowired
117
	private ShopRepository shopRepository;
23955 govind 118
 
22980 ashik.ali 119
	@Autowired
120
	private ShopAddressRepository shopAddressRepository;
23955 govind 121
 
22980 ashik.ali 122
	@Autowired
123
	private UserRoleRepository userRoleRepository;
23955 govind 124
 
22980 ashik.ali 125
	@Autowired
126
	private DocumentRepository documentRepository;
23955 govind 127
 
23043 ashik.ali 128
	@Autowired
23329 ashik.ali 129
	private PrivateDealUserRepository privateDealUserRepository;
23955 govind 130
 
23329 ashik.ali 131
	@Autowired
132
	private PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
23955 govind 133
 
23329 ashik.ali 134
	@Autowired
135
	private CounterRepository counterRepository;
25295 amit.gupta 136
 
24349 amit.gupta 137
	@Autowired
138
	private StateRepository stateRepository;
23955 govind 139
 
23329 ashik.ali 140
	@Autowired
23043 ashik.ali 141
	@Qualifier("userUserRepository")
142
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
23955 govind 143
 
23329 ashik.ali 144
	@Autowired
145
	private DistrictMasterRepository districtMasterRepository;
23955 govind 146
 
23329 ashik.ali 147
	@Autowired
148
	private FofoStoreRepository fofoStoreRepository;
23955 govind 149
 
23329 ashik.ali 150
	@Autowired
23365 ashik.ali 151
	private PaymentOptionRepository paymentOptionRepository;
23955 govind 152
 
23365 ashik.ali 153
	@Autowired
154
	private FofoPartnerPaymentOptionRepository fofoPartnerPaymentOptionRepository;
23955 govind 155
 
23842 ashik.ali 156
	@Autowired
157
	private RoleRepository roleRepository;
23955 govind 158
 
28908 tejbeer 159
	@Autowired
160
	private StoreTimelineTatService storeTimelineTatService;
161
 
162
	@Autowired
163
	private PartnerOnBoardingPanelRepository partnerOnBoardingPanelRepository;
164
 
29042 tejbeer 165
	@Autowired
166
	private PartnerOnboardingVerificationRepository partnerOnboardingVerificationRepository;
167
 
22980 ashik.ali 168
	@Override
169
	public Map<String, Object> getByEmailIdOrMobileNumber(String emailIdOrMobileNumber)
170
			throws ProfitMandiBusinessException {
23202 ashik.ali 171
		User user = null;
28024 tejbeer 172
		int fofoId = Utils.SYSTEM_PARTNER_ID;
173
 
23955 govind 174
		try {
23202 ashik.ali 175
			user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
23955 govind 176
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
177
 
23202 ashik.ali 178
		}
23955 govind 179
		if (user == null) {
180
			try {
23329 ashik.ali 181
				user = userRepository.selectBySecondryEmailId(emailIdOrMobileNumber);
23955 govind 182
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
183
 
23329 ashik.ali 184
			}
23202 ashik.ali 185
		}
22980 ashik.ali 186
		Map<String, Object> map = new HashMap<>();
25295 amit.gupta 187
		map.put("stateNames", stateRepository.selectAll().stream().map(x -> x.getName()).collect(Collectors.toList()));
23955 govind 188
 
189
		if (user != null) {
190
 
23329 ashik.ali 191
			List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
23955 govind 192
 
23781 ashik.ali 193
			Role role = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 194
 
23329 ashik.ali 195
			map.put("userRoles", userRoles);
196
			map.put("user", user);
23955 govind 197
			// map.put("retailer", retailer);
23781 ashik.ali 198
			map.put("fofoRole", this.containsRoleType(userRoles, role.getId()));
23955 govind 199
 
23329 ashik.ali 200
			map.put("userRoleNames", this.toString(userRoles));
23955 govind 201
			try {
23329 ashik.ali 202
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
203
				Retailer retailer = retailerRepository.selectById(retailerId);
23955 govind 204
 
23329 ashik.ali 205
				map.put("retailer", retailer);
23955 govind 206
 
28024 tejbeer 207
				List<String> retailerBlockBrands = null;
208
				Set<String> brands = null;
209
				if (retailer.getId() != fofoId) {
210
					brands = mongoClient.getMongoBrands(retailer.getId(), null, 3).stream()
211
							.map(x -> (String) x.get("name")).collect(Collectors.toSet());
28189 tejbeer 212
					retailerBlockBrands = retailerBlockBrandsRepository.selectAllByRetailer(retailer.getId()).stream()
213
							.map(x -> x.getBlockBrands()).collect(Collectors.toList());
214
 
28024 tejbeer 215
					LOGGER.info("loginDetailsFofoId" + retailer.getId());
216
				} else {
217
					LOGGER.info("fofoId" + fofoId);
218
					brands = mongoClient.getMongoBrands(fofoId, null, 3).stream().map(x -> (String) x.get("name"))
219
							.collect(Collectors.toSet());
220
				}
221
				map.put("brands", brands);
222
				map.put("retailerBlockBrands", retailerBlockBrands);
23955 govind 223
				if (this.containsRoleType(userRoles, role.getId())) {
224
					try {
23329 ashik.ali 225
						FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
226
						map.put("fofoStore", fofoStore);
23955 govind 227
						// map.put("counterSize", fofoStore.getCounterSize().toString());
228
						// LOGGER.info("fofoStore" + fofoStore);
229
					} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 230
						LOGGER.error("FofoStore code not found");
231
					}
232
				}
23955 govind 233
 
234
				try {
23329 ashik.ali 235
					PrivateDealUser privateDealUser = privateDealUserRepository.selectById(retailer.getId());
23955 govind 236
					if (privateDealUser.getCounterId() != null) {
23329 ashik.ali 237
						Counter counter = counterRepository.selectById(privateDealUser.getCounterId());
238
						map.put("gstNumber", counter.getGstin());
23955 govind 239
						// LOGGER.info("gstNumber" + counter.getGstin());
23329 ashik.ali 240
					}
23955 govind 241
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 242
					LOGGER.error("PrivateDealUser not found");
243
				}
23955 govind 244
				try {
245
					int retailerAddressId = retailerRegisteredAddressRepository
246
							.selectAddressIdByRetailerId(retailer.getId());
23329 ashik.ali 247
					Address retailerAddress = addressRepository.selectById(retailerAddressId);
248
					map.put("retailerAddress", retailerAddress);
24349 amit.gupta 249
					State state = stateRepository.selectByName(retailerAddress.getState());
23955 govind 250
					// LOGGER.info("retailerAddress.." + retailerAddress);
251
					List<DistrictMaster> districtMasters = districtMasterRepository
24349 amit.gupta 252
							.selectByStateShortName(state.getShortName());
23329 ashik.ali 253
					map.put("districtMasters", districtMasters);
23955 govind 254
					// LOGGER.info("districtMasters" + districtMasters);
23329 ashik.ali 255
					List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
23955 govind 256
					map.put("shops", shops);
23329 ashik.ali 257
					this.addAddress(shops);
23955 govind 258
					// LOGGER.info("shops" + shops);
259
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 260
					LOGGER.error("Retailer Registered Address not found");
261
				}
23955 govind 262
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 263
				LOGGER.error("Retailer not found in user_account");
23043 ashik.ali 264
			}
265
		}
23955 govind 266
 
22980 ashik.ali 267
		return map;
268
	}
23955 govind 269
 
270
	private boolean containsRoleType(List<UserRole> userRoles, int roleId) {
271
		for (UserRole userRole : userRoles) {
272
			if (userRole.getRoleId() == roleId) {
23329 ashik.ali 273
				return true;
274
			}
275
		}
276
		return false;
277
	}
23955 govind 278
 
279
	private List<UserRole> addRole(List<UserRole> userRoles, int userId, int roleId) {
280
		if (userRoles == null) {
23329 ashik.ali 281
			userRoles = new ArrayList<>();
282
		}
23955 govind 283
		if (!this.containsRoleType(userRoles, roleId)) {
23329 ashik.ali 284
			UserRole userRole = new UserRole();
285
			userRole.setUserId(userId);
23781 ashik.ali 286
			userRole.setRoleId(roleId);
23329 ashik.ali 287
			try {
288
				userRoleRepository.persist(userRole);
289
			} catch (ProfitMandiBusinessException e) {
290
				LOGGER.error("UserRole is already exist");
291
			}
292
			userRoles.add(userRole);
293
		}
294
		return userRoles;
23955 govind 295
 
23329 ashik.ali 296
	}
23955 govind 297
 
25295 amit.gupta 298
	private List<UserRole> removeRole(List<UserRole> userRoles, int userId, int roleId) {
299
		if (userRoles == null) {
300
			userRoles = new ArrayList<>();
301
			return userRoles;
302
		}
303
		try {
304
			userRoles.remove(userRoleRepository.deleteByRoleUserId(userId, roleId));
305
		} catch (Exception e) {
306
 
307
		}
308
		return userRoles;
309
 
310
	}
311
 
22980 ashik.ali 312
	@SuppressWarnings("unchecked")
313
	@Override
23025 ashik.ali 314
	public Map<String, Object> updateRetailerDetails(UpdateRetailerRequest updateRetailerRequest)
315
			throws ProfitMandiBusinessException {
316
		Map<String, Object> map = this.getByEmailIdOrMobileNumber(updateRetailerRequest.getEmailIdOrMobileNumber());
23955 govind 317
		User user = (User) map.get("user");
28024 tejbeer 318
 
23329 ashik.ali 319
		user = this.createUser(user, updateRetailerRequest);
28024 tejbeer 320
 
23329 ashik.ali 321
		map.put("user", user);
23955 govind 322
		List<UserRole> userRoles = (List<UserRole>) map.get("userRoles");
23781 ashik.ali 323
		Role roleUser = roleRepository.selectByName(RoleType.USER.toString());
324
		userRoles = this.addRole(userRoles, user.getId(), roleUser.getId());
23955 govind 325
		Retailer retailer = (Retailer) map.get("retailer");
23329 ashik.ali 326
		retailer = this.updateRetailer(user, retailer, updateRetailerRequest);
28024 tejbeer 327
 
23329 ashik.ali 328
		map.put("retailer", retailer);
23955 govind 329
 
28189 tejbeer 330
		List<String> retailerBlockBrands = retailerBlockBrandsRepository.selectAllByRetailer(retailer.getId()).stream()
331
				.map(x -> x.getBlockBrands()).collect(Collectors.toList());
28024 tejbeer 332
 
28189 tejbeer 333
		LOGGER.info("retailerBlockBrands" + retailerBlockBrands);
334
 
28024 tejbeer 335
		if (!retailerBlockBrands.isEmpty()) {
336
			retailerBlockBrandsRepository.deleteBrands(retailer.getId());
337
		}
338
 
339
		for (String blockBrand : updateRetailerRequest.getBlocksBrands()) {
340
			RetailerBlockBrands retailerBlockBrand = new RetailerBlockBrands();
341
 
342
			retailerBlockBrand.setFofoId(retailer.getId());
343
			retailerBlockBrand.setBlockBrands(blockBrand);
344
			retailerBlockBrandsRepository.persist(retailerBlockBrand);
345
 
346
		}
347
		map.put("retailerBlockBrands", retailerBlockBrands);
348
 
23781 ashik.ali 349
		Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
23955 govind 350
 
23781 ashik.ali 351
		userRoles = this.addRole(userRoles, user.getId(), roleRetailer.getId());
23955 govind 352
 
23781 ashik.ali 353
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 354
 
355
		if (updateRetailerRequest.isFofo()) {
23781 ashik.ali 356
			userRoles = this.addRole(userRoles, user.getId(), roleFofo.getId());
25295 amit.gupta 357
		} else {
358
			userRoles = this.removeRole(userRoles, user.getId(), roleFofo.getId());
23061 ashik.ali 359
		}
25295 amit.gupta 360
		map.put("fofoRole", this.containsRoleType(userRoles, roleFofo.getId()));
23955 govind 361
 
362
		if (this.containsRoleType(userRoles, roleFofo.getId())) {
23365 ashik.ali 363
			this.createDefaultPaymentOption(retailer.getId());
23329 ashik.ali 364
		}
23955 govind 365
 
23329 ashik.ali 366
		map.put("userRoles", userRoles);
367
		map.put("userRoleNames", this.toString(userRoles));
23955 govind 368
 
369
		Address retailerAddress = (Address) map.get("retailerAddress");
370
		retailerAddress = this.updateRetailerAddress(retailerAddress, updateRetailerRequest.getAddress(),
371
				retailer.getId());
23329 ashik.ali 372
		map.put("retailerAddress", retailerAddress);
23955 govind 373
 
374
		if (updateRetailerRequest.isFofo()) {
375
			FofoStore fofoStore = this.createFofoStoreCodeByRetailerId(retailer.getId(),
376
					updateRetailerRequest.getDistrictName(), retailerAddress.getState(), updateRetailerRequest);
23378 ashik.ali 377
			map.put("fofoStore", fofoStore);
25297 amit.gupta 378
			fofoStore.setActive(updateRetailerRequest.isActive());
23061 ashik.ali 379
		}
23955 govind 380
 
381
		this.createPrivateDealUser(user, updateRetailerRequest.isFofo(), updateRetailerRequest.getGstNumber(), retailer,
382
				retailerAddress.getId());
23329 ashik.ali 383
		map.put("gstNumber", updateRetailerRequest.getGstNumber());
384
		this.updateSaholicUser(retailer.getId(), retailerAddress.getId());
23955 govind 385
 
386
		List<Shop> shops = (List<Shop>) map.get("shops");
387
		if (shops == null) {
23060 ashik.ali 388
			shops = new ArrayList<>();
23061 ashik.ali 389
			map.put("shops", shops);
23060 ashik.ali 390
		}
23079 ashik.ali 391
		this.updateRetailerShops(shops, updateRetailerRequest.getShops(), retailer.getId(), retailerAddress);
23955 govind 392
 
23025 ashik.ali 393
		return map;
23955 govind 394
 
23025 ashik.ali 395
	}
23955 govind 396
 
397
	private void createDefaultPaymentOption(int fofoId) {
23365 ashik.ali 398
		List<Integer> paymentOptionIds = fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(fofoId);
23955 govind 399
		if (paymentOptionIds.isEmpty()) {
23365 ashik.ali 400
			PaymentOption paymentOption = null;
23955 govind 401
			try {
23365 ashik.ali 402
				paymentOption = paymentOptionRepository.selectByName(PaymentOptionType.CASH.toString());
23955 govind 403
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23365 ashik.ali 404
				paymentOption = new PaymentOption();
405
				paymentOption.setName(PaymentOptionType.CASH.toString());
406
				paymentOptionRepository.persist(paymentOption);
407
			}
408
			FofoPartnerPaymentOption fofoPartnerPaymentOption = new FofoPartnerPaymentOption();
409
			fofoPartnerPaymentOption.setFofoId(fofoId);
410
			fofoPartnerPaymentOption.setPaymentOptionId(paymentOption.getId());
411
			fofoPartnerPaymentOptionRepository.persist(fofoPartnerPaymentOption);
412
		}
23955 govind 413
 
23365 ashik.ali 414
	}
23955 govind 415
 
416
	private User createUser(User user, UpdateRetailerRequest updateRetailerRequest) {
417
		if (user == null) {
23329 ashik.ali 418
			user = new User();
419
			user.setCity("");
420
			user.setPinCode(0);
421
			user.setState("");
422
			user.setPassword("");
423
			user.setMobile_verified(false);
424
			user.setReferral_url("");
425
			user.setGroup_id(1);
426
			user.setStatus(1);
427
			user.setActivated(true);
428
			user.setCreateTimestamp(LocalDateTime.now());
429
		}
24705 amit.gupta 430
		user.setActivated(updateRetailerRequest.isActive());
23329 ashik.ali 431
		user.setFirstName(updateRetailerRequest.getUserFirstName());
432
		user.setLastName(updateRetailerRequest.getUserLastName());
433
		user.setMobileNumber(updateRetailerRequest.getUserMobileNumber());
434
		user.setEmailId(updateRetailerRequest.getUserEmailId());
435
		user.setUsername(updateRetailerRequest.getUserEmailId());
23509 amit.gupta 436
		user.setUpdateTimestamp(LocalDateTime.now());
437
		userRepository.persist(user);
23329 ashik.ali 438
		return user;
23955 govind 439
 
23329 ashik.ali 440
	}
23955 govind 441
 
442
	private int createSaholicUser(User user, String retailerName) {
23043 ashik.ali 443
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
444
		try {
23063 ashik.ali 445
			saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
23955 govind 446
		} catch (ProfitMandiBusinessException e) {
23043 ashik.ali 447
			LOGGER.info("User doesnt exist in old system");
448
		}
23955 govind 449
		if (saholicUser == null) {
23043 ashik.ali 450
			Cart cart = new Cart();
451
			cart.setCartStatus(CartStatus.ACTIVE);
452
			cartRepository.persist(cart);
453
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
23063 ashik.ali 454
			saholicUser.setEmailId(user.getEmailId());
23043 ashik.ali 455
			saholicUser.setName(retailerName);
456
			saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 457
			saholicUser.setCreateTimestamp(LocalDateTime.now());
23043 ashik.ali 458
			userUserRepository.persist(saholicUser);
23955 govind 459
 
23269 ashik.ali 460
			UserAccount ua = new UserAccount();
461
			ua.setAccountKey(saholicUser.getId());
462
			ua.setUserId(user.getId());
463
			ua.setType(AccountType.saholic);
23063 ashik.ali 464
			userAccountRepository.persist(ua);
465
 
23269 ashik.ali 466
			UserAccount ua2 = new UserAccount();
467
			ua2.setAccountKey(saholicUser.getActiveCartId());
468
			ua2.setUserId(user.getId());
469
			ua2.setType(AccountType.cartId);
23063 ashik.ali 470
			userAccountRepository.persist(ua2);
23955 govind 471
			LOGGER.info("created....");
23043 ashik.ali 472
		}
473
		return saholicUser.getId();
474
	}
23955 govind 475
 
26522 amit.gupta 476
	private com.spice.profitmandi.dao.entity.user.User createSaholicUser(
477
			LoginRequestResponseModel loginRequestResponseModel) {
478
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
479
		try {
480
			saholicUser = userUserRepository.selectByEmailId(loginRequestResponseModel.getEmail());
28198 tejbeer 481
			saholicUser.setCreateTimestamp(LocalDateTime.now());
26522 amit.gupta 482
		} catch (ProfitMandiBusinessException e) {
483
			LOGGER.info("User doesnt exist in old system");
484
		}
485
		if (saholicUser == null) {
486
			Cart cart = new Cart();
487
			cart.setCartStatus(CartStatus.ACTIVE);
488
			cartRepository.persist(cart);
489
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
26528 amit.gupta 490
			saholicUser.setPassword(getHash256(loginRequestResponseModel.getPassword()));
26522 amit.gupta 491
			saholicUser.setEmailId(loginRequestResponseModel.getEmail());
492
			saholicUser.setName(loginRequestResponseModel.getCustomerName());
493
			saholicUser.setActiveCartId(cart.getId());
494
			saholicUser.setCreateTimestamp(LocalDateTime.now());
495
			loginRequestResponseModel.setPassword(null);
496
			userUserRepository.persist(saholicUser);
497
		}
498
		return saholicUser;
499
	}
500
 
23955 govind 501
	private Retailer updateRetailer(User user, Retailer retailer, UpdateRetailerRequest updateRetailerRequest)
502
			throws ProfitMandiBusinessException {
503
		if (retailer == null) {
504
			LOGGER.info("createSaholicUser.....");
23063 ashik.ali 505
			int saholicUserId = this.createSaholicUser(user, updateRetailerRequest.getName());
23043 ashik.ali 506
			retailer = new Retailer();
507
			retailer.setId(saholicUserId);
508
		}
23329 ashik.ali 509
		retailer.setActive(updateRetailerRequest.isActive());
24704 amit.gupta 510
		user.setActivated(updateRetailerRequest.isActive());//
23955 govind 511
		// this.createRole(user.getId(), RoleType.RETAILER);
23025 ashik.ali 512
		retailer.setName(updateRetailerRequest.getName());
513
		retailer.setNumber(updateRetailerRequest.getNumber());
23955 govind 514
		if (updateRetailerRequest.getNumber() == null || updateRetailerRequest.getNumber().isEmpty()) {
23107 ashik.ali 515
			retailer.setType(RetailerType.UNREGISTERED_SHOP);
23955 govind 516
		} else {
23107 ashik.ali 517
			retailer.setType(RetailerType.GSTIN);
518
		}
23955 govind 519
		if (updateRetailerRequest.getDocumentId() > 0) {
520
			if (retailer.getDocumentId() != null && retailer.getDocumentId() != updateRetailerRequest.getDocumentId()) {
24374 amit.gupta 521
				try {
522
					documentRepository.deleteById(retailer.getDocumentId());
25295 amit.gupta 523
				} catch (Exception e) {
24374 amit.gupta 524
					e.printStackTrace();
525
				}
23489 ashik.ali 526
			}
23497 ashik.ali 527
			retailer.setDocumentId(updateRetailerRequest.getDocumentId());
528
			documentRepository.markDocumentAsPersisted(updateRetailerRequest.getDocumentId());
23025 ashik.ali 529
		}
530
		retailerRepository.persist(retailer);
23059 ashik.ali 531
		return retailer;
23025 ashik.ali 532
	}
23955 govind 533
 
534
	private void updateSaholicUser(int retailerId, int retailerAddressId) {
23329 ashik.ali 535
		try {
536
			com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectById(retailerId);
537
			user.setAddressId(retailerAddressId);
538
		} catch (ProfitMandiBusinessException e) {
23955 govind 539
 
23329 ashik.ali 540
		}
541
	}
23955 govind 542
 
543
	private void createPrivateDealUser(User user, boolean fofo, String gstNumber, Retailer retailer,
544
			int retailerAddressId) {
23329 ashik.ali 545
		PrivateDealUser privateDealUser = null;
23955 govind 546
		try {
23329 ashik.ali 547
			privateDealUser = privateDealUserRepository.selectById(retailer.getId());
23955 govind 548
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 549
			LOGGER.error("PrivateDealUser not found");
550
		}
23955 govind 551
 
552
		// = privateDealUserRepository.selectById(saholicUser.getId());
553
		if (privateDealUser == null) {
554
			Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(),
555
					retailer.getName(), retailerAddressId);
23473 ashik.ali 556
			try {
557
				this.createPrivateDealUser(retailer.getId(), counterId, fofo);
23955 govind 558
			} catch (Exception e) {
23473 ashik.ali 559
				LOGGER.error("ERROR : ", e);
560
			}
23955 govind 561
		} else {
562
			if (privateDealUser.getCounterId() == null) {
563
				Integer counterId = this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(),
564
						retailer.getName(), retailerAddressId);
23329 ashik.ali 565
				privateDealUser.setCounterId(counterId);
566
				privateDealUser.setFofo(fofo);
567
				privateDealUserRepository.persist(privateDealUser);
23955 govind 568
			} else {
23329 ashik.ali 569
				Counter counter = null;
23955 govind 570
				try {
23329 ashik.ali 571
					counter = counterRepository.selectById(privateDealUser.getCounterId());
23955 govind 572
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 573
					LOGGER.error("Counter not found with id [{}]", privateDealUser.getCounterId());
574
				}
23955 govind 575
				if (counter == null) {
576
					this.createCounter(user.getEmailId(), gstNumber, user.getMobileNumber(), retailer.getName(),
577
							retailerAddressId);
578
				} else {
23329 ashik.ali 579
					counter.setGstin(gstNumber);
580
					counterRepository.persist(counter);
581
				}
582
				privateDealUser.setFofo(fofo);
583
				privateDealUserRepository.persist(privateDealUser);
584
			}
585
		}
23955 govind 586
 
23329 ashik.ali 587
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
588
		privateDealUserAddressMapping.setUserId(retailer.getId());
589
		privateDealUserAddressMapping.setAddressId(retailerAddressId);
590
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
591
	}
23955 govind 592
 
593
	// Specifically set isFofo to true that has to be used by old system
594
	private void createPrivateDealUser(int retailerId, int counterId, boolean fofo) {
23329 ashik.ali 595
		PrivateDealUser privateDealUser = new PrivateDealUser();
596
		privateDealUser.setActive(true);
597
		privateDealUser.setBulkShipmentAmountLimit(fofo ? 1000000 : 50000);
598
		privateDealUser.setId(retailerId);
599
		privateDealUser.setCounterId(counterId);
600
		privateDealUser.setFofo(fofo);
601
		privateDealUserRepository.persist(privateDealUser);
602
	}
23955 govind 603
 
604
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId) {
605
		if (gstNumber != null && !gstNumber.isEmpty()) {
23329 ashik.ali 606
			Counter counter = new Counter();
607
			counter.setEmailId(emailId);
608
			counter.setGstin(gstNumber);
609
			counter.setMobileNumber(mobileNumber);
610
			counter.setName(name);
611
			counter.setAddressId(addressId);
612
			counterRepository.persist(counter);
613
			return counter.getId();
23955 govind 614
		} else {
23329 ashik.ali 615
			return null;
616
		}
617
	}
23955 govind 618
 
619
	private Address updateRetailerAddress(Address address, CustomAddress customAddress, int retailerId)
620
			throws ProfitMandiBusinessException {
621
		if (address == null) {
23043 ashik.ali 622
			address = new Address();
623
			address.setRetaierId(retailerId);
624
			this.updateAddress(address, customAddress);
23955 govind 625
			// addressRepository.persist(addressRetailer);
626
 
23043 ashik.ali 627
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
628
			retailerRegisteredAddress.setRetailerId(retailerId);
629
			retailerRegisteredAddress.setAddressId(address.getId());
630
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
23955 govind 631
		} else {
632
			this.updateAddress(address, customAddress);
633
			RetailerRegisteredAddress retailerRegisteredAddress = retailerRegisteredAddressRepository
634
					.selectByRetailerId(retailerId);
635
			retailerRegisteredAddress.setAddressId(address.getId());
636
			retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
23043 ashik.ali 637
		}
23059 ashik.ali 638
		return address;
23043 ashik.ali 639
	}
23955 govind 640
 
27411 amit.gupta 641
	private void updateAddress(Address address, CustomAddress customAddress) throws ProfitMandiBusinessException {
23025 ashik.ali 642
		address.setName(customAddress.getName());
23829 amit.gupta 643
		address.setPhoneNumber(customAddress.getPhoneNumber());
23025 ashik.ali 644
		address.setLine1(customAddress.getLine1());
645
		address.setLine2(customAddress.getLine2());
646
		address.setCity(customAddress.getCity());
647
		address.setPinCode(customAddress.getPinCode());
27411 amit.gupta 648
		State state = stateRepository.selectByName(customAddress.getState());
27877 amit.gupta 649
		if (state == null) {
650
			throw new ProfitMandiBusinessException("State name", "Invalid State - Pls Contact Technology",
651
					"Invalid State - Pls Contact Technology");
27411 amit.gupta 652
		}
653
		address.setState(state.getName());
23025 ashik.ali 654
		addressRepository.persist(address);
23955 govind 655
		LOGGER.info("Address Updated" + address);
23025 ashik.ali 656
	}
23955 govind 657
 
658
	private void updateRetailerShops(List<Shop> shops, Set<CustomShop> customShops, int retailerId,
659
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
660
		if (shops.isEmpty()) {
661
			for (CustomShop customShop : customShops) {
23043 ashik.ali 662
				Shop shop = new Shop();
23079 ashik.ali 663
				this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 664
				shops.add(shop);
665
			}
23955 govind 666
		} else {
667
			for (Shop shop : shops) {
668
				for (CustomShop customShop : customShops) {
669
					if (shop.getId() == customShop.getShopId()) {
23079 ashik.ali 670
						this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23025 ashik.ali 671
					}
672
				}
673
			}
23955 govind 674
			for (CustomShop customShop : customShops) {
675
				if (customShop.getShopId() == 0) {
23043 ashik.ali 676
					Shop shop = new Shop();
23079 ashik.ali 677
					this.createOrUpdateShop(shop, customShop, retailerId, sameAsRetailerAddressValue);
23043 ashik.ali 678
					shops.add(shop);
679
				}
680
			}
23025 ashik.ali 681
		}
682
	}
23955 govind 683
 
684
	private void createOrUpdateShop(Shop shop, CustomShop customShop, int retailerId,
685
			Address sameAsRetailerAddressValue) throws ProfitMandiBusinessException {
23043 ashik.ali 686
		shop.setRetailerId(retailerId);
687
		shop.setName(customShop.getName());
23955 govind 688
		if (customShop.getDocumentId() > 0) {
689
			if (shop.getDocumentId() != null && shop.getDocumentId() != customShop.getDocumentId()) {
24374 amit.gupta 690
				try {
691
					documentRepository.deleteById(shop.getDocumentId());
25295 amit.gupta 692
				} catch (Exception e) {
24374 amit.gupta 693
					e.printStackTrace();
694
				}
23489 ashik.ali 695
			}
23497 ashik.ali 696
			shop.setDocumentId(customShop.getDocumentId());
697
			documentRepository.markDocumentAsPersisted(customShop.getDocumentId());
23043 ashik.ali 698
		}
23955 govind 699
		if (shop.getAddressId() == null) {
23079 ashik.ali 700
			Address address = null;
23955 govind 701
			if (customShop.isSameAsRetailerAddress()) {
23079 ashik.ali 702
				address = sameAsRetailerAddressValue;
23955 govind 703
			} else {
704
				// shop.setDocumentId(customShop.getDocumentId());
23079 ashik.ali 705
				address = new Address();
706
				this.updateAddress(address, customShop.getAddress());
707
			}
23043 ashik.ali 708
			shop.setAddressId(address.getId());
709
			shop.setAddress(address);
710
			shopRepository.persist(shop);
23131 ashik.ali 711
			ShopAddress shopAddress = null;
23955 govind 712
			try {
23131 ashik.ali 713
				shopAddress = shopAddressRepository.selectByShopId(shop.getId());
714
				shopAddress.setAddressId(address.getId());
23955 govind 715
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23131 ashik.ali 716
				shopAddress = new ShopAddress();
717
				shopAddress.setAddressId(address.getId());
718
				shopAddress.setShopId(shop.getId());
719
			}
23043 ashik.ali 720
			shopAddressRepository.persist(shopAddress);
23955 govind 721
		} else {
24375 amit.gupta 722
			Address address = null;
723
			try {
724
				address = addressRepository.selectById(shop.getAddressId());
725
				CustomAddress customAddress = customShop.getAddress();
726
				if (!(address.getName().equals(customAddress.getName())
727
						&& address.getLine1().equals(customAddress.getLine1())
728
						&& address.getLine2().equals(customAddress.getLine2())
729
						&& address.getCity().equals(customAddress.getCity())
730
						&& address.getPinCode().equals(customAddress.getPinCode())
731
						&& address.getState().equals(customAddress.getState()))) {
732
					address = new Address();
733
					ShopAddress shopAddress = shopAddressRepository.selectByShopId(shop.getId());
734
					this.updateAddress(address, customAddress);
735
					shopAddress.setAddressId(address.getId());
736
					shopAddressRepository.persist(shopAddress);
737
					shop.setAddress(address);
738
					shopRepository.persist(shop);
739
				}
25295 amit.gupta 740
			} catch (Exception e) {
24375 amit.gupta 741
				if (customShop.isSameAsRetailerAddress()) {
742
					address = sameAsRetailerAddressValue;
743
				} else {
744
					// shop.setDocumentId(customShop.getDocumentId());
745
					address = new Address();
746
					this.updateAddress(address, customShop.getAddress());
747
				}
748
				shop.setAddressId(address.getId());
749
				shop.setAddress(address);
750
				shopRepository.persist(shop);
751
				ShopAddress shopAddress = null;
752
				try {
753
					shopAddress = shopAddressRepository.selectByShopId(shop.getId());
754
					shopAddress.setAddressId(address.getId());
755
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
756
					shopAddress = new ShopAddress();
757
					shopAddress.setAddressId(address.getId());
758
					shopAddress.setShopId(shop.getId());
759
				}
25295 amit.gupta 760
				shopAddressRepository.persist(shopAddress);
761
			}
23955 govind 762
 
23043 ashik.ali 763
		}
23955 govind 764
 
23043 ashik.ali 765
	}
23955 govind 766
 
767
	private void addAddress(List<Shop> shops) {
22980 ashik.ali 768
		Set<Integer> shopIds = this.toShopIds(shops);
23955 govind 769
		if (shopIds.isEmpty()) {
22980 ashik.ali 770
			return;
771
		}
772
		List<ShopAddress> shopAddresses = shopAddressRepository.selectByShopIds(shopIds);
773
		Map<Integer, Address> addressIdAddressMap = this.toAddressIdAddressMap(shopAddresses);
23955 govind 774
 
775
		for (Shop shop : shops) {
22980 ashik.ali 776
			shop.setAddress(addressIdAddressMap.get(shop.getAddressId()));
777
		}
778
	}
779
 
23955 govind 780
	private Map<Integer, Address> toAddressIdAddressMap(List<ShopAddress> shopAddresses) {
22980 ashik.ali 781
		Map<Integer, Address> addressIdAddressMap = new HashMap<>();
782
		List<Integer> addressIds = this.toAddressIds(shopAddresses);
783
		List<Address> addresses = addressRepository.selectByIds(addressIds);
23955 govind 784
		for (Address address : addresses) {
22980 ashik.ali 785
			addressIdAddressMap.put(address.getId(), address);
786
		}
787
		return addressIdAddressMap;
788
	}
23955 govind 789
 
790
	private List<Integer> toAddressIds(List<ShopAddress> shopAddresses) {
791
		Function<ShopAddress, Integer> shopAddressToAddressIdFunction = new Function<ShopAddress, Integer>() {
22980 ashik.ali 792
			@Override
793
			public Integer apply(ShopAddress shopAddress) {
794
				return shopAddress.getAddressId();
795
			}
796
		};
797
		return shopAddresses.stream().map(shopAddressToAddressIdFunction).collect(Collectors.toList());
798
	}
23955 govind 799
 
800
	private Set<Integer> toShopIds(List<Shop> shops) {
801
		Function<Shop, Integer> shopToAddressIdFunction = new Function<Shop, Integer>() {
22980 ashik.ali 802
			@Override
803
			public Integer apply(Shop shop) {
804
				return shop.getId();
805
			}
806
		};
807
		return shops.stream().map(shopToAddressIdFunction).collect(Collectors.toSet());
808
	}
23955 govind 809
 
810
	private String toString(List<UserRole> userRoles) {
23781 ashik.ali 811
		Set<Integer> roleIds = new HashSet<>();
23955 govind 812
		for (UserRole userRole : userRoles) {
23781 ashik.ali 813
			roleIds.add(userRole.getRoleId());
814
		}
815
		List<Role> roles = roleRepository.selectByIds(roleIds);
23955 govind 816
		Function<Role, String> roleToNameFunction = new Function<Role, String>() {
23781 ashik.ali 817
			public String apply(Role role) {
818
				return String.valueOf(role.getName());
22980 ashik.ali 819
			};
820
		};
23781 ashik.ali 821
		Set<String> userRoleStrings = roles.stream().map(roleToNameFunction).collect(Collectors.toSet());
22980 ashik.ali 822
		return String.join(", ", userRoleStrings);
823
	}
23955 govind 824
 
825
	private FofoStore createFofoStoreCodeByRetailerId(int retailerId, String districtName, String stateName,
826
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23378 ashik.ali 827
		FofoStore fofoStore = null;
23955 govind 828
		try {
23378 ashik.ali 829
			fofoStore = fofoStoreRepository.selectByRetailerId(retailerId);
23955 govind 830
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
831
 
23378 ashik.ali 832
		}
23955 govind 833
 
834
		if (fofoStore != null) {
835
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
836
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
26209 tejbeer 837
			fofoStore.setWarehouseId(updateRetailerRequest.getWarehouseId());
28825 tejbeer 838
			fofoStore.setFofoType(updateRetailerRequest.getFofoType());
23955 govind 839
 
840
		} else {
23378 ashik.ali 841
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId);
842
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
23955 govind 843
 
23378 ashik.ali 844
			StateInfo stateInfo = null;
845
			try {
846
				stateInfo = Utils.getStateInfo(retailerAddress.getState());
847
			} catch (Exception e) {
848
				// TODO Auto-generated catch block
849
				e.printStackTrace();
23955 govind 850
				// throw new ProfitMandiBusinessException();
23378 ashik.ali 851
			}
23955 govind 852
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName,
853
					stateInfo.getShortName());
854
 
23378 ashik.ali 855
			fofoStore = new FofoStore();
856
			fofoStore.setId(retailerId);
24023 amit.gupta 857
			fofoStore.setCounterSize(updateRetailerRequest.getCountersize());
858
			fofoStore.setMinimumInvestment(updateRetailerRequest.getMinInvestment());
26209 tejbeer 859
			fofoStore.setWarehouseId(updateRetailerRequest.getWarehouseId());
23786 amit.gupta 860
			String latestStoreCode = fofoStoreRepository.selectLatestStore().getCode();
861
			int latestCodeCounter = Integer.parseInt(latestStoreCode.replaceAll("[A-Z]", ""));
23955 govind 862
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(
863
					districtMaster.getStateShortName() + districtMaster.getShortName(), latestCodeCounter + 1);
23378 ashik.ali 864
			fofoStore.setCode(fofoStoreCode);
28189 tejbeer 865
			fofoStore.setBagsLastCredited(LocalDateTime.now());
28825 tejbeer 866
			fofoStore.setFofoType(updateRetailerRequest.getFofoType());
23378 ashik.ali 867
			fofoStoreRepository.persist(fofoStore);
28908 tejbeer 868
 
869
			// Auto fill in onboarding Panel
870
			PartnerOnBoardingPanel pobp = partnerOnBoardingPanelRepository
871
					.selectByPhoneNumber(Long.parseLong(retailerAddress.getPhoneNumber()));
872
			if (pobp != null) {
873
 
29042 tejbeer 874
				PartnerVerificationpanel partnerVerificationpanel = partnerOnboardingVerificationRepository
875
						.selectByOnboardingId(pobp.getId());
876
 
877
				if (partnerVerificationpanel != null) {
878
					if (partnerVerificationpanel.getApproval().equals(PartnerVerificationApprovalStatus.YES)) {
879
						pobp.setCode(fofoStore.getCode());
880
 
881
						storeTimelineTatService.onCodeCreated(pobp.getId());
882
 
883
					}
884
 
885
				}
886
 
28908 tejbeer 887
			}
23378 ashik.ali 888
		}
23955 govind 889
 
23378 ashik.ali 890
		return fofoStore;
891
	}
23955 govind 892
 
23329 ashik.ali 893
	@Override
23955 govind 894
	public FofoStore createFofoStoreCodeByUserId(int userId, String districtName, String stateName,
895
			UpdateRetailerRequest updateRetailerRequest) throws ProfitMandiBusinessException {
23329 ashik.ali 896
		User user = userRepository.selectById(userId);
897
		// = userAccountRepository.selectRetailerIdByUserId(user.getId());
898
		UserAccount userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
899
		Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
23781 ashik.ali 900
		Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
23955 govind 901
		try {
23781 ashik.ali 902
			userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleFofo.getId());
23955 govind 903
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23329 ashik.ali 904
			throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
905
		}
23955 govind 906
		return this.createFofoStoreCodeByRetailerId(retailer.getId(), districtName, stateName, updateRetailerRequest);
907
 
23329 ashik.ali 908
	}
23955 govind 909
 
23329 ashik.ali 910
	@Override
911
	public List<DistrictMaster> getAllDistrictMaster(String stateName) {
912
		StateInfo stateInfo = null;
913
		try {
914
			stateInfo = Utils.getStateInfo(stateName);
915
		} catch (Exception e) {
916
			e.printStackTrace();
23955 govind 917
			// throw new ProfitMandiBusinessException();
23329 ashik.ali 918
		}
919
		return districtMasterRepository.selectByStateShortName(stateInfo.getShortName());
920
	}
22980 ashik.ali 921
 
23509 amit.gupta 922
	@Override
923
	public Map<Integer, CustomRetailer> getFofoRetailers(List<Integer> fofoIds) {
924
		List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
25295 amit.gupta 925
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream()
926
				.filter(x -> x.getAddressId() != null).collect(Collectors.toMap(x -> x.getAddressId(), x -> x));
23903 amit.gupta 927
		List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
23509 amit.gupta 928
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
25295 amit.gupta 929
		for (Address address : addresses) {
23903 amit.gupta 930
			com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
23509 amit.gupta 931
			CustomRetailer customRetailer = new CustomRetailer();
23903 amit.gupta 932
			customRetailer.setEmail(user.getEmailId());
23509 amit.gupta 933
			customRetailer.setBusinessName(address.getName());
934
			customRetailer.setMobileNumber(address.getPhoneNumber());
23532 amit.gupta 935
			try {
26151 amit.gupta 936
				FofoStore fs = fofoStoreRepository.selectByRetailerId(address.getRetaierId());
937
				customRetailer.setCode(fs.getCode());
938
				customRetailer.setWarehouseId(fs.getWarehouseId());
26977 amit.gupta 939
				customRetailer.setPartnerId(fs.getId());
26125 amit.gupta 940
			} catch (Exception e) {
941
				continue;
942
			}
943
			try {
23796 amit.gupta 944
				customRetailer.setCartId(user.getActiveCartId());
23532 amit.gupta 945
				PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
946
				Counter counter = counterRepository.selectById(pdu.getCounterId());
947
				customRetailer.setGstNumber(counter.getGstin());
23955 govind 948
			} catch (Exception e) {
23532 amit.gupta 949
				customRetailer.setGstNumber(null);
950
			}
951
			CustomAddress address1 = new CustomAddress();
952
			address1.setCity(address.getCity());
953
			address1.setState(address.getState());
954
			address1.setLine1(address.getLine1());
955
			address1.setLine2(address.getLine2());
956
			address1.setPinCode(address.getPinCode());
957
			address1.setName(address.getName());
958
			customRetailer.setAddress(address1);
26522 amit.gupta 959
			customRetailer.setDisplayName(
960
					customRetailer.getBusinessName() + "-" + customRetailer.getCode() + "- " + address1.getCity());
26977 amit.gupta 961
			customRetailersMap.put(customRetailer.getPartnerId(), customRetailer);
23509 amit.gupta 962
		}
963
		return customRetailersMap;
964
	}
25295 amit.gupta 965
 
23781 ashik.ali 966
	@Override
25295 amit.gupta 967
	public CustomRetailer getFofoRetailer(int fofoId) throws ProfitMandiBusinessException {
23957 tejbeer 968
		com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(fofoId);
26089 amit.gupta 969
		FofoStore store = fofoStoreRepository.selectByRetailerId(fofoId);
23957 tejbeer 970
		Address address = null;
971
		try {
972
			address = addressRepository.selectById(saholicUser.getAddressId());
973
		} catch (ProfitMandiBusinessException e1) {
974
			// TODO Auto-generated catch block
975
			e1.printStackTrace();
976
		}
977
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
25295 amit.gupta 978
		CustomRetailer customRetailer = new CustomRetailer();
979
		customRetailer.setEmail(saholicUser.getEmailId());
980
		customRetailer.setBusinessName(address.getName());
981
		customRetailer.setMobileNumber(address.getPhoneNumber());
982
		try {
983
			customRetailer.setCartId(saholicUser.getActiveCartId());
984
			PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
985
			Counter counter = counterRepository.selectById(pdu.getCounterId());
986
			customRetailer.setGstNumber(counter.getGstin());
987
		} catch (Exception e) {
988
			customRetailer.setGstNumber(null);
989
		}
990
		CustomAddress address1 = new CustomAddress();
991
		address1.setCity(address.getCity());
992
		address1.setState(address.getState());
993
		address1.setLine1(address.getLine1());
994
		address1.setLine2(address.getLine2());
995
		address1.setPinCode(address.getPinCode());
26653 amit.gupta 996
		address1.setPhoneNumber(address.getPhoneNumber());
25295 amit.gupta 997
		address1.setName(address.getName());
26125 amit.gupta 998
		customRetailer.setDisplayName(address.getName() + " - " + address.getCity());
25295 amit.gupta 999
		customRetailer.setAddress(address1);
1000
		customRetailer.setPartnerId(address.getRetaierId());
26089 amit.gupta 1001
		customRetailer.setCode(store.getCode());
26112 amit.gupta 1002
		customRetailer.setWarehouseId(store.getWarehouseId());
25295 amit.gupta 1003
		customRetailersMap.put(address.getRetaierId(), customRetailer);
23957 tejbeer 1004
		return customRetailer;
25295 amit.gupta 1005
 
23957 tejbeer 1006
	}
26125 amit.gupta 1007
 
25383 tejbeer 1008
	@Override
27877 amit.gupta 1009
	public Map<Integer, CustomRetailer> getFofoRetailerUserId(List<Integer> fofoIds) {
25383 tejbeer 1010
		List<com.spice.profitmandi.dao.entity.user.User> saholicUsers = userUserRepository.selectByIds(fofoIds);
1011
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userAddressMap = saholicUsers.stream()
1012
				.filter(x -> x.getAddressId() != null).collect(Collectors.toMap(x -> x.getAddressId(), x -> x));
1013
		List<Address> addresses = addressRepository.selectByIds(new ArrayList<>(userAddressMap.keySet()));
1014
		Map<Integer, CustomRetailer> customRetailersMap = new HashMap<>();
1015
		for (Address address : addresses) {
1016
			com.spice.profitmandi.dao.entity.user.User user = userAddressMap.get(address.getId());
1017
			CustomRetailer customRetailer = new CustomRetailer();
1018
			customRetailer.setEmail(user.getEmailId());
1019
			customRetailer.setBusinessName(address.getName());
1020
			customRetailer.setMobileNumber(address.getPhoneNumber());
27877 amit.gupta 1021
			int userId = 0;
25383 tejbeer 1022
			try {
27877 amit.gupta 1023
				userId = userAccountRepository.selectUserIdByRetailerId(user.getId());
28024 tejbeer 1024
			} catch (ProfitMandiBusinessException e) {
27877 amit.gupta 1025
				continue;
1026
			}
1027
			try {
25383 tejbeer 1028
				customRetailer.setCartId(user.getActiveCartId());
1029
				PrivateDealUser pdu = privateDealUserRepository.selectById(address.getRetaierId());
1030
				Counter counter = counterRepository.selectById(pdu.getCounterId());
1031
				customRetailer.setGstNumber(counter.getGstin());
1032
			} catch (Exception e) {
1033
				customRetailer.setGstNumber(null);
1034
			}
1035
			CustomAddress address1 = new CustomAddress();
1036
			address1.setCity(address.getCity());
1037
			address1.setState(address.getState());
1038
			address1.setLine1(address.getLine1());
1039
			address1.setLine2(address.getLine2());
1040
			address1.setPinCode(address.getPinCode());
1041
			address1.setName(address.getName());
1042
			customRetailer.setAddress(address1);
1043
			customRetailer.setPartnerId(address.getRetaierId());
1044
			customRetailer.setDisplayName(customRetailer.getBusinessName() + "-" + address1.getCity());
1045
			customRetailersMap.put(userId, customRetailer);
1046
		}
1047
		return customRetailersMap;
1048
	}
25295 amit.gupta 1049
 
23957 tejbeer 1050
	@Override
23781 ashik.ali 1051
	public Map<Integer, String> getAllFofoRetailerIdEmailIdMap() {
1052
		Role roleFofo = null;
1053
		try {
1054
			roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
1055
		} catch (ProfitMandiBusinessException e) {
1056
			// TODO Auto-generated catch block
1057
			e.printStackTrace();
1058
		}
1059
		Role roleRetailer = null;
1060
		try {
1061
			roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
1062
		} catch (ProfitMandiBusinessException e) {
1063
			// TODO Auto-generated catch block
1064
			e.printStackTrace();
1065
		}
1066
		Set<Integer> roleIds = new HashSet<>();
1067
		roleIds.add(roleFofo.getId());
1068
		roleIds.add(roleRetailer.getId());
1069
		List<Integer> userIds = userRoleRepository.selectUserIdsByRoleIds(roleIds);
1070
		List<User> users = userRepository.selectAllByIds(new HashSet<>(userIds));
1071
		Map<Integer, Integer> userIdRetailerIdMap = this.getUserIdRetailerIdMap(userIds);
1072
		Map<Integer, String> retailerIdEmailIdMap = new HashMap<>();
23955 govind 1073
		for (User user : users) {
23781 ashik.ali 1074
			retailerIdEmailIdMap.put(userIdRetailerIdMap.get(user.getId()), user.getEmailId());
1075
		}
1076
		return retailerIdEmailIdMap;
1077
	}
23955 govind 1078
 
1079
	private Map<Integer, Integer> getUserIdRetailerIdMap(List<Integer> userIds) {
23781 ashik.ali 1080
		List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userIds));
1081
		Map<Integer, Integer> userIdRetailerIdMap = new HashMap<>();
23955 govind 1082
		for (UserAccount userAccount : userAccounts) {
23781 ashik.ali 1083
			userIdRetailerIdMap.put(userAccount.getUserId(), userAccount.getAccountKey());
1084
		}
1085
		return userIdRetailerIdMap;
1086
	}
25295 amit.gupta 1087
 
1088
	private Map<Integer, String> getUserIdEmailIdMap(Set<Integer> userIds) {
23880 ashik.ali 1089
		List<User> users = userRepository.selectAllByIds(userIds);
1090
		Map<Integer, String> userIdEmailIdMap = new HashMap<>();
23955 govind 1091
		for (User user : users) {
23880 ashik.ali 1092
			userIdEmailIdMap.put(user.getId(), user.getEmailId());
1093
		}
1094
		return userIdEmailIdMap;
1095
	}
23509 amit.gupta 1096
 
23880 ashik.ali 1097
	@Override
1098
	public Map<Integer, String> getAllFofoRetailerIdEmailIdMap(Set<Integer> retailerIds) {
1099
		List<UserAccount> userAccounts = userAccountRepository.selectAllSaholicByRetailerIds(retailerIds);
1100
		Set<Integer> userIds = new HashSet<>();
23955 govind 1101
		for (UserAccount userAccount : userAccounts) {
23880 ashik.ali 1102
			userIds.add(userAccount.getUserId());
1103
		}
1104
		Map<Integer, String> retailerIdEmailIdMap = new HashMap<>();
1105
		Map<Integer, String> userIdEmailIdMap = this.getUserIdEmailIdMap(userIds);
23955 govind 1106
		for (UserAccount userAccount : userAccounts) {
23880 ashik.ali 1107
			retailerIdEmailIdMap.put(userAccount.getAccountKey(), userIdEmailIdMap.get(userAccount.getUserId()));
1108
		}
1109
		return retailerIdEmailIdMap;
1110
	}
25295 amit.gupta 1111
 
24168 amit.gupta 1112
	@Override
24349 amit.gupta 1113
	public Map<Integer, String> getAllFofoRetailerIdNameMap(List<Integer> storeIds) {
24168 amit.gupta 1114
		Map<Integer, CustomRetailer> retailersMap = this.getFofoRetailers(storeIds);
1115
		Map<Integer, String> retailerIdNameMap = new HashMap<>();
25295 amit.gupta 1116
		for (Map.Entry<Integer, CustomRetailer> entry : retailersMap.entrySet()) {
1117
			retailerIdNameMap.put(entry.getKey(),
1118
					entry.getValue().getBusinessName() + "-" + entry.getValue().getAddress().getCity());
24168 amit.gupta 1119
		}
1120
		return retailerIdNameMap;
1121
	}
1122
 
1123
	@Override
28330 amit.gupta 1124
	@Cacheable(value = "FofoRetailerIdNameMap", cacheManager = "oneDayCacheManager")
24168 amit.gupta 1125
	public Map<Integer, String> getAllFofoRetailerIdNameMap() {
24349 amit.gupta 1126
		List<FofoStore> stores = fofoStoreRepository.selectAll();
25295 amit.gupta 1127
		List<Integer> storeIds = stores.stream().map(x -> x.getId()).collect(Collectors.toList());
24349 amit.gupta 1128
		return this.getAllFofoRetailerIdNameMap(storeIds);
1129
	}
1130
 
1131
	@Override
1132
	public List<MapWrapper<Integer, String>> getAllFofoRetailerIdNameList() {
1133
		List<MapWrapper<Integer, String>> mapWrappers = new ArrayList<>();
24168 amit.gupta 1134
		// TODO Auto-generated method stub
24349 amit.gupta 1135
		Map<Integer, String> fofoIdNameMap = this.getAllFofoRetailerIdNameMap();
25295 amit.gupta 1136
		fofoIdNameMap.forEach((fofoId, name) -> {
24349 amit.gupta 1137
			MapWrapper<Integer, String> idWrapper = new MapWrapper<>();
1138
			idWrapper.setKey(fofoId);
1139
			idWrapper.setValue(name);
1140
			mapWrappers.add(idWrapper);
1141
		});
1142
		return mapWrappers;
1143
	}
1144
 
1145
	@Override
27124 amit.gupta 1146
	@Cacheable(value = "retailerNames", cacheManager = "oneDayCacheManager")
26962 amit.gupta 1147
	public Map<Integer, CustomRetailer> getFofoRetailers(boolean activeOnly) {
24349 amit.gupta 1148
		// TODO Auto-generated method stub
26962 amit.gupta 1149
		Stream<FofoStore> storeStream = fofoStoreRepository.selectAll().stream();
27861 tejbeer 1150
		if (activeOnly) {
1151
			storeStream = storeStream.filter(x -> x.isActive());
26962 amit.gupta 1152
		}
1153
		List<Integer> storeIds = storeStream.map(x -> x.getId()).collect(Collectors.toList());
24349 amit.gupta 1154
		return this.getFofoRetailers(storeIds);
24168 amit.gupta 1155
	}
25295 amit.gupta 1156
 
26522 amit.gupta 1157
	@Override
27861 tejbeer 1158
	@Cacheable(value = "retailerNames", cacheManager = "oneDayCacheManager")
1159
	public Map<Integer, CustomRetailer> getAllFofoRetailers() {
1160
		// TODO Auto-generated method stub
1161
		Stream<FofoStore> storeStream = fofoStoreRepository.selectAll().stream();
1162
 
1163
		List<Integer> storeIds = storeStream.map(x -> x.getId()).collect(Collectors.toList());
1164
		return this.getFofoRetailers(storeIds);
1165
	}
1166
 
1167
	@Override
26522 amit.gupta 1168
	public LoginRequestResponseModel registerWebUser(LoginRequestResponseModel loginRequestModel)
1169
			throws ProfitMandiBusinessException {
1170
 
1171
		com.spice.profitmandi.dao.entity.user.User saholicUser = this.createSaholicUser(loginRequestModel);
1172
		loginRequestModel.setUserId(saholicUser.getId());
1173
 
1174
		this.createRetailerAddress(loginRequestModel);
1175
 
1176
		this.createPrivateDealUser(loginRequestModel);
1177
		this.updateSaholicUser(saholicUser.getId(), loginRequestModel.getBusinessAddress().getId());
1178
 
1179
		return loginRequestModel;
1180
 
1181
	}
1182
 
1183
	private void createPrivateDealUser(LoginRequestResponseModel loginRequestModel) {
1184
 
1185
		Integer counterId = this.createCounter(loginRequestModel.getEmail(), loginRequestModel.getGstNumber(),
1186
				loginRequestModel.getBusinessAddress().getPhoneNumber(),
1187
				loginRequestModel.getBusinessAddress().getName(), loginRequestModel.getBusinessAddress().getId());
1188
		try {
1189
			this.createPrivateDealUser(loginRequestModel.getUserId(), counterId, false);
1190
		} catch (Exception e) {
1191
			LOGGER.error("ERROR : ", e);
1192
		}
1193
 
1194
		PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
1195
		privateDealUserAddressMapping.setUserId(loginRequestModel.getUserId());
1196
		privateDealUserAddressMapping.setAddressId(loginRequestModel.getBusinessAddress().getId());
1197
		privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
1198
 
1199
	}
1200
 
1201
	private void createRetailerAddress(LoginRequestResponseModel loginRequestResponseModel)
1202
			throws ProfitMandiBusinessException {
1203
		Address businessAddress = loginRequestResponseModel.getBusinessAddress();
1204
		businessAddress.setRetaierId(loginRequestResponseModel.getUserId());
1205
		addressRepository.persist(businessAddress);
1206
	}
26536 amit.gupta 1207
 
26528 amit.gupta 1208
	private String getHash256(String originalString) {
26533 amit.gupta 1209
		String hashString = Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
1210
		LOGGER.info("Hash String {}", hashString);
1211
		return hashString;
26528 amit.gupta 1212
	}
26533 amit.gupta 1213
 
1214
	@Override
26536 amit.gupta 1215
	@Cacheable(value = "fofoIdUrl", cacheManager = "oneDayCacheManager")
26533 amit.gupta 1216
	public Map<Integer, String> getAllFofoRetailerIdUrlMap() {
26536 amit.gupta 1217
		Map<Integer, String> fofoRetailerUrlMap = new HashMap<>();
1218
		List<FofoStore> stores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive())
1219
				.collect(Collectors.toList());
1220
		Map<Integer, com.spice.profitmandi.dao.entity.user.User> userMap = userUserRepository
1221
				.selectByIds(stores.stream().map(x -> x.getId()).collect(Collectors.toList())).stream()
1222
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
1223
		for (FofoStore store : stores) {
26537 amit.gupta 1224
			LOGGER.info("Store is {}", store);
26544 amit.gupta 1225
			String districtShortName = store.getCode().replaceAll("\\d+", "").substring(2);
26593 amit.gupta 1226
			String stateShortName = store.getCode().replaceAll("\\d+", "").substring(0, 2);
26539 amit.gupta 1227
			DistrictMaster districtMaster = null;
26544 amit.gupta 1228
			LOGGER.info("Store shortname is {}", districtShortName);
26593 amit.gupta 1229
			districtMaster = districtMasterRepository.selectByShortnameNameAndStateShortName(districtShortName,
1230
					stateShortName);
26543 amit.gupta 1231
			if (districtMaster == null) {
26544 amit.gupta 1232
				districtMaster = districtMasterRepository.selectByShortnameNameAndStateShortName("FB", "HR");
26539 amit.gupta 1233
			}
26542 amit.gupta 1234
			LOGGER.info("Store id - {}", store.getId());
28825 tejbeer 1235
			// com.spice.profitmandi.dao.entity.user.User user = userMap.get(store.getId());
26536 amit.gupta 1236
			String urlString = districtMaster.getStateShortName() + "/"
27877 amit.gupta 1237
					+ Utils.getHyphenatedString(districtMaster.getName()) + "/" + store.getCode();
28346 amit.gupta 1238
			fofoRetailerUrlMap.put(store.getId(), urlString.toLowerCase());
26541 amit.gupta 1239
			LOGGER.info("Store is {} end", store);
26536 amit.gupta 1240
 
1241
		}
1242
		return fofoRetailerUrlMap;
26533 amit.gupta 1243
	}
1244
 
1245
	@Override
26590 amit.gupta 1246
	@Cacheable(value = "storeCodeRetailerMap", cacheManager = "oneDayCacheManager")
1247
	public Map<String, Integer> getStoreCodeRetailerMap() {
26533 amit.gupta 1248
		Map<Integer, String> map = this.getAllFofoRetailerIdUrlMap();
26596 amit.gupta 1249
		Map<String, Integer> returnMap = map.entrySet().stream().collect(Collectors.toMap(x -> {
26593 amit.gupta 1250
			String[] splitString = x.getValue().split("/");
27877 amit.gupta 1251
			return splitString[splitString.length - 1];
26593 amit.gupta 1252
		}, x -> x.getKey()));
26596 amit.gupta 1253
		LOGGER.info("returnMap {}", returnMap);
1254
		return returnMap;
26533 amit.gupta 1255
	}
27877 amit.gupta 1256
 
1257
	@Override
1258
	public String getPartnerCriteriaString(PartnerCriteria partnerCriteria) {
1259
 
1260
		StringBuilder sb = new StringBuilder();
1261
		if (partnerCriteria.getFofoIds().size() > 0) {
1262
			List<Integer> fofoIds = partnerCriteria.getFofoIds();
1263
			Map<Integer, CustomRetailer> customRetailers = this.getFofoRetailerUserId(fofoIds);
28024 tejbeer 1264
			List<String> businessNames = customRetailers.values().stream().map(x -> x.getBusinessName())
1265
					.collect(Collectors.toList());
27877 amit.gupta 1266
			sb.append(String.join(", ", businessNames));
1267
 
1268
		} else {
1269
			sb.append("All");
28024 tejbeer 1270
			if (partnerCriteria.getPartnerTypes().size() > 0) {
1271
				sb.append(" ").append(String.join(", ", partnerCriteria.getPartnerTypes().stream()
1272
						.map(x -> x.getValue()).collect(Collectors.toList())));
27877 amit.gupta 1273
			}
1274
			sb.append(" partners ");
28024 tejbeer 1275
			if (partnerCriteria.getRegionIds().size() > 0) {
27877 amit.gupta 1276
				sb.append("from ");
28024 tejbeer 1277
				sb.append(String.join(", ", partnerCriteria.getRegionIds().stream()
1278
						.map(x -> ProfitMandiConstants.WAREHOUSE_MAP.get(x)).collect(Collectors.toList())));
27877 amit.gupta 1279
			}
1280
		}
1281
		return sb.toString();
1282
	}
22980 ashik.ali 1283
}