Subversion Repositories SmartDukaan

Rev

Rev 26131 | Rev 26209 | 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);
26131 tejbeer 332
		if (!Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com")
333
				.contains(loginDetails.getEmailId())) {
334
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
335
					"You are not authorise to deactivate retailer");
25276 amit.gupta 336
		}
24843 govind 337
		fofoStore.setActive(false);
338
		fofoStoreRepository.persist(fofoStore);
24124 govind 339
		LOGGER.info("inserted into InActiveFofoStore successfully");
340
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
341
 
342
		return "response";
343
 
344
	}
25276 amit.gupta 345
 
346
	@GetMapping(value = "/getAllInactiveStores")
347
	public String getInactiveStores(HttpServletRequest request, Model model) {
348
		List<FofoStore> inActiveFofoStores = fofoStoreRepository.selectInActiveStore();
349
		Map<Integer, CustomRetailer> customRetailers = retailerService
350
				.getFofoRetailers(inActiveFofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
24680 govind 351
		model.addAttribute("inActiveFofoStores", inActiveFofoStores);
352
		model.addAttribute("customRetailers", customRetailers);
353
		return "inactive-stores";
354
	}
25276 amit.gupta 355
 
356
	// Extend billing for 2 days
24349 amit.gupta 357
	@PostMapping(value = "/extendBilling")
358
	public String extendBilling(HttpServletRequest request,
359
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
360
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 361
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
26131 tejbeer 362
		if (Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "prakash.rai@smartdukaan.com")
363
				.contains(loginDetails.getEmailId())) {
25276 amit.gupta 364
			fofoStore.setGraceDate(LocalDate.now().plusDays(2));
365
			fofoStore.setGraceCount(fofoStore.getGraceCount() + 1);
366
			fofoStoreRepository.persist(fofoStore);
367
			model.addAttribute("response", mvcResponseSender.createResponseString(fofoStore.getGraceCount()));
368
		} else {
26131 tejbeer 369
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
370
					"You are not authorise to extend billing");
25276 amit.gupta 371
		}
24349 amit.gupta 372
		return "response";
25276 amit.gupta 373
 
24124 govind 374
	}
375
 
24159 tejbeer 376
	@RequestMapping(value = "/getPromoterInfo", method = RequestMethod.GET)
377
	public String getPromoterInfo(HttpServletRequest request,
378
			@RequestParam(name = "offset", defaultValue = "0") int offset,
379
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
380
		LOGGER.info("requested url : " + request.getRequestURL().toString());
381
		List<Promoter> promoterInfo = null;
382
 
383
		long size = 0;
384
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
385
				.collect(Collectors.toList());
386
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
387
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
24986 tejbeer 388
		Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);
389
		brands.add("Airtel");
390
		brands.add("Vodafone");
391
		brands.add("Idea");
25276 amit.gupta 392
		LOGGER.info("brands" + brands);
24159 tejbeer 393
		model.addAttribute("customRetailers", customRetailers);
25276 amit.gupta 394
		model.addAttribute("brands", brands);
24159 tejbeer 395
		promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
396
 
397
		size = promoterRepository.selectAllCount();
398
 
399
		LOGGER.info("promoterInfo" + promoterInfo);
400
		if (!promoterInfo.isEmpty()) {
401
			List<Integer> partnerIds = new ArrayList<>();
402
 
403
			for (Promoter promoterdetail : promoterInfo) {
404
				partnerIds.add(promoterdetail.getRetailerId());
405
			}
406
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 407
			if (!partnerIds.equals(null)) {
24159 tejbeer 408
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 409
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 410
			}
24349 amit.gupta 411
 
24159 tejbeer 412
			LOGGER.info("partnerIds" + partnersMap);
413
			model.addAttribute("promoterInfo", promoterInfo);
414
			model.addAttribute("partnersMap", partnersMap);
415
			model.addAttribute("start", offset + 1);
416
			model.addAttribute("size", size);
417
			model.addAttribute("url", "/getPaginatedPromoterInfo");
418
 
419
			if (promoterInfo.size() < limit) {
420
				model.addAttribute("end", offset + promoterInfo.size());
421
			} else {
422
				model.addAttribute("end", offset + limit);
423
			}
424
		} else {
425
			model.addAttribute("promoterInfo", promoterInfo);
426
			model.addAttribute("size", size);
427
		}
428
		return "promoter-info";
429
 
430
	}
24349 amit.gupta 431
 
24159 tejbeer 432
	@RequestMapping(value = "/getPaginatedPromoterInfo", method = RequestMethod.GET)
433
	public String getPaginatedPromoterInfo(HttpServletRequest request,
434
			@RequestParam(name = "offset", defaultValue = "0") int offset,
435
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
436
			throws ProfitMandiBusinessException {
437
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
438
		List<Promoter> promoterInfo = null;
439
		promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
440
 
441
		LOGGER.info("promoterInfo" + promoterInfo);
442
		if (!promoterInfo.isEmpty()) {
443
			List<Integer> partnerIds = new ArrayList<>();
444
 
445
			for (Promoter promoterdetail : promoterInfo) {
446
				partnerIds.add(promoterdetail.getRetailerId());
447
			}
448
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 449
			if (!partnerIds.equals(null)) {
24159 tejbeer 450
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 451
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 452
			}
24349 amit.gupta 453
 
24159 tejbeer 454
			LOGGER.info("partnerIds" + partnersMap);
455
			model.addAttribute("promoterInfo", promoterInfo);
456
			model.addAttribute("partnersMap", partnersMap);
457
			model.addAttribute("url", "/getPaginatedPromoterInfo");
458
		} else {
459
			model.addAttribute("promoterInfo", promoterInfo);
460
 
461
		}
462
 
463
		return "promoter-info-paginated";
464
	}
465
 
466
	@RequestMapping(value = "/createPromoter", method = RequestMethod.POST)
467
	public String createPromoter(HttpServletRequest request, @RequestBody PromoterDetailModel promoterdetailModel,
468
			Model model) throws Exception {
469
		LOGGER.info("requested url : " + request.getRequestURL().toString());
470
		LOGGER.info("requested url : " + promoterdetailModel);
26131 tejbeer 471
		Promoter promoter = promoterRepository.selectById(promoterdetailModel.getId());
472
		if (promoter == null) {
473
			promoter = new Promoter();
24159 tejbeer 474
			promoter.setName(promoterdetailModel.getName());
475
			promoter.setBrand(promoterdetailModel.getBrand());
476
			promoter.setEmail(promoterdetailModel.getEmail());
477
			promoter.setMobile(promoterdetailModel.getMobile());
478
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
479
			promoter.setCreatedTimestamp(LocalDateTime.now());
26131 tejbeer 480
			promoter.setStatus(promoterdetailModel.isStatus());
24429 tejbeer 481
			promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
24159 tejbeer 482
			promoterRepository.persist(promoter);
483
		} else {
26131 tejbeer 484
 
485
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
486
			promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
26132 tejbeer 487
			promoter.setStatus(promoterdetailModel.isStatus());
488
			LOGGER.info("sdport" + promoterdetailModel.isStatus());
26131 tejbeer 489
			promoterRepository.persist(promoter);
25276 amit.gupta 490
 
24159 tejbeer 491
		}
492
 
493
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
494
 
495
		return "response";
496
 
497
	}
498
 
499
	@RequestMapping(value = "/removePromoterfromPartnerStore", method = RequestMethod.POST)
500
	public String removePromoterfromPartnerStore(HttpServletRequest request,
501
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
502
 
503
		Promoter promoter = promoterRepository.selectById(id);
504
 
26131 tejbeer 505
		promoter.setStatus(false);
24159 tejbeer 506
		promoterRepository.persist(promoter);
507
 
508
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
509
 
510
		return "response";
511
	}
512
 
513
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
514
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
515
			throws Exception {
516
 
517
		Location location = new Location();
518
		location.setName(addLocationModel.getName());
519
		location.setLine1(addLocationModel.getLine1());
520
		location.setLine2(addLocationModel.getLine2());
521
		location.setCity(addLocationModel.getCity());
522
		location.setState(addLocationModel.getState());
523
		location.setPin(addLocationModel.getPin());
524
 
525
		LOGGER.info("PostLocation" + location);
526
		locationRepository.persist(location);
527
 
528
		User user = userRepository.selectById(addLocationModel.getUserId());
529
		user.setLocation(location.getId());
530
		userRepository.persist(user);
531
 
532
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
533
		return "response";
534
 
535
	}
536
 
537
	@RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
538
	public String updateLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel,
539
			Model model) throws Exception {
540
 
541
		Location userlocation = locationRepository.selectById(addLocationModel.getUserId());
542
 
543
		userlocation.setName(addLocationModel.getName());
544
		userlocation.setCity(addLocationModel.getCity());
545
		userlocation.setLine1(addLocationModel.getLine1());
546
		userlocation.setLine2(addLocationModel.getLine2());
547
		userlocation.setPin(addLocationModel.getPin());
548
		userlocation.setState(addLocationModel.getState());
549
 
550
		locationRepository.persist(userlocation);
551
 
552
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
553
		return "response";
554
 
555
	}
556
 
24124 govind 557
}