Subversion Repositories SmartDukaan

Rev

Rev 29318 | Rev 30003 | 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 com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24417 govind 4
import com.spice.profitmandi.common.model.CustomRetailer;
24439 govind 5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
6
import com.spice.profitmandi.common.util.Utils;
24417 govind 7
import com.spice.profitmandi.dao.entity.auth.AuthUser;
29927 amit.gupta 8
import com.spice.profitmandi.dao.entity.cs.*;
24383 amit.gupta 9
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
24417 govind 10
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
11
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
25570 tejbeer 12
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24417 govind 13
import com.spice.profitmandi.service.user.RetailerService;
29927 amit.gupta 14
import org.apache.commons.collections4.map.HashedMap;
15
import org.apache.commons.lang.RandomStringUtils;
16
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.Logger;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.cache.annotation.Cacheable;
20
import org.springframework.mail.javamail.JavaMailSender;
21
import org.springframework.stereotype.Component;
24383 amit.gupta 22
 
29927 amit.gupta 23
import java.time.LocalDateTime;
24
import java.util.*;
25
import java.util.stream.Collectors;
26
 
24383 amit.gupta 27
@Component
28
public class CsServiceImpl implements CsService {
24417 govind 29
 
27124 amit.gupta 30
	private static final int ALL_PARTNERS_REGION = 5;
31
 
24439 govind 32
	private static final Logger LOGGER = LogManager.getLogger(CsServiceImpl.class);
33
 
27124 amit.gupta 34
	private static final String ASSIGNED_TICKET = "Dear %s, New ticket #%s created by %s has been assigned to you. Regards\nSmartdukaan";
35
	private static final String ESCALATED_TICKET = "Dear %s, Ticket #%s created by %s has been escalated to you. Regards\nSmartdukaan";
36
	private static final String CATEGORY_CHANGED_TICKET = "Dear team, Category of Ticket #%s created by %s has been changed to %s. Regards\nSmartdukaan";
24439 govind 37
	private static final String ASSINMENT_SUBJECT = "Assignment Ticket";
38
 
24383 amit.gupta 39
	@Autowired
40
	TicketRepository ticketRepository;
24417 govind 41
 
24383 amit.gupta 42
	@Autowired
24439 govind 43
	JavaMailSender mailSender;
44
 
45
	@Autowired
24383 amit.gupta 46
	TicketCategoryRepository ticketCategoryRepository;
24417 govind 47
 
24383 amit.gupta 48
	@Autowired
49
	TicketSubCategoryRepository ticketSubCategoryRepository;
24417 govind 50
 
51
	@Autowired
24388 amit.gupta 52
	ActivityRepository activityRepository;
24417 govind 53
 
54
	@Autowired
24439 govind 55
	PartnerRegionRepository partnerRegionRepository;
24417 govind 56
 
57
	@Autowired
58
	private PositionRepository positionRepository;
59
 
60
	@Autowired
61
	private AuthRepository authRepository;
62
 
63
	@Autowired
64
	private RetailerService retailerService;
24500 govind 65
 
24471 govind 66
	@Autowired
67
	private RegionRepository regionRepository;
24417 govind 68
 
24500 govind 69
	@Autowired
70
	private TicketAssignedRepository ticketAssignedRepository;
71
 
25570 tejbeer 72
	@Autowired
73
	private PartnersPositionRepository partnersPositionRepository;
74
 
75
	@Autowired
76
	private FofoStoreRepository fofoStoreRepository;
77
 
24383 amit.gupta 78
	@Override
24439 govind 79
	public void createTicket(int fofoId, int categoryId, int subcategoryId, String message)
80
			throws ProfitMandiBusinessException {
24417 govind 81
 
24383 amit.gupta 82
		ActivityType type = ActivityType.OPENED;
83
		Ticket ticket = new Ticket();
84
		ticket.setSubCategoryId(subcategoryId);
24417 govind 85
		ticket.setFofoId(fofoId);
24383 amit.gupta 86
		ticket.setCreateTimestamp(LocalDateTime.now());
24417 govind 87
		ticket.setUpdateTimestamp(LocalDateTime.now());
24439 govind 88
		ticket.setHappyCode(getRandomString());
27124 amit.gupta 89
		ticketRepository.persist(ticket);
90
		this.addActivity(ticket, this.createActivity(type, message, 0));
91
		this.assignTicket(ticket);
92
 
93
	}
94
 
95
	@Override
96
	public void assignTicket(Ticket ticket) throws ProfitMandiBusinessException {
97
		TicketAssigned ticketAssigned = null;
98
		EscalationType newEscalationType = EscalationType.L1;
27545 tejbeer 99
		if (ticket.getAssignmentId() > 0) {
27124 amit.gupta 100
			ticketAssigned = ticketAssignedRepository.selectById(ticket.getAssignmentId());
101
			newEscalationType = ticketAssigned.getEscalationType().next();
27393 amit.gupta 102
			if (newEscalationType == null || newEscalationType.equals(EscalationType.Final)) {
27124 amit.gupta 103
				LOGGER.info("Cant escalate further");
104
				return;
24439 govind 105
			}
27393 amit.gupta 106
		} else {
107
			ticket.setL1AuthUser(0);
108
			ticket.setL2AuthUser(0);
109
			ticket.setL3AuthUser(0);
110
			ticket.setL4AuthUser(0);
111
			ticket.setL5AuthUser(0);
27124 amit.gupta 112
		}
113
		ticket.setUpdateTimestamp(LocalDateTime.now());
114
		TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
115
		TicketCategory ticketCategory = ticketCategoryRepository.selectById(ticketSubCategory.getCategoryId());
116
		Map<Integer, EscalationType> escalationUserMap = new HashedMap<>();
117
		Set<Integer> assigneeUserIds = new LinkedHashSet<>();
118
		EscalationType escalationTypeNext = newEscalationType;
119
		do {
120
			int assigneeUserId = this.getAuthUserId(ticketCategory.getId(), escalationTypeNext, ticket.getFofoId());
121
			if (assigneeUserId == 0) {
122
				escalationTypeNext = escalationTypeNext.next();
123
				LOGGER.info("Escalation type next {}", escalationTypeNext);
124
				continue;
125
			}
126
			assigneeUserIds.add(assigneeUserId);
127
			escalationUserMap.put(assigneeUserId, escalationTypeNext);
128
			switch (escalationTypeNext) {
129
			case L1:
130
				ticket.setL1AuthUser(assigneeUserId);
131
				break;
132
			case L2:
133
				ticket.setL2AuthUser(assigneeUserId);
134
				break;
135
			case L3:
136
				ticket.setL3AuthUser(assigneeUserId);
137
				break;
138
			case L4:
139
				ticket.setL4AuthUser(assigneeUserId);
140
				break;
141
			case L5:
142
				ticket.setL5AuthUser(assigneeUserId);
143
				break;
144
 
145
			default:
146
				break;
147
			}
148
			escalationTypeNext = escalationTypeNext.next();
149
			// LOGGER.info("Escalation type next {}", escalationTypeNext);
150
		} while (!escalationTypeNext.equals(EscalationType.Final));
151
 
152
		int assigneeAuthId = assigneeUserIds.stream().findFirst().orElse(0);
27545 tejbeer 153
 
27124 amit.gupta 154
		int managerAuthId = 0;
155
		EscalationType assigneeEscalationType = null;
156
		if (assigneeAuthId == 0) {
157
			assigneeAuthId = ProfitMandiConstants.FINAL_AUTH_ID;
158
			assigneeEscalationType = EscalationType.Final;
27393 amit.gupta 159
			managerAuthId = ProfitMandiConstants.FINAL_AUTH_ID;
24439 govind 160
		} else {
27124 amit.gupta 161
			assigneeEscalationType = escalationUserMap.get(assigneeAuthId);
162
			managerAuthId = assigneeUserIds.stream().skip(1).findFirst().orElse(0);
163
			if (managerAuthId == 0) {
164
				managerAuthId = ProfitMandiConstants.FINAL_AUTH_ID;
165
			}
24439 govind 166
		}
27124 amit.gupta 167
 
168
		TicketAssigned ticketAssignedNew = new TicketAssigned();
169
		ticketAssignedNew.setTicketId(ticket.getId());
170
		ticketAssignedNew.setAssineeId(assigneeAuthId);
171
		ticketAssignedNew.setEscalationType(assigneeEscalationType);
27393 amit.gupta 172
		ticketAssignedNew.setManagerId(managerAuthId);
27124 amit.gupta 173
		ticketAssignedNew.setCreateTimestamp(LocalDateTime.now());
174
		ticketAssignedRepository.persist(ticketAssignedNew);
175
		LOGGER.info(ticketAssigned);
27545 tejbeer 176
 
27124 amit.gupta 177
		AuthUser authUser = authRepository.selectById(ticketAssignedNew.getAssineeId());
178
		AuthUser authUserManager = null;
27545 tejbeer 179
 
180
		// Dont send cc mail if both are same
181
		if (managerAuthId != assigneeAuthId) {
27124 amit.gupta 182
			authUserManager = authRepository.selectById(managerAuthId);
183
		}
184
		ticket.setAssignmentId(ticketAssignedNew.getId());
27545 tejbeer 185
		this.sendAssignedTicketMail(authUser, Arrays.asList(authUserManager), ticket, ticketAssigned != null);
27124 amit.gupta 186
		if (!ticketAssignedNew.getEscalationType().equals(EscalationType.L1)) {
187
			this.addActivity(ticket, this.createActivity(ActivityType.ESCALATED,
188
					"Ticket ecalated and assigned to " + authUser.getName(), 0));
189
		} else {
190
			this.addActivity(ticket,
191
					this.createActivity(ActivityType.ASSIGNED, "Ticket assigned to " + authUser.getName(), 0));
192
		}
24383 amit.gupta 193
	}
194
 
27124 amit.gupta 195
	@Override
196
	public void updateTicket(int categoryId, int subCategoryId, Ticket ticket) throws ProfitMandiBusinessException {
197
 
198
		TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.getTicketSubCategoryMap().get(subCategoryId);
27545 tejbeer 199
		if (ticketSubCategory == null) {
200
			throw new ProfitMandiBusinessException("Could not find subCategoryId " + subCategoryId + "- " + categoryId,
201
					"Problem", "Problem");
27126 amit.gupta 202
		}
27861 tejbeer 203
		String storeName = retailerService.getAllFofoRetailers().get(ticket.getFofoId()).getBusinessName();
27545 tejbeer 204
		List<TicketAssigned> oldTicketAssignedList = ticketAssignedRepository
205
				.selectByTicketIds(Arrays.asList(ticket.getId()));
206
		for (TicketAssigned oldTicketAssigned : oldTicketAssignedList) {
27124 amit.gupta 207
			ticketAssignedRepository.delete(oldTicketAssigned);
208
		}
209
 
27153 amit.gupta 210
		if (ticket.getSubCategoryId() != ticketSubCategory.getId()) {
211
			ticket.setSubCategoryId(ticketSubCategory.getId());
27545 tejbeer 212
			List<Integer> oldAssignedAuthUserIds = oldTicketAssignedList.stream().map(x -> x.getAssineeId())
213
					.collect(Collectors.toList());
214
			List<String> oldAssignedEmailIds = authRepository.selectAllAuthUserByIds(oldAssignedAuthUserIds).stream()
215
					.map(x -> x.getEmailId()).collect(Collectors.toList());
27124 amit.gupta 216
			String mailTo = oldAssignedEmailIds.remove(0);
27545 tejbeer 217
			String message = String.format(CATEGORY_CHANGED_TICKET, ticket.getId(), storeName,
218
					ticketSubCategory.getTicketCategory().getName() + " - " + ticketSubCategory.getName());
27124 amit.gupta 219
			try {
27545 tejbeer 220
				Utils.sendMailWithAttachments(mailSender, mailTo, oldAssignedEmailIds.toArray(new String[0]),
221
						"Ticket Category/Subcategory Changed", message, null);
27124 amit.gupta 222
			} catch (Exception e) {
223
				LOGGER.info("Could not send mail for ticket {}", ticket.toString());
224
			}
225
		}
226
 
227
		Activity categoryChangedActivity = this.createActivity(ActivityType.CATEGORY_CHANGED, "Category changed to "
228
				+ ticketSubCategory.getTicketCategory().getName() + " - " + ticketSubCategory.getName(), 0);
229
		this.addActivity(ticket, categoryChangedActivity);
27393 amit.gupta 230
		ticket.setAssignmentId(0);
27124 amit.gupta 231
		this.assignTicket(ticket);
27545 tejbeer 232
 
27124 amit.gupta 233
	}
234
 
27393 amit.gupta 235
	@Override
236
	public Activity createActivity(ActivityType activityType, String message, int createdBy) {
24383 amit.gupta 237
		Activity activity = new Activity();
238
		activity.setMessage(message);
239
		activity.setCreatedBy(createdBy);
240
		activity.setType(activityType);
24417 govind 241
		activity.setCreateTimestamp(LocalDateTime.now());
27124 amit.gupta 242
		activityRepository.persist(activity);
24383 amit.gupta 243
		return activity;
244
	}
24417 govind 245
 
27078 amit.gupta 246
	@Override
247
	public int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
27124 amit.gupta 248
		int authUserId = 0;
249
		List<Position> positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
250
				escalationType);
251
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
252
				.collect(Collectors.toList());
253
		// Add all Partner regions id
254
		regionIds.add(ALL_PARTNERS_REGION);
255
		LOGGER.info("Escalation Type {}, Region Ids {}", escalationType, regionIds);
256
		List<Integer> partnerPositionsIds = partnersPositionRepository
257
				.selectByRegionIdAndPartnerId(regionIds, Arrays.asList(0, fofoId)).stream().map(x -> x.getPositionId())
258
				.collect(Collectors.toList());
259
		positions = positions.stream().filter(x -> partnerPositionsIds.contains(x.getId()))
260
				.collect(Collectors.toList());
261
		LOGGER.info("User List List {}", positions.stream().map(x -> x.getAuthUserId()).collect(Collectors.toList()));
262
		if (positions.size() > 0) {
263
			List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(
264
					positions.stream().map(x -> x.getAuthUserId()).collect(Collectors.toList()));
265
			authUsers = authUsers.stream().filter(x -> x.isActive()).collect(Collectors.toList());
266
			if (authUsers.size() > 0) {
27260 amit.gupta 267
				authUserId = authUsers.get(0).getId();
27545 tejbeer 268
				/*
269
				 * Random rand = new Random(); authUserId =
270
				 * authUsers.get(rand.nextInt(authUsers.size())).getId();
271
				 */
24452 govind 272
			}
24439 govind 273
		}
27124 amit.gupta 274
		return authUserId;
24439 govind 275
 
24383 amit.gupta 276
	}
277
 
278
	@Override
27124 amit.gupta 279
	public void addActivity(Ticket ticket, Activity activity) {
280
		activity.setTicketId(ticket.getId());
281
		ticket.setLastActivity(activity.getType());
282
		ticket.setLastActivityId(activity.getId());
24383 amit.gupta 283
	}
284
 
24439 govind 285
	private String getRandomString() {
286
		int length = 4;
287
		boolean useLetters = false;
288
		boolean useNumbers = true;
289
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
290
		return generatedString;
291
	}
292
 
24417 govind 293
	@Override
294
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
295
 
24557 govind 296
		for (int fofoId : fofoIds) {
297
			PartnerRegion partnerRegion = new PartnerRegion();
298
			partnerRegion.setFofoId(fofoId);
299
			partnerRegion.setRegionId(regionId);
300
			partnerRegionRepository.persist(partnerRegion);
24417 govind 301
		}
302
	}
303
 
304
	@Override
24500 govind 305
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
24417 govind 306
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
24500 govind 307
 
308
		for (TicketAssigned ticketAssigned : ticketAssigneds) {
309
			AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
310
			authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
311
		}
312
		return authUserIdAndAuthUserMap;
313
	}
314
 
315
	@Override
316
	public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
317
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
318
 
24417 govind 319
		for (Ticket ticket : tickets) {
24500 govind 320
			AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
321
			authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
24417 govind 322
		}
323
		return authUserIdAndAuthUserMap;
324
	}
325
 
326
	@Override
327
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
328
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
24699 govind 329
		if (tickets != null) {
330
			for (Ticket ticket : tickets) {
331
				TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
332
				subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
333
			}
24417 govind 334
		}
335
		return subCategoryIdAndSubCategoryMap;
336
	}
337
 
338
	@Override
339
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
340
		List<Integer> fofoIds = new ArrayList<>();
24569 govind 341
		LOGGER.info(tickets);
24699 govind 342
		if (tickets == null) {
24569 govind 343
			return null;
344
		}
24417 govind 345
		for (Ticket ticket : tickets) {
346
			fofoIds.add(ticket.getFofoId());
347
		}
24699 govind 348
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
24417 govind 349
		return fofoIdsAndCustomRetailer;
350
	}
351
 
24439 govind 352
	@Override
353
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
354
 
355
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
356
		HashSet<Integer> categoryIds = new HashSet<>();
357
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
27124 amit.gupta 358
			categoryIds.add(ticketSubcategory.getCategoryId());
24439 govind 359
		}
360
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
361
		return ticketcategories;
362
	}
363
 
24471 govind 364
	@Override
365
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
366
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
367
		for (Position position : positions) {
368
			AuthUser authUser = authRepository.selectById(position.getAuthUserId());
369
			authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
370
		}
371
		return authUserIdAndAuthUserMap;
372
	}
24500 govind 373
 
24471 govind 374
	@Override
375
	public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
376
		Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
377
		for (Position position : positions) {
24500 govind 378
			TicketCategory ticketCategory = ticketCategoryRepository.selectById(position.getCategoryId());
24471 govind 379
			categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
380
		}
381
		return categoryIdAndCategoryMap;
382
	}
24439 govind 383
 
24471 govind 384
	@Override
385
	public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
386
		Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
387
		for (Position position : positions) {
24500 govind 388
			Region region = regionRepository.selectById(position.getRegionId());
24471 govind 389
			regionIdAndRegionMap.put(position.getRegionId(), region);
390
		}
391
		return regionIdAndRegionMap;
392
	}
24500 govind 393
 
27545 tejbeer 394
	private void sendAssignedTicketMail(AuthUser authUserTo, List<AuthUser> authUsersCc, Ticket ticket,
395
			boolean isEscalated) throws ProfitMandiBusinessException {
27124 amit.gupta 396
		try {
397
			String[] ccTo = authUsersCc.stream().filter(x -> x != null).map(x -> x.getEmailId()).toArray(String[]::new);
398
 
399
			String messageFormat = null;
27545 tejbeer 400
			if (isEscalated) {
27124 amit.gupta 401
				messageFormat = ESCALATED_TICKET;
24500 govind 402
			} else {
27124 amit.gupta 403
				messageFormat = ASSIGNED_TICKET;
24500 govind 404
			}
27124 amit.gupta 405
			String message = String.format(messageFormat, authUserTo.getName(), ticket.getId(),
406
					retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName());
27545 tejbeer 407
			Utils.sendMailWithAttachments(mailSender, authUserTo.getEmailId(), ccTo, ASSINMENT_SUBJECT, message, null);
24500 govind 408
		} catch (Exception e) {
27124 amit.gupta 409
			e.printStackTrace();
410
			throw new ProfitMandiBusinessException("Ticket Assingment", authUserTo.getEmailId(),
24500 govind 411
					"Could not send ticket assignment mail");
412
		}
413
	}
25570 tejbeer 414
 
415
	@Override
416
	public Map<Integer, List<CustomRetailer>> getPositionCustomRetailerMap(List<Position> positions) {
417
		Map<Integer, List<CustomRetailer>> positionRetailerMap = new HashMap<>();
418
		for (Position position : positions) {
419
			List<Integer> fofoIds = partnersPositionRepository.selectByPositionId(position.getId()).stream()
420
					.map(x -> x.getFofoId()).collect(Collectors.toList());
421
 
26992 amit.gupta 422
			LOGGER.info("fofoIds - {}", fofoIds);
25570 tejbeer 423
			if (!fofoIds.isEmpty()) {
424
				if (fofoIds.contains(0)) {
425
					fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
426
							.map(x -> x.getFofoId()).collect(Collectors.toList());
427
					if (fofoIds.contains(0)) {
428
						fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId())
429
								.collect(Collectors.toList());
430
					}
431
 
432
				}
26991 amit.gupta 433
				LOGGER.info("fofoIds - {}", fofoIds);
25570 tejbeer 434
				positionRetailerMap.put(position.getId(),
435
						new ArrayList<CustomRetailer>(retailerService.getFofoRetailers(fofoIds).values()));
436
 
437
			}
438
		}
439
		return positionRetailerMap;
440
	}
441
 
442
	@Override
27410 tejbeer 443
	public Map<Integer, List<CustomRetailer>> getRegionPartners(List<Position> positions) {
25570 tejbeer 444
		Map<Integer, List<CustomRetailer>> positionIdAndpartnerRegionMap = new HashedMap<>();
445
		for (Position position : positions) {
446
			List<Integer> fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
447
					.map(x -> x.getFofoId()).collect(Collectors.toList());
448
 
449
			if (!fofoIds.isEmpty()) {
450
				if (fofoIds.contains(0)) {
451
					fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());
452
 
453
				}
454
 
455
				Map<Integer, CustomRetailer> fofoRetailers = retailerService.getFofoRetailers(fofoIds);
456
				positionIdAndpartnerRegionMap.put(position.getRegionId(),
457
						new ArrayList<CustomRetailer>(fofoRetailers.values()));
458
			}
459
 
460
		}
461
 
462
		return positionIdAndpartnerRegionMap;
463
	}
25597 amit.gupta 464
 
465
	@Override
25726 amit.gupta 466
	@Cacheable(value = "authUserEmailMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25597 amit.gupta 467
	public Map<String, Set<String>> getAuthUserPartnerEmailMapping() {
468
		Map<String, Set<String>> storeGuyMap = new HashMap<>();
469
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
470
				.collect(Collectors.toSet());
471
		List<Position> categoryPositions = positionRepository
472
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
28908 tejbeer 473
		categoryPositions
474
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
25597 amit.gupta 475
		Map<Integer, Position> positionsMap = categoryPositions.stream()
476
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
477
 
25721 tejbeer 478
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
25597 amit.gupta 479
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
480
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
481
			Set<String> partnerEmails = positionPartnerEntry.getValue().stream()
482
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail())
483
					.collect(Collectors.toSet());
484
			AuthUser authUser = authRepository.selectById(authUserId);
485
			if (authUser.isActive()) {
26298 tejbeer 486
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
26111 amit.gupta 487
					storeGuyMap.put(authUser.getEmailId(), partnerEmails);
488
				} else {
489
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
490
				}
25597 amit.gupta 491
			}
492
		}
493
		return storeGuyMap;
494
	}
25721 tejbeer 495
 
496
	@Override
25777 amit.gupta 497
	@Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25721 tejbeer 498
	public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() {
499
		Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
25777 amit.gupta 500
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
501
				.collect(Collectors.toSet());
25721 tejbeer 502
		List<Position> categoryPositions = positionRepository
503
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
28908 tejbeer 504
		categoryPositions
505
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
25721 tejbeer 506
		Map<Integer, Position> positionsMap = categoryPositions.stream()
507
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
508
 
509
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
510
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
511
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
27044 amit.gupta 512
			Set<Integer> partnerIds = positionPartnerEntry.getValue().stream()
25777 amit.gupta 513
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
25721 tejbeer 514
					.collect(Collectors.toSet());
515
			AuthUser authUser = authRepository.selectById(authUserId);
516
			if (authUser.isActive()) {
26298 tejbeer 517
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
27044 amit.gupta 518
					storeGuyMap.put(authUser.getEmailId(), partnerIds);
26111 amit.gupta 519
				} else {
27044 amit.gupta 520
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerIds);
26111 amit.gupta 521
				}
25721 tejbeer 522
			}
523
		}
524
		return storeGuyMap;
525
	}
526
 
527
	@Override
528
	public List<String> getAuthUserByPartnerId(int fofoId) {
529
 
530
		List<String> emails = new ArrayList<>();
531
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
532
				.collect(Collectors.toList());
533
 
534
		regionIds.add(5);// All partners Id;
25799 tejbeer 535
 
29318 tejbeer 536
		List<Integer> partnerPositionIds = partnersPositionRepository
537
				.selectByRegionIdAndPartnerId(regionIds, Arrays.asList(fofoId, 0)).stream().map(x -> x.getPositionId())
538
				.collect(Collectors.toList());
25721 tejbeer 539
 
25799 tejbeer 540
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
541
 
29318 tejbeer 542
		List<Position> positions = positionRepository.selectAll(partnerPositionIds);
25721 tejbeer 543
 
29318 tejbeer 544
		positions = positions.stream().filter(x -> (x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_SALES
545
				|| x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_RBM)).collect(Collectors.toList());
546
 
547
		if (!positions.isEmpty()) {
548
			for (Position partnerPostionId : positions) {
549
 
550
				AuthUser authUser = authRepository.selectById(partnerPostionId.getAuthUserId());
25799 tejbeer 551
				LOGGER.info("authUser" + authUser);
552
				emails.add(authUser.getEmailId());
553
			}
25721 tejbeer 554
		}
555
 
556
		return emails;
557
	}
26298 tejbeer 558
 
559
	@Override
26978 tejbeer 560
	public Map<EscalationType, String> getAuthUserAndEsclationTypeByPartnerId(int fofoId) {
561
 
562
		List<String> emails = new ArrayList<>();
563
		Map<EscalationType, String> emailEsclationTypeMap = new HashMap<>();
564
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
565
				.collect(Collectors.toList());
566
 
567
		regionIds.add(5);// All partners Id;
568
		List<Integer> fofoIds = new ArrayList<>();
569
		fofoIds.add(fofoId);
570
		fofoIds.add(0);
571
 
572
		LOGGER.info("fofoIds" + fofoIds);
573
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
574
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
575
 
576
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
577
 
578
		for (Integer partnerPostionId : partnerPositionIds) {
579
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
580
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
581
			LOGGER.info("position" + position);
582
			if (position != null) {
583
 
584
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
585
				LOGGER.info("authUser" + authUser);
586
 
587
				emailEsclationTypeMap.put(position.getEscalationType(), authUser.getEmailId());
588
			}
589
		}
590
 
591
		LOGGER.info("emailEsclationTypeMap" + emailEsclationTypeMap);
592
 
593
		return emailEsclationTypeMap;
594
	}
595
 
596
	@Override
26298 tejbeer 597
	@Cacheable(value = "authUserIdPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
598
	public Map<Integer, List<Integer>> getAuthUserIdPartnerIdMapping() {
599
		Map<Integer, List<Integer>> storeGuyMap = new HashMap<>();
600
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
601
				.collect(Collectors.toSet());
602
		List<Position> categoryPositions = positionRepository
603
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
28908 tejbeer 604
		categoryPositions
605
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
29229 tejbeer 606
 
607
		categoryPositions
608
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CRM));
609
 
26298 tejbeer 610
		Map<Integer, Position> positionsMap = categoryPositions.stream()
611
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
612
 
613
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
614
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
615
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
26991 amit.gupta 616
			List<Integer> partnerIds = positionPartnerEntry.getValue().stream()
26298 tejbeer 617
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
618
					.collect(Collectors.toList());
619
			AuthUser authUser = authRepository.selectById(authUserId);
27044 amit.gupta 620
			if (authUser != null && authUser.isActive()) {
26960 amit.gupta 621
				if (!storeGuyMap.containsKey(authUserId)) {
26991 amit.gupta 622
					storeGuyMap.put(authUserId, partnerIds);
26298 tejbeer 623
				} else {
26991 amit.gupta 624
					storeGuyMap.get(authUserId).addAll(partnerIds);
26298 tejbeer 625
				}
626
			}
627
		}
628
		return storeGuyMap;
629
	}
630
 
631
	@Override
632
	@Cacheable(value = "L1L2Mapping", cacheManager = "thirtyMinsTimeOutCacheManager")
26460 amit.gupta 633
	public Map<Integer, List<Integer>> getL2L1Mapping() {
26448 amit.gupta 634
		List<Position> l1SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
635
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1);
636
		List<Position> l2SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
637
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2);
26298 tejbeer 638
 
26448 amit.gupta 639
		LOGGER.info("position" + l1SalesPositions);
640
		Map<Integer, List<Integer>> authUserPartnerListMap = this.getAuthUserIdPartnerIdMapping();
641
		LOGGER.info("map" + authUserPartnerListMap);
26298 tejbeer 642
 
26448 amit.gupta 643
		Map<Integer, List<Integer>> l2l1ListMap = new HashMap<>();
26298 tejbeer 644
 
26448 amit.gupta 645
		for (Position l2SalePosition : l2SalesPositions) {
646
			int l2AuthUserId = l2SalePosition.getAuthUserId();
647
			if (authUserPartnerListMap.containsKey(l2SalePosition.getAuthUserId())) {
648
				List<Integer> mappedL1AuthUsers = new ArrayList<>();
649
				l2l1ListMap.put(l2AuthUserId, mappedL1AuthUsers);
650
				List<Integer> l2FofoIds = authUserPartnerListMap.get(l2AuthUserId);
651
				for (Position l1SalePosition : l1SalesPositions) {
652
					int l1AuthUserId = l1SalePosition.getAuthUserId();
653
					if (authUserPartnerListMap.containsKey(l1AuthUserId)) {
654
						List<Integer> l1FofoIds = authUserPartnerListMap.get(l1SalePosition.getAuthUserId());
655
						if (l2FofoIds.containsAll(l1FofoIds)) {
656
							mappedL1AuthUsers.add(l1AuthUserId);
657
						}
658
					}
26298 tejbeer 659
 
660
				}
661
			}
662
 
663
		}
664
 
26448 amit.gupta 665
		return l2l1ListMap;
26298 tejbeer 666
	}
26448 amit.gupta 667
 
27205 amit.gupta 668
	@Override
669
	public Map<Integer, List<AuthUser>> getAssignedAuthList(List<Ticket> tickets) {
27545 tejbeer 670
		if (tickets.size() == 0) {
27259 amit.gupta 671
			return new HashMap<>();
672
		}
27545 tejbeer 673
		List<TicketAssigned> ticketAssignedList = ticketAssignedRepository
674
				.selectByTicketIds(tickets.stream().map(x -> x.getId()).collect(Collectors.toList()));
675
		List<Integer> authUserIds = ticketAssignedList.stream().map(x -> x.getAssineeId()).distinct()
676
				.collect(Collectors.toList());
677
		Map<Integer, AuthUser> authUserMap = authRepository.selectAllAuthUserByIds(authUserIds).stream()
678
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
679
		return ticketAssignedList.stream().collect(Collectors.groupingBy(x -> x.getTicketId(),
680
				Collectors.mapping(x -> authUserMap.get(x.getAssineeId()), Collectors.toList())));
27205 amit.gupta 681
	}
682
 
27548 tejbeer 683
	@Override
684
	public Map<EscalationType, AuthUser> getAuthUserAndEsclationByPartnerId(int fofoId) {
685
		Map<EscalationType, AuthUser> authuserEsclationTypeMap = new HashMap<>();
686
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
687
				.collect(Collectors.toList());
688
 
689
		regionIds.add(5);// All partners Id;
690
		List<Integer> fofoIds = new ArrayList<>();
691
		fofoIds.add(fofoId);
692
		fofoIds.add(0);
693
 
694
		LOGGER.info("fofoIds" + fofoIds);
695
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
696
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
697
 
698
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
699
 
700
		for (Integer partnerPostionId : partnerPositionIds) {
701
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
702
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
703
			LOGGER.info("position" + position);
704
			if (position != null) {
705
 
706
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
707
				LOGGER.info("authUser" + authUser);
708
 
709
				authuserEsclationTypeMap.put(position.getEscalationType(), authUser);
710
			}
711
		}
712
 
713
		LOGGER.info("emailEsclationTypeMap" + authuserEsclationTypeMap);
714
 
715
		return authuserEsclationTypeMap;
716
	}
717
 
28377 tejbeer 718
	@Override
719
	public List<AuthUser> getAuthUserIdByPartnerId(int fofoId) {
720
		List<AuthUser> authUsers = new ArrayList<>();
721
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
722
				.collect(Collectors.toList());
723
 
724
		regionIds.add(5);// All partners Id;
725
		List<Integer> fofoIds = new ArrayList<>();
726
		fofoIds.add(fofoId);
727
		fofoIds.add(0);
728
 
729
		LOGGER.info("fofoIds" + fofoIds);
730
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
731
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
732
 
733
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
734
 
735
		for (Integer partnerPostionId : partnerPositionIds) {
736
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
737
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
738
			LOGGER.info("position" + position);
739
			if (position != null) {
740
 
741
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
742
				LOGGER.info("authUser" + authUser);
743
				authUsers.add(authUser);
744
			}
745
		}
746
 
747
		return authUsers;
748
	}
749
 
28856 manish 750
	@Override
28977 manish 751
	public List<AuthUser> getAuthUserIds(int categoryId, List<EscalationType> escalationType) {
29229 tejbeer 752
 
28977 manish 753
		List<Position> positions = positionRepository.selectPositionbyCategoryIdAndEscalationTypes(categoryId,
28856 manish 754
				escalationType);
28908 tejbeer 755
		List<Integer> authIds = positions.stream().map(x -> x.getAuthUserId()).distinct().collect(Collectors.toList());
756
 
757
		LOGGER.info("authIds" + authIds);
758
 
759
		List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(authIds);
760
 
761
		LOGGER.info("authUsers" + authUsers);
28856 manish 762
		return authUsers;
763
	}
29318 tejbeer 764
 
29296 manish 765
	@Override
29927 amit.gupta 766
	public List<AuthUser> getAuthUserByCategoryId(int categoryId, EscalationType escalationType) {
28856 manish 767
 
29296 manish 768
		List<Position> positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
769
				escalationType);
770
		List<Integer> authIds = positions.stream().map(x -> x.getAuthUserId()).distinct().collect(Collectors.toList());
771
 
772
		LOGGER.info("authIds" + authIds);
773
 
774
		List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(authIds);
775
 
776
		LOGGER.info("authUsers" + authUsers);
777
		return authUsers;
778
	}
779
 
28908 tejbeer 780
	@Override
29927 amit.gupta 781
	@Cacheable(value = "authUserByCategoryId", cacheManager = "thirtyMinsTimeOutCacheManager")
28908 tejbeer 782
	public List<AuthUser> getAuthUserByCategoryId(int categoryId) {
783
		List<Position> positions = positionRepository.selectPositionByCategoryId(categoryId);
784
		List<Integer> authIds = positions.stream().map(x -> x.getAuthUserId()).distinct().collect(Collectors.toList());
785
 
786
		LOGGER.info("authIds" + authIds);
787
 
29927 amit.gupta 788
		List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(authIds).stream().filter(x -> x.getActive()).collect(Collectors.toList());
28908 tejbeer 789
 
790
		LOGGER.info("authUsers" + authUsers);
791
		return authUsers;
792
	}
793
 
24383 amit.gupta 794
}