Subversion Repositories SmartDukaan

Rev

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