Subversion Repositories SmartDukaan

Rev

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