Subversion Repositories SmartDukaan

Rev

Rev 22495 | Rev 22524 | 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
	}
21292 ashik.ali 374
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
21496 ashik.ali 375
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21292 ashik.ali 376
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 377
		return responseSender.ok(retailerRepository.selectAll(pageNumber, pageSize));
21292 ashik.ali 378
	}
379
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21431 ashik.ali 380
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 381
		LOGGER.info("requested url : "+request.getRequestURL().toString());
382
		try {
21440 ashik.ali 383
			return responseSender.ok(retailerRepository.selectById(id));
384
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
385
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
386
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 387
		}
388
	}
389
 
390
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
391
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
392
		LOGGER.info("requested url : "+request.getRequestURL().toString());
393
		try {
21440 ashik.ali 394
			return responseSender.ok(retailerRepository.selectByName(name));
395
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
396
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
397
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 398
		}
399
	}
400
 
401
 
402
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21431 ashik.ali 403
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 404
		LOGGER.info("requested url : "+request.getRequestURL().toString());
405
		try {
406
			retailerRepository.deleteById(id);
21440 ashik.ali 407
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
408
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
409
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
410
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 411
		}
412
	}
413
 
21426 ashik.ali 414
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
21431 ashik.ali 415
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 416
		LOGGER.info("requested url : "+request.getRequestURL().toString());
417
		try {
418
			Document document = documentRepository.selectById(createShop.getDocumentId());
419
			if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
420
				LOGGER.error("documet is already mapped with another shop");
421
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
422
			}
423
			retailerRepository.selectById(retailerId);
424
			Shop shop = new Shop();
425
			shop.setRetailerId(retailerId);
426
			Address address = this.createAddress(createShop.getAddress());
427
			addressRepository.persist(address);
428
			shop.setAddressId(address.getId());
429
			shop.setDocumentId(document.getId());
430
			shopRepository.persist(shop);
431
			ShopAddress shopAddress = new ShopAddress();
432
			shopAddress.setAddressId(address.getId());
433
			shopAddress.setShopId(shop.getId());
434
			shopAddressRepository.persist(shopAddress);
21440 ashik.ali 435
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
436
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
437
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
438
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 439
		}
440
	}
441
 
442
 
21302 ashik.ali 443
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 444
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 445
		LOGGER.info("requested url : "+request.getRequestURL().toString());
446
		try {
447
			retailerRepository.removeShop(shopId, retailerId);
21440 ashik.ali 448
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
449
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
450
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
451
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 452
		}
453
	}
454
 
21426 ashik.ali 455
	private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
456
		Address address = new Address();
457
		address.setName(createAddress.getName());
458
		address.setLine1(createAddress.getLine1());
459
		address.setLine2(createAddress.getLine2());
460
		address.setLandmark(createAddress.getLandmark());
461
		address.setCity(createAddress.getCity());
462
		address.setState(createAddress.getState());
463
		address.setPinCode(createAddress.getPinCode());
464
		address.setCountry(createAddress.getCountry());
465
		address.setPhoneNumber(createAddress.getPhoneNumber());
21706 amit.gupta 466
 
21426 ashik.ali 467
		return address;
468
	}
21440 ashik.ali 469
 
21426 ashik.ali 470
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
471
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
472
		LOGGER.info("requested url : "+request.getRequestURL().toString());
473
		try {
474
			retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
21440 ashik.ali 475
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
476
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
477
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
478
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 479
		}
480
	}
21292 ashik.ali 481
 
21426 ashik.ali 482
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 483
	public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 484
		LOGGER.info("requested url : "+request.getRequestURL().toString());
485
		try {
486
			retailerRepository.removeAddress(addressId, retailerId);
21440 ashik.ali 487
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
488
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
489
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
490
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 491
		}
492
	}
493
 
494
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
495
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
496
		LOGGER.info("requested url : "+request.getRequestURL().toString());
497
		try {
21448 ashik.ali 498
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
499
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
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
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 508
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 509
		LOGGER.info("requested url : "+request.getRequestURL().toString());
510
		try {
511
			brandRepository.selectById(brandId);
512
			retailerRepository.selectById(retailerId);
21448 ashik.ali 513
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
21440 ashik.ali 514
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
515
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
516
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
517
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 518
		}
519
	}
520
 
521
 
21448 ashik.ali 522
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
523
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 524
		LOGGER.info("requested url : "+request.getRequestURL().toString());
525
		try {
21448 ashik.ali 526
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
21440 ashik.ali 527
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
528
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
529
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 530
		}
531
	}
532
 
21448 ashik.ali 533
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
534
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
535
		LOGGER.info("requested url : "+request.getRequestURL().toString());
536
		try {
21698 amit.gupta 537
			return responseSender.ok(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId));
21448 ashik.ali 538
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
539
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
540
			return responseSender.badRequest(profitMandiBusinessException);
541
		}
542
	}
543
 
21426 ashik.ali 544
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
21431 ashik.ali 545
	public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 546
		LOGGER.info("requested url : "+request.getRequestURL().toString());
547
		try {
21440 ashik.ali 548
			return responseSender.ok(shopRepository.selectByRetailerId(id));
549
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
550
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
551
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 552
		}
553
	}
554
 
555
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
21431 ashik.ali 556
	public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 557
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 558
		return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
21426 ashik.ali 559
	}
560
 
561
 
562
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
21431 ashik.ali 563
	public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 564
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 565
		return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
21426 ashik.ali 566
	}
22495 amit.gupta 567
 
568
	@RequestMapping(value = "/retailer/showFofoInterest", method=RequestMethod.GET)
569
	public ResponseEntity<?> showFofoInterest(HttpServletRequest request) throws Throwable{
570
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
571
		User user = userRepository.selectById(userInfo.getUserId());
572
		RetailerFofoInterest retailerInterest = new RetailerFofoInterest();
573
		retailerInterest.setCity(user.getCity());
574
		retailerInterest.setMobile(user.getMobileNumber());
575
		retailerInterest.setPinCode(user.getPinCode());
576
		retailerInterest.setUserId(userInfo.getUserId());
577
		retailerInterest.setPinCode(user.getPinCode());
578
		retailerInterest.setInterestShownOn(new Date());
579
		LOGGER.info("requested url : "+request.getRequestURL().toString());
580
		return responseSender.ok(mongoClient.saveRetailerInterestOnFofo(retailerInterest));
581
	}
582
 
583
	@RequestMapping(value = "/retailer/hasRetailerShownInterest", method=RequestMethod.GET)
584
	public ResponseEntity<?> getAllBrads(HttpServletRequest request) throws Throwable{
585
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
586
		LOGGER.info("requested url : "+request.getRequestURL().toString());
587
		return responseSender.ok(mongoClient.hasRetailerShownInterest(userInfo.getUserId()));
588
	}
21426 ashik.ali 589
 
21292 ashik.ali 590
}