Subversion Repositories SmartDukaan

Rev

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