Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21292 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
22495 amit.gupta 3
import java.util.Date;
21448 ashik.ali 4
import java.util.Set;
5
 
21292 ashik.ali 6
import javax.servlet.http.HttpServletRequest;
7
 
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
22355 ashik.ali 11
import org.springframework.beans.factory.annotation.Qualifier;
21292 ashik.ali 12
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Controller;
21692 amit.gupta 14
import org.springframework.transaction.annotation.Transactional;
21309 ashik.ali 15
import org.springframework.web.bind.annotation.RequestBody;
21292 ashik.ali 16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestMethod;
18
import org.springframework.web.bind.annotation.RequestParam;
19
 
20
import com.spice.profitmandi.common.ResponseCodeHolder;
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21463 ashik.ali 23
import com.spice.profitmandi.common.model.UserInfo;
22473 ashik.ali 24
import com.spice.profitmandi.common.util.StringUtils;
25
import com.spice.profitmandi.common.util.Utils;
21740 ashik.ali 26
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 27
import com.spice.profitmandi.dao.entity.dtr.Brand;
22473 ashik.ali 28
import com.spice.profitmandi.dao.entity.dtr.DistrictMaster;
21735 ashik.ali 29
import com.spice.profitmandi.dao.entity.dtr.Document;
30
import com.spice.profitmandi.dao.entity.dtr.Retailer;
31
import com.spice.profitmandi.dao.entity.dtr.RetailerBrand;
32
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
33
import com.spice.profitmandi.dao.entity.dtr.Shop;
34
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
35
import com.spice.profitmandi.dao.entity.dtr.User;
21768 amit.gupta 36
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
37
import com.spice.profitmandi.dao.entity.dtr.UserRole;
22656 ashik.ali 38
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
39
import com.spice.profitmandi.dao.entity.fofo.FofoStoreSequenceGeneration;
21735 ashik.ali 40
import com.spice.profitmandi.dao.entity.user.Address;
22355 ashik.ali 41
import com.spice.profitmandi.dao.entity.user.Cart;
42
import com.spice.profitmandi.dao.entity.user.Counter;
43
import com.spice.profitmandi.dao.entity.user.PrivateDealUser;
44
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressId;
45
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressMapping;
21768 amit.gupta 46
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
47
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
22495 amit.gupta 48
import com.spice.profitmandi.dao.model.RetailerFofoInterest;
21735 ashik.ali 49
import com.spice.profitmandi.dao.repository.dtr.BrandRepository;
22473 ashik.ali 50
import com.spice.profitmandi.dao.repository.dtr.DistrictMasterRepository;
21735 ashik.ali 51
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
22473 ashik.ali 52
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
53
import com.spice.profitmandi.dao.repository.dtr.FofoStoreSequenceGenerationRepository;
22495 amit.gupta 54
import com.spice.profitmandi.dao.repository.dtr.Mongo;
21735 ashik.ali 55
import com.spice.profitmandi.dao.repository.dtr.RetailerAddressRepository;
56
import com.spice.profitmandi.dao.repository.dtr.RetailerBrandRepository;
57
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
58
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
59
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
60
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
21768 amit.gupta 61
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
21735 ashik.ali 62
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
21768 amit.gupta 63
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
21735 ashik.ali 64
import com.spice.profitmandi.dao.repository.user.AddressRepository;
22355 ashik.ali 65
import com.spice.profitmandi.dao.repository.user.CartRepository;
66
import com.spice.profitmandi.dao.repository.user.CounterRepository;
67
import com.spice.profitmandi.dao.repository.user.PrivateDealUserAddressMappingRepository;
68
import com.spice.profitmandi.dao.repository.user.PrivateDealUserRepository;
21431 ashik.ali 69
import com.spice.profitmandi.web.req.Category;
21426 ashik.ali 70
import com.spice.profitmandi.web.req.CreateRetailerAddressRequest;
71
import com.spice.profitmandi.web.req.CreateRetailerRequest;
72
import com.spice.profitmandi.web.req.RetailerAddBrandRequest;
21292 ashik.ali 73
 
22473 ashik.ali 74
import in.shop2020.model.v1.inventory.StateInfo;
22355 ashik.ali 75
import in.shop2020.model.v1.user.CartStatus;
21302 ashik.ali 76
import io.swagger.annotations.ApiImplicitParam;
77
import io.swagger.annotations.ApiImplicitParams;
78
import io.swagger.annotations.ApiOperation;
79
 
21292 ashik.ali 80
@Controller
22037 amit.gupta 81
@Transactional(rollbackFor=Throwable.class)
21292 ashik.ali 82
public class RetailerController {
83
 
84
	private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
85
 
86
	@Autowired
22355 ashik.ali 87
	@Qualifier("userRepository")
21463 ashik.ali 88
	UserRepository userRepository;
89
 
90
	@Autowired
22355 ashik.ali 91
	@Qualifier("userUserRepository")
92
	com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
93
 
94
	@Autowired
21768 amit.gupta 95
	UserRoleRepository userRoleRepository;
96
 
97
	@Autowired
21440 ashik.ali 98
	ResponseSender<?> responseSender;
99
 
100
	@Autowired
21292 ashik.ali 101
	RetailerRepository retailerRepository;
102
 
21390 ashik.ali 103
	@Autowired
104
	BrandRepository brandRepository;
105
 
106
	@Autowired
107
	AddressRepository addressRepository;
108
 
109
	@Autowired
110
	DocumentRepository documentRepository;
111
 
112
	@Autowired
113
	ShopRepository shopRepository;
114
 
115
	@Autowired
21768 amit.gupta 116
	UserAccountRepository userAccountRepository;
117
 
118
	@Autowired
21390 ashik.ali 119
	RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
120
 
121
	@Autowired
21426 ashik.ali 122
	RetailerAddressRepository retailerAddressRepository;
123
 
124
	@Autowired
21390 ashik.ali 125
	ShopAddressRepository shopAddressRepository;
126
 
127
	@Autowired
128
	RetailerBrandRepository retailerBrandRepository;
129
 
22355 ashik.ali 130
	@Autowired
131
	CounterRepository counterRepository;
132
 
133
	@Autowired
134
	PrivateDealUserRepository privateDealUserRepository;
135
 
136
	@Autowired
137
	PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
138
 
139
	@Autowired
140
	CartRepository cartRepository;
141
 
22473 ashik.ali 142
	@Autowired
143
	DistrictMasterRepository districtMasterRepository;
144
 
145
	@Autowired
146
	FofoStoreRepository fofoStoreRepository;
147
 
148
	@Autowired
22495 amit.gupta 149
	private Mongo mongoClient;
150
 
151
	@Autowired
22473 ashik.ali 152
	FofoStoreSequenceGenerationRepository fofoStoreSequenceGenerationRepository;
153
 
21302 ashik.ali 154
	@ApiImplicitParams({
155
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
156
				required = true, dataType = "string", paramType = "header")
157
	})
21309 ashik.ali 158
 
21302 ashik.ali 159
	@ApiOperation(value = "Create Retailer")
160
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
22473 ashik.ali 161
	public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequest createRetailerRequest) throws ProfitMandiBusinessException, Exception{
22355 ashik.ali 162
		LOGGER.info("request received with body [{}] at url {{}}: ", createRetailerRequest, request.getRequestURL().toString());
21440 ashik.ali 163
		try{
21691 amit.gupta 164
			UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
22355 ashik.ali 165
			LOGGER.info("requestAttribute [userInfo={}]", userInfo);
21463 ashik.ali 166
			User user = userRepository.selectById(userInfo.getUserId());
21768 amit.gupta 167
			this.createRetailer(user, createRetailerRequest);
21440 ashik.ali 168
			return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1000"));
169
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
170
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
22355 ashik.ali 171
			throw profitMandiBusinessException;
172
			//return responseSender.badRequest(profitMandiBusinessException);
21440 ashik.ali 173
		}
174
	}
175
 
21768 amit.gupta 176
	private void createRetailer(User user, CreateRetailerRequest createRetailerRequest) 
22473 ashik.ali 177
			throws ProfitMandiBusinessException, Exception{
21390 ashik.ali 178
		Retailer retailer = new Retailer();
21426 ashik.ali 179
		retailer.setName(createRetailerRequest.getName());
180
		retailer.setNumber(createRetailerRequest.getNumber());
181
		retailer.setType(createRetailerRequest.getType());
182
		retailer.setMonthlySaleValue(createRetailerRequest.getMonthlySaleValue());
22732 ashik.ali 183
		retailer.setMonthlySaleVolume(createRetailerRequest.getMonthlySaleVolume());
21426 ashik.ali 184
		retailer.setRecharge(createRetailerRequest.getLineOfBusiness().isRecharge());
185
		retailer.setMobile(createRetailerRequest.getLineOfBusiness().isMobile());
186
		retailer.setAccessories(createRetailerRequest.getLineOfBusiness().isAccessories());
21431 ashik.ali 187
		retailer.setOther1(createRetailerRequest.getLineOfBusiness().getOther1());
188
		retailer.setOther2(createRetailerRequest.getLineOfBusiness().getOther2());
21440 ashik.ali 189
		Document retailerDocument = documentRepository.selectById(createRetailerRequest.getDocumentId());
190
		if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
191
			LOGGER.error("documet is already mapped with another retailer");
21448 ashik.ali 192
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "DCMNT_1000");
21440 ashik.ali 193
		}
194
		retailer.setDocumentId(retailerDocument.getId());
195
		final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
196
		if(shopRepository.isExistByDocumentId(shopDocument.getId())){
21448 ashik.ali 197
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "DCMNT_1000");
21440 ashik.ali 198
		}
21463 ashik.ali 199
		documentRepository.markDocumentAsPersisted(retailerDocument.getId());
22743 amit.gupta 200
		//This validation is not required
201
		/*if(retailerRepository.isExistByNumberAndType(retailer.getNumber(), retailer.getType())){
21448 ashik.ali 202
			throw new ProfitMandiBusinessException(ProfitMandiConstants.NUMBER + ", " + ProfitMandiConstants.TYPE, retailer.getNumber() + ", " + retailer.getType(), "RTLR_1001");
22743 amit.gupta 203
		}*/
21463 ashik.ali 204
		documentRepository.markDocumentAsPersisted(shopDocument.getId());
22355 ashik.ali 205
 
206
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
207
		boolean foundRetailer = false;
208
		saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
209
		if(saholicUser == null){
210
			Cart cart = new Cart();
211
			cart.setCartStatus(CartStatus.ACTIVE);
212
			cartRepository.persist(cart);
213
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
214
			saholicUser.setEmailId(user.getEmailId());
215
			saholicUser.setName(createRetailerRequest.getName());
216
			saholicUser.setActiveCartId(cart.getId());
217
			userUserRepository.persist(saholicUser);
218
		}else{
219
			foundRetailer = true;
220
		}
221
		//in.shop2020.model.v1.user.User saholicUser = Utils.createSaholicUser(user.getEmailId()); 
222
		retailer.setId(saholicUser.getId());
21440 ashik.ali 223
		retailerRepository.persist(retailer);
22355 ashik.ali 224
 
21440 ashik.ali 225
		Shop shop = new Shop();
226
		shop.setName(createRetailerRequest.getShop().getName());
227
		shop.setDocumentId(shopDocument.getId());
228
		shop.setRetailerId(retailer.getId());
229
		shopRepository.persist(shop);
21448 ashik.ali 230
		this.addBrandWithRetailer(retailer.getId(), createRetailerRequest.getCategories());
21440 ashik.ali 231
		final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());
21706 amit.gupta 232
		addressRetailer.setRetaierId(retailer.getId());
22041 amit.gupta 233
		addressRepository.persist(addressRetailer);
22355 ashik.ali 234
 
21440 ashik.ali 235
		final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
236
		retailerRegisteredAddress.setRetailerId(retailer.getId());
237
		retailerRegisteredAddress.setAddressId(addressRetailer.getId());
238
		retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
21463 ashik.ali 239
		if(!createRetailerRequest.isShopAddressSameAsRetailerAddress() && createRetailerRequest.getAddress() == null){
240
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ADDRESS, "", "");
21440 ashik.ali 241
		}
21463 ashik.ali 242
		if(createRetailerRequest.isShopAddressSameAsRetailerAddress()){
243
			ShopAddress shopAddress = new ShopAddress();
244
			shopAddress.setShopId(shop.getId());
245
			shopAddress.setAddressId(addressRetailer.getId());
246
			shopAddressRepository.persist(shopAddress);
247
		}else{
248
			final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());
22855 amit.gupta 249
			addressShop.setRetaierId(retailer.getId());
22041 amit.gupta 250
			addressRepository.persist(addressShop);
21463 ashik.ali 251
			ShopAddress shopAddress = new ShopAddress();
252
			shopAddress.setShopId(shop.getId());
253
			shopAddress.setAddressId(addressShop.getId());
254
			shopAddressRepository.persist(shopAddress);
255
		}
21768 amit.gupta 256
		UserAccounts ua = new UserAccounts();
22355 ashik.ali 257
		ua.setAccount_key(String.valueOf(saholicUser.getId()));
21768 amit.gupta 258
		ua.setUser_id(user.getId());
21769 amit.gupta 259
		ua.setAccount_type(AccountType.saholic);
21768 amit.gupta 260
		userAccountRepository.persist(ua);
261
 
262
		UserAccounts ua2 = new UserAccounts();
263
		ua2.setAccount_key(String.valueOf(saholicUser.getActiveCartId()));
264
		ua2.setUser_id(user.getId());
265
		ua2.setAccount_type(AccountType.cartId);
266
		userAccountRepository.persist(ua2);
267
 
268
		UserRole ur = new UserRole();
22011 ashik.ali 269
		ur.setRoleType(RoleType.RETAILER);
21768 amit.gupta 270
		ur.setUserId(user.getId());
271
		userRoleRepository.persist(ur);
22355 ashik.ali 272
 
273
		if(foundRetailer){
274
			LOGGER.info("\n\n\n****retailer found\n\n\n");
22604 ashik.ali 275
			PrivateDealUser privateDealUser = null;
276
			try{
277
				privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
278
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
279
				LOGGER.error("PrivateDealUser not found : ", profitMandiBusinessException);
280
			}
22503 amit.gupta 281
			if(privateDealUser == null) {
282
				privateDealUser = new PrivateDealUser();
283
				privateDealUser.setActive(true);
284
				privateDealUser.setBulkShipmentAmountLimit(50000);
285
				privateDealUser.setId(saholicUser.getId());
286
				privateDealUserRepository.persist(privateDealUser);
287
				PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
288
				PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
289
				privateDealUserAddressId.setUserId(retailer.getId());
290
				privateDealUserAddressId.setAddressId(addressRetailer.getId());
291
				privateDealUserAddressMapping.setId(privateDealUserAddressId);
292
				privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
293
			}
22355 ashik.ali 294
			if(privateDealUser.getCounterId() == null){
295
				Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
296
				privateDealUser.setCounterId(counterId);
297
				privateDealUserRepository.persist(privateDealUser);
298
			}
299
		}else{
300
			LOGGER.info("retailer not found");
301
			//gst number intergration with counter
302
			Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
303
 
304
 
22604 ashik.ali 305
			PrivateDealUser privateDealUser = null;
306
			try{
307
				privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
308
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
22875 amit.gupta 309
				LOGGER.warn("PrivateDealUser not found");
22604 ashik.ali 310
			}
311
 
22355 ashik.ali 312
			if(privateDealUser != null){
313
				//LOGGER.info("PrivateDealUser found with id [{}]", saholicUser.getId());
314
				privateDealUser.setCounterId(counterId);
315
				privateDealUserRepository.update(privateDealUser);
316
			}else{	
22875 amit.gupta 317
				LOGGER.info("PrivateDealUser not found with id [{}], creating it", saholicUser.getId());
22355 ashik.ali 318
				privateDealUser = new PrivateDealUser();
319
				privateDealUser.setActive(true);
320
				privateDealUser.setBulkShipmentAmountLimit(50000);
321
				privateDealUser.setId(saholicUser.getId());
322
				privateDealUser.setCounterId(counterId);
323
				privateDealUserRepository.persist(privateDealUser);
324
			}
325
			PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
326
			PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
327
			privateDealUserAddressId.setUserId(retailer.getId());
328
			privateDealUserAddressId.setAddressId(addressRetailer.getId());
329
			privateDealUserAddressMapping.setId(privateDealUserAddressId);
330
			privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
331
 
332
			saholicUser.setAddressId(addressRetailer.getId());
333
			userUserRepository.persist(saholicUser);
334
		}
22495 amit.gupta 335
		/*StateInfo stateInfo = Utils.getStateInfo(addressRetailer.getState());
22473 ashik.ali 336
		DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(createRetailerRequest.getDistrict(), stateInfo.getShortName());
337
		FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
338
		boolean foundFofoStoreSequence = false;
339
		try{
340
			fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
341
			foundFofoStoreSequence = true;
342
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
343
			fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
344
			fofoStoreSequenceGeneration.setSequence(1);
345
			fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
346
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
347
		}
348
		FofoStore fofoStore = new FofoStore();
349
		fofoStore.setId(retailer.getId());
350
		String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
351
		if(foundFofoStoreSequence){
352
			fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
353
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
354
		}
355
		fofoStore.setCode(fofoStoreCode);
22495 amit.gupta 356
		fofoStoreRepository.persist(fofoStore);*/
21292 ashik.ali 357
	}
358
 
22355 ashik.ali 359
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
360
		if(gstNumber != null && !gstNumber.isEmpty()){
361
			Counter counter = new Counter();
362
			counter.setEmailId(emailId);
363
			counter.setGstin(gstNumber);
364
			counter.setMobileNumber(mobileNumber);
365
			counter.setName(name);
366
			counter.setAddressId(addressId);
367
			counterRepository.persist(counter);
368
			return counter.getId();
369
		}else{
370
			return null;
371
		}
21463 ashik.ali 372
	}
22355 ashik.ali 373
 
374
 
21448 ashik.ali 375
	private void addBrandWithRetailer(int retailerId, Set<Category> categories)
376
		throws ProfitMandiBusinessException{
377
		for(Category category : categories){
378
			for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
379
				Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
380
				final RetailerBrand retailerBrand = new RetailerBrand();
381
				retailerBrand.setRetailerId(retailerId);
382
				retailerBrand.setBrandId(brand.getId());
383
				retailerBrandRepository.persist(retailerBrand);
384
			}
385
		}
386
	}
22524 ashik.ali 387
 
388
	@ApiOperation(value = "Create Fofo Store")
389
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_FOFO_FOFO_STORE, method=RequestMethod.POST)
390
	public ResponseEntity<?> createFofoStore(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.DISTRICT_NAME) String districtName) throws ProfitMandiBusinessException, Exception{
391
		try{
392
			UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
393
			LOGGER.info("requestAttribute [userInfo={}]", userInfo);
394
			User user = userRepository.selectById(userInfo.getUserId());
22604 ashik.ali 395
			// = userAccountRepository.selectRetailerIdByUserId(user.getId());
22524 ashik.ali 396
			UserAccounts userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
22604 ashik.ali 397
			Retailer retailer = retailerRepository.selectById(Integer.parseInt(userAccounts.getAccount_key()));
398
			try{
399
				userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.FOFO);
400
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
22524 ashik.ali 401
				throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
22554 amit.gupta 402
			}
22604 ashik.ali 403
 
22524 ashik.ali 404
			boolean foundFofoStore = false;
405
			try{
406
				fofoStoreRepository.selectByRetailerId(retailer.getId());
407
				foundFofoStore = true;
408
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
409
 
410
			}
411
 
412
			if(foundFofoStore){
413
				throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "USR_1014");
414
			}			
415
 
416
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
417
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
418
 
419
			StateInfo stateInfo = Utils.getStateInfo(retailerAddress.getState());
420
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName, stateInfo.getShortName());
421
			FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
422
			boolean foundFofoStoreSequence = false;
423
			try{
424
				fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
425
				foundFofoStoreSequence = true;
426
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
427
				fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
428
				fofoStoreSequenceGeneration.setSequence(1);
429
				fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
430
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
431
			}
432
 
433
			FofoStore fofoStore = new FofoStore();
434
			fofoStore.setId(retailer.getId());
435
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
436
			if(foundFofoStoreSequence){
437
				fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
438
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
439
			}
440
			fofoStore.setCode(fofoStoreCode);
441
			fofoStoreRepository.persist(fofoStore);
442
 
443
			return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1003"));
444
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
445
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
446
			throw profitMandiBusinessException;
447
			//return responseSender.badRequest(profitMandiBusinessException);
448
		}
449
	}
450
 
21292 ashik.ali 451
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
21496 ashik.ali 452
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21292 ashik.ali 453
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 454
		return responseSender.ok(retailerRepository.selectAll(pageNumber, pageSize));
21292 ashik.ali 455
	}
456
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21431 ashik.ali 457
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 458
		LOGGER.info("requested url : "+request.getRequestURL().toString());
459
		try {
21440 ashik.ali 460
			return responseSender.ok(retailerRepository.selectById(id));
461
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
462
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
463
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 464
		}
465
	}
466
 
467
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
468
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
469
		LOGGER.info("requested url : "+request.getRequestURL().toString());
470
		try {
21440 ashik.ali 471
			return responseSender.ok(retailerRepository.selectByName(name));
472
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
473
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
474
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 475
		}
476
	}
477
 
478
 
479
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21431 ashik.ali 480
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 481
		LOGGER.info("requested url : "+request.getRequestURL().toString());
482
		try {
483
			retailerRepository.deleteById(id);
21440 ashik.ali 484
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
485
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
486
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
487
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 488
		}
489
	}
490
 
21426 ashik.ali 491
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
21431 ashik.ali 492
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 493
		LOGGER.info("requested url : "+request.getRequestURL().toString());
494
		try {
495
			Document document = documentRepository.selectById(createShop.getDocumentId());
496
			if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
497
				LOGGER.error("documet is already mapped with another shop");
498
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
499
			}
500
			retailerRepository.selectById(retailerId);
501
			Shop shop = new Shop();
502
			shop.setRetailerId(retailerId);
503
			Address address = this.createAddress(createShop.getAddress());
504
			addressRepository.persist(address);
505
			shop.setAddressId(address.getId());
506
			shop.setDocumentId(document.getId());
507
			shopRepository.persist(shop);
508
			ShopAddress shopAddress = new ShopAddress();
509
			shopAddress.setAddressId(address.getId());
510
			shopAddress.setShopId(shop.getId());
511
			shopAddressRepository.persist(shopAddress);
21440 ashik.ali 512
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
513
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
514
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
515
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 516
		}
517
	}
518
 
519
 
21302 ashik.ali 520
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 521
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 522
		LOGGER.info("requested url : "+request.getRequestURL().toString());
523
		try {
524
			retailerRepository.removeShop(shopId, retailerId);
21440 ashik.ali 525
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
526
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
527
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
528
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 529
		}
530
	}
531
 
21426 ashik.ali 532
	private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
533
		Address address = new Address();
534
		address.setName(createAddress.getName());
535
		address.setLine1(createAddress.getLine1());
536
		address.setLine2(createAddress.getLine2());
537
		address.setLandmark(createAddress.getLandmark());
538
		address.setCity(createAddress.getCity());
539
		address.setState(createAddress.getState());
540
		address.setPinCode(createAddress.getPinCode());
541
		address.setCountry(createAddress.getCountry());
542
		address.setPhoneNumber(createAddress.getPhoneNumber());
21706 amit.gupta 543
 
21426 ashik.ali 544
		return address;
545
	}
21440 ashik.ali 546
 
21426 ashik.ali 547
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
548
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
549
		LOGGER.info("requested url : "+request.getRequestURL().toString());
550
		try {
551
			retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
21440 ashik.ali 552
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
553
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
554
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
555
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 556
		}
557
	}
21292 ashik.ali 558
 
21426 ashik.ali 559
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 560
	public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 561
		LOGGER.info("requested url : "+request.getRequestURL().toString());
562
		try {
563
			retailerRepository.removeAddress(addressId, retailerId);
21440 ashik.ali 564
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
565
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
566
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
567
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 568
		}
569
	}
570
 
571
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
572
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
573
		LOGGER.info("requested url : "+request.getRequestURL().toString());
574
		try {
21448 ashik.ali 575
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
576
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
21440 ashik.ali 577
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
578
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
579
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
580
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 581
		}
582
	}
583
 
584
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 585
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 586
		LOGGER.info("requested url : "+request.getRequestURL().toString());
587
		try {
588
			brandRepository.selectById(brandId);
589
			retailerRepository.selectById(retailerId);
21448 ashik.ali 590
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
21440 ashik.ali 591
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
592
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
593
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
594
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 595
		}
596
	}
597
 
598
 
21448 ashik.ali 599
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
600
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 601
		LOGGER.info("requested url : "+request.getRequestURL().toString());
602
		try {
21448 ashik.ali 603
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
21440 ashik.ali 604
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
605
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
606
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 607
		}
608
	}
609
 
21448 ashik.ali 610
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
611
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
612
		LOGGER.info("requested url : "+request.getRequestURL().toString());
613
		try {
21698 amit.gupta 614
			return responseSender.ok(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId));
21448 ashik.ali 615
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
616
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
617
			return responseSender.badRequest(profitMandiBusinessException);
618
		}
619
	}
620
 
21426 ashik.ali 621
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
21431 ashik.ali 622
	public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 623
		LOGGER.info("requested url : "+request.getRequestURL().toString());
624
		try {
21440 ashik.ali 625
			return responseSender.ok(shopRepository.selectByRetailerId(id));
626
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
627
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
628
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 629
		}
630
	}
631
 
632
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
21431 ashik.ali 633
	public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 634
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 635
		return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
21426 ashik.ali 636
	}
637
 
638
 
639
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
21431 ashik.ali 640
	public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 641
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 642
		return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
21426 ashik.ali 643
	}
22495 amit.gupta 644
 
645
	@RequestMapping(value = "/retailer/showFofoInterest", method=RequestMethod.GET)
646
	public ResponseEntity<?> showFofoInterest(HttpServletRequest request) throws Throwable{
647
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
648
		User user = userRepository.selectById(userInfo.getUserId());
649
		RetailerFofoInterest retailerInterest = new RetailerFofoInterest();
650
		retailerInterest.setCity(user.getCity());
651
		retailerInterest.setMobile(user.getMobileNumber());
652
		retailerInterest.setPinCode(user.getPinCode());
653
		retailerInterest.setUserId(userInfo.getUserId());
22876 amit.gupta 654
		if(user.getPinCode()!=null){
655
			retailerInterest.setPinCode(user.getPinCode());
656
		}
22495 amit.gupta 657
		retailerInterest.setInterestShownOn(new Date());
658
		LOGGER.info("requested url : "+request.getRequestURL().toString());
659
		return responseSender.ok(mongoClient.saveRetailerInterestOnFofo(retailerInterest));
660
	}
661
 
662
	@RequestMapping(value = "/retailer/hasRetailerShownInterest", method=RequestMethod.GET)
663
	public ResponseEntity<?> getAllBrads(HttpServletRequest request) throws Throwable{
664
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
665
		LOGGER.info("requested url : "+request.getRequestURL().toString());
666
		return responseSender.ok(mongoClient.hasRetailerShownInterest(userInfo.getUserId()));
667
	}
21426 ashik.ali 668
 
21292 ashik.ali 669
}