Subversion Repositories SmartDukaan

Rev

Rev 24699 | Rev 25505 | 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
 
24439 govind 10
import org.apache.commons.lang.RandomStringUtils;
11
import org.apache.logging.log4j.LogManager;
12
import org.apache.logging.log4j.Logger;
24383 amit.gupta 13
import org.springframework.beans.factory.annotation.Autowired;
24439 govind 14
import org.springframework.mail.javamail.JavaMailSender;
24383 amit.gupta 15
import org.springframework.stereotype.Component;
24701 govind 16
 
24383 amit.gupta 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
 
24570 govind 40
	private static final String ASSIGNED_TICKET = "Dear %s,You have assigned a ticket by %s with ticketId#%s.Regards\nSmartdukaan";
24439 govind 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);
24699 govind 95
		LOGGER.info("l1Auth" + l1Auth);
24439 govind 96
		int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
24699 govind 97
		LOGGER.info("l2Auth" + l2Auth);
24557 govind 98
		int l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
24699 govind 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
		}
24699 govind 113
		if (ticket.getL2AuthUser() == 0) {
24569 govind 114
			ticket.setL2AuthUser(l3Auth);
115
		}
24439 govind 116
		ticket.setL3AuthUser(l3Auth);
117
		ticketRepository.persist(ticket);
24500 govind 118
		ticketAssigned.setTicketId(ticket.getId());
119
		ticketAssignedRepository.persist(ticketAssigned);
120
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
24569 govind 121
		this.sendAssignedTicketMail(authUser, ticket);
24383 amit.gupta 122
		Activity activity = this.createActivity(type, message, 0);
123
		this.addActivity(ticket.getId(), activity);
124
	}
125
 
126
	private Activity createActivity(ActivityType activityType, String message, int createdBy) {
127
		Activity activity = new Activity();
128
		activity.setMessage(message);
129
		activity.setCreatedBy(createdBy);
130
		activity.setType(activityType);
24417 govind 131
		activity.setCreateTimestamp(LocalDateTime.now());
24383 amit.gupta 132
		return activity;
133
	}
24417 govind 134
 
24439 govind 135
	private int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
24417 govind 136
 
24439 govind 137
		List<Position> positions = null;
138
		if (escalationType == EscalationType.L3) {
139
			positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
140
		} else {
141
			List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
142
			LOGGER.info("regions=" + regions);
24500 govind 143
			if (regions.size() == 0) {
144
				regions = partnerRegionRepository.selectByfofoId(0);
24452 govind 145
				LOGGER.info("regions=" + regions);
146
			}
24439 govind 147
			for (PartnerRegion region : regions) {
24699 govind 148
				LOGGER.info(categoryId + ":" + escalationType + ":" + region.getRegionId());
24439 govind 149
				positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
150
						region.getRegionId());
151
				LOGGER.info("positions:-" + positions);
24500 govind 152
				if (positions.size() > 0) {
24439 govind 153
					break;
154
				}
155
 
156
			}
157
			if (positions.size() == 0) {
24557 govind 158
				if (escalationType == EscalationType.L1) {
159
					regions = partnerRegionRepository.selectByfofoId(0);
160
					LOGGER.info("regions=" + regions);
161
					for (PartnerRegion region : regions) {
162
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
163
								escalationType, region.getRegionId());
164
						LOGGER.info("positions:-" + positions);
165
						if (positions.size() > 0) {
166
							break;
167
						}
168
					}
169
				} else {
170
					regions = partnerRegionRepository.selectByfofoId(0);
171
					LOGGER.info("regions=" + regions);
172
					for (PartnerRegion region : regions) {
173
						positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId,
174
								escalationType, region.getRegionId());
175
						LOGGER.info("positions:-" + positions);
176
						if (positions.size() > 0) {
177
							break;
178
						}
179
					}
180
				}
24439 govind 181
			}
24699 govind 182
 
24439 govind 183
		}
24699 govind 184
		if (positions.size() == 0) {
24557 govind 185
			return 0;
186
		}
187
		LOGGER.info(positions.get(0).getAuthUserId());
24439 govind 188
		return positions.get(0).getAuthUserId();
24417 govind 189
		/*
190
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
191
		 * //escalationMatrix } else { throw new
192
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
193
		 * "Could not find Assignee for "); }
194
		 */
24439 govind 195
 
24383 amit.gupta 196
	}
197
 
198
	@Override
199
	public void addActivity(int ticketId, Activity activity) {
200
		activity.setTicketId(ticketId);
24417 govind 201
		activityRepository.persist(activity);
24383 amit.gupta 202
	}
203
 
24439 govind 204
	private String getRandomString() {
205
		int length = 4;
206
		boolean useLetters = false;
207
		boolean useNumbers = true;
208
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
209
		return generatedString;
210
	}
211
 
24417 govind 212
	@Override
213
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
214
 
24557 govind 215
		for (int fofoId : fofoIds) {
216
			PartnerRegion partnerRegion = new PartnerRegion();
217
			partnerRegion.setFofoId(fofoId);
218
			partnerRegion.setRegionId(regionId);
219
			partnerRegionRepository.persist(partnerRegion);
24417 govind 220
		}
221
	}
222
 
223
	@Override
24500 govind 224
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
24417 govind 225
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
24500 govind 226
 
227
		for (TicketAssigned ticketAssigned : ticketAssigneds) {
228
			AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
229
			authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
230
		}
231
		return authUserIdAndAuthUserMap;
232
	}
233
 
234
	@Override
235
	public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
236
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
237
 
24417 govind 238
		for (Ticket ticket : tickets) {
24500 govind 239
			AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
240
			authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
24417 govind 241
		}
242
		return authUserIdAndAuthUserMap;
243
	}
244
 
245
	@Override
246
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
247
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
24699 govind 248
		if (tickets != null) {
249
			for (Ticket ticket : tickets) {
250
				TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
251
				subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
252
			}
24417 govind 253
		}
254
		return subCategoryIdAndSubCategoryMap;
255
	}
256
 
257
	@Override
258
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
259
		List<Integer> fofoIds = new ArrayList<>();
24569 govind 260
		LOGGER.info(tickets);
24699 govind 261
		if (tickets == null) {
24569 govind 262
			return null;
263
		}
24417 govind 264
		for (Ticket ticket : tickets) {
265
			fofoIds.add(ticket.getFofoId());
266
		}
24699 govind 267
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
24417 govind 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
24699 govind 328
	public Map<Integer, List<AuthUser>> getAuthUserList(List<Ticket> tickets, AuthUser authUser) {
24500 govind 329
		Map<Integer, List<AuthUser>> authUserListMap = new HashMap<>();
330
		for (Ticket ticket : tickets) {
24570 govind 331
			if (ticket.getL2AuthUser() == authUser.getId()) {
24500 govind 332
				List<AuthUser> authUsers = new ArrayList<>();
333
				authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
334
				authUserListMap.put(ticket.getId(), authUsers);
335
 
24570 govind 336
			} else if (ticket.getL3AuthUser() == authUser.getId()) {
24500 govind 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);
24569 govind 395
				ticket.setL1AuthUser(0);
24536 govind 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());
24569 govind 408
		this.sendAssignedTicketMail(authUser, ticket);
409
	}
410
 
411
	@Override
24699 govind 412
	public void sendAssignedTicketMail(AuthUser authUser, Ticket ticket) throws ProfitMandiBusinessException {
24500 govind 413
		try {
414
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
415
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
24699 govind 416
							retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName(), ticket.getId()),
24500 govind 417
					null);
418
		} catch (Exception e) {
419
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
420
					"Could not send ticket assignment mail");
421
		}
422
	}
24383 amit.gupta 423
}