Subversion Repositories SmartDukaan

Rev

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