Subversion Repositories SmartDukaan

Rev

Rev 24536 | Rev 24569 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24383 amit.gupta 1
package com.spice.profitmandi.dao.repository.cs;
24500 govind 2
 
24383 amit.gupta 3
import java.time.LocalDateTime;
24417 govind 4
import java.util.ArrayList;
5
import java.util.HashMap;
24439 govind 6
import java.util.HashSet;
24417 govind 7
import java.util.List;
8
import java.util.Map;
24500 govind 9
import java.util.stream.Collectors;
10
 
24439 govind 11
import org.apache.commons.lang.RandomStringUtils;
12
import org.apache.logging.log4j.LogManager;
13
import org.apache.logging.log4j.Logger;
24383 amit.gupta 14
import org.springframework.beans.factory.annotation.Autowired;
24439 govind 15
import org.springframework.mail.javamail.JavaMailSender;
24383 amit.gupta 16
import org.springframework.stereotype.Component;
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24417 govind 18
import com.spice.profitmandi.common.model.CustomRetailer;
24439 govind 19
import com.spice.profitmandi.common.model.ProfitMandiConstants;
20
import com.spice.profitmandi.common.util.Utils;
24417 govind 21
import com.spice.profitmandi.dao.entity.auth.AuthUser;
24383 amit.gupta 22
import com.spice.profitmandi.dao.entity.cs.Activity;
24417 govind 23
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
24
import com.spice.profitmandi.dao.entity.cs.Position;
24471 govind 25
import com.spice.profitmandi.dao.entity.cs.Region;
24383 amit.gupta 26
import com.spice.profitmandi.dao.entity.cs.Ticket;
24500 govind 27
import com.spice.profitmandi.dao.entity.cs.TicketAssigned;
24439 govind 28
import com.spice.profitmandi.dao.entity.cs.TicketCategory;
24417 govind 29
import com.spice.profitmandi.dao.entity.cs.TicketSubCategory;
24383 amit.gupta 30
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
24417 govind 31
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
32
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
33
import com.spice.profitmandi.service.user.RetailerService;
24383 amit.gupta 34
 
35
@Component
36
public class CsServiceImpl implements CsService {
24417 govind 37
 
24439 govind 38
	private static final Logger LOGGER = LogManager.getLogger(CsServiceImpl.class);
39
 
40
	private static final String ASSIGNED_TICKET = "Dear %s,You have assigned a ticket by %s. Regards\nSmartdukaan";
41
	private static final String ASSINMENT_SUBJECT = "Assignment Ticket";
42
 
24383 amit.gupta 43
	@Autowired
44
	TicketRepository ticketRepository;
24417 govind 45
 
24383 amit.gupta 46
	@Autowired
24439 govind 47
	JavaMailSender mailSender;
48
 
49
	@Autowired
24383 amit.gupta 50
	TicketCategoryRepository ticketCategoryRepository;
24417 govind 51
 
24383 amit.gupta 52
	@Autowired
53
	TicketSubCategoryRepository ticketSubCategoryRepository;
24417 govind 54
 
55
	@Autowired
24388 amit.gupta 56
	ActivityRepository activityRepository;
24417 govind 57
 
58
	@Autowired
24439 govind 59
	PartnerRegionRepository partnerRegionRepository;
24417 govind 60
 
61
	@Autowired
62
	private PositionRepository positionRepository;
63
 
64
	@Autowired
65
	private AuthRepository authRepository;
66
 
67
	@Autowired
68
	private RetailerService retailerService;
24500 govind 69
 
24471 govind 70
	@Autowired
71
	private RegionRepository regionRepository;
24417 govind 72
 
24500 govind 73
	@Autowired
74
	private TicketAssignedRepository ticketAssignedRepository;
75
 
24383 amit.gupta 76
	@Override
24439 govind 77
	public void createTicket(int fofoId, int categoryId, int subcategoryId, String message)
78
			throws ProfitMandiBusinessException {
24417 govind 79
 
24383 amit.gupta 80
		ActivityType type = ActivityType.OPENED;
24439 govind 81
		EscalationType escalationTypeL1 = EscalationType.L1;
82
		EscalationType escalationTypeL2 = EscalationType.L2;
83
		EscalationType escalationTypeL3 = EscalationType.L3;
24383 amit.gupta 84
		Ticket ticket = new Ticket();
24500 govind 85
		TicketAssigned ticketAssigned = new TicketAssigned();
24383 amit.gupta 86
		ticket.setSubCategoryId(subcategoryId);
24417 govind 87
		ticket.setFofoId(fofoId);
24383 amit.gupta 88
		ticket.setCreateTimestamp(LocalDateTime.now());
24417 govind 89
		ticket.setUpdateTimestamp(LocalDateTime.now());
24467 govind 90
		ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
91
		ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
92
		ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
24439 govind 93
		ticket.setHappyCode(getRandomString());
94
		int l1Auth = this.getAuthUserId(categoryId, escalationTypeL1, fofoId);
24557 govind 95
		LOGGER.info("l1Auth"+l1Auth);
24439 govind 96
		int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
24557 govind 97
		LOGGER.info("l2Auth"+l2Auth);
98
		int l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
99
		LOGGER.info("l3Auth"+l3Auth);
24439 govind 100
		if (l1Auth == 0) {
101
			if (l2Auth == 0) {
24500 govind 102
				this.setAssignment(ticket, ticketAssigned);
24439 govind 103
			} else {
24500 govind 104
				ticketAssigned.setAssineeId(l2Auth);
24526 govind 105
				ticket.setL1AuthUser(l2Auth);
106
				ticket.setL2AuthUser(l2Auth);
24439 govind 107
			}
108
		} else {
24500 govind 109
			ticketAssigned.setAssineeId(l1Auth);
24439 govind 110
			ticket.setL1AuthUser(l1Auth);
111
			ticket.setL2AuthUser(l2Auth);
112
		}
113
		ticket.setL3AuthUser(l3Auth);
114
		ticketRepository.persist(ticket);
24500 govind 115
		ticketAssigned.setTicketId(ticket.getId());
116
		ticketAssignedRepository.persist(ticketAssigned);
117
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
24383 amit.gupta 118
		try {
24439 govind 119
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
120
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
121
							retailerService.getFofoRetailer(fofoId).getBusinessName()),
122
					null);
24383 amit.gupta 123
		} catch (Exception e) {
24439 govind 124
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
125
					"Could not send ticket assignment mail");
24383 amit.gupta 126
		}
127
		Activity activity = this.createActivity(type, message, 0);
128
		this.addActivity(ticket.getId(), activity);
129
	}
130
 
131
	private Activity createActivity(ActivityType activityType, String message, int createdBy) {
132
		Activity activity = new Activity();
133
		activity.setMessage(message);
134
		activity.setCreatedBy(createdBy);
135
		activity.setType(activityType);
24417 govind 136
		activity.setCreateTimestamp(LocalDateTime.now());
24383 amit.gupta 137
		return activity;
138
	}
24417 govind 139
 
24439 govind 140
	private int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
24417 govind 141
 
24439 govind 142
		List<Position> positions = null;
143
		if (escalationType == EscalationType.L3) {
144
			positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
145
		} else {
146
			List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
147
			LOGGER.info("regions=" + regions);
24500 govind 148
			if (regions.size() == 0) {
149
				regions = partnerRegionRepository.selectByfofoId(0);
24452 govind 150
				LOGGER.info("regions=" + regions);
151
			}
24439 govind 152
			for (PartnerRegion region : regions) {
24557 govind 153
				LOGGER.info(categoryId+":"+escalationType+":"+region.getRegionId());
24439 govind 154
				positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
155
						region.getRegionId());
156
				LOGGER.info("positions:-" + positions);
24500 govind 157
				if (positions.size() > 0) {
24439 govind 158
					break;
159
				}
160
 
161
			}
162
			if (positions.size() == 0) {
24557 govind 163
				if (escalationType == EscalationType.L1) {
164
					regions = partnerRegionRepository.selectByfofoId(0);
165
					LOGGER.info("regions=" + regions);
166
					for (PartnerRegion region : regions) {
167
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
168
								escalationType, region.getRegionId());
169
						LOGGER.info("positions:-" + positions);
170
						if (positions.size() > 0) {
171
							break;
172
						}
173
					}
174
				} else {
175
					regions = partnerRegionRepository.selectByfofoId(0);
176
					LOGGER.info("regions=" + regions);
177
					for (PartnerRegion region : regions) {
178
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
179
								escalationType, region.getRegionId());
180
						LOGGER.info("positions:-" + positions);
181
						if (positions.size() > 0) {
182
							break;
183
						}
184
					}
185
				}
24439 govind 186
			}
24557 govind 187
 
24439 govind 188
		}
24557 govind 189
		if(positions.size()==0)
190
		{
191
			return 0;
192
		}
193
		LOGGER.info(positions.get(0).getAuthUserId());
24439 govind 194
		return positions.get(0).getAuthUserId();
24417 govind 195
		/*
196
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
197
		 * //escalationMatrix } else { throw new
198
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
199
		 * "Could not find Assignee for "); }
200
		 */
24439 govind 201
 
24383 amit.gupta 202
	}
203
 
204
	@Override
205
	public void addActivity(int ticketId, Activity activity) {
206
		activity.setTicketId(ticketId);
24417 govind 207
		activityRepository.persist(activity);
24383 amit.gupta 208
	}
209
 
24439 govind 210
	private String getRandomString() {
211
		int length = 4;
212
		boolean useLetters = false;
213
		boolean useNumbers = true;
214
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
215
		return generatedString;
216
	}
217
 
24417 govind 218
	@Override
219
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
220
 
24557 govind 221
		for (int fofoId : fofoIds) {
222
			PartnerRegion partnerRegion = new PartnerRegion();
223
			partnerRegion.setFofoId(fofoId);
224
			partnerRegion.setRegionId(regionId);
225
			partnerRegionRepository.persist(partnerRegion);
24417 govind 226
		}
227
	}
228
 
229
	@Override
24500 govind 230
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
24417 govind 231
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
24500 govind 232
 
233
		for (TicketAssigned ticketAssigned : ticketAssigneds) {
234
			AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
235
			authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
236
		}
237
		return authUserIdAndAuthUserMap;
238
	}
239
 
240
	@Override
241
	public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
242
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
243
 
24417 govind 244
		for (Ticket ticket : tickets) {
24500 govind 245
			AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
246
			authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
24417 govind 247
		}
248
		return authUserIdAndAuthUserMap;
249
	}
250
 
251
	@Override
252
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
253
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
254
		for (Ticket ticket : tickets) {
255
			TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
256
			subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
257
		}
258
		return subCategoryIdAndSubCategoryMap;
259
	}
260
 
261
	@Override
262
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
263
		List<Integer> fofoIds = new ArrayList<>();
264
		for (Ticket ticket : tickets) {
265
			fofoIds.add(ticket.getFofoId());
266
		}
267
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
268
		return fofoIdsAndCustomRetailer;
269
	}
270
 
24439 govind 271
	@Override
272
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
273
 
274
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
275
		HashSet<Integer> categoryIds = new HashSet<>();
276
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
277
			categoryIds.add(ticketSubcategory.getcategoryId());
278
		}
279
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
280
		return ticketcategories;
281
	}
282
 
24500 govind 283
	private Ticket setAssignment(Ticket ticket, TicketAssigned ticketAssigned) {
24439 govind 284
		TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
285
		List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
286
		for (Position position : positions) {
287
			if (position.getEscalationType().equals(EscalationType.L1)) {
24500 govind 288
				ticketAssigned.setAssineeId(position.getAuthUserId());
24439 govind 289
				ticket.setL1AuthUser(position.getAuthUserId());
24557 govind 290
			} else if (position.getEscalationType().equals(EscalationType.L2)) {
24439 govind 291
				ticket.setL2AuthUser(position.getAuthUserId());
292
			}
293
		}
294
		return ticket;
295
	}
24500 govind 296
 
24471 govind 297
	@Override
298
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
299
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
300
		for (Position position : positions) {
301
			AuthUser authUser = authRepository.selectById(position.getAuthUserId());
302
			authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
303
		}
304
		return authUserIdAndAuthUserMap;
305
	}
24500 govind 306
 
24471 govind 307
	@Override
308
	public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
309
		Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
310
		for (Position position : positions) {
24500 govind 311
			TicketCategory ticketCategory = ticketCategoryRepository.selectById(position.getCategoryId());
24471 govind 312
			categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
313
		}
314
		return categoryIdAndCategoryMap;
315
	}
24439 govind 316
 
24471 govind 317
	@Override
318
	public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
319
		Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
320
		for (Position position : positions) {
24500 govind 321
			Region region = regionRepository.selectById(position.getRegionId());
24471 govind 322
			regionIdAndRegionMap.put(position.getRegionId(), region);
323
		}
324
		return regionIdAndRegionMap;
325
	}
24500 govind 326
 
327
	@Override
328
	public Map<Integer, List<AuthUser>> getAuthUserList(List<Ticket> tickets, EscalationType escalationType) {
329
		Map<Integer, List<AuthUser>> authUserListMap = new HashMap<>();
330
		for (Ticket ticket : tickets) {
331
			if (escalationType == EscalationType.L2) {
332
				List<AuthUser> authUsers = new ArrayList<>();
333
				authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
334
				authUserListMap.put(ticket.getId(), authUsers);
335
 
336
			} else if (escalationType == EscalationType.L3) {
337
				TicketAssigned ticketAssigned = ticketAssignedRepository
338
						.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(), ticket.getId());
339
				if (ticketAssigned == null) {
340
					List<AuthUser> authUsers = new ArrayList<>();
341
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
342
					authUserListMap.put(ticket.getId(), authUsers);
343
				} else {
344
					List<AuthUser> authUsers = new ArrayList<>();
345
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
346
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
347
					authUserListMap.put(ticket.getId(), authUsers);
348
				}
349
			} else {
350
				TicketAssigned ticketAssigned = ticketAssignedRepository
351
						.selectByAssigneeIdAndTicketId(ticket.getL3AuthUser(), ticket.getId());
352
				if (ticketAssigned == null) {
353
					ticketAssigned = ticketAssignedRepository.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(),
354
							ticket.getId());
355
					if (ticketAssigned == null) {
356
						List<AuthUser> authUsers = new ArrayList<>();
357
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
358
						authUserListMap.put(ticket.getId(), authUsers);
359
					} else {
360
						List<AuthUser> authUsers = new ArrayList<>();
361
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
362
						authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
363
						authUserListMap.put(ticket.getId(), authUsers);
364
					}
365
				} else {
366
					List<AuthUser> authUsers = new ArrayList<>();
367
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
368
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
369
					authUsers.add(authRepository.selectById(ticket.getL3AuthUser()));
370
					authUserListMap.put(ticket.getId(), authUsers);
371
 
372
				}
373
			}
374
		}
375
		return authUserListMap;
376
	}
24557 govind 377
 
24500 govind 378
	@Override
24557 govind 379
	public void updateTicket(int categoryId, int subCategoryId, Ticket ticket) throws ProfitMandiBusinessException {
380
		TicketAssigned ticketAssigned = new TicketAssigned();
24500 govind 381
		ticket.setSubCategoryId(subCategoryId);
382
		ticket.setUpdateTimestamp(LocalDateTime.now());
383
		ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
384
		ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
385
		ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
386
		int l3Auth = this.getAuthUserId(categoryId, EscalationType.L3, ticket.getFofoId());
387
		int l1Auth = this.getAuthUserId(categoryId, EscalationType.L1, ticket.getFofoId());
388
		int l2Auth = this.getAuthUserId(categoryId, EscalationType.L2, ticket.getFofoId());
389
		LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
390
		if (l1Auth == 0) {
391
			if (l2Auth == 0) {
392
				this.setAssignment(ticket, ticketAssigned);
393
			} else {
394
				ticketAssigned.setAssineeId(l2Auth);
24536 govind 395
				ticket.setL1AuthUser(l2Auth);
396
				ticket.setL2AuthUser(l2Auth);
24500 govind 397
			}
398
		} else {
399
			ticketAssigned.setAssineeId(l1Auth);
400
			ticket.setL1AuthUser(l1Auth);
401
			ticket.setL2AuthUser(l2Auth);
402
		}
403
		ticket.setL3AuthUser(l3Auth);
404
		ticketRepository.persist(ticket);
405
		ticketAssigned.setTicketId(ticket.getId());
406
		ticketAssignedRepository.persist(ticketAssigned);
407
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
408
		try {
409
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
410
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
411
							retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName()),
412
					null);
413
		} catch (Exception e) {
414
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
415
					"Could not send ticket assignment mail");
416
		}
417
	}
24557 govind 418
 
24383 amit.gupta 419
}