Subversion Repositories SmartDukaan

Rev

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