Subversion Repositories SmartDukaan

Rev

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