Subversion Repositories SmartDukaan

Rev

Rev 28377 | Rev 28887 | 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);
488
		Map<Integer, Position> positionsMap = categoryPositions.stream()
489
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
490
 
25721 tejbeer 491
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
25597 amit.gupta 492
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
493
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
494
			Set<String> partnerEmails = positionPartnerEntry.getValue().stream()
495
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail())
496
					.collect(Collectors.toSet());
497
			AuthUser authUser = authRepository.selectById(authUserId);
498
			if (authUser.isActive()) {
26298 tejbeer 499
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
26111 amit.gupta 500
					storeGuyMap.put(authUser.getEmailId(), partnerEmails);
501
				} else {
502
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
503
				}
25597 amit.gupta 504
			}
505
		}
506
		return storeGuyMap;
507
	}
25721 tejbeer 508
 
509
	@Override
25777 amit.gupta 510
	@Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25721 tejbeer 511
	public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() {
512
		Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
25777 amit.gupta 513
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
514
				.collect(Collectors.toSet());
25721 tejbeer 515
		List<Position> categoryPositions = positionRepository
516
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
517
		Map<Integer, Position> positionsMap = categoryPositions.stream()
518
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
519
 
520
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
521
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
522
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
27044 amit.gupta 523
			Set<Integer> partnerIds = positionPartnerEntry.getValue().stream()
25777 amit.gupta 524
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
25721 tejbeer 525
					.collect(Collectors.toSet());
526
			AuthUser authUser = authRepository.selectById(authUserId);
527
			if (authUser.isActive()) {
26298 tejbeer 528
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
27044 amit.gupta 529
					storeGuyMap.put(authUser.getEmailId(), partnerIds);
26111 amit.gupta 530
				} else {
27044 amit.gupta 531
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerIds);
26111 amit.gupta 532
				}
25721 tejbeer 533
			}
534
		}
535
		return storeGuyMap;
536
	}
537
 
538
	@Override
539
	public List<String> getAuthUserByPartnerId(int fofoId) {
540
 
541
		List<String> emails = new ArrayList<>();
542
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
543
				.collect(Collectors.toList());
544
 
545
		regionIds.add(5);// All partners Id;
546
		List<Integer> fofoIds = new ArrayList<>();
547
		fofoIds.add(fofoId);
548
		fofoIds.add(0);
25799 tejbeer 549
 
550
		LOGGER.info("fofoIds" + fofoIds);
25721 tejbeer 551
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
552
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
553
 
25799 tejbeer 554
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
555
 
25721 tejbeer 556
		for (Integer partnerPostionId : partnerPositionIds) {
25799 tejbeer 557
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
558
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
559
			LOGGER.info("position" + position);
560
			if (position != null) {
25721 tejbeer 561
 
25799 tejbeer 562
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
563
				LOGGER.info("authUser" + authUser);
564
				emails.add(authUser.getEmailId());
565
			}
25721 tejbeer 566
		}
567
 
568
		return emails;
569
	}
26298 tejbeer 570
 
571
	@Override
26978 tejbeer 572
	public Map<EscalationType, String> getAuthUserAndEsclationTypeByPartnerId(int fofoId) {
573
 
574
		List<String> emails = new ArrayList<>();
575
		Map<EscalationType, String> emailEsclationTypeMap = new HashMap<>();
576
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
577
				.collect(Collectors.toList());
578
 
579
		regionIds.add(5);// All partners Id;
580
		List<Integer> fofoIds = new ArrayList<>();
581
		fofoIds.add(fofoId);
582
		fofoIds.add(0);
583
 
584
		LOGGER.info("fofoIds" + fofoIds);
585
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
586
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
587
 
588
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
589
 
590
		for (Integer partnerPostionId : partnerPositionIds) {
591
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
592
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
593
			LOGGER.info("position" + position);
594
			if (position != null) {
595
 
596
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
597
				LOGGER.info("authUser" + authUser);
598
 
599
				emailEsclationTypeMap.put(position.getEscalationType(), authUser.getEmailId());
600
			}
601
		}
602
 
603
		LOGGER.info("emailEsclationTypeMap" + emailEsclationTypeMap);
604
 
605
		return emailEsclationTypeMap;
606
	}
607
 
608
	@Override
26298 tejbeer 609
	@Cacheable(value = "authUserIdPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
610
	public Map<Integer, List<Integer>> getAuthUserIdPartnerIdMapping() {
611
		Map<Integer, List<Integer>> storeGuyMap = new HashMap<>();
612
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
613
				.collect(Collectors.toSet());
614
		List<Position> categoryPositions = positionRepository
615
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
616
		Map<Integer, Position> positionsMap = categoryPositions.stream()
617
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
618
 
619
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
620
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
621
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
26991 amit.gupta 622
			List<Integer> partnerIds = positionPartnerEntry.getValue().stream()
26298 tejbeer 623
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
624
					.collect(Collectors.toList());
625
			AuthUser authUser = authRepository.selectById(authUserId);
27044 amit.gupta 626
			if (authUser != null && authUser.isActive()) {
26960 amit.gupta 627
				if (!storeGuyMap.containsKey(authUserId)) {
26991 amit.gupta 628
					storeGuyMap.put(authUserId, partnerIds);
26298 tejbeer 629
				} else {
26991 amit.gupta 630
					storeGuyMap.get(authUserId).addAll(partnerIds);
26298 tejbeer 631
				}
632
			}
633
		}
634
		return storeGuyMap;
635
	}
636
 
637
	@Override
638
	@Cacheable(value = "L1L2Mapping", cacheManager = "thirtyMinsTimeOutCacheManager")
26460 amit.gupta 639
	public Map<Integer, List<Integer>> getL2L1Mapping() {
26448 amit.gupta 640
		List<Position> l1SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
641
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1);
642
		List<Position> l2SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
643
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2);
26298 tejbeer 644
 
26448 amit.gupta 645
		LOGGER.info("position" + l1SalesPositions);
646
		Map<Integer, List<Integer>> authUserPartnerListMap = this.getAuthUserIdPartnerIdMapping();
647
		LOGGER.info("map" + authUserPartnerListMap);
26298 tejbeer 648
 
26448 amit.gupta 649
		Map<Integer, List<Integer>> l2l1ListMap = new HashMap<>();
26298 tejbeer 650
 
26448 amit.gupta 651
		for (Position l2SalePosition : l2SalesPositions) {
652
			int l2AuthUserId = l2SalePosition.getAuthUserId();
653
			if (authUserPartnerListMap.containsKey(l2SalePosition.getAuthUserId())) {
654
				List<Integer> mappedL1AuthUsers = new ArrayList<>();
655
				l2l1ListMap.put(l2AuthUserId, mappedL1AuthUsers);
656
				List<Integer> l2FofoIds = authUserPartnerListMap.get(l2AuthUserId);
657
				for (Position l1SalePosition : l1SalesPositions) {
658
					int l1AuthUserId = l1SalePosition.getAuthUserId();
659
					if (authUserPartnerListMap.containsKey(l1AuthUserId)) {
660
						List<Integer> l1FofoIds = authUserPartnerListMap.get(l1SalePosition.getAuthUserId());
661
						if (l2FofoIds.containsAll(l1FofoIds)) {
662
							mappedL1AuthUsers.add(l1AuthUserId);
663
						}
664
					}
26298 tejbeer 665
 
666
				}
667
			}
668
 
669
		}
670
 
26448 amit.gupta 671
		return l2l1ListMap;
26298 tejbeer 672
	}
26448 amit.gupta 673
 
27205 amit.gupta 674
	@Override
675
	public Map<Integer, List<AuthUser>> getAssignedAuthList(List<Ticket> tickets) {
27545 tejbeer 676
		if (tickets.size() == 0) {
27259 amit.gupta 677
			return new HashMap<>();
678
		}
27545 tejbeer 679
		List<TicketAssigned> ticketAssignedList = ticketAssignedRepository
680
				.selectByTicketIds(tickets.stream().map(x -> x.getId()).collect(Collectors.toList()));
681
		List<Integer> authUserIds = ticketAssignedList.stream().map(x -> x.getAssineeId()).distinct()
682
				.collect(Collectors.toList());
683
		Map<Integer, AuthUser> authUserMap = authRepository.selectAllAuthUserByIds(authUserIds).stream()
684
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
685
		return ticketAssignedList.stream().collect(Collectors.groupingBy(x -> x.getTicketId(),
686
				Collectors.mapping(x -> authUserMap.get(x.getAssineeId()), Collectors.toList())));
27205 amit.gupta 687
	}
688
 
27548 tejbeer 689
	@Override
690
	public Map<EscalationType, AuthUser> getAuthUserAndEsclationByPartnerId(int fofoId) {
691
		Map<EscalationType, AuthUser> authuserEsclationTypeMap = new HashMap<>();
692
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
693
				.collect(Collectors.toList());
694
 
695
		regionIds.add(5);// All partners Id;
696
		List<Integer> fofoIds = new ArrayList<>();
697
		fofoIds.add(fofoId);
698
		fofoIds.add(0);
699
 
700
		LOGGER.info("fofoIds" + fofoIds);
701
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
702
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
703
 
704
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
705
 
706
		for (Integer partnerPostionId : partnerPositionIds) {
707
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
708
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
709
			LOGGER.info("position" + position);
710
			if (position != null) {
711
 
712
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
713
				LOGGER.info("authUser" + authUser);
714
 
715
				authuserEsclationTypeMap.put(position.getEscalationType(), authUser);
716
			}
717
		}
718
 
719
		LOGGER.info("emailEsclationTypeMap" + authuserEsclationTypeMap);
720
 
721
		return authuserEsclationTypeMap;
722
	}
723
 
28377 tejbeer 724
	@Override
725
	public List<AuthUser> getAuthUserIdByPartnerId(int fofoId) {
726
		List<AuthUser> authUsers = new ArrayList<>();
727
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
728
				.collect(Collectors.toList());
729
 
730
		regionIds.add(5);// All partners Id;
731
		List<Integer> fofoIds = new ArrayList<>();
732
		fofoIds.add(fofoId);
733
		fofoIds.add(0);
734
 
735
		LOGGER.info("fofoIds" + fofoIds);
736
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
737
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
738
 
739
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
740
 
741
		for (Integer partnerPostionId : partnerPositionIds) {
742
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
743
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
744
			LOGGER.info("position" + position);
745
			if (position != null) {
746
 
747
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
748
				LOGGER.info("authUser" + authUser);
749
				authUsers.add(authUser);
750
			}
751
		}
752
 
753
		return authUsers;
754
	}
755
 
28856 manish 756
	@Override
757
	public List<AuthUser> getAuthUserIds(int categoryId, EscalationType escalationType) {
758
		List<Position> positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
759
				escalationType);
760
		List<Integer>authIds =positions.stream().map(x->x.getAuthUserId()).distinct().collect(Collectors.toList());
761
 
762
		LOGGER.info("authIds"+authIds);
763
 
764
		 List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(authIds);
765
 
766
		 LOGGER.info("authUsers"+authUsers);
767
		return authUsers;
768
	}
769
 
24383 amit.gupta 770
}