Subversion Repositories SmartDukaan

Rev

Rev 30647 | Rev 31152 | 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",
31130 tejbeer 95
			"tejbeer.kaur@smartdukaan.com", "amit.gupta@smartdukaan.com", "kanan.choudhary@smartdukaan.com");
28468 tejbeer 96
 
30647 tejbeer 97
	List<String> colorList = Arrays.asList("mediumseagreen", "dodgerblue", "darkblue", "gold", "coral", "#63C5DA",
28468 tejbeer 98
			"red", "deeppink", "midnightblue", "cornsilk");
99
 
100
	List<String> borderList = Arrays.asList("lawngreen", "lightblue", "#0000cd", "#f7e98e", "lightcoral", "#0000cd",
101
			"lightsalmon", "pink", "#0000cd", "cornsilk");
102
 
103
	List<String> brands = Arrays.asList("Oppo", "Vivo", "Samsung", "Realme", "Xiaomi", "Tecno", "Itel", "Lava",
104
			"Nokia");
105
 
106
	private static final Logger LOGGER = LogManager.getLogger(AdminUser.class);
107
 
30279 tejbeer 108
	public ChartModel getBrandWiseLmp(List<Integer> warehouseIds, List<Integer> fofoIds, LocalDate startDate,
109
			LocalDate endDate, String filterType) {
110
		LOGGER.info("params" + warehouseIds + fofoIds + startDate);
28468 tejbeer 111
 
30279 tejbeer 112
		List<BrandWiseModel> brandWiseLms = orderRepository.selectGroupByBrandLmp(fofoIds, warehouseIds, startDate,
113
				endDate, filterType);
28468 tejbeer 114
		LOGGER.info("brandWiseLms" + brandWiseLms);
30279 tejbeer 115
		ChartModel cm = null;
28468 tejbeer 116
 
30279 tejbeer 117
		if (filterType.equals("dateWise")) {
118
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 119
 
30279 tejbeer 120
			Map<String, Map<LocalDate, Double>> brandDateValue = new HashMap<>();
121
			for (BrandWiseModel bwl : brandWiseLms) {
122
				if (!brandDateValue.containsKey(bwl.getBrand())) {
123
					brandDateValue.put(bwl.getBrand(), new HashMap<>());
124
				}
125
				Map<LocalDate, Double> dateWiseSalesMap = brandDateValue.get(bwl.getBrand());
126
				dateWiseSalesMap.put(LocalDate.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
28468 tejbeer 127
			}
128
 
30279 tejbeer 129
			long days = startDate.until(endDate, ChronoUnit.DAYS);
130
			List<LocalDate> allDatesBetween = LongStream.range(0, days).mapToObj(x -> startDate.plusDays(x))
131
					.collect(Collectors.toList());
132
			LOGGER.info("All dates between {}", allDatesBetween);
28468 tejbeer 133
 
30279 tejbeer 134
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 135
 
30279 tejbeer 136
			for (String brand : brands) {
137
				if (!brandDateValue.containsKey(brand)) {
138
					brandDateValue.put(brand, new HashMap<>());
139
				}
140
				Map<LocalDate, Double> dateWiseBrand = brandDateValue.get(brand);
141
				for (LocalDate date : allDatesBetween) {
142
					if (dateWiseBrand.get(date) == null) {
143
						dateWiseBrand.put(date, 0.0);
144
					}
145
				}
146
 
147
				Map<LocalDate, Double> sortedMonthBrandValue = new TreeMap<>(dateWiseBrand);
148
				sortedBrandValue.put(brand, new ArrayList<>(sortedMonthBrandValue.values()));
149
				LOGGER.info("Sorted Brandwise values count {}", sortedMonthBrandValue.size());
30220 amit.gupta 150
			}
30279 tejbeer 151
 
152
			LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
153
 
154
			LOGGER.info("sortedBrandValueCount {}", sortedBrandValue.size());
155
			cm = chartService.createChartWithLabels(
156
					allDatesBetween.stream().map(x -> x.format((DateTimeFormatter.ofPattern("dd MMM''uu"))))
157
							.collect(Collectors.toCollection(LinkedHashSet::new)),
158
					sortedBrandValue, colorList, borderList, "Brand Wise Date Wise Purchase");
159
 
160
		} else {
161
 
162
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
163
 
164
			Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
165
			for (BrandWiseModel bwl : brandWiseLms) {
166
				Map<YearMonth, Double> yearMonthValue = new HashMap<>();
167
				if (brandMonthValue.containsKey(bwl.getBrand())) {
168
					yearMonthValue = brandMonthValue.get(bwl.getBrand());
169
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
170
							(double) bwl.getAmount());
171
				} else {
172
 
173
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
174
							(double) bwl.getAmount());
175
 
28468 tejbeer 176
				}
30279 tejbeer 177
				brandMonthValue.put(bwl.getBrand(), yearMonthValue);
178
 
28468 tejbeer 179
			}
30279 tejbeer 180
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 181
 
30279 tejbeer 182
			Period age = Period.between(startDate, endDate);
183
			int months = age.getMonths();
184
			LOGGER.info("months" + months);
185
			LOGGER.info("brandWiseLms" + brandWiseLms);
186
			LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 187
 
30279 tejbeer 188
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 189
 
30279 tejbeer 190
			for (String brand : brands) {
191
				Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
192
				for (int i = months; i >= 0; i--) {
28468 tejbeer 193
 
30279 tejbeer 194
					LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
28468 tejbeer 195
 
30279 tejbeer 196
					if (yearMonthValue != null) {
197
						if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
198
							yearMonthValue.put(YearMonth.from(startMonth), 0.0);
199
						}
200
 
201
					} else {
202
						yearMonthValue = new HashMap<>();
203
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
204
					}
205
				}
206
 
207
				Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
208
 
209
				brandMonthValue.put(brand, sortedMonthBrandValue);
210
 
211
				sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
212
 
213
			}
214
 
215
			cm = chartService.createChart(months, sortedBrandValue, colorList, borderList,
216
					"Brand Wise Monthly Purchase");
217
 
218
		}
219
 
28468 tejbeer 220
		return cm;
221
	}
222
 
29165 manish 223
	public ChartInvestmentModel getAllStatePartnerType(Map<Integer, PartnerDetailModel> fofoIdAndallValues)
224
			throws ProfitMandiBusinessException {
225
 
226
		Map<String, Float> partnerTypeCount = new HashMap<>();
227
 
228
		List<PartnerType> partnerTypes = Arrays.asList(PartnerType.values());
229
 
230
		for (PartnerType partnerType : partnerTypes) {
231
 
232
			float count = fofoIdAndallValues.entrySet().stream()
233
					.filter(x -> x.getValue().getPartnerType().equals(partnerType)).count();
234
 
235
			partnerTypeCount.put(partnerType.toString(), count);
236
 
237
		}
238
 
239
		ChartInvestmentModel cm = new ChartInvestmentModel();
240
 
241
		HashSet<String> labels = new HashSet<String>();
242
		labels.addAll(partnerTypes.stream().skip(1).map(x -> x.toString()).collect(Collectors.toList()));
243
		List<String> labelList = new ArrayList<>(labels);
244
 
245
		List<String> backgroundColor = new ArrayList<>();
246
 
247
		List<Float> values = new ArrayList<>();
248
 
249
		for (String label : labelList) {
250
			values.add(partnerTypeCount.get(label));
251
			if (label.equals("BRONZE")) {
29168 manish 252
				backgroundColor.add("#CD7F32");
29165 manish 253
			}
254
			if (label.equals("SILVER")) {
255
				backgroundColor.add("silver");
256
			}
257
			if (label.equals("GOLD")) {
29168 manish 258
				backgroundColor.add("#FFD700");
29165 manish 259
			}
260
			if (label.equals("DIAMOND")) {
261
				backgroundColor.add("#B9F2FF");
262
			}
263
 
264
			if (label.equals("PLATINUM")) {
29198 manish 265
				backgroundColor.add("#800080");
29165 manish 266
			}
267
			if (label.equals("NEW")) {
268
				backgroundColor.add("green");
269
			}
270
 
271
		}
272
 
273
		LOGGER.info("valuesPartnerType" + values);
274
		LOGGER.info("partnerTypeCount" + partnerTypeCount);
275
 
276
		Data data = new Data();
277
		data.setData(values);
278
		data.setBackgroundColor(backgroundColor);
279
		data.setLabel("DataSet 1");
280
 
281
		PieLables label = new PieLables();
282
		label.setFontColor("black");
283
		label.setFontSize(22);
284
 
285
		Legend legend = new Legend();
286
		legend.setLabels(label);
287
		legend.setPosition("left");
288
 
289
		List<Data> dataList = new ArrayList<>();
290
		dataList.add(data);
291
 
292
		DataInvestmentModel datasets = new DataInvestmentModel();
293
		datasets.setDatasets(dataList);
294
		datasets.setLabels(labels);
295
 
296
		OptionModel om = new OptionModel();
297
		om.setLegend(legend);
298
		cm.setType("pie");
299
		cm.setData(datasets);
300
		cm.setOptions(om);
301
 
302
		return cm;
303
	}
304
 
30279 tejbeer 305
	public Map<String, Object> getFilter(int warehouseId, String email, LocalDateTime date, LocalDateTime endDate)
28468 tejbeer 306
			throws ProfitMandiBusinessException {
307
		Map<String, Object> map = new HashMap<>();
30426 tejbeer 308
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
309
		Map<Integer, CustomRetailer> crm = null;
28468 tejbeer 310
		Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
311
		AuthUser authUser = authRepository.selectByEmailOrMobile(email);
312
		Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
313
 
314
		Set<Integer> fofoIds = null;
315
		fofoIds = storeGuyMap.get(authUser.getEmailId());
316
		if (emails.contains(authUser.getEmailId())) {
317
			fofoIds = storeGuyMap.get("tarun.verma@smartdukaan.com");
318
 
319
		}
320
 
321
		if (fofoIds == null) {
322
			List<Position> positions1 = positionRepository.selectAll(authUser.getId());
323
			if (positions1.stream().filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_MASTER)
324
					.count() > 0) {
325
				fofoIds = csService.getPositionCustomRetailerMap(positions1).values().stream().flatMap(x -> x.stream())
326
						.map(x -> x.getPartnerId()).collect(Collectors.toSet());
327
			}
328
		}
329
 
330
		if (warehouseId != 0) {
331
 
332
			fofoIds = fofoStoreRepository.selectPartnerByfofoIdAndWarehouse(new ArrayList<>(fofoIds), warehouseId)
333
					.stream().map(x -> x).collect(Collectors.toSet());
334
 
335
		}
336
 
337
		if (fofoIds != null) {
338
 
30426 tejbeer 339
			crm = fofoIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null)
340
					.collect(Collectors.toList()).stream().collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
341
 
28468 tejbeer 342
		}
343
 
344
		map.put("date", date);
30279 tejbeer 345
		map.put("endDate", endDate);
30426 tejbeer 346
		map.put("customRetailersMap", crm);
28468 tejbeer 347
		map.put("fofoIds", fofoIds);
348
		map.put("warehouseMap", warehouseMap);
349
 
350
		return map;
351
 
352
	}
353
 
30279 tejbeer 354
	public ChartModel getBrandWiseLms(List<Integer> warehouseIds, List<Integer> fofoIds, LocalDate startDate,
355
			LocalDate endDate, String filterType) {
356
		LOGGER.info("params" + warehouseIds + fofoIds + startDate);
28468 tejbeer 357
 
30279 tejbeer 358
		List<BrandWiseModel> brandWiseLms = fofoStoreRepository.selectGroupByBrandLms(fofoIds, warehouseIds, startDate,
359
				endDate, filterType);
28468 tejbeer 360
		LOGGER.info("brandWiseLms" + brandWiseLms);
361
 
30279 tejbeer 362
		ChartModel cm = null;
28468 tejbeer 363
 
30279 tejbeer 364
		if (filterType.equals("dateWise")) {
365
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
28468 tejbeer 366
 
30279 tejbeer 367
			Map<String, Map<LocalDate, Double>> brandDateValue = new HashMap<>();
368
			for (BrandWiseModel bwl : brandWiseLms) {
369
				if (!brandDateValue.containsKey(bwl.getBrand())) {
370
					brandDateValue.put(bwl.getBrand(), new HashMap<>());
371
				}
372
				Map<LocalDate, Double> dateWiseSalesMap = brandDateValue.get(bwl.getBrand());
373
				dateWiseSalesMap.put(LocalDate.parse(bwl.getYearMonth(), dateTimeFormatter), (double) bwl.getAmount());
374
			}
28468 tejbeer 375
 
30279 tejbeer 376
			long days = startDate.until(endDate, ChronoUnit.DAYS);
377
			List<LocalDate> allDatesBetween = LongStream.range(0, days).mapToObj(x -> startDate.plusDays(x))
378
					.collect(Collectors.toList());
379
			LOGGER.info("All dates between {}", allDatesBetween);
380
 
381
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
382
 
383
			for (String brand : brands) {
384
				if (!brandDateValue.containsKey(brand)) {
385
					brandDateValue.put(brand, new HashMap<>());
386
				}
387
				Map<LocalDate, Double> dateWiseBrand = brandDateValue.get(brand);
388
				for (LocalDate date : allDatesBetween) {
389
					if (dateWiseBrand.get(date) == null) {
390
						dateWiseBrand.put(date, 0.0);
391
					}
392
				}
393
 
394
				Map<LocalDate, Double> sortedMonthBrandValue = new TreeMap<>(dateWiseBrand);
395
				sortedBrandValue.put(brand, new ArrayList<>(sortedMonthBrandValue.values()));
396
				LOGGER.info("Sorted Brandwise values count {}", sortedMonthBrandValue.size());
28468 tejbeer 397
			}
398
 
30279 tejbeer 399
			LOGGER.info("brandMonthValueCount {}", brandDateValue.size());
28468 tejbeer 400
 
30279 tejbeer 401
			LOGGER.info("sortedBrandValueCount {}", sortedBrandValue.size());
402
			cm = chartService.createChartWithLabels(
403
					allDatesBetween.stream().map(x -> x.format((DateTimeFormatter.ofPattern("dd MMM''uu"))))
404
							.collect(Collectors.toCollection(LinkedHashSet::new)),
405
					sortedBrandValue, colorList, borderList, "Brand Wise Date Wise Sale");
28468 tejbeer 406
 
30279 tejbeer 407
		} else {
28468 tejbeer 408
 
30279 tejbeer 409
			DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
28468 tejbeer 410
 
30279 tejbeer 411
			Map<String, Map<YearMonth, Double>> brandMonthValue = new HashMap<>();
412
			for (BrandWiseModel bwl : brandWiseLms) {
413
				Map<YearMonth, Double> yearMonthValue = new HashMap<>();
414
				if (brandMonthValue.containsKey(bwl.getBrand())) {
415
					yearMonthValue = brandMonthValue.get(bwl.getBrand());
416
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
417
							(double) bwl.getAmount());
418
				} else {
28468 tejbeer 419
 
30279 tejbeer 420
					yearMonthValue.put(YearMonth.parse(bwl.getYearMonth(), dateTimeFormatter),
421
							(double) bwl.getAmount());
28468 tejbeer 422
 
423
				}
30279 tejbeer 424
				brandMonthValue.put(bwl.getBrand(), yearMonthValue);
425
 
28468 tejbeer 426
			}
30279 tejbeer 427
			LocalDateTime curDate = LocalDate.now().atStartOfDay();
28468 tejbeer 428
 
30279 tejbeer 429
			Period age = Period.between(startDate, endDate);
430
			int months = age.getMonths();
431
			LOGGER.info("months" + months);
432
			LOGGER.info("brandWiseLms" + brandWiseLms);
433
			LOGGER.info("brandMonthValue" + brandMonthValue);
28468 tejbeer 434
 
30279 tejbeer 435
			Map<String, List<Double>> sortedBrandValue = new LinkedHashMap<>();
28468 tejbeer 436
 
30279 tejbeer 437
			for (String brand : brands) {
438
				Map<YearMonth, Double> yearMonthValue = brandMonthValue.get(brand);
439
				for (int i = months; i >= 0; i--) {
28468 tejbeer 440
 
30279 tejbeer 441
					LocalDateTime startMonth = curDate.withDayOfMonth(1).minusMonths(i);
442
 
443
					if (yearMonthValue != null) {
444
						if (yearMonthValue.get(YearMonth.from(startMonth)) == null) {
445
							yearMonthValue.put(YearMonth.from(startMonth), 0.0);
446
						}
447
 
448
					} else {
449
						yearMonthValue = new HashMap<>();
450
						yearMonthValue.put(YearMonth.from(startMonth), 0.0);
451
					}
452
				}
453
 
454
				Map<YearMonth, Double> sortedMonthBrandValue = new TreeMap<>(yearMonthValue);
455
 
456
				brandMonthValue.put(brand, sortedMonthBrandValue);
457
 
458
				sortedBrandValue.put(brand, sortedMonthBrandValue.values().stream().collect(Collectors.toList()));
459
 
460
			}
461
 
462
			cm = chartService.createChart(months, sortedBrandValue, colorList, borderList, "Brand Wise Monthly Sale");
28468 tejbeer 463
		}
464
 
465
		return cm;
466
	}
467
 
468
	public String adminPanel(int fofoId, String email, Model model) throws Exception {
469
		List<Menu> menus = null;
470
		try {
471
			AuthUser authUser = authRepository.selectByEmailOrMobile(email);
472
			List<Position> positions = positionRepository.selectAll(authUser.getId());
473
			Map<Integer, PartnerDetailModel> warehousePartnerDetailMap = null;
474
 
475
			Map<Integer, WarehouseWiseStockModel> warehouseStockMap = new HashMap<>();
476
			LinkedHashMap<Integer, WarehouseWiseStockModel> warehouseStockSortedMap = new LinkedHashMap<>();
477
 
478
			Map<Integer, String> warehouseMap = ProfitMandiConstants.WAREHOUSE_MAP;
30449 amit.gupta 479
			Map<Integer, String> wm = new LinkedHashMap<>();
28468 tejbeer 480
 
481
			Map<AuthUser, Long> authUserTicketCount = null;
482
 
483
			for (Map.Entry<Integer, String> entry : warehouseMap.entrySet()) {
484
				wm.put(entry.getKey(), entry.getValue());
485
			}
486
 
487
			wm.put(0, "Total Values");
488
			long stockValue = 0;
489
			long stockQty = 0;
490
			long pendingIndent = 0;
491
			long tertiary = 0;
492
 
493
			Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
494
 
495
			Set<Integer> fofoIds = storeGuyMap.get(authUser.getEmailId());
496
			if (emails.contains(authUser.getEmailId())) {
497
				fofoIds = storeGuyMap.get("tarun.verma@smartdukaan.com");
498
				LOGGER.info("fofoIds" + fofoIds);
499
			}
500
			if (fofoIds == null) {
501
				List<Position> positions1 = positionRepository.selectAll(authUser.getId());
502
				if (positions1.stream().filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_MASTER)
503
						.count() > 0) {
504
					fofoIds = csService.getPositionCustomRetailerMap(positions1).values().stream()
505
							.flatMap(x -> x.stream()).map(x -> x.getPartnerId()).collect(Collectors.toSet());
506
				}
507
			}
508
 
509
			if (fofoIds != null && fofoIds.size() > 0) {
510
				Map<Integer, List<Integer>> warehouseIdFofoIdMap = fofoStoreRepository
511
						.selectActivePartnersByRetailerIds(new ArrayList<>(fofoIds)).stream()
512
						.collect(Collectors.groupingBy(FofoStore::getWarehouseId,
513
								Collectors.mapping(FofoStore::getId, Collectors.toList())));
514
				// warehouseStock
515
				List<WarehouseWiseStockModel> warehouseStocks = saholicInventoryCISRepository
516
						.selectGroupByWarehouse(new ArrayList<>(warehouseIdFofoIdMap.keySet()));
517
 
518
				warehouseStockMap = warehouseStocks.stream().collect(Collectors.toMap(x -> x.getWarehouseId(), x -> x));
519
 
520
				if (!warehouseStockMap.isEmpty()) {
521
					for (Entry<Integer, WarehouseWiseStockModel> warehouseStock : warehouseStockMap.entrySet()) {
522
						stockValue += warehouseStock.getValue().getStockValue();
523
						stockQty += warehouseStock.getValue().getStockQty();
524
						pendingIndent += warehouseStock.getValue().getPendingIndent();
525
						tertiary += warehouseStock.getValue().getTertiary();
526
					}
527
					WarehouseWiseStockModel ws = new WarehouseWiseStockModel();
528
					ws.setStockQty(stockQty);
529
					ws.setStockValue(stockValue);
530
					ws.setPendingIndent(pendingIndent);
531
					ws.setTertiary(tertiary);
532
					ws.setWarehouseId(0);
533
					warehouseStockMap.put(0, ws);
534
 
535
				}
536
 
537
				warehouseStockMap.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
538
						.forEachOrdered(x -> warehouseStockSortedMap.put(x.getKey(), x.getValue()));
539
 
30449 amit.gupta 540
				ReporticoCacheTable rctSaholic = reporticoCacheTableRepository.selectByTableName("SaholicInventoryCIS");
541
				model.addAttribute("reporticoDate", rctSaholic);
28468 tejbeer 542
 
543
				// warehouseStock
544
				warehousePartnerDetailMap = this.getWarehousePartnerDetail(warehouseIdFofoIdMap);
30449 amit.gupta 545
				ReporticoCacheTable rctPartneStat = reporticoCacheTableRepository.selectByTableName("partnerStat");
28468 tejbeer 546
 
547
				Set<CustomRetailer> positionRetailers = fofoIds.stream()
548
						.map(x -> retailerService.getAllFofoRetailers().get(x)).filter(x -> x != null)
549
						.collect(Collectors.toSet());
550
				model.addAttribute("retailers", gson.toJson(positionRetailers));
551
				model.addAttribute("reporticoProjectMap", ReporticoProject.salesReporticoProjectMap);
552
				model.addAttribute("warehouses", getWarehouses(positionRetailers));
30449 amit.gupta 553
				model.addAttribute("rctPartneStat", rctPartneStat);
28468 tejbeer 554
			} else {
555
				List<Position> warehousePositions = positions.stream()
556
						.filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_WAREHOUSE)
557
						.collect(Collectors.toList());
558
				if (warehousePositions.size() > 0) {
559
					Set<CustomRetailer> positionRetailers = new HashSet<>();
560
					csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
561
						positionRetailers.addAll(customRetailers);
562
					});
563
					model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
564
					model.addAttribute("retailers", gson.toJson(positionRetailers));
565
					model.addAttribute("warehouses", getWarehouses(positionRetailers));
566
				}
30056 amit.gupta 567
				List<Position> categoryPositions = positions.stream()
568
						.filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_CATEGORY)
569
						.collect(Collectors.toList());
570
				if (categoryPositions.size() > 0) {
571
					Set<CustomRetailer> positionRetailers = new HashSet<>();
572
					csService.getPositionCustomRetailerMap(warehousePositions).values().forEach(customRetailers -> {
573
						positionRetailers.addAll(customRetailers);
574
					});
575
					model.addAttribute("reporticoProjectMap", ReporticoProject.warehouseReporticoMap);
576
					model.addAttribute("retailers", gson.toJson(positionRetailers));
577
					model.addAttribute("warehouses", getWarehouses(positionRetailers));
578
				}
28468 tejbeer 579
			}
580
 
581
			if (positions.size() > 0) {
582
				if (positions.stream()
583
						.filter(x -> x.getEscalationType().equals(EscalationType.L3)
584
								|| x.getEscalationType().equals(EscalationType.L4)
585
								|| x.getEscalationType().equals(EscalationType.L5))
586
						.count() > 0) {
587
					authUserTicketCount = ticketRepository.selectAllAuthUserTicketCount(Optional.of(false));
588
				}
589
			}
590
 
31130 tejbeer 591
			if (Arrays.asList("amit.gupta@smartdukaan.com", "tejbeer.kaur@smartdukaan.com",
592
					"kanan.choudhary@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
}