Subversion Repositories SmartDukaan

Rev

Rev 25277 | Rev 26131 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22981 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
23494 ashik.ali 3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
24349 amit.gupta 6
import java.time.LocalDate;
24159 tejbeer 7
import java.time.LocalDateTime;
24124 govind 8
import java.util.ArrayList;
25276 amit.gupta 9
import java.util.Arrays;
24349 amit.gupta 10
import java.util.HashMap;
24124 govind 11
import java.util.List;
22981 ashik.ali 12
import java.util.Map;
24986 tejbeer 13
import java.util.Set;
24159 tejbeer 14
import java.util.stream.Collectors;
22981 ashik.ali 15
 
16
import javax.servlet.http.HttpServletRequest;
25276 amit.gupta 17
import javax.transaction.Transactional;
22981 ashik.ali 18
 
24159 tejbeer 19
import org.apache.logging.log4j.LogManager;
23955 govind 20
import org.apache.logging.log4j.Logger;
24159 tejbeer 21
import org.json.JSONObject;
22981 ashik.ali 22
import org.springframework.beans.factory.annotation.Autowired;
24159 tejbeer 23
import org.springframework.beans.factory.annotation.Qualifier;
23494 ashik.ali 24
import org.springframework.core.io.InputStreamResource;
25
import org.springframework.http.HttpHeaders;
26
import org.springframework.http.HttpStatus;
23330 ashik.ali 27
import org.springframework.http.ResponseEntity;
22981 ashik.ali 28
import org.springframework.stereotype.Controller;
29
import org.springframework.ui.Model;
24124 govind 30
import org.springframework.web.bind.annotation.GetMapping;
31
import org.springframework.web.bind.annotation.PostMapping;
23026 ashik.ali 32
import org.springframework.web.bind.annotation.RequestBody;
22981 ashik.ali 33
import org.springframework.web.bind.annotation.RequestMapping;
34
import org.springframework.web.bind.annotation.RequestMethod;
35
import org.springframework.web.bind.annotation.RequestParam;
36
 
23494 ashik.ali 37
import com.spice.profitmandi.common.enumuration.ContentType;
23955 govind 38
import com.spice.profitmandi.common.enumuration.CounterSize;
22981 ashik.ali 39
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24123 tejbeer 40
import com.spice.profitmandi.common.model.AddLocationModel;
24124 govind 41
import com.spice.profitmandi.common.model.CustomRetailer;
22981 ashik.ali 42
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24159 tejbeer 43
import com.spice.profitmandi.common.model.PromoterDetailModel;
23026 ashik.ali 44
import com.spice.profitmandi.common.model.UpdateRetailerRequest;
23330 ashik.ali 45
import com.spice.profitmandi.common.web.util.ResponseSender;
23494 ashik.ali 46
import com.spice.profitmandi.dao.entity.dtr.Document;
47
import com.spice.profitmandi.dao.entity.dtr.Retailer;
48
import com.spice.profitmandi.dao.entity.dtr.Shop;
24124 govind 49
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
24159 tejbeer 50
import com.spice.profitmandi.dao.entity.user.Location;
51
import com.spice.profitmandi.dao.entity.user.Promoter;
24123 tejbeer 52
import com.spice.profitmandi.dao.entity.user.User;
23494 ashik.ali 53
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
24124 govind 54
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24159 tejbeer 55
import com.spice.profitmandi.dao.repository.dtr.Mongo;
23494 ashik.ali 56
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
57
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
24123 tejbeer 58
import com.spice.profitmandi.dao.repository.user.LocationRepository;
24159 tejbeer 59
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
24123 tejbeer 60
import com.spice.profitmandi.dao.repository.user.UserRepository;
24349 amit.gupta 61
import com.spice.profitmandi.service.PartnerInvestmentService;
24159 tejbeer 62
import com.spice.profitmandi.service.inventory.InventoryService;
22981 ashik.ali 63
import com.spice.profitmandi.service.user.RetailerService;
25276 amit.gupta 64
import com.spice.profitmandi.web.model.LoginDetails;
65
import com.spice.profitmandi.web.util.CookiesProcessor;
24123 tejbeer 66
import com.spice.profitmandi.web.util.MVCResponseSender;
22981 ashik.ali 67
 
68
@Controller
25277 amit.gupta 69
@Transactional(rollbackOn = Throwable.class)
22981 ashik.ali 70
public class RetailerController {
71
 
23568 govind 72
	private static final Logger LOGGER = LogManager.getLogger(RetailerController.class);
24124 govind 73
 
22981 ashik.ali 74
	@Autowired
75
	private RetailerService retailerService;
24124 govind 76
 
23330 ashik.ali 77
	@Autowired
23494 ashik.ali 78
	private RetailerRepository retailerRepository;
24124 govind 79
 
23494 ashik.ali 80
	@Autowired
81
	private ShopRepository shopRepository;
24124 govind 82
 
23494 ashik.ali 83
	@Autowired
24124 govind 84
	private FofoStoreRepository fofoStoreRepository;
85
 
86
	@Autowired
23494 ashik.ali 87
	private DocumentRepository documentRepository;
24124 govind 88
 
23494 ashik.ali 89
	@Autowired
25276 amit.gupta 90
	private CookiesProcessor cookiesProcessor;
91
 
92
	@Autowired
24159 tejbeer 93
	@Qualifier("userUserRepository")
24123 tejbeer 94
	private UserRepository userRepository;
24124 govind 95
 
24123 tejbeer 96
	@Autowired
97
	private LocationRepository locationRepository;
24124 govind 98
 
24123 tejbeer 99
	@Autowired
23330 ashik.ali 100
	private ResponseSender<?> responseSender;
24124 govind 101
 
24123 tejbeer 102
	@Autowired
103
	private MVCResponseSender mvcResponseSender;
24124 govind 104
 
24159 tejbeer 105
	@Autowired
106
	private InventoryService inventoryService;
107
 
108
	@Autowired
109
	private PromoterRepository promoterRepository;
110
 
111
	@Autowired
24349 amit.gupta 112
	private PartnerInvestmentService partnerInvestmentService;
113
 
114
	@Autowired
24159 tejbeer 115
	private Mongo mongoClient;
116
 
23784 ashik.ali 117
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.GET)
24124 govind 118
	public String retailerInfoByEmailIdOrMobileNumber(HttpServletRequest request,
119
			@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
120
			Model model) throws ProfitMandiBusinessException {
121
		LOGGER.info("Request Received at url {} with emailIdOrMobileNumber {}", request.getRequestURI(),
122
				emailIdOrMobileNumber);
22981 ashik.ali 123
		Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
124
		model.addAllAttributes(map);
23955 govind 125
		model.addAttribute("counterSizes", CounterSize.values());
24159 tejbeer 126
		if (map.containsKey("retailer")) {
127
			Retailer retailer = (Retailer) map.get("retailer");
128
			User user = userRepository.selectById(retailer.getId());
129
			if (user.getLocation() != null) {
130
				Location location = locationRepository.selectById(user.getLocation());
131
				model.addAttribute("locationdetail", location);
132
				LOGGER.info("location" + location);
133
			}
24123 tejbeer 134
		}
22981 ashik.ali 135
		return "retailer-details";
136
	}
24124 govind 137
 
23026 ashik.ali 138
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
24124 govind 139
	public String updateRetailerDetails(HttpServletRequest request,
140
			@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {
23026 ashik.ali 141
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
142
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
143
		model.addAllAttributes(map);
23955 govind 144
		model.addAttribute("counterSizes", CounterSize.values());
23026 ashik.ali 145
		return "retailer-details";
146
	}
22981 ashik.ali 147
 
148
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
24124 govind 149
	public String retailerInfo(HttpServletRequest request) throws Exception {
22981 ashik.ali 150
		return "retailer-info";
151
	}
24124 govind 152
 
23330 ashik.ali 153
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
24124 govind 154
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {
23330 ashik.ali 155
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
156
	}
24124 govind 157
 
23494 ashik.ali 158
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
24124 govind 159
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
160
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,
161
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 162
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 163
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 164
 
165
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 166
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
167
		}
24124 govind 168
		if (retailer.getDocumentId() != documentId) {
23494 ashik.ali 169
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
170
		}
171
		return responseSender.ok(document);
172
	}
24124 govind 173
 
23494 ashik.ali 174
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
24124 govind 175
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,
176
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
177
			throws ProfitMandiBusinessException {
178
 
23499 ashik.ali 179
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 180
 
181
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 182
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
183
		}
24124 govind 184
 
23494 ashik.ali 185
		Document document = documentRepository.selectById(retailer.getDocumentId());
24124 govind 186
 
23494 ashik.ali 187
		FileInputStream file = null;
188
		try {
189
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
190
		} catch (FileNotFoundException e) {
191
			LOGGER.error("Retailer Document file not found : ", e);
192
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
193
		}
24159 tejbeer 194
		// ByteArrayOutputStream byteArrayOutputStream = new
195
		// ByteArrayOutputStream();
24124 govind 196
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
197
 
198
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 199
		String contentType = "";
24124 govind 200
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 201
			contentType = "image/jpeg";
24124 govind 202
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 203
			contentType = "image/png";
24124 govind 204
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 205
			contentType = "application/pdf";
206
		}
24124 govind 207
		headers.set("Content-Type", contentType);
208
		headers.set("Content-disposition", "inline; filename=" + document.getName());
209
		headers.setContentLength(document.getSize());
210
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
211
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
212
 
24159 tejbeer 213
		// return
214
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 215
	}
24124 govind 216
 
23494 ashik.ali 217
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
24124 govind 218
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,
219
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
220
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 221
		Shop shop = shopRepository.selectById(shopId);
24124 govind 222
 
223
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 224
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
225
		}
24124 govind 226
 
227
		if (shop.getDocumentId() == null) {
23494 ashik.ali 228
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
229
		}
24124 govind 230
 
23494 ashik.ali 231
		Document document = documentRepository.selectById(shop.getDocumentId());
232
		return responseSender.ok(document);
233
	}
24124 govind 234
 
23494 ashik.ali 235
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
24124 govind 236
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,
237
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
238
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
239
			throws ProfitMandiBusinessException {
240
 
23494 ashik.ali 241
		Shop shop = shopRepository.selectById(shopId);
24124 govind 242
 
243
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 244
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
245
		}
24124 govind 246
 
247
		if (shop.getDocumentId() == null) {
23494 ashik.ali 248
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
249
		}
24124 govind 250
 
23494 ashik.ali 251
		Document document = documentRepository.selectById(shop.getDocumentId());
24124 govind 252
 
23494 ashik.ali 253
		FileInputStream file = null;
254
		try {
255
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
256
		} catch (FileNotFoundException e) {
257
			LOGGER.error("Retailer Document file not found : ", e);
258
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
259
		}
24159 tejbeer 260
		// ByteArrayOutputStream byteArrayOutputStream = new
261
		// ByteArrayOutputStream();
24124 govind 262
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
263
 
264
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 265
		String contentType = "";
24124 govind 266
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 267
			contentType = "image/jpeg";
24124 govind 268
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 269
			contentType = "image/png";
24124 govind 270
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 271
			contentType = "application/pdf";
272
		}
24124 govind 273
		headers.set("Content-Type", contentType);
274
		headers.set("Content-disposition", "inline; filename=" + document.getName());
275
		headers.setContentLength(document.getSize());
276
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
277
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
278
 
24159 tejbeer 279
		// return
280
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 281
	}
24124 govind 282
 
283
	@GetMapping(value = "/getAllStores")
284
	public String getAllStores(HttpServletRequest request, Model model) {
285
 
25276 amit.gupta 286
		List<FofoStore> fofoStores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive())
287
				.collect(Collectors.toList());
24349 amit.gupta 288
		Map<Integer, Boolean> investments = new HashMap<>();
24124 govind 289
 
24349 amit.gupta 290
		for (FofoStore fofoStore : fofoStores) {
291
			boolean isOk = false;
292
			if (fofoStore.getGraceDate() != null && fofoStore.getGraceDate().isAfter(LocalDate.now())) {
293
				isOk = true;
294
			} else {
295
				try {
25276 amit.gupta 296
					isOk = partnerInvestmentService.isInvestmentOk(fofoStore.getId(), 10,
297
							ProfitMandiConstants.CUTOFF_INVESTMENT);
298
					if (fofoStore.getGraceCount() > 0) {
24349 amit.gupta 299
						fofoStore.setGraceDate(null);
300
						fofoStore.setGraceCount(0);
301
						fofoStoreRepository.persist(fofoStore);
302
					}
303
				} catch (ProfitMandiBusinessException e) {
304
				}
305
			}
306
			investments.put(fofoStore.getId(), isOk);
307
		}
24124 govind 308
 
24349 amit.gupta 309
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers();
24124 govind 310
		model.addAttribute("fofoStores", fofoStores);
24349 amit.gupta 311
		model.addAttribute("investments", investments);
24124 govind 312
		model.addAttribute("customRetailers", customRetailers);
313
 
314
		return "store";
315
 
316
	}
317
 
24349 amit.gupta 318
	@GetMapping(value = "/partners")
319
	public String getStoreName(HttpServletRequest request, Model model, @RequestParam String query) throws Exception {
320
		model.addAttribute("response", mvcResponseSender
321
				.createResponseString(retailerService.getFofoRetailers().values().stream().filter(x -> {
322
					return x.getDisplayName().toLowerCase().matches(".*?" + query.toLowerCase() + ".*?");
323
				}).collect(Collectors.toList())));
324
		return "response";
325
	}
326
 
24124 govind 327
	@PostMapping(value = "/deactivateStore")
328
	public String deActivateStore(HttpServletRequest request,
329
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
330
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 331
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
332
		if (!Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com").contains(loginDetails.getEmailId())) {
333
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access", "You are not authorise to deactivate retailer");
334
		}
24843 govind 335
		fofoStore.setActive(false);
336
		fofoStoreRepository.persist(fofoStore);
24124 govind 337
		LOGGER.info("inserted into InActiveFofoStore successfully");
338
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
339
 
340
		return "response";
341
 
342
	}
25276 amit.gupta 343
 
344
	@GetMapping(value = "/getAllInactiveStores")
345
	public String getInactiveStores(HttpServletRequest request, Model model) {
346
		List<FofoStore> inActiveFofoStores = fofoStoreRepository.selectInActiveStore();
347
		Map<Integer, CustomRetailer> customRetailers = retailerService
348
				.getFofoRetailers(inActiveFofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
24680 govind 349
		model.addAttribute("inActiveFofoStores", inActiveFofoStores);
350
		model.addAttribute("customRetailers", customRetailers);
351
		return "inactive-stores";
352
	}
25276 amit.gupta 353
 
354
	// Extend billing for 2 days
24349 amit.gupta 355
	@PostMapping(value = "/extendBilling")
356
	public String extendBilling(HttpServletRequest request,
357
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
358
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 359
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
26030 amit.gupta 360
		if (Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "prakash.rai@smartdukaan.com").contains(loginDetails.getEmailId())) {
25276 amit.gupta 361
			fofoStore.setGraceDate(LocalDate.now().plusDays(2));
362
			fofoStore.setGraceCount(fofoStore.getGraceCount() + 1);
363
			fofoStoreRepository.persist(fofoStore);
364
			model.addAttribute("response", mvcResponseSender.createResponseString(fofoStore.getGraceCount()));
365
		} else {
366
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access", "You are not authorise to extend billing");
367
		}
24349 amit.gupta 368
		return "response";
25276 amit.gupta 369
 
24124 govind 370
	}
371
 
24159 tejbeer 372
	@RequestMapping(value = "/getPromoterInfo", method = RequestMethod.GET)
373
	public String getPromoterInfo(HttpServletRequest request,
374
			@RequestParam(name = "offset", defaultValue = "0") int offset,
375
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
376
		LOGGER.info("requested url : " + request.getRequestURL().toString());
377
		List<Promoter> promoterInfo = null;
378
 
379
		long size = 0;
380
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
381
				.collect(Collectors.toList());
382
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
383
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
24986 tejbeer 384
		Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);
385
		brands.add("Airtel");
386
		brands.add("Vodafone");
387
		brands.add("Idea");
25276 amit.gupta 388
		LOGGER.info("brands" + brands);
24159 tejbeer 389
		model.addAttribute("customRetailers", customRetailers);
25276 amit.gupta 390
		model.addAttribute("brands", brands);
24159 tejbeer 391
		promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
392
 
393
		size = promoterRepository.selectAllCount();
394
 
395
		LOGGER.info("promoterInfo" + promoterInfo);
396
		if (!promoterInfo.isEmpty()) {
397
			List<Integer> partnerIds = new ArrayList<>();
398
 
399
			for (Promoter promoterdetail : promoterInfo) {
400
				partnerIds.add(promoterdetail.getRetailerId());
401
			}
402
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 403
			if (!partnerIds.equals(null)) {
24159 tejbeer 404
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 405
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 406
			}
24349 amit.gupta 407
 
24159 tejbeer 408
			LOGGER.info("partnerIds" + partnersMap);
409
			model.addAttribute("promoterInfo", promoterInfo);
410
			model.addAttribute("partnersMap", partnersMap);
411
			model.addAttribute("start", offset + 1);
412
			model.addAttribute("size", size);
413
			model.addAttribute("url", "/getPaginatedPromoterInfo");
414
 
415
			if (promoterInfo.size() < limit) {
416
				model.addAttribute("end", offset + promoterInfo.size());
417
			} else {
418
				model.addAttribute("end", offset + limit);
419
			}
420
		} else {
421
			model.addAttribute("promoterInfo", promoterInfo);
422
			model.addAttribute("size", size);
423
		}
424
		return "promoter-info";
425
 
426
	}
24349 amit.gupta 427
 
24159 tejbeer 428
	@RequestMapping(value = "/getPaginatedPromoterInfo", method = RequestMethod.GET)
429
	public String getPaginatedPromoterInfo(HttpServletRequest request,
430
			@RequestParam(name = "offset", defaultValue = "0") int offset,
431
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
432
			throws ProfitMandiBusinessException {
433
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
434
		List<Promoter> promoterInfo = null;
435
		promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
436
 
437
		LOGGER.info("promoterInfo" + promoterInfo);
438
		if (!promoterInfo.isEmpty()) {
439
			List<Integer> partnerIds = new ArrayList<>();
440
 
441
			for (Promoter promoterdetail : promoterInfo) {
442
				partnerIds.add(promoterdetail.getRetailerId());
443
			}
444
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 445
			if (!partnerIds.equals(null)) {
24159 tejbeer 446
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 447
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 448
			}
24349 amit.gupta 449
 
24159 tejbeer 450
			LOGGER.info("partnerIds" + partnersMap);
451
			model.addAttribute("promoterInfo", promoterInfo);
452
			model.addAttribute("partnersMap", partnersMap);
453
			model.addAttribute("url", "/getPaginatedPromoterInfo");
454
		} else {
455
			model.addAttribute("promoterInfo", promoterInfo);
456
 
457
		}
458
 
459
		return "promoter-info-paginated";
460
	}
461
 
462
	@RequestMapping(value = "/createPromoter", method = RequestMethod.POST)
463
	public String createPromoter(HttpServletRequest request, @RequestBody PromoterDetailModel promoterdetailModel,
464
			Model model) throws Exception {
465
		LOGGER.info("requested url : " + request.getRequestURL().toString());
466
		LOGGER.info("requested url : " + promoterdetailModel);
24349 amit.gupta 467
		if (promoterdetailModel.getId() == 0) {
24159 tejbeer 468
			Promoter promoter = new Promoter();
469
			promoter.setName(promoterdetailModel.getName());
470
			promoter.setBrand(promoterdetailModel.getBrand());
471
			promoter.setEmail(promoterdetailModel.getEmail());
472
			promoter.setMobile(promoterdetailModel.getMobile());
473
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
474
			promoter.setCreatedTimestamp(LocalDateTime.now());
24429 tejbeer 475
			promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
24159 tejbeer 476
			promoterRepository.persist(promoter);
477
		} else {
478
			Promoter updatePromoter = promoterRepository.selectById(promoterdetailModel.getId());
479
			updatePromoter.setRetailerId(promoterdetailModel.getRetailerId());
24429 tejbeer 480
			updatePromoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
25276 amit.gupta 481
			LOGGER.info("sdport" + promoterdetailModel.isSdPortalAccess());
482
			LOGGER.info("sdport" + updatePromoter);
24159 tejbeer 483
			promoterRepository.persist(updatePromoter);
25276 amit.gupta 484
 
24159 tejbeer 485
		}
486
 
487
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
488
 
489
		return "response";
490
 
491
	}
492
 
493
	@RequestMapping(value = "/removePromoterfromPartnerStore", method = RequestMethod.POST)
494
	public String removePromoterfromPartnerStore(HttpServletRequest request,
495
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
496
 
497
		Promoter promoter = promoterRepository.selectById(id);
498
 
499
		promoter.setRetailerId(0);
500
		promoterRepository.persist(promoter);
501
 
502
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
503
 
504
		return "response";
505
	}
506
 
507
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
508
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
509
			throws Exception {
510
 
511
		Location location = new Location();
512
		location.setName(addLocationModel.getName());
513
		location.setLine1(addLocationModel.getLine1());
514
		location.setLine2(addLocationModel.getLine2());
515
		location.setCity(addLocationModel.getCity());
516
		location.setState(addLocationModel.getState());
517
		location.setPin(addLocationModel.getPin());
518
 
519
		LOGGER.info("PostLocation" + location);
520
		locationRepository.persist(location);
521
 
522
		User user = userRepository.selectById(addLocationModel.getUserId());
523
		user.setLocation(location.getId());
524
		userRepository.persist(user);
525
 
526
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
527
		return "response";
528
 
529
	}
530
 
531
	@RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
532
	public String updateLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel,
533
			Model model) throws Exception {
534
 
535
		Location userlocation = locationRepository.selectById(addLocationModel.getUserId());
536
 
537
		userlocation.setName(addLocationModel.getName());
538
		userlocation.setCity(addLocationModel.getCity());
539
		userlocation.setLine1(addLocationModel.getLine1());
540
		userlocation.setLine2(addLocationModel.getLine2());
541
		userlocation.setPin(addLocationModel.getPin());
542
		userlocation.setState(addLocationModel.getState());
543
 
544
		locationRepository.persist(userlocation);
545
 
546
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
547
		return "response";
548
 
549
	}
550
 
24124 govind 551
}