Subversion Repositories SmartDukaan

Rev

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