Subversion Repositories SmartDukaan

Rev

Rev 26601 | Rev 26770 | 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);
26769 amit.gupta 123
		try {
124
 
125
			Map<String, Object> map = retailerService.getByEmailIdOrMobileNumber(emailIdOrMobileNumber);
126
			model.addAllAttributes(map);
127
		} catch(Exception e) {
128
			//Do nothing
129
		}
23955 govind 130
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 131
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
132
		LOGGER.info("warehouseMap", warehouseMap);
133
		model.addAttribute("warehouses", warehouseMap);
24159 tejbeer 134
		if (map.containsKey("retailer")) {
135
			Retailer retailer = (Retailer) map.get("retailer");
136
			User user = userRepository.selectById(retailer.getId());
137
			if (user.getLocation() != null) {
138
				Location location = locationRepository.selectById(user.getLocation());
139
				model.addAttribute("locationdetail", location);
140
				LOGGER.info("location" + location);
141
			}
24123 tejbeer 142
		}
22981 ashik.ali 143
		return "retailer-details";
144
	}
24124 govind 145
 
23026 ashik.ali 146
	@RequestMapping(value = "/retailerDetails", method = RequestMethod.PUT)
24124 govind 147
	public String updateRetailerDetails(HttpServletRequest request,
148
			@RequestBody UpdateRetailerRequest updateRetailerRequest, Model model) throws ProfitMandiBusinessException {
23026 ashik.ali 149
		LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), updateRetailerRequest);
150
		Map<String, Object> map = retailerService.updateRetailerDetails(updateRetailerRequest);
151
		model.addAllAttributes(map);
23955 govind 152
		model.addAttribute("counterSizes", CounterSize.values());
26209 tejbeer 153
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
154
		LOGGER.info("warehouseMap", warehouseMap);
155
		model.addAttribute("warehouses", warehouseMap);
23026 ashik.ali 156
		return "retailer-details";
157
	}
22981 ashik.ali 158
 
159
	@RequestMapping(value = "/retailerInfo", method = RequestMethod.GET)
24124 govind 160
	public String retailerInfo(HttpServletRequest request) throws Exception {
22981 ashik.ali 161
		return "retailer-info";
162
	}
24124 govind 163
 
23330 ashik.ali 164
	@RequestMapping(value = "/district/all/stateName", method = RequestMethod.GET)
24124 govind 165
	public ResponseEntity<?> getAllDistrict(@RequestParam(name = "stateName") String stateName) {
23330 ashik.ali 166
		return responseSender.ok(retailerService.getAllDistrictMaster(stateName));
167
	}
24124 govind 168
 
23494 ashik.ali 169
	@RequestMapping(value = "/retailerDocument/documentId", method = RequestMethod.GET)
24124 govind 170
	public ResponseEntity<?> retailerDocumentById(HttpServletRequest request,
171
			@RequestParam(name = ProfitMandiConstants.DOCUMENT_ID) int documentId,
172
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 173
		Document document = documentRepository.selectById(documentId);
23499 ashik.ali 174
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 175
 
176
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 177
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
178
		}
24124 govind 179
		if (retailer.getDocumentId() != documentId) {
23494 ashik.ali 180
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, documentId, "RTLR_1014");
181
		}
182
		return responseSender.ok(document);
183
	}
24124 govind 184
 
23494 ashik.ali 185
	@RequestMapping(value = "/retailerDocument/download", method = RequestMethod.GET)
24124 govind 186
	public ResponseEntity<?> downloadRetailerDocument(HttpServletRequest request,
187
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
188
			throws ProfitMandiBusinessException {
189
 
23499 ashik.ali 190
		Retailer retailer = retailerRepository.selectById(retailerId);
24124 govind 191
 
192
		if (retailer.getDocumentId() == null) {
23494 ashik.ali 193
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailer.getId(), "RTLR_1012");
194
		}
24124 govind 195
 
23494 ashik.ali 196
		Document document = documentRepository.selectById(retailer.getDocumentId());
24124 govind 197
 
23494 ashik.ali 198
		FileInputStream file = null;
199
		try {
200
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
201
		} catch (FileNotFoundException e) {
202
			LOGGER.error("Retailer Document file not found : ", e);
203
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
204
		}
24159 tejbeer 205
		// ByteArrayOutputStream byteArrayOutputStream = new
206
		// ByteArrayOutputStream();
24124 govind 207
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
208
 
209
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 210
		String contentType = "";
24124 govind 211
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 212
			contentType = "image/jpeg";
24124 govind 213
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 214
			contentType = "image/png";
24124 govind 215
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 216
			contentType = "application/pdf";
217
		}
24124 govind 218
		headers.set("Content-Type", contentType);
219
		headers.set("Content-disposition", "inline; filename=" + document.getName());
220
		headers.setContentLength(document.getSize());
221
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
222
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
223
 
24159 tejbeer 224
		// return
225
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 226
	}
24124 govind 227
 
23494 ashik.ali 228
	@RequestMapping(value = "/retailerShopDocument/shopId", method = RequestMethod.GET)
24124 govind 229
	public ResponseEntity<?> retailerShopDocumentById(HttpServletRequest request,
230
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
231
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId) throws ProfitMandiBusinessException {
23494 ashik.ali 232
		Shop shop = shopRepository.selectById(shopId);
24124 govind 233
 
234
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 235
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
236
		}
24124 govind 237
 
238
		if (shop.getDocumentId() == null) {
23494 ashik.ali 239
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
240
		}
24124 govind 241
 
23494 ashik.ali 242
		Document document = documentRepository.selectById(shop.getDocumentId());
243
		return responseSender.ok(document);
244
	}
24124 govind 245
 
23494 ashik.ali 246
	@RequestMapping(value = "/retailerShopDocument/download", method = RequestMethod.GET)
24124 govind 247
	public ResponseEntity<?> downloadRetailerShopDocument(HttpServletRequest request,
248
			@RequestParam(name = ProfitMandiConstants.SHOP_ID) int shopId,
249
			@RequestParam(name = ProfitMandiConstants.RETAILER_ID) int retailerId, Model model)
250
			throws ProfitMandiBusinessException {
251
 
23494 ashik.ali 252
		Shop shop = shopRepository.selectById(shopId);
24124 govind 253
 
254
		if (shop.getRetailerId() != retailerId) {
23494 ashik.ali 255
			throw new ProfitMandiBusinessException(ProfitMandiConstants.SHOP_ID, shop.getId(), "SHP_1004");
256
		}
24124 govind 257
 
258
		if (shop.getDocumentId() == null) {
23494 ashik.ali 259
			throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, shop.getId(), "SHP_1005");
260
		}
24124 govind 261
 
23494 ashik.ali 262
		Document document = documentRepository.selectById(shop.getDocumentId());
24124 govind 263
 
23494 ashik.ali 264
		FileInputStream file = null;
265
		try {
266
			file = new FileInputStream(document.getPath() + File.separator + document.getName());
267
		} catch (FileNotFoundException e) {
268
			LOGGER.error("Retailer Document file not found : ", e);
269
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "RTLR_1013");
270
		}
24159 tejbeer 271
		// ByteArrayOutputStream byteArrayOutputStream = new
272
		// ByteArrayOutputStream();
24124 govind 273
		// ExcelUtils.writeSchemeModels(schemeModels, byteArrayOutputStream);
274
 
275
		final HttpHeaders headers = new HttpHeaders();
23494 ashik.ali 276
		String contentType = "";
24124 govind 277
		if (document.getContentType() == ContentType.JPEG) {
23494 ashik.ali 278
			contentType = "image/jpeg";
24124 govind 279
		} else if (document.getContentType() == ContentType.PNG) {
23494 ashik.ali 280
			contentType = "image/png";
24124 govind 281
		} else if (document.getContentType() == ContentType.PDF) {
23494 ashik.ali 282
			contentType = "application/pdf";
283
		}
24124 govind 284
		headers.set("Content-Type", contentType);
285
		headers.set("Content-disposition", "inline; filename=" + document.getName());
286
		headers.setContentLength(document.getSize());
287
		final InputStreamResource inputStreamResource = new InputStreamResource(file);
288
		return new ResponseEntity<InputStreamResource>(inputStreamResource, headers, HttpStatus.OK);
289
 
24159 tejbeer 290
		// return
291
		// responseSender.ok(ResponseCodeHolder.getMessage("ITM_AGNG_OK_1000"));
23494 ashik.ali 292
	}
24124 govind 293
 
294
	@GetMapping(value = "/getAllStores")
295
	public String getAllStores(HttpServletRequest request, Model model) {
296
 
25276 amit.gupta 297
		List<FofoStore> fofoStores = fofoStoreRepository.selectAll().stream().filter(x -> x.isActive())
298
				.collect(Collectors.toList());
24349 amit.gupta 299
		Map<Integer, Boolean> investments = new HashMap<>();
24124 govind 300
 
24349 amit.gupta 301
		for (FofoStore fofoStore : fofoStores) {
302
			boolean isOk = false;
303
			if (fofoStore.getGraceDate() != null && fofoStore.getGraceDate().isAfter(LocalDate.now())) {
304
				isOk = true;
305
			} else {
306
				try {
25276 amit.gupta 307
					isOk = partnerInvestmentService.isInvestmentOk(fofoStore.getId(), 10,
308
							ProfitMandiConstants.CUTOFF_INVESTMENT);
309
					if (fofoStore.getGraceCount() > 0) {
24349 amit.gupta 310
						fofoStore.setGraceDate(null);
311
						fofoStore.setGraceCount(0);
312
						fofoStoreRepository.persist(fofoStore);
313
					}
314
				} catch (ProfitMandiBusinessException e) {
315
				}
316
			}
317
			investments.put(fofoStore.getId(), isOk);
318
		}
24124 govind 319
 
24349 amit.gupta 320
		Map<Integer, CustomRetailer> customRetailers = retailerService.getFofoRetailers();
24124 govind 321
		model.addAttribute("fofoStores", fofoStores);
24349 amit.gupta 322
		model.addAttribute("investments", investments);
24124 govind 323
		model.addAttribute("customRetailers", customRetailers);
324
 
325
		return "store";
326
 
327
	}
328
 
24349 amit.gupta 329
	@GetMapping(value = "/partners")
330
	public String getStoreName(HttpServletRequest request, Model model, @RequestParam String query) throws Exception {
331
		model.addAttribute("response", mvcResponseSender
332
				.createResponseString(retailerService.getFofoRetailers().values().stream().filter(x -> {
333
					return x.getDisplayName().toLowerCase().matches(".*?" + query.toLowerCase() + ".*?");
334
				}).collect(Collectors.toList())));
335
		return "response";
336
	}
337
 
24124 govind 338
	@PostMapping(value = "/deactivateStore")
339
	public String deActivateStore(HttpServletRequest request,
340
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
341
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 342
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
26131 tejbeer 343
		if (!Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com")
344
				.contains(loginDetails.getEmailId())) {
345
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
346
					"You are not authorise to deactivate retailer");
25276 amit.gupta 347
		}
24843 govind 348
		fofoStore.setActive(false);
349
		fofoStoreRepository.persist(fofoStore);
24124 govind 350
		LOGGER.info("inserted into InActiveFofoStore successfully");
351
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
352
 
353
		return "response";
354
 
355
	}
25276 amit.gupta 356
 
357
	@GetMapping(value = "/getAllInactiveStores")
358
	public String getInactiveStores(HttpServletRequest request, Model model) {
359
		List<FofoStore> inActiveFofoStores = fofoStoreRepository.selectInActiveStore();
360
		Map<Integer, CustomRetailer> customRetailers = retailerService
361
				.getFofoRetailers(inActiveFofoStores.stream().map(x -> x.getId()).collect(Collectors.toList()));
24680 govind 362
		model.addAttribute("inActiveFofoStores", inActiveFofoStores);
363
		model.addAttribute("customRetailers", customRetailers);
364
		return "inactive-stores";
365
	}
25276 amit.gupta 366
 
367
	// Extend billing for 2 days
24349 amit.gupta 368
	@PostMapping(value = "/extendBilling")
369
	public String extendBilling(HttpServletRequest request,
370
			@RequestParam(name = ProfitMandiConstants.FOFO_ID) int fofoId, Model model) throws Exception {
371
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
25276 amit.gupta 372
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
26601 amit.gupta 373
		if (Arrays.asList("kamini.sharma@smartdukaan.com", "tarun.verma@smartdukaan.com", "prakash.rai@smartdukaan.com", "amit.gupta@shop2020.in")
26131 tejbeer 374
				.contains(loginDetails.getEmailId())) {
25276 amit.gupta 375
			fofoStore.setGraceDate(LocalDate.now().plusDays(2));
376
			fofoStore.setGraceCount(fofoStore.getGraceCount() + 1);
377
			fofoStoreRepository.persist(fofoStore);
378
			model.addAttribute("response", mvcResponseSender.createResponseString(fofoStore.getGraceCount()));
379
		} else {
26131 tejbeer 380
			throw new ProfitMandiBusinessException("Access Denied", "Unauthorised Access",
381
					"You are not authorise to extend billing");
25276 amit.gupta 382
		}
24349 amit.gupta 383
		return "response";
25276 amit.gupta 384
 
24124 govind 385
	}
386
 
24159 tejbeer 387
	@RequestMapping(value = "/getPromoterInfo", method = RequestMethod.GET)
388
	public String getPromoterInfo(HttpServletRequest request,
389
			@RequestParam(name = "offset", defaultValue = "0") int offset,
390
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws Exception {
391
		LOGGER.info("requested url : " + request.getRequestURL().toString());
392
		List<Promoter> promoterInfo = null;
393
 
394
		long size = 0;
395
		List<Integer> fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
396
				.collect(Collectors.toList());
397
		Map<Integer, CustomRetailer> customRetailersMap = retailerService.getFofoRetailers(fofoIds);
398
		String customRetailers = JSONObject.valueToString(customRetailersMap.values());
24986 tejbeer 399
		Set<String> brands = inventoryService.getAllTagListingBrands(ProfitMandiConstants.MOBILE_CATEGORY_ID);
400
		brands.add("Airtel");
401
		brands.add("Vodafone");
402
		brands.add("Idea");
25276 amit.gupta 403
		LOGGER.info("brands" + brands);
24159 tejbeer 404
		model.addAttribute("customRetailers", customRetailers);
25276 amit.gupta 405
		model.addAttribute("brands", brands);
24159 tejbeer 406
		promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
407
 
408
		size = promoterRepository.selectAllCount();
409
 
410
		LOGGER.info("promoterInfo" + promoterInfo);
411
		if (!promoterInfo.isEmpty()) {
412
			List<Integer> partnerIds = new ArrayList<>();
413
 
414
			for (Promoter promoterdetail : promoterInfo) {
415
				partnerIds.add(promoterdetail.getRetailerId());
416
			}
417
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 418
			if (!partnerIds.equals(null)) {
24159 tejbeer 419
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 420
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 421
			}
24349 amit.gupta 422
 
24159 tejbeer 423
			LOGGER.info("partnerIds" + partnersMap);
424
			model.addAttribute("promoterInfo", promoterInfo);
425
			model.addAttribute("partnersMap", partnersMap);
426
			model.addAttribute("start", offset + 1);
427
			model.addAttribute("size", size);
428
			model.addAttribute("url", "/getPaginatedPromoterInfo");
429
 
430
			if (promoterInfo.size() < limit) {
431
				model.addAttribute("end", offset + promoterInfo.size());
432
			} else {
433
				model.addAttribute("end", offset + limit);
434
			}
435
		} else {
436
			model.addAttribute("promoterInfo", promoterInfo);
437
			model.addAttribute("size", size);
438
		}
439
		return "promoter-info";
440
 
441
	}
24349 amit.gupta 442
 
24159 tejbeer 443
	@RequestMapping(value = "/getPaginatedPromoterInfo", method = RequestMethod.GET)
444
	public String getPaginatedPromoterInfo(HttpServletRequest request,
445
			@RequestParam(name = "offset", defaultValue = "0") int offset,
446
			@RequestParam(name = "limit", defaultValue = "10") int limit, Model model)
447
			throws ProfitMandiBusinessException {
448
		LOGGER.info("requested offset=[{}], limit = [{}]", offset, limit);
449
		List<Promoter> promoterInfo = null;
450
		promoterInfo = promoterRepository.selectAllPromoter(offset, limit);
451
 
452
		LOGGER.info("promoterInfo" + promoterInfo);
453
		if (!promoterInfo.isEmpty()) {
454
			List<Integer> partnerIds = new ArrayList<>();
455
 
456
			for (Promoter promoterdetail : promoterInfo) {
457
				partnerIds.add(promoterdetail.getRetailerId());
458
			}
459
			Map<Integer, CustomRetailer> partnersMap = null;
24349 amit.gupta 460
			if (!partnerIds.equals(null)) {
24159 tejbeer 461
				LOGGER.info("partnerIds" + partnerIds);
24349 amit.gupta 462
				partnersMap = retailerService.getFofoRetailers(partnerIds);
24159 tejbeer 463
			}
24349 amit.gupta 464
 
24159 tejbeer 465
			LOGGER.info("partnerIds" + partnersMap);
466
			model.addAttribute("promoterInfo", promoterInfo);
467
			model.addAttribute("partnersMap", partnersMap);
468
			model.addAttribute("url", "/getPaginatedPromoterInfo");
469
		} else {
470
			model.addAttribute("promoterInfo", promoterInfo);
471
 
472
		}
473
 
474
		return "promoter-info-paginated";
475
	}
476
 
477
	@RequestMapping(value = "/createPromoter", method = RequestMethod.POST)
478
	public String createPromoter(HttpServletRequest request, @RequestBody PromoterDetailModel promoterdetailModel,
479
			Model model) throws Exception {
480
		LOGGER.info("requested url : " + request.getRequestURL().toString());
481
		LOGGER.info("requested url : " + promoterdetailModel);
26131 tejbeer 482
		Promoter promoter = promoterRepository.selectById(promoterdetailModel.getId());
483
		if (promoter == null) {
484
			promoter = new Promoter();
24159 tejbeer 485
			promoter.setName(promoterdetailModel.getName());
486
			promoter.setBrand(promoterdetailModel.getBrand());
487
			promoter.setEmail(promoterdetailModel.getEmail());
488
			promoter.setMobile(promoterdetailModel.getMobile());
489
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
490
			promoter.setCreatedTimestamp(LocalDateTime.now());
26131 tejbeer 491
			promoter.setStatus(promoterdetailModel.isStatus());
24429 tejbeer 492
			promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
24159 tejbeer 493
			promoterRepository.persist(promoter);
494
		} else {
26131 tejbeer 495
 
496
			promoter.setRetailerId(promoterdetailModel.getRetailerId());
497
			promoter.setSdPortalAccess(promoterdetailModel.isSdPortalAccess());
26132 tejbeer 498
			promoter.setStatus(promoterdetailModel.isStatus());
499
			LOGGER.info("sdport" + promoterdetailModel.isStatus());
26131 tejbeer 500
			promoterRepository.persist(promoter);
25276 amit.gupta 501
 
24159 tejbeer 502
		}
503
 
504
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
505
 
506
		return "response";
507
 
508
	}
509
 
510
	@RequestMapping(value = "/removePromoterfromPartnerStore", method = RequestMethod.POST)
511
	public String removePromoterfromPartnerStore(HttpServletRequest request,
512
			@RequestParam(name = "id", defaultValue = "0") int id, Model model) throws Exception {
513
 
514
		Promoter promoter = promoterRepository.selectById(id);
515
 
26131 tejbeer 516
		promoter.setStatus(false);
24159 tejbeer 517
		promoterRepository.persist(promoter);
518
 
519
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
520
 
521
		return "response";
522
	}
523
 
524
	@RequestMapping(value = "/addLocation", method = RequestMethod.POST)
525
	public String addLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel, Model model)
526
			throws Exception {
527
 
528
		Location location = new Location();
529
		location.setName(addLocationModel.getName());
530
		location.setLine1(addLocationModel.getLine1());
531
		location.setLine2(addLocationModel.getLine2());
532
		location.setCity(addLocationModel.getCity());
533
		location.setState(addLocationModel.getState());
534
		location.setPin(addLocationModel.getPin());
535
 
536
		LOGGER.info("PostLocation" + location);
537
		locationRepository.persist(location);
538
 
539
		User user = userRepository.selectById(addLocationModel.getUserId());
540
		user.setLocation(location.getId());
541
		userRepository.persist(user);
542
 
543
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
544
		return "response";
545
 
546
	}
547
 
548
	@RequestMapping(value = "/updateLocation", method = RequestMethod.POST)
549
	public String updateLocation(HttpServletRequest request, @RequestBody AddLocationModel addLocationModel,
550
			Model model) throws Exception {
551
 
552
		Location userlocation = locationRepository.selectById(addLocationModel.getUserId());
553
 
554
		userlocation.setName(addLocationModel.getName());
555
		userlocation.setCity(addLocationModel.getCity());
556
		userlocation.setLine1(addLocationModel.getLine1());
557
		userlocation.setLine2(addLocationModel.getLine2());
558
		userlocation.setPin(addLocationModel.getPin());
559
		userlocation.setState(addLocationModel.getState());
560
 
561
		locationRepository.persist(userlocation);
562
 
563
		model.addAttribute("response", mvcResponseSender.createResponseString(true));
564
		return "response";
565
 
566
	}
567
 
24124 govind 568
}