Subversion Repositories SmartDukaan

Rev

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