Subversion Repositories SmartDukaan

Rev

Rev 27044 | Rev 27124 | 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
 
27078 amit.gupta 166
	@Override
167
	public int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
24417 govind 168
 
24439 govind 169
		List<Position> positions = null;
170
		if (escalationType == EscalationType.L3) {
171
			positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
172
		} else {
173
			List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
174
			LOGGER.info("regions=" + regions);
24500 govind 175
			if (regions.size() == 0) {
176
				regions = partnerRegionRepository.selectByfofoId(0);
24452 govind 177
				LOGGER.info("regions=" + regions);
178
			}
24439 govind 179
			for (PartnerRegion region : regions) {
24699 govind 180
				LOGGER.info(categoryId + ":" + escalationType + ":" + region.getRegionId());
24439 govind 181
				positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
182
						region.getRegionId());
183
				LOGGER.info("positions:-" + positions);
24500 govind 184
				if (positions.size() > 0) {
24439 govind 185
					break;
186
				}
187
 
188
			}
189
			if (positions.size() == 0) {
24557 govind 190
				if (escalationType == EscalationType.L1) {
191
					regions = partnerRegionRepository.selectByfofoId(0);
192
					LOGGER.info("regions=" + regions);
193
					for (PartnerRegion region : regions) {
194
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
195
								escalationType, region.getRegionId());
196
						LOGGER.info("positions:-" + positions);
197
						if (positions.size() > 0) {
198
							break;
199
						}
200
					}
201
				} else {
202
					regions = partnerRegionRepository.selectByfofoId(0);
203
					LOGGER.info("regions=" + regions);
204
					for (PartnerRegion region : regions) {
205
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
206
								escalationType, region.getRegionId());
207
						LOGGER.info("positions:-" + positions);
208
						if (positions.size() > 0) {
209
							break;
210
						}
211
					}
212
				}
24439 govind 213
			}
24699 govind 214
 
24439 govind 215
		}
24699 govind 216
		if (positions.size() == 0) {
24557 govind 217
			return 0;
218
		}
219
		LOGGER.info(positions.get(0).getAuthUserId());
24439 govind 220
		return positions.get(0).getAuthUserId();
24417 govind 221
		/*
222
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
223
		 * //escalationMatrix } else { throw new
224
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
225
		 * "Could not find Assignee for "); }
226
		 */
24439 govind 227
 
24383 amit.gupta 228
	}
229
 
230
	@Override
231
	public void addActivity(int ticketId, Activity activity) {
232
		activity.setTicketId(ticketId);
24417 govind 233
		activityRepository.persist(activity);
24383 amit.gupta 234
	}
235
 
24439 govind 236
	private String getRandomString() {
237
		int length = 4;
238
		boolean useLetters = false;
239
		boolean useNumbers = true;
240
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
241
		return generatedString;
242
	}
243
 
24417 govind 244
	@Override
245
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
246
 
24557 govind 247
		for (int fofoId : fofoIds) {
248
			PartnerRegion partnerRegion = new PartnerRegion();
249
			partnerRegion.setFofoId(fofoId);
250
			partnerRegion.setRegionId(regionId);
251
			partnerRegionRepository.persist(partnerRegion);
24417 govind 252
		}
253
	}
254
 
255
	@Override
24500 govind 256
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
24417 govind 257
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
24500 govind 258
 
259
		for (TicketAssigned ticketAssigned : ticketAssigneds) {
260
			AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
261
			authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
262
		}
263
		return authUserIdAndAuthUserMap;
264
	}
265
 
266
	@Override
267
	public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
268
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
269
 
24417 govind 270
		for (Ticket ticket : tickets) {
24500 govind 271
			AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
272
			authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
24417 govind 273
		}
274
		return authUserIdAndAuthUserMap;
275
	}
276
 
277
	@Override
278
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
279
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
24699 govind 280
		if (tickets != null) {
281
			for (Ticket ticket : tickets) {
282
				TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
283
				subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
284
			}
24417 govind 285
		}
286
		return subCategoryIdAndSubCategoryMap;
287
	}
288
 
289
	@Override
290
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
291
		List<Integer> fofoIds = new ArrayList<>();
24569 govind 292
		LOGGER.info(tickets);
24699 govind 293
		if (tickets == null) {
24569 govind 294
			return null;
295
		}
24417 govind 296
		for (Ticket ticket : tickets) {
297
			fofoIds.add(ticket.getFofoId());
298
		}
24699 govind 299
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
24417 govind 300
		return fofoIdsAndCustomRetailer;
301
	}
302
 
24439 govind 303
	@Override
304
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
305
 
306
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
307
		HashSet<Integer> categoryIds = new HashSet<>();
308
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
309
			categoryIds.add(ticketSubcategory.getcategoryId());
310
		}
311
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
312
		return ticketcategories;
313
	}
314
 
24500 govind 315
	private Ticket setAssignment(Ticket ticket, TicketAssigned ticketAssigned) {
24439 govind 316
		TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
317
		List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
318
		for (Position position : positions) {
319
			if (position.getEscalationType().equals(EscalationType.L1)) {
24500 govind 320
				ticketAssigned.setAssineeId(position.getAuthUserId());
24439 govind 321
				ticket.setL1AuthUser(position.getAuthUserId());
24557 govind 322
			} else if (position.getEscalationType().equals(EscalationType.L2)) {
24439 govind 323
				ticket.setL2AuthUser(position.getAuthUserId());
324
			}
325
		}
326
		return ticket;
327
	}
24500 govind 328
 
24471 govind 329
	@Override
330
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
331
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
332
		for (Position position : positions) {
333
			AuthUser authUser = authRepository.selectById(position.getAuthUserId());
334
			authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
335
		}
336
		return authUserIdAndAuthUserMap;
337
	}
24500 govind 338
 
24471 govind 339
	@Override
340
	public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
341
		Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
342
		for (Position position : positions) {
24500 govind 343
			TicketCategory ticketCategory = ticketCategoryRepository.selectById(position.getCategoryId());
24471 govind 344
			categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
345
		}
346
		return categoryIdAndCategoryMap;
347
	}
24439 govind 348
 
24471 govind 349
	@Override
350
	public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
351
		Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
352
		for (Position position : positions) {
24500 govind 353
			Region region = regionRepository.selectById(position.getRegionId());
24471 govind 354
			regionIdAndRegionMap.put(position.getRegionId(), region);
355
		}
356
		return regionIdAndRegionMap;
357
	}
24500 govind 358
 
359
	@Override
24699 govind 360
	public Map<Integer, List<AuthUser>> getAuthUserList(List<Ticket> tickets, AuthUser authUser) {
24500 govind 361
		Map<Integer, List<AuthUser>> authUserListMap = new HashMap<>();
362
		for (Ticket ticket : tickets) {
24570 govind 363
			if (ticket.getL2AuthUser() == authUser.getId()) {
24500 govind 364
				List<AuthUser> authUsers = new ArrayList<>();
365
				authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
366
				authUserListMap.put(ticket.getId(), authUsers);
367
 
24570 govind 368
			} else if (ticket.getL3AuthUser() == authUser.getId()) {
24500 govind 369
				TicketAssigned ticketAssigned = ticketAssignedRepository
370
						.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(), ticket.getId());
371
				if (ticketAssigned == null) {
372
					List<AuthUser> authUsers = new ArrayList<>();
373
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
374
					authUserListMap.put(ticket.getId(), authUsers);
375
				} else {
376
					List<AuthUser> authUsers = new ArrayList<>();
377
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
378
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
379
					authUserListMap.put(ticket.getId(), authUsers);
380
				}
381
			} else {
382
				TicketAssigned ticketAssigned = ticketAssignedRepository
383
						.selectByAssigneeIdAndTicketId(ticket.getL3AuthUser(), ticket.getId());
384
				if (ticketAssigned == null) {
385
					ticketAssigned = ticketAssignedRepository.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(),
386
							ticket.getId());
387
					if (ticketAssigned == null) {
388
						List<AuthUser> authUsers = new ArrayList<>();
389
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
390
						authUserListMap.put(ticket.getId(), authUsers);
391
					} else {
392
						List<AuthUser> authUsers = new ArrayList<>();
393
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
394
						authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
395
						authUserListMap.put(ticket.getId(), authUsers);
396
					}
397
				} else {
398
					List<AuthUser> authUsers = new ArrayList<>();
399
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
400
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
401
					authUsers.add(authRepository.selectById(ticket.getL3AuthUser()));
402
					authUserListMap.put(ticket.getId(), authUsers);
403
 
404
				}
405
			}
406
		}
407
		return authUserListMap;
408
	}
24557 govind 409
 
24500 govind 410
	@Override
24557 govind 411
	public void updateTicket(int categoryId, int subCategoryId, Ticket ticket) throws ProfitMandiBusinessException {
412
		TicketAssigned ticketAssigned = new TicketAssigned();
24500 govind 413
		ticket.setSubCategoryId(subCategoryId);
414
		ticket.setUpdateTimestamp(LocalDateTime.now());
415
		ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
416
		ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
417
		ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
418
		int l3Auth = this.getAuthUserId(categoryId, EscalationType.L3, ticket.getFofoId());
25505 amit.gupta 419
		int l2Auth = this.getAuthUserId(categoryId, EscalationType.L2, ticket.getFofoId());
24500 govind 420
		int l1Auth = this.getAuthUserId(categoryId, EscalationType.L1, ticket.getFofoId());
421
		LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
422
		if (l1Auth == 0) {
423
			if (l2Auth == 0) {
424
				this.setAssignment(ticket, ticketAssigned);
425
			} else {
426
				ticketAssigned.setAssineeId(l2Auth);
24569 govind 427
				ticket.setL1AuthUser(0);
24536 govind 428
				ticket.setL2AuthUser(l2Auth);
24500 govind 429
			}
430
		} else {
431
			ticketAssigned.setAssineeId(l1Auth);
432
			ticket.setL1AuthUser(l1Auth);
433
			ticket.setL2AuthUser(l2Auth);
434
		}
435
		ticket.setL3AuthUser(l3Auth);
436
		ticketRepository.persist(ticket);
437
		ticketAssigned.setTicketId(ticket.getId());
438
		ticketAssignedRepository.persist(ticketAssigned);
439
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
24569 govind 440
		this.sendAssignedTicketMail(authUser, ticket);
441
	}
442
 
443
	@Override
24699 govind 444
	public void sendAssignedTicketMail(AuthUser authUser, Ticket ticket) throws ProfitMandiBusinessException {
24500 govind 445
		try {
446
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
447
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
24699 govind 448
							retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName(), ticket.getId()),
24500 govind 449
					null);
450
		} catch (Exception e) {
451
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
452
					"Could not send ticket assignment mail");
453
		}
454
	}
25570 tejbeer 455
 
456
	@Override
457
	public Map<Integer, List<CustomRetailer>> getPositionCustomRetailerMap(List<Position> positions) {
458
		Map<Integer, List<CustomRetailer>> positionRetailerMap = new HashMap<>();
459
		for (Position position : positions) {
460
			List<Integer> fofoIds = partnersPositionRepository.selectByPositionId(position.getId()).stream()
461
					.map(x -> x.getFofoId()).collect(Collectors.toList());
462
 
26992 amit.gupta 463
			LOGGER.info("fofoIds - {}", fofoIds);
25570 tejbeer 464
			if (!fofoIds.isEmpty()) {
465
				if (fofoIds.contains(0)) {
466
					fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
467
							.map(x -> x.getFofoId()).collect(Collectors.toList());
468
					if (fofoIds.contains(0)) {
469
						fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
470
								.collect(Collectors.toList());
471
					}
472
 
473
				}
26991 amit.gupta 474
				LOGGER.info("fofoIds - {}", fofoIds);
25570 tejbeer 475
				positionRetailerMap.put(position.getId(),
476
						new ArrayList<CustomRetailer>(retailerService.getFofoRetailers(fofoIds).values()));
477
 
478
			}
479
		}
480
		return positionRetailerMap;
481
	}
482
 
483
	@Override
484
	public Map<Integer, List<CustomRetailer>> getpositionIdAndpartnerRegionMap(List<Position> positions) {
485
		Map<Integer, List<CustomRetailer>> positionIdAndpartnerRegionMap = new HashedMap<>();
486
		for (Position position : positions) {
487
			List<Integer> fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
488
					.map(x -> x.getFofoId()).collect(Collectors.toList());
489
 
490
			if (!fofoIds.isEmpty()) {
491
				if (fofoIds.contains(0)) {
492
					fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());
493
 
494
				}
495
 
496
				Map<Integer, CustomRetailer> fofoRetailers = retailerService.getFofoRetailers(fofoIds);
497
				positionIdAndpartnerRegionMap.put(position.getRegionId(),
498
						new ArrayList<CustomRetailer>(fofoRetailers.values()));
499
			}
500
 
501
		}
502
 
503
		return positionIdAndpartnerRegionMap;
504
	}
25597 amit.gupta 505
 
506
	@Override
25726 amit.gupta 507
	@Cacheable(value = "authUserEmailMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25597 amit.gupta 508
	public Map<String, Set<String>> getAuthUserPartnerEmailMapping() {
509
		Map<String, Set<String>> storeGuyMap = new HashMap<>();
510
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
511
				.collect(Collectors.toSet());
512
		List<Position> categoryPositions = positionRepository
513
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
514
		Map<Integer, Position> positionsMap = categoryPositions.stream()
515
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
516
 
25721 tejbeer 517
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
25597 amit.gupta 518
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
519
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
520
			Set<String> partnerEmails = positionPartnerEntry.getValue().stream()
521
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail())
522
					.collect(Collectors.toSet());
523
			AuthUser authUser = authRepository.selectById(authUserId);
524
			if (authUser.isActive()) {
26298 tejbeer 525
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
26111 amit.gupta 526
					storeGuyMap.put(authUser.getEmailId(), partnerEmails);
527
				} else {
528
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
529
				}
25597 amit.gupta 530
			}
531
		}
532
		return storeGuyMap;
533
	}
25721 tejbeer 534
 
535
	@Override
25777 amit.gupta 536
	@Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25721 tejbeer 537
	public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() {
538
		Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
25777 amit.gupta 539
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
540
				.collect(Collectors.toSet());
25721 tejbeer 541
		List<Position> categoryPositions = positionRepository
542
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
543
		Map<Integer, Position> positionsMap = categoryPositions.stream()
544
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
545
 
546
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
547
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
548
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
27044 amit.gupta 549
			Set<Integer> partnerIds = positionPartnerEntry.getValue().stream()
25777 amit.gupta 550
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
25721 tejbeer 551
					.collect(Collectors.toSet());
552
			AuthUser authUser = authRepository.selectById(authUserId);
553
			if (authUser.isActive()) {
26298 tejbeer 554
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
27044 amit.gupta 555
					storeGuyMap.put(authUser.getEmailId(), partnerIds);
26111 amit.gupta 556
				} else {
27044 amit.gupta 557
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerIds);
26111 amit.gupta 558
				}
25721 tejbeer 559
			}
560
		}
561
		return storeGuyMap;
562
	}
563
 
564
	@Override
565
	public List<String> getAuthUserByPartnerId(int fofoId) {
566
 
567
		List<String> emails = new ArrayList<>();
568
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
569
				.collect(Collectors.toList());
570
 
571
		regionIds.add(5);// All partners Id;
572
		List<Integer> fofoIds = new ArrayList<>();
573
		fofoIds.add(fofoId);
574
		fofoIds.add(0);
25799 tejbeer 575
 
576
		LOGGER.info("fofoIds" + fofoIds);
25721 tejbeer 577
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
578
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
579
 
25799 tejbeer 580
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
581
 
25721 tejbeer 582
		for (Integer partnerPostionId : partnerPositionIds) {
25799 tejbeer 583
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
584
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
585
			LOGGER.info("position" + position);
586
			if (position != null) {
25721 tejbeer 587
 
25799 tejbeer 588
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
589
				LOGGER.info("authUser" + authUser);
590
				emails.add(authUser.getEmailId());
591
			}
25721 tejbeer 592
		}
593
 
594
		return emails;
595
	}
26298 tejbeer 596
 
597
	@Override
26978 tejbeer 598
	public Map<EscalationType, String> getAuthUserAndEsclationTypeByPartnerId(int fofoId) {
599
 
600
		List<String> emails = new ArrayList<>();
601
		Map<EscalationType, String> emailEsclationTypeMap = new HashMap<>();
602
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
603
				.collect(Collectors.toList());
604
 
605
		regionIds.add(5);// All partners Id;
606
		List<Integer> fofoIds = new ArrayList<>();
607
		fofoIds.add(fofoId);
608
		fofoIds.add(0);
609
 
610
		LOGGER.info("fofoIds" + fofoIds);
611
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
612
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
613
 
614
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
615
 
616
		for (Integer partnerPostionId : partnerPositionIds) {
617
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
618
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
619
			LOGGER.info("position" + position);
620
			if (position != null) {
621
 
622
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
623
				LOGGER.info("authUser" + authUser);
624
 
625
				emailEsclationTypeMap.put(position.getEscalationType(), authUser.getEmailId());
626
			}
627
		}
628
 
629
		LOGGER.info("emailEsclationTypeMap" + emailEsclationTypeMap);
630
 
631
		return emailEsclationTypeMap;
632
	}
633
 
634
	@Override
26298 tejbeer 635
	@Cacheable(value = "authUserIdPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
636
	public Map<Integer, List<Integer>> getAuthUserIdPartnerIdMapping() {
637
		Map<Integer, List<Integer>> storeGuyMap = new HashMap<>();
638
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
639
				.collect(Collectors.toSet());
640
		List<Position> categoryPositions = positionRepository
641
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
642
		Map<Integer, Position> positionsMap = categoryPositions.stream()
643
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
644
 
645
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
646
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
647
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
26991 amit.gupta 648
			List<Integer> partnerIds = positionPartnerEntry.getValue().stream()
26298 tejbeer 649
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
650
					.collect(Collectors.toList());
651
			AuthUser authUser = authRepository.selectById(authUserId);
27044 amit.gupta 652
			if (authUser != null && authUser.isActive()) {
26960 amit.gupta 653
				if (!storeGuyMap.containsKey(authUserId)) {
26991 amit.gupta 654
					storeGuyMap.put(authUserId, partnerIds);
26298 tejbeer 655
				} else {
26991 amit.gupta 656
					storeGuyMap.get(authUserId).addAll(partnerIds);
26298 tejbeer 657
				}
658
			}
659
		}
660
		return storeGuyMap;
661
	}
662
 
663
	@Override
664
	@Cacheable(value = "L1L2Mapping", cacheManager = "thirtyMinsTimeOutCacheManager")
26460 amit.gupta 665
	public Map<Integer, List<Integer>> getL2L1Mapping() {
26448 amit.gupta 666
		List<Position> l1SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
667
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1);
668
		List<Position> l2SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
669
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2);
26298 tejbeer 670
 
26448 amit.gupta 671
		LOGGER.info("position" + l1SalesPositions);
672
		Map<Integer, List<Integer>> authUserPartnerListMap = this.getAuthUserIdPartnerIdMapping();
673
		LOGGER.info("map" + authUserPartnerListMap);
26298 tejbeer 674
 
26448 amit.gupta 675
		Map<Integer, List<Integer>> l2l1ListMap = new HashMap<>();
26298 tejbeer 676
 
26448 amit.gupta 677
		for (Position l2SalePosition : l2SalesPositions) {
678
			int l2AuthUserId = l2SalePosition.getAuthUserId();
679
			if (authUserPartnerListMap.containsKey(l2SalePosition.getAuthUserId())) {
680
				List<Integer> mappedL1AuthUsers = new ArrayList<>();
681
				l2l1ListMap.put(l2AuthUserId, mappedL1AuthUsers);
682
				List<Integer> l2FofoIds = authUserPartnerListMap.get(l2AuthUserId);
683
				for (Position l1SalePosition : l1SalesPositions) {
684
					int l1AuthUserId = l1SalePosition.getAuthUserId();
685
					if (authUserPartnerListMap.containsKey(l1AuthUserId)) {
686
						List<Integer> l1FofoIds = authUserPartnerListMap.get(l1SalePosition.getAuthUserId());
687
						if (l2FofoIds.containsAll(l1FofoIds)) {
688
							mappedL1AuthUsers.add(l1AuthUserId);
689
						}
26298 tejbeer 690
 
26448 amit.gupta 691
					}
26298 tejbeer 692
 
693
				}
694
			}
695
 
696
		}
697
 
26448 amit.gupta 698
		return l2l1ListMap;
26298 tejbeer 699
	}
26448 amit.gupta 700
 
24383 amit.gupta 701
}