Subversion Repositories SmartDukaan

Rev

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