Subversion Repositories SmartDukaan

Rev

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