Subversion Repositories SmartDukaan

Rev

Rev 22554 | Rev 22656 | 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");
22604 ashik.ali 273
			PrivateDealUser privateDealUser = null;
274
			try{
275
				privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
276
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
277
				LOGGER.error("PrivateDealUser not found : ", profitMandiBusinessException);
278
			}
22503 amit.gupta 279
			if(privateDealUser == null) {
280
				privateDealUser = new PrivateDealUser();
281
				privateDealUser.setActive(true);
282
				privateDealUser.setBulkShipmentAmountLimit(50000);
283
				privateDealUser.setId(saholicUser.getId());
284
				privateDealUserRepository.persist(privateDealUser);
285
				PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
286
				PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
287
				privateDealUserAddressId.setUserId(retailer.getId());
288
				privateDealUserAddressId.setAddressId(addressRetailer.getId());
289
				privateDealUserAddressMapping.setId(privateDealUserAddressId);
290
				privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
291
			}
22355 ashik.ali 292
			if(privateDealUser.getCounterId() == null){
293
				Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
294
				privateDealUser.setCounterId(counterId);
295
				privateDealUserRepository.persist(privateDealUser);
296
			}
297
		}else{
298
			LOGGER.info("retailer not found");
299
			//gst number intergration with counter
300
			Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
301
 
302
 
22604 ashik.ali 303
			PrivateDealUser privateDealUser = null;
304
			try{
305
				privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
306
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
307
				LOGGER.error("PrivateDealUser not found : ", profitMandiBusinessException);
308
			}
309
 
22355 ashik.ali 310
			if(privateDealUser != null){
311
				//LOGGER.info("PrivateDealUser found with id [{}]", saholicUser.getId());
312
				privateDealUser.setCounterId(counterId);
313
				privateDealUserRepository.update(privateDealUser);
314
			}else{	
315
				LOGGER.info("PrivateDealUser not found with id [{}]", saholicUser.getId());
316
				privateDealUser = new PrivateDealUser();
317
				privateDealUser.setActive(true);
318
				privateDealUser.setBulkShipmentAmountLimit(50000);
319
				privateDealUser.setId(saholicUser.getId());
320
				privateDealUser.setCounterId(counterId);
321
				privateDealUserRepository.persist(privateDealUser);
322
			}
323
			PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
324
			PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
325
			privateDealUserAddressId.setUserId(retailer.getId());
326
			privateDealUserAddressId.setAddressId(addressRetailer.getId());
327
			privateDealUserAddressMapping.setId(privateDealUserAddressId);
328
			privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
329
 
330
			saholicUser.setAddressId(addressRetailer.getId());
331
			userUserRepository.persist(saholicUser);
332
		}
22495 amit.gupta 333
		/*StateInfo stateInfo = Utils.getStateInfo(addressRetailer.getState());
22473 ashik.ali 334
		DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(createRetailerRequest.getDistrict(), stateInfo.getShortName());
335
		FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
336
		boolean foundFofoStoreSequence = false;
337
		try{
338
			fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
339
			foundFofoStoreSequence = true;
340
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
341
			fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
342
			fofoStoreSequenceGeneration.setSequence(1);
343
			fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
344
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
345
		}
346
		FofoStore fofoStore = new FofoStore();
347
		fofoStore.setId(retailer.getId());
348
		String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
349
		if(foundFofoStoreSequence){
350
			fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
351
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
352
		}
353
		fofoStore.setCode(fofoStoreCode);
22495 amit.gupta 354
		fofoStoreRepository.persist(fofoStore);*/
21292 ashik.ali 355
	}
356
 
22355 ashik.ali 357
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
358
		if(gstNumber != null && !gstNumber.isEmpty()){
359
			Counter counter = new Counter();
360
			counter.setEmailId(emailId);
361
			counter.setGstin(gstNumber);
362
			counter.setMobileNumber(mobileNumber);
363
			counter.setName(name);
364
			counter.setAddressId(addressId);
365
			counterRepository.persist(counter);
366
			return counter.getId();
367
		}else{
368
			return null;
369
		}
21463 ashik.ali 370
	}
22355 ashik.ali 371
 
372
 
21448 ashik.ali 373
	private void addBrandWithRetailer(int retailerId, Set<Category> categories)
374
		throws ProfitMandiBusinessException{
375
		for(Category category : categories){
376
			for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
377
				Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
378
				final RetailerBrand retailerBrand = new RetailerBrand();
379
				retailerBrand.setRetailerId(retailerId);
380
				retailerBrand.setBrandId(brand.getId());
381
				retailerBrandRepository.persist(retailerBrand);
382
			}
383
		}
384
	}
22524 ashik.ali 385
 
386
	@ApiOperation(value = "Create Fofo Store")
387
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_FOFO_FOFO_STORE, method=RequestMethod.POST)
388
	public ResponseEntity<?> createFofoStore(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.DISTRICT_NAME) String districtName) throws ProfitMandiBusinessException, Exception{
389
		try{
390
			UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
391
			LOGGER.info("requestAttribute [userInfo={}]", userInfo);
392
			User user = userRepository.selectById(userInfo.getUserId());
22604 ashik.ali 393
			// = userAccountRepository.selectRetailerIdByUserId(user.getId());
22524 ashik.ali 394
			UserAccounts userAccounts = userAccountRepository.selectSaholicByUserId(user.getId());
22604 ashik.ali 395
			Retailer retailer = retailerRepository.selectById(Integer.parseInt(userAccounts.getAccount_key()));
396
			try{
397
				userRoleRepository.selectByUserIdAndRoleType(user.getId(), RoleType.FOFO);
398
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
22524 ashik.ali 399
				throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, user.getId(), "USR_1013");
22554 amit.gupta 400
			}
22604 ashik.ali 401
 
22524 ashik.ali 402
			boolean foundFofoStore = false;
403
			try{
404
				fofoStoreRepository.selectByRetailerId(retailer.getId());
405
				foundFofoStore = true;
406
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
407
 
408
			}
409
 
410
			if(foundFofoStore){
411
				throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "USR_1014");
412
			}			
413
 
414
			int retailerAddressId = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
415
			Address retailerAddress = addressRepository.selectById(retailerAddressId);
416
 
417
			StateInfo stateInfo = Utils.getStateInfo(retailerAddress.getState());
418
			DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(districtName, stateInfo.getShortName());
419
			FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
420
			boolean foundFofoStoreSequence = false;
421
			try{
422
				fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
423
				foundFofoStoreSequence = true;
424
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
425
				fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
426
				fofoStoreSequenceGeneration.setSequence(1);
427
				fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
428
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
429
			}
430
 
431
			FofoStore fofoStore = new FofoStore();
432
			fofoStore.setId(retailer.getId());
433
			String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
434
			if(foundFofoStoreSequence){
435
				fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
436
				fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
437
			}
438
			fofoStore.setCode(fofoStoreCode);
439
			fofoStoreRepository.persist(fofoStore);
440
 
441
			return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1003"));
442
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
443
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
444
			throw profitMandiBusinessException;
445
			//return responseSender.badRequest(profitMandiBusinessException);
446
		}
447
	}
448
 
21292 ashik.ali 449
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
21496 ashik.ali 450
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21292 ashik.ali 451
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 452
		return responseSender.ok(retailerRepository.selectAll(pageNumber, pageSize));
21292 ashik.ali 453
	}
454
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21431 ashik.ali 455
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 456
		LOGGER.info("requested url : "+request.getRequestURL().toString());
457
		try {
21440 ashik.ali 458
			return responseSender.ok(retailerRepository.selectById(id));
459
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
460
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
461
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 462
		}
463
	}
464
 
465
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
466
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
467
		LOGGER.info("requested url : "+request.getRequestURL().toString());
468
		try {
21440 ashik.ali 469
			return responseSender.ok(retailerRepository.selectByName(name));
470
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
471
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
472
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 473
		}
474
	}
475
 
476
 
477
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21431 ashik.ali 478
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 479
		LOGGER.info("requested url : "+request.getRequestURL().toString());
480
		try {
481
			retailerRepository.deleteById(id);
21440 ashik.ali 482
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
483
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
484
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
485
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 486
		}
487
	}
488
 
21426 ashik.ali 489
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
21431 ashik.ali 490
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 491
		LOGGER.info("requested url : "+request.getRequestURL().toString());
492
		try {
493
			Document document = documentRepository.selectById(createShop.getDocumentId());
494
			if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
495
				LOGGER.error("documet is already mapped with another shop");
496
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
497
			}
498
			retailerRepository.selectById(retailerId);
499
			Shop shop = new Shop();
500
			shop.setRetailerId(retailerId);
501
			Address address = this.createAddress(createShop.getAddress());
502
			addressRepository.persist(address);
503
			shop.setAddressId(address.getId());
504
			shop.setDocumentId(document.getId());
505
			shopRepository.persist(shop);
506
			ShopAddress shopAddress = new ShopAddress();
507
			shopAddress.setAddressId(address.getId());
508
			shopAddress.setShopId(shop.getId());
509
			shopAddressRepository.persist(shopAddress);
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);
21426 ashik.ali 514
		}
515
	}
516
 
517
 
21302 ashik.ali 518
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 519
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 520
		LOGGER.info("requested url : "+request.getRequestURL().toString());
521
		try {
522
			retailerRepository.removeShop(shopId, retailerId);
21440 ashik.ali 523
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
524
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
525
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
526
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 527
		}
528
	}
529
 
21426 ashik.ali 530
	private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
531
		Address address = new Address();
532
		address.setName(createAddress.getName());
533
		address.setLine1(createAddress.getLine1());
534
		address.setLine2(createAddress.getLine2());
535
		address.setLandmark(createAddress.getLandmark());
536
		address.setCity(createAddress.getCity());
537
		address.setState(createAddress.getState());
538
		address.setPinCode(createAddress.getPinCode());
539
		address.setCountry(createAddress.getCountry());
540
		address.setPhoneNumber(createAddress.getPhoneNumber());
21706 amit.gupta 541
 
21426 ashik.ali 542
		return address;
543
	}
21440 ashik.ali 544
 
21426 ashik.ali 545
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
546
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
547
		LOGGER.info("requested url : "+request.getRequestURL().toString());
548
		try {
549
			retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
21440 ashik.ali 550
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
551
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
552
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
553
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 554
		}
555
	}
21292 ashik.ali 556
 
21426 ashik.ali 557
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 558
	public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 559
		LOGGER.info("requested url : "+request.getRequestURL().toString());
560
		try {
561
			retailerRepository.removeAddress(addressId, retailerId);
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_ADD, method=RequestMethod.POST)
570
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
571
		LOGGER.info("requested url : "+request.getRequestURL().toString());
572
		try {
21448 ashik.ali 573
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
574
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
21440 ashik.ali 575
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
576
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
577
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
578
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 579
		}
580
	}
581
 
582
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 583
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 584
		LOGGER.info("requested url : "+request.getRequestURL().toString());
585
		try {
586
			brandRepository.selectById(brandId);
587
			retailerRepository.selectById(retailerId);
21448 ashik.ali 588
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
21440 ashik.ali 589
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
590
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
591
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
592
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 593
		}
594
	}
595
 
596
 
21448 ashik.ali 597
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
598
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 599
		LOGGER.info("requested url : "+request.getRequestURL().toString());
600
		try {
21448 ashik.ali 601
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
21440 ashik.ali 602
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
603
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
604
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 605
		}
606
	}
607
 
21448 ashik.ali 608
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
609
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
610
		LOGGER.info("requested url : "+request.getRequestURL().toString());
611
		try {
21698 amit.gupta 612
			return responseSender.ok(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId));
21448 ashik.ali 613
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
614
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
615
			return responseSender.badRequest(profitMandiBusinessException);
616
		}
617
	}
618
 
21426 ashik.ali 619
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
21431 ashik.ali 620
	public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 621
		LOGGER.info("requested url : "+request.getRequestURL().toString());
622
		try {
21440 ashik.ali 623
			return responseSender.ok(shopRepository.selectByRetailerId(id));
624
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
625
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
626
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 627
		}
628
	}
629
 
630
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
21431 ashik.ali 631
	public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 632
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 633
		return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
21426 ashik.ali 634
	}
635
 
636
 
637
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
21431 ashik.ali 638
	public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 639
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 640
		return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
21426 ashik.ali 641
	}
22495 amit.gupta 642
 
643
	@RequestMapping(value = "/retailer/showFofoInterest", method=RequestMethod.GET)
644
	public ResponseEntity<?> showFofoInterest(HttpServletRequest request) throws Throwable{
645
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
646
		User user = userRepository.selectById(userInfo.getUserId());
647
		RetailerFofoInterest retailerInterest = new RetailerFofoInterest();
648
		retailerInterest.setCity(user.getCity());
649
		retailerInterest.setMobile(user.getMobileNumber());
650
		retailerInterest.setPinCode(user.getPinCode());
651
		retailerInterest.setUserId(userInfo.getUserId());
652
		retailerInterest.setPinCode(user.getPinCode());
653
		retailerInterest.setInterestShownOn(new Date());
654
		LOGGER.info("requested url : "+request.getRequestURL().toString());
655
		return responseSender.ok(mongoClient.saveRetailerInterestOnFofo(retailerInterest));
656
	}
657
 
658
	@RequestMapping(value = "/retailer/hasRetailerShownInterest", method=RequestMethod.GET)
659
	public ResponseEntity<?> getAllBrads(HttpServletRequest request) throws Throwable{
660
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
661
		LOGGER.info("requested url : "+request.getRequestURL().toString());
662
		return responseSender.ok(mongoClient.hasRetailerShownInterest(userInfo.getUserId()));
663
	}
21426 ashik.ali 664
 
21292 ashik.ali 665
}