Subversion Repositories SmartDukaan

Rev

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