Subversion Repositories SmartDukaan

Rev

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