Subversion Repositories SmartDukaan

Rev

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