Subversion Repositories SmartDukaan

Rev

Rev 31465 | Rev 31472 | 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 com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24417 govind 4
import com.spice.profitmandi.common.model.CustomRetailer;
24439 govind 5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
6
import com.spice.profitmandi.common.util.Utils;
24417 govind 7
import com.spice.profitmandi.dao.entity.auth.AuthUser;
30423 amit.gupta 8
import com.spice.profitmandi.dao.entity.cs.*;
24383 amit.gupta 9
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
30003 tejbeer 10
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
24417 govind 11
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
30003 tejbeer 12
import com.spice.profitmandi.dao.model.FofoReportingModel;
24417 govind 13
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
25570 tejbeer 14
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
24417 govind 15
import com.spice.profitmandi.service.user.RetailerService;
30423 amit.gupta 16
import org.apache.commons.collections4.map.HashedMap;
17
import org.apache.commons.lang.RandomStringUtils;
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21
import org.springframework.beans.factory.annotation.Autowired;
22
import org.springframework.cache.annotation.Cacheable;
23
import org.springframework.mail.javamail.JavaMailSender;
24
import org.springframework.stereotype.Component;
24383 amit.gupta 25
 
30423 amit.gupta 26
import java.time.LocalDateTime;
27
import java.util.*;
28
import java.util.stream.Collectors;
29
 
24383 amit.gupta 30
@Component
31
public class CsServiceImpl implements CsService {
24417 govind 32
 
27124 amit.gupta 33
	private static final int ALL_PARTNERS_REGION = 5;
34
 
24439 govind 35
	private static final Logger LOGGER = LogManager.getLogger(CsServiceImpl.class);
36
 
27124 amit.gupta 37
	private static final String ASSIGNED_TICKET = "Dear %s, New ticket #%s created by %s has been assigned to you. Regards\nSmartdukaan";
38
	private static final String ESCALATED_TICKET = "Dear %s, Ticket #%s created by %s has been escalated to you. Regards\nSmartdukaan";
39
	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 40
	private static final String ASSINMENT_SUBJECT = "Assignment Ticket";
41
 
24383 amit.gupta 42
	@Autowired
43
	TicketRepository ticketRepository;
24417 govind 44
 
24383 amit.gupta 45
	@Autowired
24439 govind 46
	JavaMailSender mailSender;
47
 
48
	@Autowired
24383 amit.gupta 49
	TicketCategoryRepository ticketCategoryRepository;
24417 govind 50
 
24383 amit.gupta 51
	@Autowired
52
	TicketSubCategoryRepository ticketSubCategoryRepository;
24417 govind 53
 
54
	@Autowired
24388 amit.gupta 55
	ActivityRepository activityRepository;
24417 govind 56
 
57
	@Autowired
24439 govind 58
	PartnerRegionRepository partnerRegionRepository;
24417 govind 59
 
60
	@Autowired
61
	private PositionRepository positionRepository;
62
 
63
	@Autowired
64
	private AuthRepository authRepository;
65
 
66
	@Autowired
67
	private RetailerService retailerService;
24500 govind 68
 
24471 govind 69
	@Autowired
70
	private RegionRepository regionRepository;
24417 govind 71
 
24500 govind 72
	@Autowired
73
	private TicketAssignedRepository ticketAssignedRepository;
74
 
25570 tejbeer 75
	@Autowired
76
	private PartnersPositionRepository partnersPositionRepository;
77
 
78
	@Autowired
79
	private FofoStoreRepository fofoStoreRepository;
80
 
24383 amit.gupta 81
	@Override
24439 govind 82
	public void createTicket(int fofoId, int categoryId, int subcategoryId, String message)
83
			throws ProfitMandiBusinessException {
24417 govind 84
 
24383 amit.gupta 85
		ActivityType type = ActivityType.OPENED;
86
		Ticket ticket = new Ticket();
87
		ticket.setSubCategoryId(subcategoryId);
24417 govind 88
		ticket.setFofoId(fofoId);
24383 amit.gupta 89
		ticket.setCreateTimestamp(LocalDateTime.now());
24417 govind 90
		ticket.setUpdateTimestamp(LocalDateTime.now());
24439 govind 91
		ticket.setHappyCode(getRandomString());
27124 amit.gupta 92
		ticketRepository.persist(ticket);
93
		this.addActivity(ticket, this.createActivity(type, message, 0));
94
		this.assignTicket(ticket);
95
 
96
	}
97
 
98
	@Override
99
	public void assignTicket(Ticket ticket) throws ProfitMandiBusinessException {
100
		TicketAssigned ticketAssigned = null;
101
		EscalationType newEscalationType = EscalationType.L1;
27545 tejbeer 102
		if (ticket.getAssignmentId() > 0) {
27124 amit.gupta 103
			ticketAssigned = ticketAssignedRepository.selectById(ticket.getAssignmentId());
104
			newEscalationType = ticketAssigned.getEscalationType().next();
27393 amit.gupta 105
			if (newEscalationType == null || newEscalationType.equals(EscalationType.Final)) {
27124 amit.gupta 106
				LOGGER.info("Cant escalate further");
107
				return;
24439 govind 108
			}
27393 amit.gupta 109
		} else {
110
			ticket.setL1AuthUser(0);
111
			ticket.setL2AuthUser(0);
112
			ticket.setL3AuthUser(0);
113
			ticket.setL4AuthUser(0);
114
			ticket.setL5AuthUser(0);
27124 amit.gupta 115
		}
116
		ticket.setUpdateTimestamp(LocalDateTime.now());
117
		TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
118
		TicketCategory ticketCategory = ticketCategoryRepository.selectById(ticketSubCategory.getCategoryId());
119
		Map<Integer, EscalationType> escalationUserMap = new HashedMap<>();
120
		Set<Integer> assigneeUserIds = new LinkedHashSet<>();
121
		EscalationType escalationTypeNext = newEscalationType;
122
		do {
123
			int assigneeUserId = this.getAuthUserId(ticketCategory.getId(), escalationTypeNext, ticket.getFofoId());
124
			if (assigneeUserId == 0) {
125
				escalationTypeNext = escalationTypeNext.next();
126
				LOGGER.info("Escalation type next {}", escalationTypeNext);
127
				continue;
128
			}
129
			assigneeUserIds.add(assigneeUserId);
130
			escalationUserMap.put(assigneeUserId, escalationTypeNext);
131
			switch (escalationTypeNext) {
132
			case L1:
133
				ticket.setL1AuthUser(assigneeUserId);
134
				break;
135
			case L2:
136
				ticket.setL2AuthUser(assigneeUserId);
137
				break;
138
			case L3:
139
				ticket.setL3AuthUser(assigneeUserId);
140
				break;
141
			case L4:
142
				ticket.setL4AuthUser(assigneeUserId);
143
				break;
144
			case L5:
145
				ticket.setL5AuthUser(assigneeUserId);
146
				break;
147
 
148
			default:
149
				break;
150
			}
151
			escalationTypeNext = escalationTypeNext.next();
152
			// LOGGER.info("Escalation type next {}", escalationTypeNext);
153
		} while (!escalationTypeNext.equals(EscalationType.Final));
154
 
155
		int assigneeAuthId = assigneeUserIds.stream().findFirst().orElse(0);
27545 tejbeer 156
 
27124 amit.gupta 157
		int managerAuthId = 0;
158
		EscalationType assigneeEscalationType = null;
159
		if (assigneeAuthId == 0) {
160
			assigneeAuthId = ProfitMandiConstants.FINAL_AUTH_ID;
161
			assigneeEscalationType = EscalationType.Final;
27393 amit.gupta 162
			managerAuthId = ProfitMandiConstants.FINAL_AUTH_ID;
24439 govind 163
		} else {
27124 amit.gupta 164
			assigneeEscalationType = escalationUserMap.get(assigneeAuthId);
165
			managerAuthId = assigneeUserIds.stream().skip(1).findFirst().orElse(0);
166
			if (managerAuthId == 0) {
167
				managerAuthId = ProfitMandiConstants.FINAL_AUTH_ID;
168
			}
24439 govind 169
		}
27124 amit.gupta 170
 
171
		TicketAssigned ticketAssignedNew = new TicketAssigned();
172
		ticketAssignedNew.setTicketId(ticket.getId());
173
		ticketAssignedNew.setAssineeId(assigneeAuthId);
174
		ticketAssignedNew.setEscalationType(assigneeEscalationType);
27393 amit.gupta 175
		ticketAssignedNew.setManagerId(managerAuthId);
27124 amit.gupta 176
		ticketAssignedNew.setCreateTimestamp(LocalDateTime.now());
177
		ticketAssignedRepository.persist(ticketAssignedNew);
178
		LOGGER.info(ticketAssigned);
27545 tejbeer 179
 
27124 amit.gupta 180
		AuthUser authUser = authRepository.selectById(ticketAssignedNew.getAssineeId());
181
		AuthUser authUserManager = null;
27545 tejbeer 182
 
183
		// Dont send cc mail if both are same
184
		if (managerAuthId != assigneeAuthId) {
27124 amit.gupta 185
			authUserManager = authRepository.selectById(managerAuthId);
186
		}
187
		ticket.setAssignmentId(ticketAssignedNew.getId());
27545 tejbeer 188
		this.sendAssignedTicketMail(authUser, Arrays.asList(authUserManager), ticket, ticketAssigned != null);
27124 amit.gupta 189
		if (!ticketAssignedNew.getEscalationType().equals(EscalationType.L1)) {
190
			this.addActivity(ticket, this.createActivity(ActivityType.ESCALATED,
191
					"Ticket ecalated and assigned to " + authUser.getName(), 0));
192
		} else {
193
			this.addActivity(ticket,
194
					this.createActivity(ActivityType.ASSIGNED, "Ticket assigned to " + authUser.getName(), 0));
195
		}
24383 amit.gupta 196
	}
197
 
27124 amit.gupta 198
	@Override
199
	public void updateTicket(int categoryId, int subCategoryId, Ticket ticket) throws ProfitMandiBusinessException {
200
 
201
		TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.getTicketSubCategoryMap().get(subCategoryId);
27545 tejbeer 202
		if (ticketSubCategory == null) {
203
			throw new ProfitMandiBusinessException("Could not find subCategoryId " + subCategoryId + "- " + categoryId,
204
					"Problem", "Problem");
27126 amit.gupta 205
		}
27861 tejbeer 206
		String storeName = retailerService.getAllFofoRetailers().get(ticket.getFofoId()).getBusinessName();
27545 tejbeer 207
		List<TicketAssigned> oldTicketAssignedList = ticketAssignedRepository
208
				.selectByTicketIds(Arrays.asList(ticket.getId()));
209
		for (TicketAssigned oldTicketAssigned : oldTicketAssignedList) {
27124 amit.gupta 210
			ticketAssignedRepository.delete(oldTicketAssigned);
211
		}
212
 
27153 amit.gupta 213
		if (ticket.getSubCategoryId() != ticketSubCategory.getId()) {
214
			ticket.setSubCategoryId(ticketSubCategory.getId());
27545 tejbeer 215
			List<Integer> oldAssignedAuthUserIds = oldTicketAssignedList.stream().map(x -> x.getAssineeId())
216
					.collect(Collectors.toList());
217
			List<String> oldAssignedEmailIds = authRepository.selectAllAuthUserByIds(oldAssignedAuthUserIds).stream()
218
					.map(x -> x.getEmailId()).collect(Collectors.toList());
27124 amit.gupta 219
			String mailTo = oldAssignedEmailIds.remove(0);
27545 tejbeer 220
			String message = String.format(CATEGORY_CHANGED_TICKET, ticket.getId(), storeName,
221
					ticketSubCategory.getTicketCategory().getName() + " - " + ticketSubCategory.getName());
27124 amit.gupta 222
			try {
27545 tejbeer 223
				Utils.sendMailWithAttachments(mailSender, mailTo, oldAssignedEmailIds.toArray(new String[0]),
224
						"Ticket Category/Subcategory Changed", message, null);
27124 amit.gupta 225
			} catch (Exception e) {
226
				LOGGER.info("Could not send mail for ticket {}", ticket.toString());
227
			}
228
		}
229
 
230
		Activity categoryChangedActivity = this.createActivity(ActivityType.CATEGORY_CHANGED, "Category changed to "
231
				+ ticketSubCategory.getTicketCategory().getName() + " - " + ticketSubCategory.getName(), 0);
232
		this.addActivity(ticket, categoryChangedActivity);
27393 amit.gupta 233
		ticket.setAssignmentId(0);
27124 amit.gupta 234
		this.assignTicket(ticket);
27545 tejbeer 235
 
27124 amit.gupta 236
	}
237
 
27393 amit.gupta 238
	@Override
239
	public Activity createActivity(ActivityType activityType, String message, int createdBy) {
24383 amit.gupta 240
		Activity activity = new Activity();
241
		activity.setMessage(message);
242
		activity.setCreatedBy(createdBy);
243
		activity.setType(activityType);
24417 govind 244
		activity.setCreateTimestamp(LocalDateTime.now());
27124 amit.gupta 245
		activityRepository.persist(activity);
24383 amit.gupta 246
		return activity;
247
	}
24417 govind 248
 
27078 amit.gupta 249
	@Override
250
	public int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
27124 amit.gupta 251
		int authUserId = 0;
252
		List<Position> positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
253
				escalationType);
254
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
255
				.collect(Collectors.toList());
256
		// Add all Partner regions id
257
		regionIds.add(ALL_PARTNERS_REGION);
258
		LOGGER.info("Escalation Type {}, Region Ids {}", escalationType, regionIds);
259
		List<Integer> partnerPositionsIds = partnersPositionRepository
260
				.selectByRegionIdAndPartnerId(regionIds, Arrays.asList(0, fofoId)).stream().map(x -> x.getPositionId())
261
				.collect(Collectors.toList());
262
		positions = positions.stream().filter(x -> partnerPositionsIds.contains(x.getId()))
263
				.collect(Collectors.toList());
264
		LOGGER.info("User List List {}", positions.stream().map(x -> x.getAuthUserId()).collect(Collectors.toList()));
265
		if (positions.size() > 0) {
266
			List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(
267
					positions.stream().map(x -> x.getAuthUserId()).collect(Collectors.toList()));
268
			authUsers = authUsers.stream().filter(x -> x.isActive()).collect(Collectors.toList());
269
			if (authUsers.size() > 0) {
27260 amit.gupta 270
				authUserId = authUsers.get(0).getId();
27545 tejbeer 271
				/*
272
				 * Random rand = new Random(); authUserId =
273
				 * authUsers.get(rand.nextInt(authUsers.size())).getId();
274
				 */
24452 govind 275
			}
24439 govind 276
		}
27124 amit.gupta 277
		return authUserId;
24439 govind 278
 
24383 amit.gupta 279
	}
280
 
281
	@Override
27124 amit.gupta 282
	public void addActivity(Ticket ticket, Activity activity) {
283
		activity.setTicketId(ticket.getId());
284
		ticket.setLastActivity(activity.getType());
285
		ticket.setLastActivityId(activity.getId());
24383 amit.gupta 286
	}
287
 
24439 govind 288
	private String getRandomString() {
289
		int length = 4;
290
		boolean useLetters = false;
291
		boolean useNumbers = true;
292
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
293
		return generatedString;
294
	}
295
 
24417 govind 296
	@Override
297
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
298
 
24557 govind 299
		for (int fofoId : fofoIds) {
300
			PartnerRegion partnerRegion = new PartnerRegion();
301
			partnerRegion.setFofoId(fofoId);
302
			partnerRegion.setRegionId(regionId);
303
			partnerRegionRepository.persist(partnerRegion);
24417 govind 304
		}
305
	}
306
 
307
	@Override
24500 govind 308
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
24417 govind 309
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
24500 govind 310
 
311
		for (TicketAssigned ticketAssigned : ticketAssigneds) {
312
			AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
313
			authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
314
		}
315
		return authUserIdAndAuthUserMap;
316
	}
317
 
318
	@Override
319
	public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
320
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
321
 
24417 govind 322
		for (Ticket ticket : tickets) {
24500 govind 323
			AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
324
			authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
24417 govind 325
		}
326
		return authUserIdAndAuthUserMap;
327
	}
328
 
329
	@Override
330
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
331
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
24699 govind 332
		if (tickets != null) {
333
			for (Ticket ticket : tickets) {
334
				TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
335
				subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
336
			}
24417 govind 337
		}
338
		return subCategoryIdAndSubCategoryMap;
339
	}
340
 
341
	@Override
342
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
343
		List<Integer> fofoIds = new ArrayList<>();
24569 govind 344
		LOGGER.info(tickets);
24699 govind 345
		if (tickets == null) {
24569 govind 346
			return null;
347
		}
24417 govind 348
		for (Ticket ticket : tickets) {
349
			fofoIds.add(ticket.getFofoId());
350
		}
30426 tejbeer 351
 
352
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
353
 
30764 tejbeer 354
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = fofoIds.stream().distinct()
355
				.map(x -> customRetailerMap.get(x)).filter(x -> x != null).collect(Collectors.toList()).stream()
30426 tejbeer 356
				.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
24417 govind 357
		return fofoIdsAndCustomRetailer;
358
	}
359
 
24439 govind 360
	@Override
361
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
362
 
363
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
364
		HashSet<Integer> categoryIds = new HashSet<>();
365
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
27124 amit.gupta 366
			categoryIds.add(ticketSubcategory.getCategoryId());
24439 govind 367
		}
368
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
369
		return ticketcategories;
370
	}
371
 
24471 govind 372
	@Override
373
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
374
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
375
		for (Position position : positions) {
376
			AuthUser authUser = authRepository.selectById(position.getAuthUserId());
377
			authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
378
		}
379
		return authUserIdAndAuthUserMap;
380
	}
24500 govind 381
 
24471 govind 382
	@Override
383
	public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
384
		Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
385
		for (Position position : positions) {
24500 govind 386
			TicketCategory ticketCategory = ticketCategoryRepository.selectById(position.getCategoryId());
24471 govind 387
			categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
388
		}
389
		return categoryIdAndCategoryMap;
390
	}
24439 govind 391
 
24471 govind 392
	@Override
393
	public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
394
		Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
395
		for (Position position : positions) {
24500 govind 396
			Region region = regionRepository.selectById(position.getRegionId());
24471 govind 397
			regionIdAndRegionMap.put(position.getRegionId(), region);
398
		}
399
		return regionIdAndRegionMap;
400
	}
24500 govind 401
 
27545 tejbeer 402
	private void sendAssignedTicketMail(AuthUser authUserTo, List<AuthUser> authUsersCc, Ticket ticket,
403
			boolean isEscalated) throws ProfitMandiBusinessException {
27124 amit.gupta 404
		try {
405
			String[] ccTo = authUsersCc.stream().filter(x -> x != null).map(x -> x.getEmailId()).toArray(String[]::new);
406
 
407
			String messageFormat = null;
27545 tejbeer 408
			if (isEscalated) {
27124 amit.gupta 409
				messageFormat = ESCALATED_TICKET;
24500 govind 410
			} else {
27124 amit.gupta 411
				messageFormat = ASSIGNED_TICKET;
24500 govind 412
			}
27124 amit.gupta 413
			String message = String.format(messageFormat, authUserTo.getName(), ticket.getId(),
414
					retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName());
27545 tejbeer 415
			Utils.sendMailWithAttachments(mailSender, authUserTo.getEmailId(), ccTo, ASSINMENT_SUBJECT, message, null);
24500 govind 416
		} catch (Exception e) {
27124 amit.gupta 417
			e.printStackTrace();
418
			throw new ProfitMandiBusinessException("Ticket Assingment", authUserTo.getEmailId(),
24500 govind 419
					"Could not send ticket assignment mail");
420
		}
421
	}
25570 tejbeer 422
 
423
	@Override
424
	public Map<Integer, List<CustomRetailer>> getPositionCustomRetailerMap(List<Position> positions) {
30423 amit.gupta 425
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
25570 tejbeer 426
		Map<Integer, List<CustomRetailer>> positionRetailerMap = new HashMap<>();
427
		for (Position position : positions) {
428
			List<Integer> fofoIds = partnersPositionRepository.selectByPositionId(position.getId()).stream()
429
					.map(x -> x.getFofoId()).collect(Collectors.toList());
430
 
26992 amit.gupta 431
			LOGGER.info("fofoIds - {}", fofoIds);
25570 tejbeer 432
			if (!fofoIds.isEmpty()) {
433
				if (fofoIds.contains(0)) {
434
					fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
435
							.map(x -> x.getFofoId()).collect(Collectors.toList());
436
					if (fofoIds.contains(0)) {
30424 amit.gupta 437
						fofoIds = new ArrayList<>(customRetailerMap.keySet());
25570 tejbeer 438
					}
439
 
440
				}
26991 amit.gupta 441
				LOGGER.info("fofoIds - {}", fofoIds);
30426 tejbeer 442
				positionRetailerMap.put(position.getId(), fofoIds.stream().map(x -> customRetailerMap.get(x))
443
						.filter(x -> x != null).collect(Collectors.toList()));
25570 tejbeer 444
 
445
			}
446
		}
447
		return positionRetailerMap;
448
	}
449
 
450
	@Override
27410 tejbeer 451
	public Map<Integer, List<CustomRetailer>> getRegionPartners(List<Position> positions) {
30426 tejbeer 452
		Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
25570 tejbeer 453
		Map<Integer, List<CustomRetailer>> positionIdAndpartnerRegionMap = new HashedMap<>();
454
		for (Position position : positions) {
455
			List<Integer> fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream()
456
					.map(x -> x.getFofoId()).collect(Collectors.toList());
457
 
458
			if (!fofoIds.isEmpty()) {
459
				if (fofoIds.contains(0)) {
460
					fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());
461
 
462
				}
463
 
30426 tejbeer 464
				Map<Integer, CustomRetailer> fofoRetailers = fofoIds.stream().map(x -> customRetailerMap.get(x))
465
						.filter(x -> x != null).collect(Collectors.toList()).stream()
466
						.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
25570 tejbeer 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);
28908 tejbeer 484
		categoryPositions
485
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
25597 amit.gupta 486
		Map<Integer, Position> positionsMap = categoryPositions.stream()
487
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
488
 
25721 tejbeer 489
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
25597 amit.gupta 490
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
491
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
492
			Set<String> partnerEmails = positionPartnerEntry.getValue().stream()
493
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail())
494
					.collect(Collectors.toSet());
495
			AuthUser authUser = authRepository.selectById(authUserId);
496
			if (authUser.isActive()) {
26298 tejbeer 497
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
26111 amit.gupta 498
					storeGuyMap.put(authUser.getEmailId(), partnerEmails);
499
				} else {
500
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
501
				}
25597 amit.gupta 502
			}
503
		}
504
		return storeGuyMap;
505
	}
25721 tejbeer 506
 
507
	@Override
25777 amit.gupta 508
	@Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
25721 tejbeer 509
	public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() {
510
		Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
25777 amit.gupta 511
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
512
				.collect(Collectors.toSet());
25721 tejbeer 513
		List<Position> categoryPositions = positionRepository
514
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
28908 tejbeer 515
		categoryPositions
516
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
31465 tejbeer 517
		categoryPositions.addAll(positionRepository
518
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_BUSINESSINTELLIGENT));
25721 tejbeer 519
		Map<Integer, Position> positionsMap = categoryPositions.stream()
520
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
521
 
522
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
523
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
524
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
27044 amit.gupta 525
			Set<Integer> partnerIds = positionPartnerEntry.getValue().stream()
25777 amit.gupta 526
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
25721 tejbeer 527
					.collect(Collectors.toSet());
528
			AuthUser authUser = authRepository.selectById(authUserId);
529
			if (authUser.isActive()) {
26298 tejbeer 530
				if (!storeGuyMap.containsKey(authUser.getEmailId())) {
27044 amit.gupta 531
					storeGuyMap.put(authUser.getEmailId(), partnerIds);
26111 amit.gupta 532
				} else {
27044 amit.gupta 533
					storeGuyMap.get(authUser.getEmailId()).addAll(partnerIds);
26111 amit.gupta 534
				}
25721 tejbeer 535
			}
536
		}
537
		return storeGuyMap;
538
	}
539
 
540
	@Override
31152 tejbeer 541
	@Cacheable(value = "authUserPartnerEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
31465 tejbeer 542
	public Map<String, Set<Integer>> getAuthUserPartnerIdMappingByCategoryIds(List<Integer> categoryIds,
543
			boolean active) {
31152 tejbeer 544
		Map<String, Set<Integer>> authUserPartnerMap = new HashMap<>();
31465 tejbeer 545
		Set<Integer> activeFofoIds;
546
		if (active) {
547
			activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
548
					.collect(Collectors.toSet());
31152 tejbeer 549
 
31465 tejbeer 550
		} else {
551
			activeFofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toSet());
552
 
553
		}
554
 
31152 tejbeer 555
		List<Position> categoryPositions = positionRepository.selectPositionByCategoryIds(categoryIds);
556
		Map<Integer, Position> positionsMap = categoryPositions.stream()
557
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
558
 
559
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
560
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
561
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
562
			Set<Integer> partnerIds = positionPartnerEntry.getValue().stream()
563
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
564
					.collect(Collectors.toSet());
565
			AuthUser authUser = authRepository.selectById(authUserId);
566
			if (authUser.isActive()) {
567
				if (!authUserPartnerMap.containsKey(authUser.getEmailId())) {
568
					authUserPartnerMap.put(authUser.getEmailId(), partnerIds);
569
				} else {
570
					authUserPartnerMap.get(authUser.getEmailId()).addAll(partnerIds);
571
				}
572
			}
573
		}
574
		return authUserPartnerMap;
575
	}
576
 
577
	@Override
25721 tejbeer 578
	public List<String> getAuthUserByPartnerId(int fofoId) {
579
 
580
		List<String> emails = new ArrayList<>();
581
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
582
				.collect(Collectors.toList());
583
 
584
		regionIds.add(5);// All partners Id;
25799 tejbeer 585
 
29318 tejbeer 586
		List<Integer> partnerPositionIds = partnersPositionRepository
587
				.selectByRegionIdAndPartnerId(regionIds, Arrays.asList(fofoId, 0)).stream().map(x -> x.getPositionId())
588
				.collect(Collectors.toList());
25721 tejbeer 589
 
25799 tejbeer 590
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
591
 
29318 tejbeer 592
		List<Position> positions = positionRepository.selectAll(partnerPositionIds);
25721 tejbeer 593
 
29318 tejbeer 594
		positions = positions.stream().filter(x -> (x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_SALES
595
				|| x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_RBM)).collect(Collectors.toList());
596
 
597
		if (!positions.isEmpty()) {
598
			for (Position partnerPostionId : positions) {
599
 
600
				AuthUser authUser = authRepository.selectById(partnerPostionId.getAuthUserId());
25799 tejbeer 601
				LOGGER.info("authUser" + authUser);
602
				emails.add(authUser.getEmailId());
603
			}
25721 tejbeer 604
		}
605
 
606
		return emails;
607
	}
26298 tejbeer 608
 
609
	@Override
26978 tejbeer 610
	public Map<EscalationType, String> getAuthUserAndEsclationTypeByPartnerId(int fofoId) {
611
 
612
		List<String> emails = new ArrayList<>();
613
		Map<EscalationType, String> emailEsclationTypeMap = new HashMap<>();
614
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
615
				.collect(Collectors.toList());
616
 
617
		regionIds.add(5);// All partners Id;
618
		List<Integer> fofoIds = new ArrayList<>();
619
		fofoIds.add(fofoId);
620
		fofoIds.add(0);
621
 
622
		LOGGER.info("fofoIds" + fofoIds);
623
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
624
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
625
 
626
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
627
 
628
		for (Integer partnerPostionId : partnerPositionIds) {
629
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
630
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
631
			LOGGER.info("position" + position);
632
			if (position != null) {
633
 
634
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
635
				LOGGER.info("authUser" + authUser);
636
 
637
				emailEsclationTypeMap.put(position.getEscalationType(), authUser.getEmailId());
638
			}
639
		}
640
 
641
		LOGGER.info("emailEsclationTypeMap" + emailEsclationTypeMap);
642
 
643
		return emailEsclationTypeMap;
644
	}
645
 
646
	@Override
26298 tejbeer 647
	@Cacheable(value = "authUserIdPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
648
	public Map<Integer, List<Integer>> getAuthUserIdPartnerIdMapping() {
649
		Map<Integer, List<Integer>> storeGuyMap = new HashMap<>();
650
		Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId())
651
				.collect(Collectors.toSet());
652
		List<Position> categoryPositions = positionRepository
653
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
28908 tejbeer 654
		categoryPositions
655
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
29229 tejbeer 656
 
657
		categoryPositions
658
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CRM));
659
 
31161 tejbeer 660
		categoryPositions
661
				.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_MARKETING));
662
 
26298 tejbeer 663
		Map<Integer, Position> positionsMap = categoryPositions.stream()
664
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
665
 
666
		Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
667
		for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
668
			int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
26991 amit.gupta 669
			List<Integer> partnerIds = positionPartnerEntry.getValue().stream()
26298 tejbeer 670
					.filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId())
671
					.collect(Collectors.toList());
672
			AuthUser authUser = authRepository.selectById(authUserId);
27044 amit.gupta 673
			if (authUser != null && authUser.isActive()) {
26960 amit.gupta 674
				if (!storeGuyMap.containsKey(authUserId)) {
26991 amit.gupta 675
					storeGuyMap.put(authUserId, partnerIds);
26298 tejbeer 676
				} else {
26991 amit.gupta 677
					storeGuyMap.get(authUserId).addAll(partnerIds);
26298 tejbeer 678
				}
679
			}
680
		}
30985 amit.gupta 681
		storeGuyMap.keySet().stream().forEach(x -> {
682
			List<Integer> fofoIds = storeGuyMap.get(x);
683
			storeGuyMap.put(x, fofoIds.stream().distinct().collect(Collectors.toList()));
684
		});
26298 tejbeer 685
		return storeGuyMap;
686
	}
687
 
688
	@Override
689
	@Cacheable(value = "L1L2Mapping", cacheManager = "thirtyMinsTimeOutCacheManager")
26460 amit.gupta 690
	public Map<Integer, List<Integer>> getL2L1Mapping() {
26448 amit.gupta 691
		List<Position> l1SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
692
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1);
693
		List<Position> l2SalesPositions = positionRepository.selectPositionbyCategoryIdAndEscalationType(
694
				ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L2);
26298 tejbeer 695
 
26448 amit.gupta 696
		LOGGER.info("position" + l1SalesPositions);
697
		Map<Integer, List<Integer>> authUserPartnerListMap = this.getAuthUserIdPartnerIdMapping();
698
		LOGGER.info("map" + authUserPartnerListMap);
26298 tejbeer 699
 
26448 amit.gupta 700
		Map<Integer, List<Integer>> l2l1ListMap = new HashMap<>();
26298 tejbeer 701
 
26448 amit.gupta 702
		for (Position l2SalePosition : l2SalesPositions) {
703
			int l2AuthUserId = l2SalePosition.getAuthUserId();
704
			if (authUserPartnerListMap.containsKey(l2SalePosition.getAuthUserId())) {
705
				List<Integer> mappedL1AuthUsers = new ArrayList<>();
706
				l2l1ListMap.put(l2AuthUserId, mappedL1AuthUsers);
707
				List<Integer> l2FofoIds = authUserPartnerListMap.get(l2AuthUserId);
708
				for (Position l1SalePosition : l1SalesPositions) {
709
					int l1AuthUserId = l1SalePosition.getAuthUserId();
710
					if (authUserPartnerListMap.containsKey(l1AuthUserId)) {
711
						List<Integer> l1FofoIds = authUserPartnerListMap.get(l1SalePosition.getAuthUserId());
712
						if (l2FofoIds.containsAll(l1FofoIds)) {
713
							mappedL1AuthUsers.add(l1AuthUserId);
714
						}
715
					}
26298 tejbeer 716
 
717
				}
718
			}
719
 
720
		}
721
 
26448 amit.gupta 722
		return l2l1ListMap;
26298 tejbeer 723
	}
26448 amit.gupta 724
 
27205 amit.gupta 725
	@Override
726
	public Map<Integer, List<AuthUser>> getAssignedAuthList(List<Ticket> tickets) {
27545 tejbeer 727
		if (tickets.size() == 0) {
27259 amit.gupta 728
			return new HashMap<>();
729
		}
27545 tejbeer 730
		List<TicketAssigned> ticketAssignedList = ticketAssignedRepository
731
				.selectByTicketIds(tickets.stream().map(x -> x.getId()).collect(Collectors.toList()));
732
		List<Integer> authUserIds = ticketAssignedList.stream().map(x -> x.getAssineeId()).distinct()
733
				.collect(Collectors.toList());
734
		Map<Integer, AuthUser> authUserMap = authRepository.selectAllAuthUserByIds(authUserIds).stream()
735
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
736
		return ticketAssignedList.stream().collect(Collectors.groupingBy(x -> x.getTicketId(),
737
				Collectors.mapping(x -> authUserMap.get(x.getAssineeId()), Collectors.toList())));
27205 amit.gupta 738
	}
739
 
27548 tejbeer 740
	@Override
741
	public Map<EscalationType, AuthUser> getAuthUserAndEsclationByPartnerId(int fofoId) {
742
		Map<EscalationType, AuthUser> authuserEsclationTypeMap = new HashMap<>();
743
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
744
				.collect(Collectors.toList());
745
 
746
		regionIds.add(5);// All partners Id;
747
		List<Integer> fofoIds = new ArrayList<>();
748
		fofoIds.add(fofoId);
749
		fofoIds.add(0);
750
 
751
		LOGGER.info("fofoIds" + fofoIds);
752
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
753
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
754
 
755
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
756
 
757
		for (Integer partnerPostionId : partnerPositionIds) {
758
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
759
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
760
			LOGGER.info("position" + position);
761
			if (position != null) {
762
 
763
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
764
				LOGGER.info("authUser" + authUser);
765
 
766
				authuserEsclationTypeMap.put(position.getEscalationType(), authUser);
767
			}
768
		}
769
 
770
		LOGGER.info("emailEsclationTypeMap" + authuserEsclationTypeMap);
771
 
772
		return authuserEsclationTypeMap;
773
	}
774
 
28377 tejbeer 775
	@Override
776
	public List<AuthUser> getAuthUserIdByPartnerId(int fofoId) {
777
		List<AuthUser> authUsers = new ArrayList<>();
778
		List<Integer> regionIds = partnerRegionRepository.selectByfofoId(fofoId).stream().map(x -> x.getRegionId())
779
				.collect(Collectors.toList());
780
 
781
		regionIds.add(5);// All partners Id;
782
		List<Integer> fofoIds = new ArrayList<>();
783
		fofoIds.add(fofoId);
784
		fofoIds.add(0);
785
 
786
		LOGGER.info("fofoIds" + fofoIds);
787
		List<Integer> partnerPositionIds = partnersPositionRepository.selectByRegionIdAndPartnerId(regionIds, fofoIds)
788
				.stream().map(x -> x.getPositionId()).collect(Collectors.toList());
789
 
790
		LOGGER.info("partnerPositionIds" + partnerPositionIds);
791
 
792
		for (Integer partnerPostionId : partnerPositionIds) {
793
			Position position = positionRepository.selectByIdAndCategoryId(partnerPostionId,
794
					ProfitMandiConstants.TICKET_CATEGORY_SALES);
795
			LOGGER.info("position" + position);
796
			if (position != null) {
797
 
798
				AuthUser authUser = authRepository.selectById(position.getAuthUserId());
799
				LOGGER.info("authUser" + authUser);
800
				authUsers.add(authUser);
801
			}
31020 tejbeer 802
 
803
			Position rbmPosition = positionRepository.selectByIdAndCategoryId(partnerPostionId,
804
					ProfitMandiConstants.TICKET_CATEGORY_RBM);
805
			if (rbmPosition != null) {
806
 
807
				AuthUser authUser = authRepository.selectById(rbmPosition.getAuthUserId());
808
				LOGGER.info("authUser" + authUser);
809
				authUsers.add(authUser);
810
			}
811
 
28377 tejbeer 812
		}
813
 
814
		return authUsers;
815
	}
816
 
28856 manish 817
	@Override
28977 manish 818
	public List<AuthUser> getAuthUserIds(int categoryId, List<EscalationType> escalationType) {
29229 tejbeer 819
 
28977 manish 820
		List<Position> positions = positionRepository.selectPositionbyCategoryIdAndEscalationTypes(categoryId,
28856 manish 821
				escalationType);
28908 tejbeer 822
		List<Integer> authIds = positions.stream().map(x -> x.getAuthUserId()).distinct().collect(Collectors.toList());
823
 
824
		LOGGER.info("authIds" + authIds);
825
 
826
		List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(authIds);
827
 
828
		LOGGER.info("authUsers" + authUsers);
28856 manish 829
		return authUsers;
830
	}
29318 tejbeer 831
 
29296 manish 832
	@Override
29927 amit.gupta 833
	public List<AuthUser> getAuthUserByCategoryId(int categoryId, EscalationType escalationType) {
28856 manish 834
 
29296 manish 835
		List<Position> positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
836
				escalationType);
837
		List<Integer> authIds = positions.stream().map(x -> x.getAuthUserId()).distinct().collect(Collectors.toList());
838
 
839
		LOGGER.info("authIds" + authIds);
30764 tejbeer 840
		List<AuthUser> authUsers = new ArrayList<>();
841
		if (!authIds.isEmpty()) {
842
			authUsers = authRepository.selectAllAuthUserByIds(authIds);
843
		}
29296 manish 844
 
845
		LOGGER.info("authUsers" + authUsers);
846
		return authUsers;
847
	}
848
 
28908 tejbeer 849
	@Override
29927 amit.gupta 850
	@Cacheable(value = "authUserByCategoryId", cacheManager = "thirtyMinsTimeOutCacheManager")
28908 tejbeer 851
	public List<AuthUser> getAuthUserByCategoryId(int categoryId) {
852
		List<Position> positions = positionRepository.selectPositionByCategoryId(categoryId);
853
		List<Integer> authIds = positions.stream().map(x -> x.getAuthUserId()).distinct().collect(Collectors.toList());
854
 
855
		LOGGER.info("authIds" + authIds);
856
 
30003 tejbeer 857
		List<AuthUser> authUsers = authRepository.selectAllAuthUserByIds(authIds).stream().filter(x -> x.getActive())
858
				.collect(Collectors.toList());
28908 tejbeer 859
 
860
		LOGGER.info("authUsers" + authUsers);
861
		return authUsers;
862
	}
863
 
30003 tejbeer 864
	private class SaleRoles {
865
 
866
		private List<String> l1;
867
		private List<String> l2;
30044 tejbeer 868
		private List<String> l3;
869
		private List<String> l4;
30003 tejbeer 870
 
871
		public SaleRoles() {
872
			l1 = new ArrayList<>();
873
			l2 = new ArrayList<>();
30044 tejbeer 874
			l3 = new ArrayList<>();
875
			l4 = new ArrayList<>();
30003 tejbeer 876
		}
877
 
878
		public List<String> getL1() {
879
			return l1;
880
		}
881
 
882
		public List<String> getL2() {
883
			return l2;
884
		}
885
 
30044 tejbeer 886
		public List<String> getL3() {
887
			return l3;
888
		}
889
 
890
		public List<String> getL4() {
891
			return l4;
892
		}
893
 
894
		@Override
895
		public String toString() {
896
			return "SaleRoles [l1=" + l1 + ", l2=" + l2 + ", l3=" + l3 + ", l4=" + l4 + "]";
897
		}
898
 
30003 tejbeer 899
	}
900
 
901
	@Override
902
	@Cacheable(value = "partnerSaleHeader", cacheManager = "oneDayCacheManager")
903
	public Map<Integer, FofoReportingModel> getPartnerIdSalesHeaders() {
904
		Map<String, SaleRoles> partnerEmailSalesMap = new HashMap<>();
905
 
906
		List<Position> positions = positionRepository
907
				.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
908
		Map<Integer, AuthUser> authUsersMap = authRepository.selectAllActiveUser().stream()
909
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
910
		Map<Integer, List<CustomRetailer>> positionIdRetailerMap = this.getPositionCustomRetailerMap(positions);
911
		for (Position position : positions) {
912
			List<CustomRetailer> crList = positionIdRetailerMap.get(position.getId());
913
			if (crList == null)
914
				continue;
915
			for (CustomRetailer cr : crList) {
916
				if (!partnerEmailSalesMap.containsKey(cr.getEmail())) {
917
					partnerEmailSalesMap.put(cr.getEmail(), new SaleRoles());
918
				}
919
				SaleRoles saleRoles = partnerEmailSalesMap.get(cr.getEmail());
920
				AuthUser authUser = authUsersMap.get(position.getAuthUserId());
921
				if (authUser == null) {
922
					continue;
923
				}
924
				String name = authUser.getFirstName() + " " + authUser.getLastName();
925
				if (position.getEscalationType().equals(EscalationType.L1)) {
926
					saleRoles.getL1().add(name);
927
				} else if (position.getEscalationType().equals(EscalationType.L2)) {
928
					saleRoles.getL2().add(name);
929
				}
30044 tejbeer 930
 
30003 tejbeer 931
			}
932
		}
933
 
934
		Set<CustomRetailer> allCrList = new HashSet<>();
935
		for (List<CustomRetailer> cr : positionIdRetailerMap.values()) {
936
			allCrList.addAll(cr);
937
		}
938
 
939
		Map<Integer, FofoStore> fofoStoresMap = fofoStoreRepository.selectActiveStores().stream()
940
				.collect(Collectors.toMap(x -> x.getId(), x -> x));
941
 
942
		Map<Integer, FofoReportingModel> partnerIdSalesHeadersMap = new HashMap<>();
943
 
944
		for (CustomRetailer cr : allCrList) {
945
			FofoStore fofoStore = fofoStoresMap.get(cr.getPartnerId());
946
			if (fofoStore == null) {
947
				LOGGER.info("Could not find Store {} in active Store", cr.getBusinessName());
948
				continue;
949
			}
950
			String code = fofoStore.getCode();
951
			// String storeName = "SmartDukaan-" +
952
			// fofoStore.getCode().replaceAll("[a-zA-Z]", "");
953
			String businessName = cr.getBusinessName();
954
			try {
955
				String stateManager = StringUtils.join(partnerEmailSalesMap.get(cr.getEmail()).getL2(), ", ");
956
				String territoryManager = StringUtils.join(partnerEmailSalesMap.get(cr.getEmail()).getL1(), ", ");
30044 tejbeer 957
 
958
				if (StringUtils.isEmpty(territoryManager)) {
959
					territoryManager = StringUtils.join(partnerEmailSalesMap.get(cr.getEmail()).getL2(), ", ");
960
				}
961
 
30003 tejbeer 962
				FofoReportingModel reportingModel = new FofoReportingModel();
963
				reportingModel.setBusinessName(businessName);
964
				reportingModel.setCode(code);
965
				reportingModel.setFofoId(fofoStore.getId());
966
				reportingModel.setRegionalManager(stateManager);
967
				reportingModel.setTerritoryManager(territoryManager);
968
				partnerIdSalesHeadersMap.put(fofoStore.getId(), reportingModel);
969
			} catch (Exception e) {
970
				LOGGER.warn("Could not find partner with email - {}", cr.getEmail());
971
			}
972
		}
973
		return partnerIdSalesHeadersMap;
974
 
975
	}
976
 
31152 tejbeer 977
	@Override
31465 tejbeer 978
	public Set<Integer> getAuthFofoIds(String email, boolean active) throws ProfitMandiBusinessException {
31152 tejbeer 979
 
31153 tejbeer 980
		List<Integer> categoryIds = Arrays.asList(ProfitMandiConstants.TICKET_CATEGORY_LOGISTICS,
981
				ProfitMandiConstants.TICKET_CATEGORY_FINANCIAL_SERVICES, ProfitMandiConstants.TICKET_CATEGORY_CATEGORY,
982
				ProfitMandiConstants.TICKET_CATEGORY_RBM, ProfitMandiConstants.TICKET_CATEGORY_SALES,
983
				ProfitMandiConstants.TICKET_CATEGORY_MARKETING, ProfitMandiConstants.TICKET_CATEGORY_ACCOUNTS,
984
				ProfitMandiConstants.TICKET_CATEGORY_BUSINESSINTELLIGENT,
31470 tejbeer 985
				ProfitMandiConstants.TICKET_CATEGORY_TECHNOLOGY,ProfitMandiConstants.TICKET_CATEGORY_TRAINING);
31153 tejbeer 986
 
31465 tejbeer 987
		Map<String, Set<Integer>> storeGuyMap = this.getAuthUserPartnerIdMappingByCategoryIds(categoryIds, active);
31152 tejbeer 988
		Set<Integer> authfofoIds = storeGuyMap.get(email);
989
 
990
		AuthUser authUser = authRepository.selectByEmailOrMobile(email);
991
		if (authfofoIds == null) {
992
			List<Position> positions1 = positionRepository.selectAll(authUser.getId());
993
			if (positions1.stream().filter(x -> x.getCategoryId() == ProfitMandiConstants.TICKET_CATEGORY_MASTER)
994
					.count() > 0) {
995
				authfofoIds = this.getPositionCustomRetailerMap(positions1).values().stream().flatMap(x -> x.stream())
996
						.map(x -> x.getPartnerId()).collect(Collectors.toSet());
997
			}
998
		}
999
 
1000
		return authfofoIds;
1001
	}
1002
 
24383 amit.gupta 1003
}