Subversion Repositories SmartDukaan

Rev

Rev 31662 | Rev 31829 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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