Subversion Repositories SmartDukaan

Rev

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