Subversion Repositories SmartDukaan

Rev

Rev 22473 | Rev 22503 | 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());
274
			if(privateDealUser.getCounterId() == null){
275
				Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
276
				privateDealUser.setCounterId(counterId);
277
				privateDealUserRepository.persist(privateDealUser);
278
			}
279
		}else{
280
			LOGGER.info("retailer not found");
281
			//gst number intergration with counter
282
			Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
283
 
284
 
285
				PrivateDealUser privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
286
			if(privateDealUser != null){
287
				//LOGGER.info("PrivateDealUser found with id [{}]", saholicUser.getId());
288
				privateDealUser.setCounterId(counterId);
289
				privateDealUserRepository.update(privateDealUser);
290
			}else{	
291
				LOGGER.info("PrivateDealUser not found with id [{}]", saholicUser.getId());
292
				privateDealUser = new PrivateDealUser();
293
				privateDealUser.setActive(true);
294
				privateDealUser.setBulkShipmentAmountLimit(50000);
295
				privateDealUser.setId(saholicUser.getId());
296
				privateDealUser.setCounterId(counterId);
297
				privateDealUserRepository.persist(privateDealUser);
298
			}
299
			PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
300
			PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
301
			privateDealUserAddressId.setUserId(retailer.getId());
302
			privateDealUserAddressId.setAddressId(addressRetailer.getId());
303
			privateDealUserAddressMapping.setId(privateDealUserAddressId);
304
			privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
305
 
306
			saholicUser.setAddressId(addressRetailer.getId());
307
			userUserRepository.persist(saholicUser);
308
		}
22495 amit.gupta 309
		/*StateInfo stateInfo = Utils.getStateInfo(addressRetailer.getState());
22473 ashik.ali 310
		DistrictMaster districtMaster = districtMasterRepository.selectByNameAndStateShortName(createRetailerRequest.getDistrict(), stateInfo.getShortName());
311
		FofoStoreSequenceGeneration fofoStoreSequenceGeneration = null;
312
		boolean foundFofoStoreSequence = false;
313
		try{
314
			fofoStoreSequenceGeneration = fofoStoreSequenceGenerationRepository.selectByStateShortName(stateInfo.getShortName());
315
			foundFofoStoreSequence = true;
316
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
317
			fofoStoreSequenceGeneration = new FofoStoreSequenceGeneration();
318
			fofoStoreSequenceGeneration.setSequence(1);
319
			fofoStoreSequenceGeneration.setStateShortName(stateInfo.getShortName());
320
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
321
		}
322
		FofoStore fofoStore = new FofoStore();
323
		fofoStore.setId(retailer.getId());
324
		String fofoStoreCode = StringUtils.generateFofoStoreSequence(districtMaster.getStateShortName() + districtMaster.getShortName(), fofoStoreSequenceGeneration.getSequence());
325
		if(foundFofoStoreSequence){
326
			fofoStoreSequenceGeneration.setSequence(fofoStoreSequenceGeneration.getSequence() + 1);
327
			fofoStoreSequenceGenerationRepository.persist(fofoStoreSequenceGeneration);
328
		}
329
		fofoStore.setCode(fofoStoreCode);
22495 amit.gupta 330
		fofoStoreRepository.persist(fofoStore);*/
21292 ashik.ali 331
	}
332
 
22355 ashik.ali 333
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
334
		if(gstNumber != null && !gstNumber.isEmpty()){
335
			Counter counter = new Counter();
336
			counter.setEmailId(emailId);
337
			counter.setGstin(gstNumber);
338
			counter.setMobileNumber(mobileNumber);
339
			counter.setName(name);
340
			counter.setAddressId(addressId);
341
			counterRepository.persist(counter);
342
			return counter.getId();
343
		}else{
344
			return null;
345
		}
21463 ashik.ali 346
	}
22355 ashik.ali 347
 
348
 
21448 ashik.ali 349
	private void addBrandWithRetailer(int retailerId, Set<Category> categories)
350
		throws ProfitMandiBusinessException{
351
		for(Category category : categories){
352
			for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
353
				Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
354
				final RetailerBrand retailerBrand = new RetailerBrand();
355
				retailerBrand.setRetailerId(retailerId);
356
				retailerBrand.setBrandId(brand.getId());
357
				retailerBrandRepository.persist(retailerBrand);
358
			}
359
		}
360
	}
21292 ashik.ali 361
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
21496 ashik.ali 362
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21292 ashik.ali 363
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 364
		return responseSender.ok(retailerRepository.selectAll(pageNumber, pageSize));
21292 ashik.ali 365
	}
366
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21431 ashik.ali 367
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 368
		LOGGER.info("requested url : "+request.getRequestURL().toString());
369
		try {
21440 ashik.ali 370
			return responseSender.ok(retailerRepository.selectById(id));
371
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
372
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
373
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 374
		}
375
	}
376
 
377
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
378
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
379
		LOGGER.info("requested url : "+request.getRequestURL().toString());
380
		try {
21440 ashik.ali 381
			return responseSender.ok(retailerRepository.selectByName(name));
382
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
383
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
384
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 385
		}
386
	}
387
 
388
 
389
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21431 ashik.ali 390
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 391
		LOGGER.info("requested url : "+request.getRequestURL().toString());
392
		try {
393
			retailerRepository.deleteById(id);
21440 ashik.ali 394
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
395
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
396
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
397
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 398
		}
399
	}
400
 
21426 ashik.ali 401
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
21431 ashik.ali 402
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 403
		LOGGER.info("requested url : "+request.getRequestURL().toString());
404
		try {
405
			Document document = documentRepository.selectById(createShop.getDocumentId());
406
			if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
407
				LOGGER.error("documet is already mapped with another shop");
408
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
409
			}
410
			retailerRepository.selectById(retailerId);
411
			Shop shop = new Shop();
412
			shop.setRetailerId(retailerId);
413
			Address address = this.createAddress(createShop.getAddress());
414
			addressRepository.persist(address);
415
			shop.setAddressId(address.getId());
416
			shop.setDocumentId(document.getId());
417
			shopRepository.persist(shop);
418
			ShopAddress shopAddress = new ShopAddress();
419
			shopAddress.setAddressId(address.getId());
420
			shopAddress.setShopId(shop.getId());
421
			shopAddressRepository.persist(shopAddress);
21440 ashik.ali 422
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
423
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
424
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
425
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 426
		}
427
	}
428
 
429
 
21302 ashik.ali 430
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 431
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 432
		LOGGER.info("requested url : "+request.getRequestURL().toString());
433
		try {
434
			retailerRepository.removeShop(shopId, retailerId);
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);
21292 ashik.ali 439
		}
440
	}
441
 
21426 ashik.ali 442
	private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
443
		Address address = new Address();
444
		address.setName(createAddress.getName());
445
		address.setLine1(createAddress.getLine1());
446
		address.setLine2(createAddress.getLine2());
447
		address.setLandmark(createAddress.getLandmark());
448
		address.setCity(createAddress.getCity());
449
		address.setState(createAddress.getState());
450
		address.setPinCode(createAddress.getPinCode());
451
		address.setCountry(createAddress.getCountry());
452
		address.setPhoneNumber(createAddress.getPhoneNumber());
21706 amit.gupta 453
 
21426 ashik.ali 454
		return address;
455
	}
21440 ashik.ali 456
 
21426 ashik.ali 457
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
458
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
459
		LOGGER.info("requested url : "+request.getRequestURL().toString());
460
		try {
461
			retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
21440 ashik.ali 462
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
463
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
464
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
465
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 466
		}
467
	}
21292 ashik.ali 468
 
21426 ashik.ali 469
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 470
	public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 471
		LOGGER.info("requested url : "+request.getRequestURL().toString());
472
		try {
473
			retailerRepository.removeAddress(addressId, retailerId);
21440 ashik.ali 474
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
475
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
476
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
477
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 478
		}
479
	}
480
 
481
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
482
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
483
		LOGGER.info("requested url : "+request.getRequestURL().toString());
484
		try {
21448 ashik.ali 485
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
486
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
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_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 495
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 496
		LOGGER.info("requested url : "+request.getRequestURL().toString());
497
		try {
498
			brandRepository.selectById(brandId);
499
			retailerRepository.selectById(retailerId);
21448 ashik.ali 500
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
21440 ashik.ali 501
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
502
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
503
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
504
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 505
		}
506
	}
507
 
508
 
21448 ashik.ali 509
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
510
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 511
		LOGGER.info("requested url : "+request.getRequestURL().toString());
512
		try {
21448 ashik.ali 513
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
21440 ashik.ali 514
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
515
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
516
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 517
		}
518
	}
519
 
21448 ashik.ali 520
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
521
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
522
		LOGGER.info("requested url : "+request.getRequestURL().toString());
523
		try {
21698 amit.gupta 524
			return responseSender.ok(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId));
21448 ashik.ali 525
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
526
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
527
			return responseSender.badRequest(profitMandiBusinessException);
528
		}
529
	}
530
 
21426 ashik.ali 531
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
21431 ashik.ali 532
	public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 533
		LOGGER.info("requested url : "+request.getRequestURL().toString());
534
		try {
21440 ashik.ali 535
			return responseSender.ok(shopRepository.selectByRetailerId(id));
536
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
537
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
538
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 539
		}
540
	}
541
 
542
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
21431 ashik.ali 543
	public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 544
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 545
		return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
21426 ashik.ali 546
	}
547
 
548
 
549
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
21431 ashik.ali 550
	public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 551
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 552
		return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
21426 ashik.ali 553
	}
22495 amit.gupta 554
 
555
	@RequestMapping(value = "/retailer/showFofoInterest", method=RequestMethod.GET)
556
	public ResponseEntity<?> showFofoInterest(HttpServletRequest request) throws Throwable{
557
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
558
		User user = userRepository.selectById(userInfo.getUserId());
559
		RetailerFofoInterest retailerInterest = new RetailerFofoInterest();
560
		retailerInterest.setCity(user.getCity());
561
		retailerInterest.setMobile(user.getMobileNumber());
562
		retailerInterest.setPinCode(user.getPinCode());
563
		retailerInterest.setUserId(userInfo.getUserId());
564
		retailerInterest.setPinCode(user.getPinCode());
565
		retailerInterest.setInterestShownOn(new Date());
566
		LOGGER.info("requested url : "+request.getRequestURL().toString());
567
		return responseSender.ok(mongoClient.saveRetailerInterestOnFofo(retailerInterest));
568
	}
569
 
570
	@RequestMapping(value = "/retailer/hasRetailerShownInterest", method=RequestMethod.GET)
571
	public ResponseEntity<?> getAllBrads(HttpServletRequest request) throws Throwable{
572
		UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
573
		LOGGER.info("requested url : "+request.getRequestURL().toString());
574
		return responseSender.ok(mongoClient.hasRetailerShownInterest(userInfo.getUserId()));
575
	}
21426 ashik.ali 576
 
21292 ashik.ali 577
}