Subversion Repositories SmartDukaan

Rev

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