Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
24383 amit.gupta 1
package com.spice.profitmandi.dao.repository.cs;
24500 govind 2
 
24383 amit.gupta 3
import java.time.LocalDateTime;
24417 govind 4
import java.util.ArrayList;
5
import java.util.HashMap;
24439 govind 6
import java.util.HashSet;
24417 govind 7
import java.util.List;
8
import java.util.Map;
25570 tejbeer 9
import java.util.Set;
10
import java.util.stream.Collectors;
24500 govind 11
 
25570 tejbeer 12
import org.apache.commons.collections4.map.HashedMap;
24439 govind 13
import org.apache.commons.lang.RandomStringUtils;
14
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
24383 amit.gupta 16
import org.springframework.beans.factory.annotation.Autowired;
25726 amit.gupta 17
import org.springframework.cache.annotation.Cacheable;
24439 govind 18
import org.springframework.mail.javamail.JavaMailSender;
24383 amit.gupta 19
import org.springframework.stereotype.Component;
24701 govind 20
 
24383 amit.gupta 21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24417 govind 22
import com.spice.profitmandi.common.model.CustomRetailer;
24439 govind 23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
24
import com.spice.profitmandi.common.util.Utils;
24417 govind 25
import com.spice.profitmandi.dao.entity.auth.AuthUser;
24383 amit.gupta 26
import com.spice.profitmandi.dao.entity.cs.Activity;
24417 govind 27
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
28
import com.spice.profitmandi.dao.entity.cs.Position;
24471 govind 29
import com.spice.profitmandi.dao.entity.cs.Region;
24383 amit.gupta 30
import com.spice.profitmandi.dao.entity.cs.Ticket;
24500 govind 31
import com.spice.profitmandi.dao.entity.cs.TicketAssigned;
24439 govind 32
import com.spice.profitmandi.dao.entity.cs.TicketCategory;
24417 govind 33
import com.spice.profitmandi.dao.entity.cs.TicketSubCategory;
24383 amit.gupta 34
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
24417 govind 35
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
36
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
25570 tejbeer 37
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
26418 tejbeer 38
import com.spice.profitmandi.dao.repository.dtr.LeadRepository;
39
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
40
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
41
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
42
import com.spice.profitmandi.service.PartnerInvestmentService;
24417 govind 43
import com.spice.profitmandi.service.user.RetailerService;
24383 amit.gupta 44
 
45
@Component
46
public class CsServiceImpl implements CsService {
24417 govind 47
 
24439 govind 48
	private static final Logger LOGGER = LogManager.getLogger(CsServiceImpl.class);
49
 
24570 govind 50
	private static final String ASSIGNED_TICKET = "Dear %s,You have assigned a ticket by %s with ticketId#%s.Regards\nSmartdukaan";
24439 govind 51
	private static final String ASSINMENT_SUBJECT = "Assignment Ticket";
52
 
24383 amit.gupta 53
	@Autowired
54
	TicketRepository ticketRepository;
24417 govind 55
 
24383 amit.gupta 56
	@Autowired
24439 govind 57
	JavaMailSender mailSender;
58
 
59
	@Autowired
24383 amit.gupta 60
	TicketCategoryRepository ticketCategoryRepository;
24417 govind 61
 
24383 amit.gupta 62
	@Autowired
63
	TicketSubCategoryRepository ticketSubCategoryRepository;
24417 govind 64
 
65
	@Autowired
24388 amit.gupta 66
	ActivityRepository activityRepository;
24417 govind 67
 
68
	@Autowired
24439 govind 69
	PartnerRegionRepository partnerRegionRepository;
24417 govind 70
 
71
	@Autowired
72
	private PositionRepository positionRepository;
73
 
74
	@Autowired
75
	private AuthRepository authRepository;
76
 
77
	@Autowired
78
	private RetailerService retailerService;
24500 govind 79
 
24471 govind 80
	@Autowired
81
	private RegionRepository regionRepository;
24417 govind 82
 
24500 govind 83
	@Autowired
84
	private TicketAssignedRepository ticketAssignedRepository;
85
 
25570 tejbeer 86
	@Autowired
87
	private PartnersPositionRepository partnersPositionRepository;
88
 
89
	@Autowired
90
	private FofoStoreRepository fofoStoreRepository;
91
 
26418 tejbeer 92
	@Autowired
93
	private FofoOrderItemRepository fofoOrderItemRepository;
94
 
95
	@Autowired
96
	private LeadRepository leadRepository;
97
 
98
	@Autowired
99
	private HygieneDataRepository hygieneDataRepository;
100
 
101
	@Autowired
102
	private PartnerInvestmentService partnerInvestmentService;
103
 
104
	@Autowired
105
	private PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
106
 
24383 amit.gupta 107
	@Override
24439 govind 108
	public void createTicket(int fofoId, int categoryId, int subcategoryId, String message)
109
			throws ProfitMandiBusinessException {
24417 govind 110
 
24383 amit.gupta 111
		ActivityType type = ActivityType.OPENED;
24439 govind 112
		EscalationType escalationTypeL1 = EscalationType.L1;
113
		EscalationType escalationTypeL2 = EscalationType.L2;
114
		EscalationType escalationTypeL3 = EscalationType.L3;
24383 amit.gupta 115
		Ticket ticket = new Ticket();
24500 govind 116
		TicketAssigned ticketAssigned = new TicketAssigned();
24383 amit.gupta 117
		ticket.setSubCategoryId(subcategoryId);
24417 govind 118
		ticket.setFofoId(fofoId);
24383 amit.gupta 119
		ticket.setCreateTimestamp(LocalDateTime.now());
24417 govind 120
		ticket.setUpdateTimestamp(LocalDateTime.now());
24467 govind 121
		ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
122
		ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
123
		ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
24439 govind 124
		ticket.setHappyCode(getRandomString());
125
		int l1Auth = this.getAuthUserId(categoryId, escalationTypeL1, fofoId);
24699 govind 126
		LOGGER.info("l1Auth" + l1Auth);
24439 govind 127
		int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
24699 govind 128
		LOGGER.info("l2Auth" + l2Auth);
24557 govind 129
		int l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
24699 govind 130
		LOGGER.info("l3Auth" + l3Auth);
24439 govind 131
		if (l1Auth == 0) {
132
			if (l2Auth == 0) {
24500 govind 133
				this.setAssignment(ticket, ticketAssigned);
24439 govind 134
			} else {
24500 govind 135
				ticketAssigned.setAssineeId(l2Auth);
24526 govind 136
				ticket.setL1AuthUser(l2Auth);
137
				ticket.setL2AuthUser(l2Auth);
24439 govind 138
			}
139
		} else {
24500 govind 140
			ticketAssigned.setAssineeId(l1Auth);
24439 govind 141
			ticket.setL1AuthUser(l1Auth);
142
			ticket.setL2AuthUser(l2Auth);
143
		}
24699 govind 144
		if (ticket.getL2AuthUser() == 0) {
24569 govind 145
			ticket.setL2AuthUser(l3Auth);
146
		}
24439 govind 147
		ticket.setL3AuthUser(l3Auth);
148
		ticketRepository.persist(ticket);
24500 govind 149
		ticketAssigned.setTicketId(ticket.getId());
150
		ticketAssignedRepository.persist(ticketAssigned);
151
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
24569 govind 152
		this.sendAssignedTicketMail(authUser, ticket);
24383 amit.gupta 153
		Activity activity = this.createActivity(type, message, 0);
154
		this.addActivity(ticket.getId(), activity);
155
	}
156
 
157
	private Activity createActivity(ActivityType activityType, String message, int createdBy) {
158
		Activity activity = new Activity();
159
		activity.setMessage(message);
160
		activity.setCreatedBy(createdBy);
161
		activity.setType(activityType);
24417 govind 162
		activity.setCreateTimestamp(LocalDateTime.now());
24383 amit.gupta 163
		return activity;
164
	}
24417 govind 165
 
24439 govind 166
	private int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
24417 govind 167
 
24439 govind 168
		List<Position> positions = null;
169
		if (escalationType == EscalationType.L3) {
170
			positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
171
		} else {
172
			List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
173
			LOGGER.info("regions=" + regions);
24500 govind 174
			if (regions.size() == 0) {
175
				regions = partnerRegionRepository.selectByfofoId(0);
24452 govind 176
				LOGGER.info("regions=" + regions);
177
			}
24439 govind 178
			for (PartnerRegion region : regions) {
24699 govind 179
				LOGGER.info(categoryId + ":" + escalationType + ":" + region.getRegionId());
24439 govind 180
				positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
181
						region.getRegionId());
182
				LOGGER.info("positions:-" + positions);
24500 govind 183
				if (positions.size() > 0) {
24439 govind 184
					break;
185
				}
186
 
187
			}
188
			if (positions.size() == 0) {
24557 govind 189
				if (escalationType == EscalationType.L1) {
190
					regions = partnerRegionRepository.selectByfofoId(0);
191
					LOGGER.info("regions=" + regions);
192
					for (PartnerRegion region : regions) {
193
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
194
								escalationType, region.getRegionId());
195
						LOGGER.info("positions:-" + positions);
196
						if (positions.size() > 0) {
197
							break;
198
						}
199
					}
200
				} else {
201
					regions = partnerRegionRepository.selectByfofoId(0);
202
					LOGGER.info("regions=" + regions);
203
					for (PartnerRegion region : regions) {
204
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
205
								escalationType, region.getRegionId());
206
						LOGGER.info("positions:-" + positions);
207
						if (positions.size() > 0) {
208
							break;
209
						}
210
					}
211
				}
24439 govind 212
			}
24699 govind 213
 
24439 govind 214
		}
24699 govind 215
		if (positions.size() == 0) {
24557 govind 216
			return 0;
217
		}
218
		LOGGER.info(positions.get(0).getAuthUserId());
24439 govind 219
		return positions.get(0).getAuthUserId();
24417 govind 220
		/*
221
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
222
		 * //escalationMatrix } else { throw new
223
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
224
		 * "Could not find Assignee for "); }
225
		 */
24439 govind 226
 
24383 amit.gupta 227
	}
228
 
229
	@Override
230
	public void addActivity(int ticketId, Activity activity) {
231
		activity.setTicketId(ticketId);
24417 govind 232
		activityRepository.persist(activity);
24383 amit.gupta 233
	}
234
 
24439 govind 235
	private String getRandomString() {
236
		int length = 4;
237
		boolean useLetters = false;
238
		boolean useNumbers = true;
239
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
240
		return generatedString;
241
	}
242
 
24417 govind 243
	@Override
244
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
245
 
24557 govind 246
		for (int fofoId : fofoIds) {
247
			PartnerRegion partnerRegion = new PartnerRegion();
248
			partnerRegion.setFofoId(fofoId);
249
			partnerRegion.setRegionId(regionId);
250
			partnerRegionRepository.persist(partnerRegion);
24417 govind 251
		}
252
	}
253
 
254
	@Override
24500 govind 255
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
24417 govind 256
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
24500 govind 257
 
258
		for (TicketAssigned ticketAssigned : ticketAssigneds) {
259
			AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
260
			authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
261
		}
262
		return authUserIdAndAuthUserMap;
263
	}
264
 
265
	@Override
266
	public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
267
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
268
 
24417 govind 269
		for (Ticket ticket : tickets) {
24500 govind 270
			AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
271
			authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
24417 govind 272
		}
273
		return authUserIdAndAuthUserMap;
274
	}
275
 
276
	@Override
277
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
278
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
24699 govind 279
		if (tickets != null) {
280
			for (Ticket ticket : tickets) {
281
				TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
282
				subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
283
			}
24417 govind 284
		}
285
		return subCategoryIdAndSubCategoryMap;
286
	}
287
 
288
	@Override
289
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
290
		List<Integer> fofoIds = new ArrayList<>();
24569 govind 291
		LOGGER.info(tickets);
24699 govind 292
		if (tickets == null) {
24569 govind 293
			return null;
294
		}
24417 govind 295
		for (Ticket ticket : tickets) {
296
			fofoIds.add(ticket.getFofoId());
297
		}
24699 govind 298
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
24417 govind 299
		return fofoIdsAndCustomRetailer;
300
	}
301
 
24439 govind 302
	@Override
303
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
304
 
305
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
306
		HashSet<Integer> categoryIds = new HashSet<>();
307
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
308
			categoryIds.add(ticketSubcategory.getcategoryId());
309
		}
310
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
311
		return ticketcategories;
312
	}
313
 
24500 govind 314
	private Ticket setAssignment(Ticket ticket, TicketAssigned ticketAssigned) {
24439 govind 315
		TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
316
		List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
317
		for (Position position : positions) {
318
			if (position.getEscalationType().equals(EscalationType.L1)) {
24500 govind 319
				ticketAssigned.setAssineeId(position.getAuthUserId());
24439 govind 320
				ticket.setL1AuthUser(position.getAuthUserId());
24557 govind 321
			} else if (position.getEscalationType().equals(EscalationType.L2)) {
24439 govind 322
				ticket.setL2AuthUser(position.getAuthUserId());
323
			}
324
		}
325
		return ticket;
326
	}
24500 govind 327
 
24471 govind 328
	@Override
329
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
330
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
331
		for (Position position : positions) {
332
			AuthUser authUser = authRepository.selectById(position.getAuthUserId());
333
			authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
334
		}
335
		return authUserIdAndAuthUserMap;
336
	}
24500 govind 337
 
24471 govind 338
	@Override
339
	public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
340
		Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
341
		for (Position position : positions) {
24500 govind 342
			TicketCategory ticketCategory = ticketCategoryRepository.selectById(position.getCategoryId());
24471 govind 343
			categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
344
		}
345
		return categoryIdAndCategoryMap;
346
	}
24439 govind 347
 
24471 govind 348
	@Override
349
	public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
350
		Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
351
		for (Position position : positions) {
24500 govind 352
			Region region = regionRepository.selectById(position.getRegionId());
24471 govind 353
			regionIdAndRegionMap.put(position.getRegionId(), region);
354
		}
355
		return regionIdAndRegionMap;
356
	}
24500 govind 357
 
358
	@Override
24699 govind 359
	public Map<Integer, List<AuthUser>> getAuthUserList(List<Ticket> tickets, AuthUser authUser) {
24500 govind 360
		Map<Integer, List<AuthUser>> authUserListMap = new HashMap<>();
361
		for (Ticket ticket : tickets) {
24570 govind 362
			if (ticket.getL2AuthUser() == authUser.getId()) {
24500 govind 363
				List<AuthUser> authUsers = new ArrayList<>();
364
				authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
365
				authUserListMap.put(ticket.getId(), authUsers);
366
 
24570 govind 367
			} else if (ticket.getL3AuthUser() == authUser.getId()) {
24500 govind 368
				TicketAssigned ticketAssigned = ticketAssignedRepository
369
						.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(), ticket.getId());
370
				if (ticketAssigned == null) {
371
					List<AuthUser> authUsers = new ArrayList<>();
372
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
373
					authUserListMap.put(ticket.getId(), authUsers);
374
				} else {
375
					List<AuthUser> authUsers = new ArrayList<>();
376
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
377
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
378
					authUserListMap.put(ticket.getId(), authUsers);
379
				}
380
			} else {
381
				TicketAssigned ticketAssigned = ticketAssignedRepository
382
						.selectByAssigneeIdAndTicketId(ticket.getL3AuthUser(), ticket.getId());
383
				if (ticketAssigned == null) {
384
					ticketAssigned = ticketAssignedRepository.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(),
385
							ticket.getId());
386
					if (ticketAssigned == null) {
387
						List<AuthUser> authUsers = new ArrayList<>();
388
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
389
						authUserListMap.put(ticket.getId(), authUsers);
390
					} else {
391
						List<AuthUser> authUsers = new ArrayList<>();
392
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
393
						authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
394
						authUserListMap.put(ticket.getId(), authUsers);
395
					}
396
				} else {
397
					List<AuthUser> authUsers = new ArrayList<>();
398
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
399
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
400
					authUsers.add(authRepository.selectById(ticket.getL3AuthUser()));
401
					authUserListMap.put(ticket.getId(), authUsers);
402
 
403
				}
404
			}
405
		}
406
		return authUserListMap;
407
	}
24557 govind 408
 
24500 govind 409
	@Override
24557 govind 410
	public void updateTicket(int categoryId, int subCategoryId, Ticket ticket) throws ProfitMandiBusinessException {
411
		TicketAssigned ticketAssigned = new TicketAssigned();
24500 govind 412
		ticket.setSubCategoryId(subCategoryId);
413
		ticket.setUpdateTimestamp(LocalDateTime.now());
414
		ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
415
		ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
416
		ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
417
		int l3Auth = this.getAuthUserId(categoryId, EscalationType.L3, ticket.getFofoId());
25505 amit.gupta 418
		int l2Auth = this.getAuthUserId(categoryId, EscalationType.L2, ticket.getFofoId());
24500 govind 419
		int l1Auth = this.getAuthUserId(categoryId, EscalationType.L1, ticket.getFofoId());
420
		LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
421
		if (l1Auth == 0) {
422
			if (l2Auth == 0) {
423
				this.setAssignment(ticket, ticketAssigned);
424
			} else {
425
				ticketAssigned.setAssineeId(l2Auth);
24569 govind 426
				ticket.setL1AuthUser(0);
24536 govind 427
				ticket.setL2AuthUser(l2Auth);
24500 govind 428
			}
429
		} else {
430
			ticketAssigned.setAssineeId(l1Auth);
431
			ticket.setL1AuthUser(l1Auth);
432
			ticket.setL2AuthUser(l2Auth);
433
		}
434
		ticket.setL3AuthUser(l3Auth);
435
		ticketRepository.persist(ticket);
436
		ticketAssigned.setTicketId(ticket.getId());
437
		ticketAssignedRepository.persist(ticketAssigned);
438
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
24569 govind 439
		this.sendAssignedTicketMail(authUser, ticket);
440
	}
441
 
442
	@Override
24699 govind 443
	public void sendAssignedTicketMail(AuthUser authUser, Ticket ticket) throws ProfitMandiBusinessException {
24500 govind 444
		try {
445
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
446
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
24699 govind 447
							retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName(), ticket.getId()),
24500 govind 448
					null);
449
		} catch (Exception e) {
450
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
451
					"Could not send ticket assignment mail");
452
		}
453
	}
25570 tejbeer 454
 
455
	@Override
456
	public Map<Integer, List<CustomRetailer>> getPositionCustomRetailerMap(List<Position> positions) {
457
		Map<Integer, List<CustomRetailer>> positionRetailerMap = new HashMap<>();
458
		for (Position position : positions) {
459
			List<Integer> fofoIds = partnersPositionRepository.selectByPositionId(position.getId()).stream()
460
					.map(x -> x.getFofoId()).collect(Collectors.toList());
461
 
462
			if (!fofoIds.isEmpty()) {
463
				if (fofoIds.contains(0)) {
464
					fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
465
							.map(x -> x.getFofoId()).collect(Collectors.toList());
466
					if (fofoIds.contains(0)) {
467
						fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
468
								.collect(Collectors.toList());
469
					}
470
 
471
				}
472
				positionRetailerMap.put(position.getId(),
473
						new ArrayList<CustomRetailer>(retailerService.getFofoRetailers(fofoIds).values()));
474
 
475
			}
476
		}
477
		return positionRetailerMap;
478
	}
479
 
480
	@Override
481
	public Map<Integer, List<CustomRetailer>> getpositionIdAndpartnerRegionMap(List<Position> positions) {
482
		Map<Integer, List<CustomRetailer>> positionIdAndpartnerRegionMap = new HashedMap<>();
483
		for (Position position : positions) {
484
			List<Integer> fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
485
					.map(x -> x.getFofoId()).collect(Collectors.toList());
486
 
487
			if (!fofoIds.isEmpty()) {
488
				if (fofoIds.contains(0)) {
489
					fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());
490
 
491
				}
492
 
493
				Map<Integer, CustomRetailer> fofoRetailers = retailerService.getFofoRetailers(fofoIds);
494
				positionIdAndpartnerRegionMap.put(position.getRegionId(),
495
						new ArrayList<CustomRetailer>(fofoRetailers.values()));
496
			}
497
 
498
		}
499
 
500
		return positionIdAndpartnerRegionMap;
501
	}
25597 amit.gupta 502
 
503
	@Override
25726 amit.gupta 504
	@Cacheable(value = "authUserEmailMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25597 amit.gupta 505
	public Map<String, Set<String>> getAuthUserPartnerEmailMapping() {
506
		Map<String, Set<String>> storeGuyMap = new HashMap<>();
507
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
508
				.collect(Collectors.toSet());
509
		List<Position> categoryPositions = positionRepository
510
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
511
		Map<Integer, Position> positionsMap = categoryPositions.stream()
512
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
513
 
25721 tejbeer 514
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
25597 amit.gupta 515
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
516
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
517
			Set<String> partnerEmails = positionPartnerEntry.getValue().stream()
518
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail())
519
					.collect(Collectors.toSet());
520
			AuthUser authUser = authRepository.selectById(authUserId);
521
			if (authUser.isActive()) {
26298 tejbeer 522
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
26111 amit.gupta 523
					storeGuyMap.put(authUser.getEmailId(), partnerEmails);
524
				} else {
525
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
526
				}
25597 amit.gupta 527
			}
528
		}
529
		return storeGuyMap;
530
	}
25721 tejbeer 531
 
532
	@Override
25777 amit.gupta 533
	@Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25721 tejbeer 534
	public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() {
535
		Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
25777 amit.gupta 536
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
537
				.collect(Collectors.toSet());
25721 tejbeer 538
		List<Position> categoryPositions = positionRepository
539
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
540
		Map<Integer, Position> positionsMap = categoryPositions.stream()
541
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
542
 
543
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
544
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
545
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
25777 amit.gupta 546
			Set<Integer> partnerEmails = positionPartnerEntry.getValue().stream()
547
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
25721 tejbeer 548
					.collect(Collectors.toSet());
549
			AuthUser authUser = authRepository.selectById(authUserId);
550
			if (authUser.isActive()) {
26298 tejbeer 551
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
26111 amit.gupta 552
					storeGuyMap.put(authUser.getEmailId(), partnerEmails);
553
				} else {
554
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
555
				}
25721 tejbeer 556
			}
557
		}
558
		return storeGuyMap;
559
	}
560
 
561
	@Override
562
	public List<String> getAuthUserByPartnerId(int fofoId) {
563
 
564
		List<String> emails = new ArrayList<>();
565
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
566
				.collect(Collectors.toList());
567
 
568
		regionIds.add(5);// All partners Id;
569
		List<Integer> fofoIds = new ArrayList<>();
570
		fofoIds.add(fofoId);
571
		fofoIds.add(0);
25799 tejbeer 572
 
573
		LOGGER.info("fofoIds" + fofoIds);
25721 tejbeer 574
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
575
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
576
 
25799 tejbeer 577
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
578
 
25721 tejbeer 579
		for (Integer partnerPostionId : partnerPositionIds) {
25799 tejbeer 580
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
581
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
582
			LOGGER.info("position" + position);
583
			if (position != null) {
25721 tejbeer 584
 
25799 tejbeer 585
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
586
				LOGGER.info("authUser" + authUser);
587
				emails.add(authUser.getEmailId());
588
			}
25721 tejbeer 589
		}
590
 
591
		return emails;
592
	}
26298 tejbeer 593
 
594
	@Override
595
	@Cacheable(value = "authUserIdPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
596
	public Map<Integer, List<Integer>> getAuthUserIdPartnerIdMapping() {
597
		Map<Integer, List<Integer>> storeGuyMap = new HashMap<>();
598
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
599
				.collect(Collectors.toSet());
600
		List<Position> categoryPositions = positionRepository
601
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
602
		Map<Integer, Position> positionsMap = categoryPositions.stream()
603
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
604
 
605
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
606
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
607
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
608
			List<Integer> partnerEmails = positionPartnerEntry.getValue().stream()
609
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
610
					.collect(Collectors.toList());
611
			AuthUser authUser = authRepository.selectById(authUserId);
612
			if (authUser.isActive()) {
26960 amit.gupta 613
				if (!storeGuyMap.containsKey(authUserId)) {
614
					storeGuyMap.put(authUserId, partnerEmails);
26298 tejbeer 615
				} else {
26960 amit.gupta 616
					storeGuyMap.get(authUserId).addAll(partnerEmails);
26298 tejbeer 617
				}
618
			}
619
		}
620
		return storeGuyMap;
621
	}
622
 
623
	@Override
624
	@Cacheable(value = "L1L2Mapping", cacheManager = "thirtyMinsTimeOutCacheManager")
26460 amit.gupta 625
	public Map<Integer, List<Integer>> getL2L1Mapping() {
26448 amit.gupta 626
		List<Position> l1SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
627
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1);
628
		List<Position> l2SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
629
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2);
26298 tejbeer 630
 
26448 amit.gupta 631
		LOGGER.info("position" + l1SalesPositions);
632
		Map<Integer, List<Integer>> authUserPartnerListMap = this.getAuthUserIdPartnerIdMapping();
633
		LOGGER.info("map" + authUserPartnerListMap);
26298 tejbeer 634
 
26448 amit.gupta 635
		Map<Integer, List<Integer>> l2l1ListMap = new HashMap<>();
26298 tejbeer 636
 
26448 amit.gupta 637
		for (Position l2SalePosition : l2SalesPositions) {
638
			int l2AuthUserId = l2SalePosition.getAuthUserId();
639
			if (authUserPartnerListMap.containsKey(l2SalePosition.getAuthUserId())) {
640
				List<Integer> mappedL1AuthUsers = new ArrayList<>();
641
				l2l1ListMap.put(l2AuthUserId, mappedL1AuthUsers);
642
				List<Integer> l2FofoIds = authUserPartnerListMap.get(l2AuthUserId);
643
				for (Position l1SalePosition : l1SalesPositions) {
644
					int l1AuthUserId = l1SalePosition.getAuthUserId();
645
					if (authUserPartnerListMap.containsKey(l1AuthUserId)) {
646
						List<Integer> l1FofoIds = authUserPartnerListMap.get(l1SalePosition.getAuthUserId());
647
						if (l2FofoIds.containsAll(l1FofoIds)) {
648
							mappedL1AuthUsers.add(l1AuthUserId);
649
						}
26298 tejbeer 650
 
26448 amit.gupta 651
					}
26298 tejbeer 652
 
653
				}
654
			}
655
 
656
		}
657
 
26448 amit.gupta 658
		return l2l1ListMap;
26298 tejbeer 659
	}
26448 amit.gupta 660
 
24383 amit.gupta 661
}