Subversion Repositories SmartDukaan

Rev

Rev 23443 | Rev 23781 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22125 ashik.ali 1
package com.spice.profitmandi.dao.util;
2
 
3
import java.io.File;
22178 amit.gupta 4
import java.io.IOException;
22125 ashik.ali 5
import java.lang.reflect.Type;
22352 ashik.ali 6
import java.time.LocalDateTime;
22125 ashik.ali 7
import java.util.HashMap;
22352 ashik.ali 8
import java.util.HashSet;
22125 ashik.ali 9
import java.util.List;
10
import java.util.Map;
22352 ashik.ali 11
import java.util.Set;
22125 ashik.ali 12
 
22178 amit.gupta 13
import org.apache.commons.io.FileUtils;
23568 govind 14
import org.apache.logging.log4j.Logger;
15
import org.apache.logging.log4j.LogManager;
22125 ashik.ali 16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.stereotype.Component;
18
 
19
import com.google.gson.Gson;
20
import com.google.gson.reflect.TypeToken;
21
import com.spice.profitmandi.common.enumuration.ContentType;
22
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22476 ashik.ali 23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22125 ashik.ali 24
import com.spice.profitmandi.common.util.FileUtil;
25
import com.spice.profitmandi.dao.entity.dtr.Document;
22352 ashik.ali 26
import com.spice.profitmandi.dao.entity.dtr.Retailer;
27
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
23066 ashik.ali 28
import com.spice.profitmandi.dao.entity.dtr.Shop;
29
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
22352 ashik.ali 30
import com.spice.profitmandi.dao.entity.dtr.User;
23269 ashik.ali 31
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
22352 ashik.ali 32
import com.spice.profitmandi.dao.entity.dtr.UserRole;
23365 ashik.ali 33
import com.spice.profitmandi.dao.entity.fofo.FofoPartnerPaymentOption;
34
import com.spice.profitmandi.dao.entity.fofo.PaymentOption;
22352 ashik.ali 35
import com.spice.profitmandi.dao.entity.user.Address;
22471 ashik.ali 36
import com.spice.profitmandi.dao.entity.user.Cart;
37
import com.spice.profitmandi.dao.entity.user.Counter;
38
import com.spice.profitmandi.dao.entity.user.PrivateDealUser;
39
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressMapping;
22352 ashik.ali 40
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
41
import com.spice.profitmandi.dao.enumuration.dtr.RetailerType;
42
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
23365 ashik.ali 43
import com.spice.profitmandi.dao.enumuration.fofo.PaymentOptionType;
22125 ashik.ali 44
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
45
import com.spice.profitmandi.dao.repository.dtr.Mongo;
22352 ashik.ali 46
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
47
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
23066 ashik.ali 48
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
49
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
22352 ashik.ali 50
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
51
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
52
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
23365 ashik.ali 53
import com.spice.profitmandi.dao.repository.fofo.FofoPartnerPaymentOptionRepository;
54
import com.spice.profitmandi.dao.repository.fofo.PaymentOptionRepository;
22352 ashik.ali 55
import com.spice.profitmandi.dao.repository.user.AddressRepository;
22471 ashik.ali 56
import com.spice.profitmandi.dao.repository.user.CartRepository;
57
import com.spice.profitmandi.dao.repository.user.CounterRepository;
58
import com.spice.profitmandi.dao.repository.user.PrivateDealUserAddressMappingRepository;
59
import com.spice.profitmandi.dao.repository.user.PrivateDealUserRepository;
22125 ashik.ali 60
 
22471 ashik.ali 61
import in.shop2020.model.v1.user.CartStatus;
62
 
22125 ashik.ali 63
@Component
22352 ashik.ali 64
public class MigrationUtil {
22125 ashik.ali 65
 
66
	@Autowired
22868 ashik.ali 67
	private DocumentRepository documentRepository;
22125 ashik.ali 68
 
22352 ashik.ali 69
	@Autowired
22868 ashik.ali 70
	private UserRepository userRepository;
22352 ashik.ali 71
 
72
	@Autowired
22868 ashik.ali 73
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
22471 ashik.ali 74
 
75
	@Autowired
22868 ashik.ali 76
	private CartRepository cartRepository;
22471 ashik.ali 77
 
78
	@Autowired
22868 ashik.ali 79
	private UserAccountRepository userAccountRepository;
22352 ashik.ali 80
 
81
	@Autowired
22868 ashik.ali 82
	private RetailerRepository retailerRepository;
22352 ashik.ali 83
 
84
	@Autowired
22868 ashik.ali 85
	private UserRoleRepository userRoleRepository;
22352 ashik.ali 86
 
87
	@Autowired
22868 ashik.ali 88
	private AddressRepository addressRepository;
22352 ashik.ali 89
 
90
	@Autowired
22868 ashik.ali 91
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
22352 ashik.ali 92
 
22471 ashik.ali 93
	@Autowired
22868 ashik.ali 94
	private PrivateDealUserRepository privateDealUserRepository;
22471 ashik.ali 95
 
96
	@Autowired
22868 ashik.ali 97
	private PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
22471 ashik.ali 98
 
99
	@Autowired
22868 ashik.ali 100
	private CounterRepository counterRepository;
22471 ashik.ali 101
 
22162 amit.gupta 102
	@Autowired
23066 ashik.ali 103
	private ShopRepository shopRepository;
104
 
105
	@Autowired
106
	private ShopAddressRepository shopAddressRepository;
107
 
108
	@Autowired
23365 ashik.ali 109
	private FofoPartnerPaymentOptionRepository fofoPartnerPaymentOptionRepository;
110
 
111
	@Autowired
112
	private PaymentOptionRepository paymentOptionRepository;
113
 
114
	@Autowired
22868 ashik.ali 115
	private Mongo mongoClient;
22162 amit.gupta 116
 
117
 
23568 govind 118
	private static final Logger LOGGER = LogManager.getLogger(MigrationUtil.class);
22125 ashik.ali 119
 
22352 ashik.ali 120
	private List<Map<String, String>> getMongoFofoDoc(){
22162 amit.gupta 121
		String fofoFormsJsonString = mongoClient.getFofoFormsJsonString();
22471 ashik.ali 122
		LOGGER.info("mongoFofoDoc {}", fofoFormsJsonString);
22125 ashik.ali 123
		Gson gson = new Gson();
124
        Type paths = new TypeToken<List<Map<String, String>>>(){}.getType();
125
        List<Map<String, String>> maps = gson.fromJson(fofoFormsJsonString, paths);
22352 ashik.ali 126
        return maps;
127
	}
128
 
129
	public void migrateMongoDocToDocumentId() throws ProfitMandiBusinessException{
130
		List<Map<String, String>> maps = this.getMongoFofoDoc();
131
        Map<Integer, Map<String, Integer>> fofoIdPathMap = new HashMap<>();
22125 ashik.ali 132
 
133
        for(Map<String, String> map : maps){
134
        	int id = 0;
135
        	Map<String, Integer> pathMap = new HashMap<>();
136
        	for(Map.Entry<String, String> entry : map.entrySet()){
137
        		if(entry.getKey().equals("_id")){
22181 amit.gupta 138
        			id = (int)Double.parseDouble(entry.getValue());
22125 ashik.ali 139
        		}
140
        		if(entry.getValue().startsWith("/hsps-docs/")){
141
 
142
        			Document document = new Document();
143
 
144
        			try{
22178 amit.gupta 145
        				File srcFile = new File(entry.getValue());
146
        				String destFileName = entry.getValue().substring(entry.getValue().lastIndexOf('/') + 1);
147
        				String destFilePath = "/uploads/"; 
22182 amit.gupta 148
        				File destFile = new File(destFilePath + destFileName);
22178 amit.gupta 149
        				FileUtils.copyFile(srcFile, destFile);
150
        				ContentType contentType = FileUtil.detectFileType(srcFile);
22125 ashik.ali 151
        				document.setContentType(contentType);
22178 amit.gupta 152
        				document.setName(destFileName);
153
        				document.setPath(destFilePath);
22125 ashik.ali 154
        				document.setPersisted(true);
22178 amit.gupta 155
        				document.setSize(srcFile.length());
22125 ashik.ali 156
        				documentRepository.persist(document);
157
        				pathMap.put(entry.getKey(), document.getId());
22178 amit.gupta 158
        			}catch(IOException ioException) {
159
        				LOGGER.info("IOException occurred");
22125 ashik.ali 160
        			}
161
        		}
162
        	}
163
        	if(!pathMap.isEmpty()){
164
        		fofoIdPathMap.put(id, pathMap);
165
        	}
166
        }
167
 
168
        LOGGER.info("fofoIdPathMap {}", fofoIdPathMap);
169
        for(Map.Entry<Integer, Map<String, Integer>> entry : fofoIdPathMap.entrySet()){
22162 amit.gupta 170
        	mongoClient.updateColumnsById(entry.getValue(), entry.getKey());
22125 ashik.ali 171
        }
172
 
173
	}
22352 ashik.ali 174
 
23269 ashik.ali 175
	private Set<Integer> toRetailerIds(List<UserAccount> userAccounts){
22868 ashik.ali 176
		Set<Integer> retailerIds = new HashSet<>();
23269 ashik.ali 177
		for(UserAccount saholicUserAccount : userAccounts){
178
			int retailerId = saholicUserAccount.getAccountKey();
22868 ashik.ali 179
			retailerIds.add(retailerId);
180
		}
181
		return retailerIds;
182
	}
183
 
23269 ashik.ali 184
	private Set<Integer> toUserIds(List<UserAccount> userAccounts, Set<Integer> retailerIds){
22868 ashik.ali 185
		Set<Integer> userIds = new HashSet<>();
23269 ashik.ali 186
		for(UserAccount saholicUserAccount : userAccounts){
187
			int retailerId = saholicUserAccount.getAccountKey();
22868 ashik.ali 188
			if(retailerIds.contains(retailerId)){
23269 ashik.ali 189
				userIds.add(saholicUserAccount.getUserId());
22416 amit.gupta 190
			}
22868 ashik.ali 191
		}
192
		return userIds;
193
	}
194
 
195
	public void migrateUserToRetailer(){
196
 
23269 ashik.ali 197
		List<UserAccount> saholicUserAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userRepository.selectIdAll()));
22868 ashik.ali 198
		if(saholicUserAccounts.isEmpty()){
199
			return;
200
		}
201
		Set<Integer> retailerIds = this.toRetailerIds(saholicUserAccounts);
202
		List<Integer> foundRetailerIds = retailerRepository.selectIdsByIds(retailerIds);
203
		retailerIds.removeAll(foundRetailerIds);
204
		for(int retailerId : retailerIds){
22416 amit.gupta 205
			Retailer retailer = new Retailer();
22352 ashik.ali 206
			try {
22416 amit.gupta 207
				retailer.setId(retailerId);
208
				retailer.setMigrated(true);
22352 ashik.ali 209
				retailerRepository.persist(retailer);
22868 ashik.ali 210
				LOGGER.info("Retailer migrated {}", retailerId);
22352 ashik.ali 211
			} catch (ProfitMandiBusinessException e) {
212
				LOGGER.info("Error occured while commiting retailer");
213
				e.printStackTrace();
214
			}
22868 ashik.ali 215
		}
216
 
217
		Set<Integer> userIds = this.toUserIds(saholicUserAccounts, retailerIds);
218
 
22886 ashik.ali 219
		if(userIds.isEmpty()){
220
			return;
221
		}
22868 ashik.ali 222
		List<Integer> foundRetailerRoleUserIds = userRoleRepository.selectUserIdsByUserIdsAndRoleType(userIds, RoleType.RETAILER);
223
 
224
		Set<Integer> notFoundRetailerRoleUserIds = new HashSet<>(userIds);
225
		notFoundRetailerRoleUserIds.removeAll(foundRetailerRoleUserIds);
226
 
227
		for(int userId : notFoundRetailerRoleUserIds){
228
			UserRole retailerRole = new UserRole();
229
			retailerRole.setRoleType(RoleType.RETAILER);
230
			retailerRole.setUserId(userId);
231
			try {
232
				userRoleRepository.persist(retailerRole);
233
				LOGGER.info("UserId {} with Retailer Role migrated", userId);
234
			} catch (ProfitMandiBusinessException e) {
235
				e.printStackTrace();
22352 ashik.ali 236
			}
22868 ashik.ali 237
		}
238
 
239
		List<Integer> foundUserRoleUserIds = userRoleRepository.selectUserIdsByUserIdsAndRoleType(userIds, RoleType.USER);
240
 
241
		Set<Integer> notFoundUserRoleUserIds = new HashSet<>(userIds);
242
		notFoundUserRoleUserIds.removeAll(foundUserRoleUserIds);
243
 
244
		for(int userId : notFoundUserRoleUserIds){
245
			UserRole userRole = new UserRole();
246
			userRole.setRoleType(RoleType.USER);
247
			userRole.setUserId(userId);
248
			try {
249
				userRoleRepository.persist(userRole);
250
				LOGGER.info("UserId {} with User Role migrated", userId);
251
			} catch (ProfitMandiBusinessException e) {
252
				e.printStackTrace();
22352 ashik.ali 253
			}
254
		}
22868 ashik.ali 255
 
22352 ashik.ali 256
	}
257
 
258
	public void migrateMongoDocToRetailer() throws ProfitMandiBusinessException{
259
		List<Map<String, String>> maps = this.getMongoFofoDoc();
22471 ashik.ali 260
		//LOGGER.info("mongoFofoDocs {}", maps);
22352 ashik.ali 261
		for(Map<String, String> map : maps){
262
			if(map.containsKey("registeredEmail1")){
263
 
22476 ashik.ali 264
				User user = null;
265
 
266
				try{
267
					user = userRepository.selectByEmailId(map.get("registeredEmail1"));
268
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
23202 ashik.ali 269
				}
270
				if(user == null){
23329 ashik.ali 271
					user = userRepository.selectBySecondryEmailId(map.get("registeredEmail1"));
272
				}
273
				if(user == null){
22476 ashik.ali 274
					user = new User();
275
					user.setFirstName("");
276
					user.setLastName("");
277
					user.setCity(map.containsKey("city") ? map.get("city") : "");
278
					user.setPinCode(Integer.valueOf(map.containsKey("pincode") ? map.get("pincode") : ""));
279
					user.setMobileNumber(map.containsKey("mobile") ? map.get("mobile") : "");
280
					user.setEmailId(map.get("registeredEmail1"));
281
					user.setUsername(map.get("registeredEmail1"));
282
					user.setPassword("");
283
					user.setMobile_verified(false);
284
					user.setReferral_url("");
285
					user.setGroup_id(1);
22504 amit.gupta 286
					user.setStatus(1);
23300 amit.gupta 287
					user.setActivated(true);
22476 ashik.ali 288
					user.setCreateTimestamp(LocalDateTime.now());
289
					user.setUpdateTimestamp(LocalDateTime.now());
290
					userRepository.persist(user);
291
				}
292
 
293
 
22471 ashik.ali 294
				//in.shop2020.model.v1.user.User saholicUser = Utils.createSaholicUser(map.get("registeredEmail1"));
22352 ashik.ali 295
 
22471 ashik.ali 296
				com.spice.profitmandi.dao.entity.user.User saholicUser = null;
297
				boolean foundRetailer = false;
23067 ashik.ali 298
				try{
299
					saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
300
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
301
 
302
				}
22471 ashik.ali 303
				if(saholicUser == null){
304
					Cart cart = new Cart();
305
					cart.setCartStatus(CartStatus.ACTIVE);
306
					cartRepository.persist(cart);
307
					saholicUser = new com.spice.profitmandi.dao.entity.user.User();
308
					saholicUser.setEmailId(user.getEmailId());
309
					saholicUser.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
310
					saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 311
					saholicUser.setCreateTimestamp(LocalDateTime.now());
22471 ashik.ali 312
					userUserRepository.persist(saholicUser);
313
				}else{
314
					foundRetailer = true;
315
				}
316
 
22476 ashik.ali 317
				Retailer retailer = null;
22471 ashik.ali 318
 
22476 ashik.ali 319
				try{
320
					retailer = retailerRepository.selectById(saholicUser.getId());
321
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
322
					retailer = new Retailer();
323
				}
23443 amit.gupta 324
				retailer.setActive(true);
325
				retailer.setId(saholicUser.getId());
326
				retailer.setMigrated(true);
327
				retailer.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
328
				retailer.setType(RetailerType.GSTIN);
329
				retailerRepository.persist(retailer);
22352 ashik.ali 330
 
22476 ashik.ali 331
 
22352 ashik.ali 332
				Address retailerAddress = new Address();
333
 
22476 ashik.ali 334
				List<Address> retailerAddresses = addressRepository.selectAll(retailer.getId(), 1, 100);
335
				if(retailerAddresses.isEmpty()){
336
					retailerAddress.setCity(map.containsKey(ProfitMandiConstants.CITY) ? map.get(ProfitMandiConstants.CITY) : "");
337
					retailerAddress.setCountry("India");
338
					retailerAddress.setEnabled(true);
339
					retailerAddress.setLandmark("");
340
					retailerAddress.setLine1(map.containsKey(ProfitMandiConstants.LINE1) ? map.get(ProfitMandiConstants.LINE1) : "");
341
					retailerAddress.setLine2(map.containsKey(ProfitMandiConstants.LINE2) ? map.get(ProfitMandiConstants.LINE2) : "");
342
					retailerAddress.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
343
					retailerAddress.setPhoneNumber(map.containsKey("mobile") ? map.get("mobile") : "");
344
					retailerAddress.setPinCode(map.containsKey("pincode") ? map.get("pincode") : "");
345
					retailerAddress.setState(map.containsKey(ProfitMandiConstants.STATE) ? map.get(ProfitMandiConstants.STATE) : "");
346
					retailerAddress.setRetaierId(retailer.getId());
347
					addressRepository.persist(retailerAddress);
348
				}
22352 ashik.ali 349
 
350
 
22476 ashik.ali 351
				RetailerRegisteredAddress retailerRegisteredAddress = null;
352
				try{
353
					retailerRegisteredAddress = retailerRegisteredAddressRepository.selectByAddressIdAndRetailerId(retailerAddress.getId(), retailer.getId());
23075 ashik.ali 354
					//retailerRegisteredAddress = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
22476 ashik.ali 355
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
356
					retailerRegisteredAddress = new RetailerRegisteredAddress();
357
					retailerRegisteredAddress.setAddressId(retailerAddress.getId());
358
					retailerRegisteredAddress.setRetailerId(retailer.getId());
359
					retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
360
				}
22471 ashik.ali 361
 
22476 ashik.ali 362
 
23066 ashik.ali 363
				List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
364
				if(shops.isEmpty()){
365
					Shop shop = new Shop();
366
					shop.setName(retailer.getName());
367
					shop.setAddressId(retailerAddress.getId());
368
					shop.setDocumentId(retailer.getDocumentId());
369
					shop.setRetailerId(retailer.getId());
370
					shopRepository.persist(shop);
371
					ShopAddress shopAddress = new ShopAddress();
372
					shopAddress.setAddressId(retailerAddress.getId());
373
					shopAddress.setShopId(shop.getId());
374
					shopAddressRepository.persist(shopAddress);
375
				}
376
 
23269 ashik.ali 377
				UserAccount saholicUserAccounts = null;
22476 ashik.ali 378
				try{
379
					saholicUserAccounts = userAccountRepository.selectByUserIdRetailerIdAccountType(user.getId(), retailer.getId(), AccountType.saholic);
380
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
23269 ashik.ali 381
					saholicUserAccounts = new UserAccount();
382
					saholicUserAccounts.setAccountKey(saholicUser.getId());
383
					saholicUserAccounts.setType(AccountType.saholic);
384
					saholicUserAccounts.setUserId(user.getId());
22476 ashik.ali 385
					userAccountRepository.persist(saholicUserAccounts);
23269 ashik.ali 386
					UserAccount cartUserAccounts = new UserAccount();
387
					cartUserAccounts.setAccountKey(saholicUser.getActiveCartId());
388
					cartUserAccounts.setType(AccountType.cartId);
389
					cartUserAccounts.setUserId(user.getId());
22476 ashik.ali 390
					userAccountRepository.persist(cartUserAccounts);
391
				}
392
 
393
 
394
				UserRole userRole = null;
395
				try{
396
					userRole = userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.USER);
397
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
398
					userRole = new UserRole();
399
					userRole.setRoleType(RoleType.USER);
400
					userRole.setUserId(user.getId());
401
					userRoleRepository.persist(userRole);
402
				}
403
 
404
				UserRole retailerRole = null;
405
				try{
406
					retailerRole = userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.RETAILER);
407
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
408
					retailerRole = new UserRole();
409
					retailerRole.setRoleType(RoleType.RETAILER);
410
					retailerRole.setUserId(user.getId());
411
					userRoleRepository.persist(retailerRole);
412
				}
413
 
414
				UserRole fofoRole = null;
415
				try{
416
					fofoRole = userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.FOFO);
417
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
418
					fofoRole = new UserRole();
419
					fofoRole.setRoleType(RoleType.FOFO);
420
					fofoRole.setUserId(user.getId());
421
					userRoleRepository.persist(fofoRole);
422
				}
423
 
23365 ashik.ali 424
				List<Integer> paymentOptionIds = fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(retailer.getId());
425
				if(paymentOptionIds.isEmpty()){
426
					PaymentOption paymentOption = null;
427
					try{
428
						paymentOption = paymentOptionRepository.selectByName(PaymentOptionType.CASH.toString());
429
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
430
						paymentOption = new PaymentOption();
431
						paymentOption.setName(PaymentOptionType.CASH.toString());
432
						paymentOptionRepository.persist(paymentOption);
433
					}
434
					FofoPartnerPaymentOption fofoPartnerPaymentOption = new FofoPartnerPaymentOption();
435
					fofoPartnerPaymentOption.setFofoId(retailer.getId());
436
					fofoPartnerPaymentOption.setPaymentOptionId(paymentOption.getId());
437
					fofoPartnerPaymentOptionRepository.persist(fofoPartnerPaymentOption);
438
				}
439
 
22471 ashik.ali 440
				if(foundRetailer){
441
					LOGGER.info("\n\n\n****retailer found\n\n\n");
22608 ashik.ali 442
					PrivateDealUser privateDealUser = null;
443
					try{
444
						privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
445
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
446
						LOGGER.error("PrivateDealUser not found : ", profitMandiBusinessException);
447
					}
23066 ashik.ali 448
 
22608 ashik.ali 449
					 //= privateDealUserRepository.selectById(saholicUser.getId());
450
					if(privateDealUser == null){
22471 ashik.ali 451
						Integer counterId = this.createCounter(user.getEmailId(), map.get("gst"), user.getMobileNumber(), retailer.getName(), retailerAddress.getId());
22608 ashik.ali 452
						this.createPrivateDealUser(saholicUser.getId(), counterId);
453
					}else{
454
						if(privateDealUser.getCounterId() == null){
455
							Integer counterId = this.createCounter(user.getEmailId(), map.get("gst"), user.getMobileNumber(), retailer.getName(), retailerAddress.getId());
456
							privateDealUser.setCounterId(counterId);
23066 ashik.ali 457
							privateDealUser.setFofo(true);
22608 ashik.ali 458
							privateDealUserRepository.persist(privateDealUser);
459
						}
22471 ashik.ali 460
					}
461
				}else{
462
					LOGGER.info("retailer not found");
463
					//gst number intergration with counter
464
					Integer counterId = this.createCounter(user.getEmailId(), map.get("gst"), user.getMobileNumber(), retailer.getName(), retailerAddress.getId());
22608 ashik.ali 465
					PrivateDealUser privateDealUser = null;
466
					try{
467
						privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
468
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
469
						LOGGER.error("PrivateDealUser not found : ", profitMandiBusinessException);
470
					}
471
					/* //= privateDealUserRepository.selectById(saholicUser.getId());
472
					if(privateDealUser == null){
473
						int counterId = this.createCounter(user.getEmailId(), map.get("gst"), user.getMobileNumber(), retailer.getName(), retailerAddress.getId());
474
						this.createPrivateDealUser(saholicUser.getId(), counterId);
475
					}*/
22471 ashik.ali 476
 
22608 ashik.ali 477
 
22471 ashik.ali 478
					if(privateDealUser != null){
479
						//LOGGER.info("PrivateDealUser found with id [{}]", saholicUser.getId());
480
						privateDealUser.setCounterId(counterId);
23066 ashik.ali 481
						privateDealUser.setFofo(true);
22471 ashik.ali 482
						privateDealUserRepository.update(privateDealUser);
483
					}else{	
484
						LOGGER.info("PrivateDealUser not found with id [{}]", saholicUser.getId());
22608 ashik.ali 485
						this.createPrivateDealUser(saholicUser.getId(), counterId);
22471 ashik.ali 486
					}
487
					PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
23329 ashik.ali 488
					privateDealUserAddressMapping.setUserId(retailer.getId());
489
					privateDealUserAddressMapping.setAddressId(retailerAddress.getId());
22471 ashik.ali 490
					privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
491
 
492
					saholicUser.setAddressId(retailerAddress.getId());
493
					userUserRepository.persist(saholicUser);
494
				}
22352 ashik.ali 495
			}
22471 ashik.ali 496
 
22352 ashik.ali 497
		}
22471 ashik.ali 498
 
499
 
22352 ashik.ali 500
	}
501
 
22716 amit.gupta 502
	//Specifically set isFofo to true that has to be used by old system
22608 ashik.ali 503
	private void createPrivateDealUser(int retailerId, int counterId){
504
		PrivateDealUser privateDealUser = new PrivateDealUser();
505
		privateDealUser.setActive(true);
23443 amit.gupta 506
		privateDealUser.setBulkShipmentAmountLimit(100000);
22608 ashik.ali 507
		privateDealUser.setId(retailerId);
508
		privateDealUser.setCounterId(counterId);
23066 ashik.ali 509
		privateDealUser.setFofo(true);
22608 ashik.ali 510
		privateDealUserRepository.persist(privateDealUser);
511
	}
512
 
22471 ashik.ali 513
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
514
		if(gstNumber != null && !gstNumber.isEmpty()){
515
			Counter counter = new Counter();
516
			counter.setEmailId(emailId);
517
			counter.setGstin(gstNumber);
518
			counter.setMobileNumber(mobileNumber);
519
			counter.setName(name);
520
			counter.setAddressId(addressId);
23300 amit.gupta 521
			counter.setCreatedOn(LocalDateTime.now());
22471 ashik.ali 522
			counterRepository.persist(counter);
523
			return counter.getId();
524
		}else{
525
			return null;
526
		}
527
	}
528
 
22352 ashik.ali 529
	public void migratePrivateDealToRetailer(){
530
 
531
	}
22125 ashik.ali 532
}