Subversion Repositories SmartDukaan

Rev

Rev 22524 | Rev 22554 | 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;
22473 ashik.ali 30
import com.spice.profitmandi.dao.entity.dtr.FofoStore;
31
import com.spice.profitmandi.dao.entity.dtr.FofoStoreSequenceGeneration;
21735 ashik.ali 32
import com.spice.profitmandi.dao.entity.dtr.Retailer;
33
import com.spice.profitmandi.dao.entity.dtr.RetailerBrand;
34
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
35
import com.spice.profitmandi.dao.entity.dtr.Shop;
36
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
37
import com.spice.profitmandi.dao.entity.dtr.User;
21768 amit.gupta 38
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
39
import com.spice.profitmandi.dao.entity.dtr.UserRole;
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());
183
		retailer.setSmartphoneSaleValue(createRetailerRequest.getSmartphoneSaleValue());
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());
21448 ashik.ali 200
		if(retailerRepository.isExistByNumberAndType(retailer.getNumber(), retailer.getType())){
201
			throw new ProfitMandiBusinessException(ProfitMandiConstants.NUMBER + ", " + ProfitMandiConstants.TYPE, retailer.getNumber() + ", " + retailer.getType(), "RTLR_1001");
202
		}
21463 ashik.ali 203
		documentRepository.markDocumentAsPersisted(shopDocument.getId());
22355 ashik.ali 204
 
205
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
206
		boolean foundRetailer = false;
207
		saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
208
		if(saholicUser == null){
209
			Cart cart = new Cart();
210
			cart.setCartStatus(CartStatus.ACTIVE);
211
			cartRepository.persist(cart);
212
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
213
			saholicUser.setEmailId(user.getEmailId());
214
			saholicUser.setName(createRetailerRequest.getName());
215
			saholicUser.setActiveCartId(cart.getId());
216
			userUserRepository.persist(saholicUser);
217
		}else{
218
			foundRetailer = true;
219
		}
220
		//in.shop2020.model.v1.user.User saholicUser = Utils.createSaholicUser(user.getEmailId()); 
221
		retailer.setId(saholicUser.getId());
21440 ashik.ali 222
		retailerRepository.persist(retailer);
22355 ashik.ali 223
 
21440 ashik.ali 224
		Shop shop = new Shop();
225
		shop.setName(createRetailerRequest.getShop().getName());
226
		shop.setDocumentId(shopDocument.getId());
227
		shop.setRetailerId(retailer.getId());
228
		shopRepository.persist(shop);
21448 ashik.ali 229
		this.addBrandWithRetailer(retailer.getId(), createRetailerRequest.getCategories());
21440 ashik.ali 230
		final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());
21706 amit.gupta 231
		addressRetailer.setRetaierId(retailer.getId());
22041 amit.gupta 232
		addressRepository.persist(addressRetailer);
22355 ashik.ali 233
 
21440 ashik.ali 234
		final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
235
		retailerRegisteredAddress.setRetailerId(retailer.getId());
236
		retailerRegisteredAddress.setAddressId(addressRetailer.getId());
237
		retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
21463 ashik.ali 238
		if(!createRetailerRequest.isShopAddressSameAsRetailerAddress() && createRetailerRequest.getAddress() == null){
239
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ADDRESS, "", "");
21440 ashik.ali 240
		}
21463 ashik.ali 241
		if(createRetailerRequest.isShopAddressSameAsRetailerAddress()){
242
			ShopAddress shopAddress = new ShopAddress();
243
			shopAddress.setShopId(shop.getId());
244
			shopAddress.setAddressId(addressRetailer.getId());
245
			shopAddressRepository.persist(shopAddress);
246
		}else{
247
			final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());
22041 amit.gupta 248
			addressRepository.persist(addressShop);
21463 ashik.ali 249
			ShopAddress shopAddress = new ShopAddress();
250
			shopAddress.setShopId(shop.getId());
251
			shopAddress.setAddressId(addressShop.getId());
252
			shopAddressRepository.persist(shopAddress);
253
		}
21768 amit.gupta 254
		UserAccounts ua = new UserAccounts();
22355 ashik.ali 255
		ua.setAccount_key(String.valueOf(saholicUser.getId()));
21768 amit.gupta 256
		ua.setUser_id(user.getId());
21769 amit.gupta 257
		ua.setAccount_type(AccountType.saholic);
21768 amit.gupta 258
		userAccountRepository.persist(ua);
259
 
260
		UserAccounts ua2 = new UserAccounts();
261
		ua2.setAccount_key(String.valueOf(saholicUser.getActiveCartId()));
262
		ua2.setUser_id(user.getId());
263
		ua2.setAccount_type(AccountType.cartId);
264
		userAccountRepository.persist(ua2);
265
 
266
		UserRole ur = new UserRole();
22011 ashik.ali 267
		ur.setRoleType(RoleType.RETAILER);
21768 amit.gupta 268
		ur.setUserId(user.getId());
269
		userRoleRepository.persist(ur);
22355 ashik.ali 270
 
271
		if(foundRetailer){
272
			LOGGER.info("\n\n\n****retailer found\n\n\n");
273
			PrivateDealUser privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
22503 amit.gupta 274
			if(privateDealUser == null) {
275
				privateDealUser = new PrivateDealUser();
276
				privateDealUser.setActive(true);
277
				privateDealUser.setBulkShipmentAmountLimit(50000);
278
				privateDealUser.setId(saholicUser.getId());
279
				privateDealUserRepository.persist(privateDealUser);
280
				PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
281
				PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
282
				privateDealUserAddressId.setUserId(retailer.getId());
283
				privateDealUserAddressId.setAddressId(addressRetailer.getId());
284
				privateDealUserAddressMapping.setId(privateDealUserAddressId);
285
				privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
286
			}
22355 ashik.ali 287
			if(privateDealUser.getCounterId() == null){
288
				Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
289
				privateDealUser.setCounterId(counterId);
290
				privateDealUserRepository.persist(privateDealUser);
291
			}
292
		}else{
293
			LOGGER.info("retailer not found");
294
			//gst number intergration with counter
295
			Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
296
 
297
 
298
				PrivateDealUser privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
299
			if(privateDealUser != null){
300
				//LOGGER.info("PrivateDealUser found with id [{}]", saholicUser.getId());
301
				privateDealUser.setCounterId(counterId);
302
				privateDealUserRepository.update(privateDealUser);
303
			}else{	
304
				LOGGER.info("PrivateDealUser not found with id [{}]", saholicUser.getId());
305
				privateDealUser = new PrivateDealUser();
306
				privateDealUser.setActive(true);
307
				privateDealUser.setBulkShipmentAmountLimit(50000);
308
				privateDealUser.setId(saholicUser.getId());
309
				privateDealUser.setCounterId(counterId);
310
				privateDealUserRepository.persist(privateDealUser);
311
			}
312
			PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
313
			PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
314
			privateDealUserAddressId.setUserId(retailer.getId());
315
			privateDealUserAddressId.setAddressId(addressRetailer.getId());
316
			privateDealUserAddressMapping.setId(privateDealUserAddressId);
317
			privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
318
 
319
			saholicUser.setAddressId(addressRetailer.getId());
320
			userUserRepository.persist(saholicUser);
321
		}
22495 amit.gupta 322
		/*StateInfo stateInfo = Utils.getStateInfo(addressRetailer.getState());
22473 ashik.ali 323
		DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(createRetailerRequest.getDistrict(), stateInfo.getShortName());
324
		FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
325
		boolean foundFofoStoreSequence = false;
326
		try{
327
			fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
328
			foundFofoStoreSequence = true;
329
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
330
			fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
331
			fofoStoreSequenceGeneration.setSequence(1);
332
			fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
333
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
334
		}
335
		FofoStore fofoStore = new FofoStore();
336
		fofoStore.setId(retailer.getId());
337
		String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
338
		if(foundFofoStoreSequence){
339
			fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
340
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
341
		}
342
		fofoStore.setCode(fofoStoreCode);
22495 amit.gupta 343
		fofoStoreRepository.persist(fofoStore);*/
21292 ashik.ali 344
	}
345
 
22355 ashik.ali 346
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
347
		if(gstNumber != null && !gstNumber.isEmpty()){
348
			Counter counter = new Counter();
349
			counter.setEmailId(emailId);
350
			counter.setGstin(gstNumber);
351
			counter.setMobileNumber(mobileNumber);
352
			counter.setName(name);
353
			counter.setAddressId(addressId);
354
			counterRepository.persist(counter);
355
			return counter.getId();
356
		}else{
357
			return null;
358
		}
21463 ashik.ali 359
	}
22355 ashik.ali 360
 
361
 
21448 ashik.ali 362
	private void addBrandWithRetailer(int retailerId, Set<Category> categories)
363
		throws ProfitMandiBusinessException{
364
		for(Category category : categories){
365
			for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
366
				Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
367
				final RetailerBrand retailerBrand = new RetailerBrand();
368
				retailerBrand.setRetailerId(retailerId);
369
				retailerBrand.setBrandId(brand.getId());
370
				retailerBrandRepository.persist(retailerBrand);
371
			}
372
		}
373
	}
22524 ashik.ali 374
 
375
	@ApiOperation(value = "Create Fofo Store")
376
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_FOFO_FOFO_STORE, method=RequestMethod.POST)
377
	public ResponseEntity<?> createFofoStore(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.DISTRICT_NAME) String districtName) throws ProfitMandiBusinessException, Exception{
378
		try{
379
			UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
380
			LOGGER.info("requestAttribute [userInfo={}]", userInfo);
381
			User user = userRepository.selectById(userInfo.getUserId());
382
			// = userAccountRepository.selectRetailerIdByUserId(user.getId());
383
			UserAccounts userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
384
			Retailer retailer = retailerRepository.selectById(Integer.parseInt(userAccounts.getAccount_key()));
22553 amit.gupta 385
			//TODO: Ashik fix it
386
			/*if(!retailer.isFofo()){
22524 ashik.ali 387
				throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
22553 amit.gupta 388
			}*/
22524 ashik.ali 389
			boolean foundFofoStore = false;
390
			try{
391
				fofoStoreRepository.selectByRetailerId(retailer.getId());
392
				foundFofoStore = true;
393
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
394
 
395
			}
396
 
397
			if(foundFofoStore){
398
				throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "USR_1014");
399
			}			
400
 
401
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
402
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
403
 
404
			StateInfo stateInfo = Utils.getStateInfo(retailerAddress.getState());
405
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName, stateInfo.getShortName());
406
			FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
407
			boolean foundFofoStoreSequence = false;
408
			try{
409
				fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
410
				foundFofoStoreSequence = true;
411
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
412
				fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
413
				fofoStoreSequenceGeneration.setSequence(1);
414
				fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
415
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
416
			}
417
 
418
			FofoStore fofoStore = new FofoStore();
419
			fofoStore.setId(retailer.getId());
420
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
421
			if(foundFofoStoreSequence){
422
				fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
423
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
424
			}
425
			fofoStore.setCode(fofoStoreCode);
426
			fofoStoreRepository.persist(fofoStore);
427
 
428
			return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1003"));
429
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
430
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
431
			throw profitMandiBusinessException;
432
			//return responseSender.badRequest(profitMandiBusinessException);
433
		}
434
	}
435
 
21292 ashik.ali 436
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
21496 ashik.ali 437
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21292 ashik.ali 438
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 439
		return responseSender.ok(retailerRepository.selectAll(pageNumber, pageSize));
21292 ashik.ali 440
	}
441
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21431 ashik.ali 442
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 443
		LOGGER.info("requested url : "+request.getRequestURL().toString());
444
		try {
21440 ashik.ali 445
			return responseSender.ok(retailerRepository.selectById(id));
446
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
447
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
448
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 449
		}
450
	}
451
 
452
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
453
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
454
		LOGGER.info("requested url : "+request.getRequestURL().toString());
455
		try {
21440 ashik.ali 456
			return responseSender.ok(retailerRepository.selectByName(name));
457
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
458
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
459
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 460
		}
461
	}
462
 
463
 
464
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21431 ashik.ali 465
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 466
		LOGGER.info("requested url : "+request.getRequestURL().toString());
467
		try {
468
			retailerRepository.deleteById(id);
21440 ashik.ali 469
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
470
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
471
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
472
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 473
		}
474
	}
475
 
21426 ashik.ali 476
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
21431 ashik.ali 477
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 478
		LOGGER.info("requested url : "+request.getRequestURL().toString());
479
		try {
480
			Document document = documentRepository.selectById(createShop.getDocumentId());
481
			if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
482
				LOGGER.error("documet is already mapped with another shop");
483
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
484
			}
485
			retailerRepository.selectById(retailerId);
486
			Shop shop = new Shop();
487
			shop.setRetailerId(retailerId);
488
			Address address = this.createAddress(createShop.getAddress());
489
			addressRepository.persist(address);
490
			shop.setAddressId(address.getId());
491
			shop.setDocumentId(document.getId());
492
			shopRepository.persist(shop);
493
			ShopAddress shopAddress = new ShopAddress();
494
			shopAddress.setAddressId(address.getId());
495
			shopAddress.setShopId(shop.getId());
496
			shopAddressRepository.persist(shopAddress);
21440 ashik.ali 497
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
498
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
499
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
500
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 501
		}
502
	}
503
 
504
 
21302 ashik.ali 505
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 506
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 507
		LOGGER.info("requested url : "+request.getRequestURL().toString());
508
		try {
509
			retailerRepository.removeShop(shopId, retailerId);
21440 ashik.ali 510
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
511
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
512
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
513
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 514
		}
515
	}
516
 
21426 ashik.ali 517
	private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
518
		Address address = new Address();
519
		address.setName(createAddress.getName());
520
		address.setLine1(createAddress.getLine1());
521
		address.setLine2(createAddress.getLine2());
522
		address.setLandmark(createAddress.getLandmark());
523
		address.setCity(createAddress.getCity());
524
		address.setState(createAddress.getState());
525
		address.setPinCode(createAddress.getPinCode());
526
		address.setCountry(createAddress.getCountry());
527
		address.setPhoneNumber(createAddress.getPhoneNumber());
21706 amit.gupta 528
 
21426 ashik.ali 529
		return address;
530
	}
21440 ashik.ali 531
 
21426 ashik.ali 532
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
533
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
534
		LOGGER.info("requested url : "+request.getRequestURL().toString());
535
		try {
536
			retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
21440 ashik.ali 537
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
538
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
539
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
540
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 541
		}
542
	}
21292 ashik.ali 543
 
21426 ashik.ali 544
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 545
	public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 546
		LOGGER.info("requested url : "+request.getRequestURL().toString());
547
		try {
548
			retailerRepository.removeAddress(addressId, retailerId);
21440 ashik.ali 549
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
550
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
551
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
552
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 553
		}
554
	}
555
 
556
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
557
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
558
		LOGGER.info("requested url : "+request.getRequestURL().toString());
559
		try {
21448 ashik.ali 560
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
561
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
21440 ashik.ali 562
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
563
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
564
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
565
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 566
		}
567
	}
568
 
569
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 570
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 571
		LOGGER.info("requested url : "+request.getRequestURL().toString());
572
		try {
573
			brandRepository.selectById(brandId);
574
			retailerRepository.selectById(retailerId);
21448 ashik.ali 575
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
21440 ashik.ali 576
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
577
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
578
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
579
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 580
		}
581
	}
582
 
583
 
21448 ashik.ali 584
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
585
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 586
		LOGGER.info("requested url : "+request.getRequestURL().toString());
587
		try {
21448 ashik.ali 588
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
21440 ashik.ali 589
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
590
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
591
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 592
		}
593
	}
594
 
21448 ashik.ali 595
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
596
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
597
		LOGGER.info("requested url : "+request.getRequestURL().toString());
598
		try {
21698 amit.gupta 599
			return responseSender.ok(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId));
21448 ashik.ali 600
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
601
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
602
			return responseSender.badRequest(profitMandiBusinessException);
603
		}
604
	}
605
 
21426 ashik.ali 606
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
21431 ashik.ali 607
	public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 608
		LOGGER.info("requested url : "+request.getRequestURL().toString());
609
		try {
21440 ashik.ali 610
			return responseSender.ok(shopRepository.selectByRetailerId(id));
611
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
612
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
613
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 614
		}
615
	}
616
 
617
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
21431 ashik.ali 618
	public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 619
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 620
		return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
21426 ashik.ali 621
	}
622
 
623
 
624
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
21431 ashik.ali 625
	public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 626
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 627
		return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
21426 ashik.ali 628
	}
22495 amit.gupta 629
 
630
	@RequestMapping(value = "/retailer/showFofoInterest", method=RequestMethod.GET)
631
	public ResponseEntity<?> showFofoInterest(HttpServletRequest request) throws Throwable{
632
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
633
		User user = userRepository.selectById(userInfo.getUserId());
634
		RetailerFofoInterest retailerInterest = new RetailerFofoInterest();
635
		retailerInterest.setCity(user.getCity());
636
		retailerInterest.setMobile(user.getMobileNumber());
637
		retailerInterest.setPinCode(user.getPinCode());
638
		retailerInterest.setUserId(userInfo.getUserId());
639
		retailerInterest.setPinCode(user.getPinCode());
640
		retailerInterest.setInterestShownOn(new Date());
641
		LOGGER.info("requested url : "+request.getRequestURL().toString());
642
		return responseSender.ok(mongoClient.saveRetailerInterestOnFofo(retailerInterest));
643
	}
644
 
645
	@RequestMapping(value = "/retailer/hasRetailerShownInterest", method=RequestMethod.GET)
646
	public ResponseEntity<?> getAllBrads(HttpServletRequest request) throws Throwable{
647
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
648
		LOGGER.info("requested url : "+request.getRequestURL().toString());
649
		return responseSender.ok(mongoClient.hasRetailerShownInterest(userInfo.getUserId()));
650
	}
21426 ashik.ali 651
 
21292 ashik.ali 652
}