Subversion Repositories SmartDukaan

Rev

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