Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
28468 tejbeer 1
package com.spice.profitmandi.service;
2
 
3
import com.google.gson.Gson;
4
import com.spice.profitmandi.common.enumuration.ReporticoProject;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30449 amit.gupta 6
import com.spice.profitmandi.common.model.*;
28468 tejbeer 7
import com.spice.profitmandi.dao.entity.auth.AuthUser;
8
import com.spice.profitmandi.dao.entity.auth.Menu;
9
import com.spice.profitmandi.dao.entity.cs.Position;
10
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
29165 manish 11
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
28468 tejbeer 12
import com.spice.profitmandi.dao.entity.inventory.ReporticoCacheTable;
13
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
14
import com.spice.profitmandi.dao.model.BrandWiseModel;
15
import com.spice.profitmandi.dao.model.PartnerDetailModel;
16
import com.spice.profitmandi.dao.model.WarehouseWiseStockModel;
17
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
18
import com.spice.profitmandi.dao.repository.auth.MenuCategoryRepository;
19
import com.spice.profitmandi.dao.repository.auth.MenuRepository;
20
import com.spice.profitmandi.dao.repository.cs.CsService;
21
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
22
import com.spice.profitmandi.dao.repository.cs.TicketRepository;
23
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
28825 tejbeer 24
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
28468 tejbeer 25
import com.spice.profitmandi.dao.repository.inventory.ReporticoCacheTableRepository;
26
import com.spice.profitmandi.dao.repository.inventory.SaholicInventoryCISRepository;
27
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
28
import com.spice.profitmandi.service.user.RetailerService;
30449 amit.gupta 29
import org.apache.logging.log4j.LogManager;
30
import org.apache.logging.log4j.Logger;
31
import org.springframework.beans.factory.annotation.Autowired;
32
import org.springframework.stereotype.Component;
33
import org.springframework.ui.Model;
28468 tejbeer 34
 
30449 amit.gupta 35
import java.io.FileInputStream;
36
import java.io.ObjectInputStream;
37
import java.time.LocalDate;
38
import java.time.LocalDateTime;
39
import java.time.Period;
40
import java.time.YearMonth;
41
import java.time.format.DateTimeFormatter;
42
import java.time.temporal.ChronoUnit;
43
import java.util.*;
44
import java.util.Map.Entry;
45
import java.util.stream.Collectors;
46
import java.util.stream.LongStream;
47
 
28468 tejbeer 48
@Component
49
public class AdminUser {
50
	@Autowired
51
	private Gson gson;
52
 
53
	@Autowired
54
	private OrderRepository orderRepository;
55
 
56
	@Autowired
57
	TicketRepository ticketRepository;
58
 
59
	@Autowired
60
	private ChartService chartService;
61
 
62
	@Autowired
63
	RetailerService retailerService;
64
 
65
	@Autowired
66
	AuthRepository authRepository;
67
 
68
	@Autowired
69
	FofoStoreRepository fofoStoreRepository;
70
 
71
	@Autowired
72
	CsService csService;
73
 
74
	@Autowired
75
	private MenuRepository menuRepository;
76
 
77
	@Autowired
78
	private PositionRepository positionRepository;
79
 
80
	@Autowired
81
	private MenuCategoryRepository menuCategoryRepository;
82
 
83
	@Autowired
84
	private SaholicInventoryCISRepository saholicInventoryCISRepository;
85
 
86
	@Autowired
87
	private ReporticoCacheTableRepository reporticoCacheTableRepository;
88
 
89
	@Autowired
28825 tejbeer 90
	private ActivatedImeiRepository activatedImeiRepository;
91
 
92
	@Autowired
28468 tejbeer 93
	private PartnerStatsService partnerStatsService;
94
	List<String> emails = Arrays.asList("kamini.sharma@smartdukaan.com", "neeraj.gupta@smartdukaan.com",
30585 amit.gupta 95
			"niranjan.kala@smartdukaan.com", "hemant.kaura@smartdukaan.com", "amit.gupta@smartdukaan.com",
96
			"tejbeer.kaur@smartdukaan.com");
28468 tejbeer 97
 
30647 tejbeer 98
	List<String> colorList = Arrays.asList("mediumseagreen", "dodgerblue", "darkblue", "gold", "coral", "#63C5DA",
28468 tejbeer 99
			"red", "deeppink", "midnightblue", "cornsilk");
100
 
101
	List<String> borderList = Arrays.asList("lawngreen", "lightblue", "#0000cd", "#f7e98e", "lightcoral", "#0000cd",
102
			"lightsalmon", "pink", "#0000cd", "cornsilk");
103
 
104
	List<String> brands = Arrays.asList("Oppo", "Vivo", "Samsung", "Realme", "Xiaomi", "Tecno", "Itel", "Lava",
105
			"Nokia");
106
 
107
	private static final Logger LOGGER = LogManager.getLogger(AdminUser.class);
108
 
30279 tejbeer 109
	public ChartModel getBrandWiseLmp(List<Integer> warehouseIds, List<Integer> fofoIds, LocalDate startDate,
110
			LocalDate endDate, String filterType) {
111
		LOGGER.info("params" + warehouseIds + fofoIds + startDate);
28468 tejbeer 112
 
30279 tejbeer 113
		List<BrandWiseModel> brandWiseLms = orderRepository.selectGroupByBrandLmp(fofoIds, warehouseIds, startDate,
114
				endDate, filterType);
28468 tejbeer 115
		LOGGER.info("brandWiseLms" + brandWiseLms);
30279 tejbeer 116
		ChartModel cm = null;
28468 tejbeer 117
 
30279 tejbeer 118
		if (filterType.equals("dateWise")) {
119
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 120
 
30279 tejbeer 121
			Map<String, Map<LocalDate, Double>> brandDateValue = new HashMap<>();
122
			for (BrandWiseModel bwl : brandWiseLms) {
123
				if (!brandDateValue.containsKey(bwl.getBrand())) {
124
					brandDateValue.put(bwl.getBrand(), new HashMap<>());
125
				}
126
				Map<LocalDate, Double> dateWiseSalesMap = brandDateValue.get(bwl.getBrand());
127
				dateWiseSalesMap.put(LocalDate.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
28468 tejbeer 128
			}
129
 
30279 tejbeer 130
			long days = startDate.until(endDate, ChronoUnit.DAYS);
131
			List<LocalDate> allDatesBetween = LongStream.range(0, days).mapToObj(x -> startDate.plusDays(x))
132
					.collect(Collectors.toList());
133
			LOGGER.info("All dates between {}", allDatesBetween);
28468 tejbeer 134
 
30279 tejbeer 135
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 136
 
30279 tejbeer 137
			for (String brand : brands) {
138
				if (!brandDateValue.containsKey(brand)) {
139
					brandDateValue.put(brand, new HashMap<>());
140
				}
141
				Map<LocalDate, Double> dateWiseBrand = brandDateValue.get(brand);
142
				for (LocalDate date : allDatesBetween) {
143
					if (dateWiseBrand.get(date) == null) {
144
						dateWiseBrand.put(date, 0.0);
145
					}
146
				}
147
 
148
				Map<LocalDate, Double> sortedMonthBrandValue = new TreeMap<>(dateWiseBrand);
149
				sortedBrandValue.put(brand, new ArrayList<>(sortedMonthBrandValue.values()));
150
				LOGGER.info("Sorted Brandwise values count {}", sortedMonthBrandValue.size());
30220 amit.gupta 151
			}
30279 tejbeer 152
 
153
			LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
154
 
155
			LOGGER.info("sortedBrandValueCount {}", sortedBrandValue.size());
156
			cm = chartService.createChartWithLabels(
157
					allDatesBetween.stream().map(x -> x.format((DateTimeFormatter.ofPattern("dd MMM''uu"))))
158
							.collect(Collectors.toCollection(LinkedHashSet::new)),
159
					sortedBrandValue, colorList, borderList, "Brand Wise Date Wise Purchase");
160
 
161
		} else {
162
 
163
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
164
 
165
			Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
166
			for (BrandWiseModel bwl : brandWiseLms) {
167
				Map<YearMonth, Double> yearMonthValue = new HashMap<>();
168
				if (brandMonthValue.containsKey(bwl.getBrand())) {
169
					yearMonthValue = brandMonthValue.get(bwl.getBrand());
170
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
171
							(double) bwl.getAmount());
172
				} else {
173
 
174
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
175
							(double) bwl.getAmount());
176
 
28468 tejbeer 177
				}
30279 tejbeer 178
				brandMonthValue.put(bwl.getBrand(), yearMonthValue);
179
 
28468 tejbeer 180
			}
30279 tejbeer 181
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 182
 
30279 tejbeer 183
			Period age = Period.between(startDate, endDate);
184
			int months = age.getMonths();
185
			LOGGER.info("months" + months);
186
			LOGGER.info("brandWiseLms" + brandWiseLms);
187
			LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 188
 
30279 tejbeer 189
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 190
 
30279 tejbeer 191
			for (String brand : brands) {
192
				Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
193
				for (int i = months; i >= 0; i--) {
28468 tejbeer 194
 
30279 tejbeer 195
					LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
28468 tejbeer 196
 
30279 tejbeer 197
					if (yearMonthValue != null) {
198
						if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
199
							yearMonthValue.put(YearMonth.from(startMonth), 0.0);
200
						}
201
 
202
					} else {
203
						yearMonthValue = new HashMap<>();
204
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
205
					}
206
				}
207
 
208
				Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
209
 
210
				brandMonthValue.put(brand, sortedMonthBrandValue);
211
 
212
				sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
213
 
214
			}
215
 
216
			cm = chartService.createChart(months, sortedBrandValue, colorList, borderList,
217
					"Brand Wise Monthly Purchase");
218
 
219
		}
220
 
28468 tejbeer 221
		return cm;
222
	}
223
 
29165 manish 224
	public ChartInvestmentModel getAllStatePartnerType(Map<Integer, PartnerDetailModel> fofoIdAndallValues)
225
			throws ProfitMandiBusinessException {
226
 
227
		Map<String, Float> partnerTypeCount = new HashMap<>();
228
 
229
		List<PartnerType> partnerTypes = Arrays.asList(PartnerType.values());
230
 
231
		for (PartnerType partnerType : partnerTypes) {
232
 
233
			float count = fofoIdAndallValues.entrySet().stream()
234
					.filter(x -> x.getValue().getPartnerType().equals(partnerType)).count();
235
 
236
			partnerTypeCount.put(partnerType.toString(), count);
237
 
238
		}
239
 
240
		ChartInvestmentModel cm = new ChartInvestmentModel();
241
 
242
		HashSet<String> labels = new HashSet<String>();
243
		labels.addAll(partnerTypes.stream().skip(1).map(x -> x.toString()).collect(Collectors.toList()));
244
		List<String> labelList = new ArrayList<>(labels);
245
 
246
		List<String> backgroundColor = new ArrayList<>();
247
 
248
		List<Float> values = new ArrayList<>();
249
 
250
		for (String label : labelList) {
251
			values.add(partnerTypeCount.get(label));
252
			if (label.equals("BRONZE")) {
29168 manish 253
				backgroundColor.add("#CD7F32");
29165 manish 254
			}
255
			if (label.equals("SILVER")) {
256
				backgroundColor.add("silver");
257
			}
258
			if (label.equals("GOLD")) {
29168 manish 259
				backgroundColor.add("#FFD700");
29165 manish 260
			}
261
			if (label.equals("DIAMOND")) {
262
				backgroundColor.add("#B9F2FF");
263
			}
264
 
265
			if (label.equals("PLATINUM")) {
29198 manish 266
				backgroundColor.add("#800080");
29165 manish 267
			}
268
			if (label.equals("NEW")) {
269
				backgroundColor.add("green");
270
			}
271
 
272
		}
273
 
274
		LOGGER.info("valuesPartnerType" + values);
275
		LOGGER.info("partnerTypeCount" + partnerTypeCount);
276
 
277
		Data data = new Data();
278
		data.setData(values);
279
		data.setBackgroundColor(backgroundColor);
280
		data.setLabel("DataSet 1");
281
 
282
		PieLables label = new PieLables();
283
		label.setFontColor("black");
284
		label.setFontSize(22);
285
 
286
		Legend legend = new Legend();
287
		legend.setLabels(label);
288
		legend.setPosition("left");
289
 
290
		List<Data> dataList = new ArrayList<>();
291
		dataList.add(data);
292
 
293
		DataInvestmentModel datasets = new DataInvestmentModel();
294
		datasets.setDatasets(dataList);
295
		datasets.setLabels(labels);
296
 
297
		OptionModel om = new OptionModel();
298
		om.setLegend(legend);
299
		cm.setType("pie");
300
		cm.setData(datasets);
301
		cm.setOptions(om);
302
 
303
		return cm;
304
	}
305
 
30279 tejbeer 306
	public Map<String, Object> getFilter(int warehouseId, String email, LocalDateTime date, LocalDateTime endDate)
28468 tejbeer 307
			throws ProfitMandiBusinessException {
308
		Map<String, Object> map = new HashMap<>();
30426 tejbeer 309
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
310
		Map<Integer, CustomRetailer> crm = null;
28468 tejbeer 311
		Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
312
		AuthUser authUser = authRepository.selectByEmailOrMobile(email);
313
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
314
 
315
		Set<Integer> fofoIds = null;
316
		fofoIds = storeGuyMap.get(authUser.getEmailId());
317
		if (emails.contains(authUser.getEmailId())) {
318
			fofoIds = storeGuyMap.get("tarun.verma@smartdukaan.com");
319
 
320
		}
321
 
322
		if (fofoIds == null) {
323
			List<Position> positions1 = positionRepository.selectAll(authUser.getId());
324
			if (positions1.stream().filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_MASTER)
325
					.count() > 0) {
326
				fofoIds = csService.getPositionCustomRetailerMap(positions1).values().stream().flatMap(x -> x.stream())
327
						.map(x -> x.getPartnerId()).collect(Collectors.toSet());
328
			}
329
		}
330
 
331
		if (warehouseId != 0) {
332
 
333
			fofoIds = fofoStoreRepository.selectPartnerByfofoIdAndWarehouse(new ArrayList<>(fofoIds), warehouseId)
334
					.stream().map(x -> x).collect(Collectors.toSet());
335
 
336
		}
337
 
338
		if (fofoIds != null) {
339
 
30426 tejbeer 340
			crm = fofoIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null)
341
					.collect(Collectors.toList()).stream().collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
342
 
28468 tejbeer 343
		}
344
 
345
		map.put("date", date);
30279 tejbeer 346
		map.put("endDate", endDate);
30426 tejbeer 347
		map.put("customRetailersMap", crm);
28468 tejbeer 348
		map.put("fofoIds", fofoIds);
349
		map.put("warehouseMap", warehouseMap);
350
 
351
		return map;
352
 
353
	}
354
 
30279 tejbeer 355
	public ChartModel getBrandWiseLms(List<Integer> warehouseIds, List<Integer> fofoIds, LocalDate startDate,
356
			LocalDate endDate, String filterType) {
357
		LOGGER.info("params" + warehouseIds + fofoIds + startDate);
28468 tejbeer 358
 
30279 tejbeer 359
		List<BrandWiseModel> brandWiseLms = fofoStoreRepository.selectGroupByBrandLms(fofoIds, warehouseIds, startDate,
360
				endDate, filterType);
28468 tejbeer 361
		LOGGER.info("brandWiseLms" + brandWiseLms);
362
 
30279 tejbeer 363
		ChartModel cm = null;
28468 tejbeer 364
 
30279 tejbeer 365
		if (filterType.equals("dateWise")) {
366
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 367
 
30279 tejbeer 368
			Map<String, Map<LocalDate, Double>> brandDateValue = new HashMap<>();
369
			for (BrandWiseModel bwl : brandWiseLms) {
370
				if (!brandDateValue.containsKey(bwl.getBrand())) {
371
					brandDateValue.put(bwl.getBrand(), new HashMap<>());
372
				}
373
				Map<LocalDate, Double> dateWiseSalesMap = brandDateValue.get(bwl.getBrand());
374
				dateWiseSalesMap.put(LocalDate.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
375
			}
28468 tejbeer 376
 
30279 tejbeer 377
			long days = startDate.until(endDate, ChronoUnit.DAYS);
378
			List<LocalDate> allDatesBetween = LongStream.range(0, days).mapToObj(x -> startDate.plusDays(x))
379
					.collect(Collectors.toList());
380
			LOGGER.info("All dates between {}", allDatesBetween);
381
 
382
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
383
 
384
			for (String brand : brands) {
385
				if (!brandDateValue.containsKey(brand)) {
386
					brandDateValue.put(brand, new HashMap<>());
387
				}
388
				Map<LocalDate, Double> dateWiseBrand = brandDateValue.get(brand);
389
				for (LocalDate date : allDatesBetween) {
390
					if (dateWiseBrand.get(date) == null) {
391
						dateWiseBrand.put(date, 0.0);
392
					}
393
				}
394
 
395
				Map<LocalDate, Double> sortedMonthBrandValue = new TreeMap<>(dateWiseBrand);
396
				sortedBrandValue.put(brand, new ArrayList<>(sortedMonthBrandValue.values()));
397
				LOGGER.info("Sorted Brandwise values count {}", sortedMonthBrandValue.size());
28468 tejbeer 398
			}
399
 
30279 tejbeer 400
			LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
28468 tejbeer 401
 
30279 tejbeer 402
			LOGGER.info("sortedBrandValueCount {}", sortedBrandValue.size());
403
			cm = chartService.createChartWithLabels(
404
					allDatesBetween.stream().map(x -> x.format((DateTimeFormatter.ofPattern("dd MMM''uu"))))
405
							.collect(Collectors.toCollection(LinkedHashSet::new)),
406
					sortedBrandValue, colorList, borderList, "Brand Wise Date Wise Sale");
28468 tejbeer 407
 
30279 tejbeer 408
		} else {
28468 tejbeer 409
 
30279 tejbeer 410
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
28468 tejbeer 411
 
30279 tejbeer 412
			Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
413
			for (BrandWiseModel bwl : brandWiseLms) {
414
				Map<YearMonth, Double> yearMonthValue = new HashMap<>();
415
				if (brandMonthValue.containsKey(bwl.getBrand())) {
416
					yearMonthValue = brandMonthValue.get(bwl.getBrand());
417
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
418
							(double) bwl.getAmount());
419
				} else {
28468 tejbeer 420
 
30279 tejbeer 421
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
422
							(double) bwl.getAmount());
28468 tejbeer 423
 
424
				}
30279 tejbeer 425
				brandMonthValue.put(bwl.getBrand(), yearMonthValue);
426
 
28468 tejbeer 427
			}
30279 tejbeer 428
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 429
 
30279 tejbeer 430
			Period age = Period.between(startDate, endDate);
431
			int months = age.getMonths();
432
			LOGGER.info("months" + months);
433
			LOGGER.info("brandWiseLms" + brandWiseLms);
434
			LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 435
 
30279 tejbeer 436
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 437
 
30279 tejbeer 438
			for (String brand : brands) {
439
				Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
440
				for (int i = months; i >= 0; i--) {
28468 tejbeer 441
 
30279 tejbeer 442
					LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
443
 
444
					if (yearMonthValue != null) {
445
						if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
446
							yearMonthValue.put(YearMonth.from(startMonth), 0.0);
447
						}
448
 
449
					} else {
450
						yearMonthValue = new HashMap<>();
451
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
452
					}
453
				}
454
 
455
				Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
456
 
457
				brandMonthValue.put(brand, sortedMonthBrandValue);
458
 
459
				sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
460
 
461
			}
462
 
463
			cm = chartService.createChart(months, sortedBrandValue, colorList, borderList, "Brand Wise Monthly Sale");
28468 tejbeer 464
		}
465
 
466
		return cm;
467
	}
468
 
469
	public String adminPanel(int fofoId, String email, Model model) throws Exception {
470
		List<Menu> menus = null;
471
		try {
472
			AuthUser authUser = authRepository.selectByEmailOrMobile(email);
473
			List<Position> positions = positionRepository.selectAll(authUser.getId());
474
			Map<Integer, PartnerDetailModel> warehousePartnerDetailMap = null;
475
 
476
			Map<Integer, WarehouseWiseStockModel> warehouseStockMap = new HashMap<>();
477
			LinkedHashMap<Integer, WarehouseWiseStockModel> warehouseStockSortedMap = new LinkedHashMap<>();
478
 
479
			Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
30449 amit.gupta 480
			Map<Integer, String> wm = new LinkedHashMap<>();
28468 tejbeer 481
 
482
			Map<AuthUser, Long> authUserTicketCount = null;
483
 
484
			for (Map.Entry<Integer, String> entry : warehouseMap.entrySet()) {
485
				wm.put(entry.getKey(), entry.getValue());
486
			}
487
 
488
			wm.put(0, "Total Values");
489
			long stockValue = 0;
490
			long stockQty = 0;
491
			long pendingIndent = 0;
492
			long tertiary = 0;
493
 
494
			Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
495
 
496
			Set<Integer> fofoIds = storeGuyMap.get(authUser.getEmailId());
497
			if (emails.contains(authUser.getEmailId())) {
498
				fofoIds = storeGuyMap.get("tarun.verma@smartdukaan.com");
499
				LOGGER.info("fofoIds" + fofoIds);
500
			}
501
			if (fofoIds == null) {
502
				List<Position> positions1 = positionRepository.selectAll(authUser.getId());
503
				if (positions1.stream().filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_MASTER)
504
						.count() > 0) {
505
					fofoIds = csService.getPositionCustomRetailerMap(positions1).values().stream()
506
							.flatMap(x -> x.stream()).map(x -> x.getPartnerId()).collect(Collectors.toSet());
507
				}
508
			}
509
 
510
			if (fofoIds != null && fofoIds.size() > 0) {
511
				Map<Integer, List<Integer>> warehouseIdFofoIdMap = fofoStoreRepository
512
						.selectActivePartnersByRetailerIds(new ArrayList<>(fofoIds)).stream()
513
						.collect(Collectors.groupingBy(FofoStore::getWarehouseId,
514
								Collectors.mapping(FofoStore::getId, Collectors.toList())));
515
				// warehouseStock
516
				List<WarehouseWiseStockModel> warehouseStocks = saholicInventoryCISRepository
517
						.selectGroupByWarehouse(new ArrayList<>(warehouseIdFofoIdMap.keySet()));
518
 
519
				warehouseStockMap = warehouseStocks.stream().collect(Collectors.toMap(x -> x.getWarehouseId(), x -> x));
520
 
521
				if (!warehouseStockMap.isEmpty()) {
522
					for (Entry<Integer, WarehouseWiseStockModel> warehouseStock : warehouseStockMap.entrySet()) {
523
						stockValue += warehouseStock.getValue().getStockValue();
524
						stockQty += warehouseStock.getValue().getStockQty();
525
						pendingIndent += warehouseStock.getValue().getPendingIndent();
526
						tertiary += warehouseStock.getValue().getTertiary();
527
					}
528
					WarehouseWiseStockModel ws = new WarehouseWiseStockModel();
529
					ws.setStockQty(stockQty);
530
					ws.setStockValue(stockValue);
531
					ws.setPendingIndent(pendingIndent);
532
					ws.setTertiary(tertiary);
533
					ws.setWarehouseId(0);
534
					warehouseStockMap.put(0, ws);
535
 
536
				}
537
 
538
				warehouseStockMap.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
539
						.forEachOrdered(x -> warehouseStockSortedMap.put(x.getKey(), x.getValue()));
540
 
30449 amit.gupta 541
				ReporticoCacheTable rctSaholic = reporticoCacheTableRepository.selectByTableName("SaholicInventoryCIS");
542
				model.addAttribute("reporticoDate", rctSaholic);
28468 tejbeer 543
 
544
				// warehouseStock
545
				warehousePartnerDetailMap = this.getWarehousePartnerDetail(warehouseIdFofoIdMap);
30449 amit.gupta 546
				ReporticoCacheTable rctPartneStat = reporticoCacheTableRepository.selectByTableName("partnerStat");
28468 tejbeer 547
 
548
				Set<CustomRetailer> positionRetailers = fofoIds.stream()
549
						.map(x -> retailerService.getAllFofoRetailers().get(x)).filter(x -> x != null)
550
						.collect(Collectors.toSet());
551
				model.addAttribute("retailers", gson.toJson(positionRetailers));
552
				model.addAttribute("reporticoProjectMap", ReporticoProject.salesReporticoProjectMap);
553
				model.addAttribute("warehouses", getWarehouses(positionRetailers));
30449 amit.gupta 554
				model.addAttribute("rctPartneStat", rctPartneStat);
28468 tejbeer 555
			} else {
556
				List<Position> warehousePositions = positions.stream()
557
						.filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_WAREHOUSE)
558
						.collect(Collectors.toList());
559
				if (warehousePositions.size() > 0) {
560
					Set<CustomRetailer> positionRetailers = new HashSet<>();
561
					csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
562
						positionRetailers.addAll(customRetailers);
563
					});
564
					model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
565
					model.addAttribute("retailers", gson.toJson(positionRetailers));
566
					model.addAttribute("warehouses", getWarehouses(positionRetailers));
567
				}
30056 amit.gupta 568
				List<Position> categoryPositions = positions.stream()
569
						.filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_CATEGORY)
570
						.collect(Collectors.toList());
571
				if (categoryPositions.size() > 0) {
572
					Set<CustomRetailer> positionRetailers = new HashSet<>();
573
					csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
574
						positionRetailers.addAll(customRetailers);
575
					});
576
					model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
577
					model.addAttribute("retailers", gson.toJson(positionRetailers));
578
					model.addAttribute("warehouses", getWarehouses(positionRetailers));
579
				}
28468 tejbeer 580
			}
581
 
582
			if (positions.size() > 0) {
583
				if (positions.stream()
584
						.filter(x -> x.getEscalationType().equals(EscalationType.L3)
585
								|| x.getEscalationType().equals(EscalationType.L4)
586
								|| x.getEscalationType().equals(EscalationType.L5))
587
						.count() > 0) {
588
					authUserTicketCount = ticketRepository.selectAllAuthUserTicketCount(Optional.of(false));
589
				}
590
			}
591
 
30564 tejbeer 592
			if (Arrays.asList("amit.gupta@smartdukaan.com", "tejbeer.kaur@smartdukaan.com").contains(email)) {
28468 tejbeer 593
				menus = menuRepository.selectAll();
594
			} else if (positions.size() > 0) {
595
				if (positions.stream().filter(x -> x.getEscalationType().equals(EscalationType.L5)).count() > 0) {
596
					menus = menuRepository.selectAll();
597
				} else {
598
					List<Integer> menuIds = menuCategoryRepository.selectAllByPositions(positions).stream()
599
							.map(x -> x.getMenuId()).collect(Collectors.toList());
600
					if (menuIds.size() > 0) {
601
						menus = menuRepository.selectAllByIds(menuIds);
602
					}
603
				}
604
			}
605
 
606
			model.addAttribute("authId", authUser.getId());
607
 
608
			model.addAttribute("warehousePartnerDetailMap", warehousePartnerDetailMap);
609
			model.addAttribute("warehouseMap", wm);
610
			model.addAttribute("authUserTicketCount", authUserTicketCount);
611
			model.addAttribute("warehouseStockMap", warehouseStockSortedMap);
612
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
613
 
614
			model.addAttribute("date", curDate.withDayOfMonth(1).minusMonths(6).toLocalDate());
615
 
616
		} catch (ProfitMandiBusinessException e) {
617
		}
618
		List<Menu> menuList = (menus != null) ? this.prepareMenu(menus) : new ArrayList<>();
619
		LOGGER.info("menu" + menuList);
620
		model.addAttribute("menu", menuList);
621
		return "admin";
622
	}
623
 
624
	private List<Menu> prepareMenu(List<Menu> menus) {
625
		List<Menu> returnMenu = new ArrayList<>();
30268 amit.gupta 626
		Map<Menu, List<Menu>> subMenuMap = new HashMap<>();
28468 tejbeer 627
		for (Menu menu : menus) {
628
			if (menu.get_parent() == null) {
629
				if (!subMenuMap.containsKey(menu)) {
630
					subMenuMap.put(menu, new ArrayList<>());
631
				}
632
			} else {
633
				Menu parentMenu = menu.get_parent();
634
				if (!subMenuMap.containsKey(parentMenu)) {
635
					subMenuMap.put(parentMenu, new ArrayList<>());
636
				}
637
				subMenuMap.get(parentMenu).add(menu);
638
			}
639
		}
640
		subMenuMap.entrySet().stream().forEach(entry -> {
641
			entry.getKey().setSubMenus(entry.getValue());
642
			returnMenu.add(entry.getKey());
643
		});
644
		return returnMenu;
645
	}
646
 
647
	public Map<Integer, PartnerDetailModel> getWarehousePartnerDetail(Map<Integer, List<Integer>> warehousePartnerMap)
648
			throws Exception {
649
 
650
		Map<Integer, PartnerDetailModel> warehouseIdAndallValues = new LinkedHashMap<>();
651
		// Map<Integer, List<FofoStore>> warehousePartnerMap =
652
		// fofoStoreRepository.getWarehousePartnerMap();
653
		Map<Integer, PartnerDetailModel> partnerStats = this.getPartnersStatDataFromFile();
654
		List<Integer> allfofoIds = new ArrayList<>();
655
		if (partnerStats != null) {
656
			for (Entry<Integer, List<Integer>> warehouse : warehousePartnerMap.entrySet()) {
30246 amit.gupta 657
				List<Integer> fofoIds = warehouse.getValue().stream().collect(Collectors.toList());
28468 tejbeer 658
				allfofoIds.addAll(fofoIds);
659
				List<PartnerDetailModel> partnerDetails = fofoIds.stream().map(x -> partnerStats.get(x))
660
						.collect(Collectors.toList());
661
				if (partnerDetails != null && !partnerDetails.isEmpty()) {
662
					PartnerDetailModel partnerDetailModel = partnerStatsService.getAggregateStats(partnerDetails);
663
					warehouseIdAndallValues.put(warehouse.getKey(), partnerDetailModel);
664
				}
665
 
666
			}
667
			List<PartnerDetailModel> allPartnerDetails = allfofoIds.stream().map(x -> partnerStats.get(x))
668
					.collect(Collectors.toList());
669
			LOGGER.info("allPartnerDetails" + allPartnerDetails);
670
			PartnerDetailModel partnerDetailModel = partnerStatsService
671
					.getAggregateStats(new ArrayList<>(allPartnerDetails));
30268 amit.gupta 672
			warehouseIdAndallValues.put(0, partnerDetailModel);
28468 tejbeer 673
		}
674
		return warehouseIdAndallValues;
675
	}
676
 
677
	public Map<Integer, PartnerDetailModel> getPartnersStatDataFromFile() throws Exception {
678
		ObjectInputStream objectinputstream = null;
679
		Map<Integer, PartnerDetailModel> partnerStat = null;
680
		try {
681
			FileInputStream streamIn = new FileInputStream("/tmp/partnerStat.tmp");
682
			objectinputstream = new ObjectInputStream(streamIn);
683
			partnerStat = (Map<Integer, PartnerDetailModel>) objectinputstream.readObject();
684
 
685
			LOGGER.info("partnerStat" + partnerStat);
686
			objectinputstream.close();
687
 
688
		} catch (Exception e) {
689
			LOGGER.info("exceptionddd" + e);
690
 
691
			e.printStackTrace();
692
 
693
		} finally {
694
			if (objectinputstream != null) {
695
				objectinputstream.close();
696
				LOGGER.info("clofddd" + partnerStat);
697
 
698
			}
699
		}
700
		return partnerStat;
701
 
702
	}
703
 
704
	private String getWarehouses(Set<CustomRetailer> positionRetailers) {
705
		Map<Integer, String> warehouses = new HashMap<>();
706
		positionRetailers.stream().forEach(x -> {
707
			if (x.getWarehouseId() != 0) {
708
				warehouses.put(x.getWarehouseId(), ProfitMandiConstants.WAREHOUSE_MAP.get(x.getWarehouseId()));
709
			}
710
		});
711
		return gson.toJson(warehouses);
712
 
713
	}
714
 
715
}