Subversion Repositories SmartDukaan

Rev

Rev 24471 | Rev 24526 | 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 l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
95
		int l1Auth = this.getAuthUserId(categoryId, escalationTypeL1, fofoId);
96
		int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
97
		LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
98
		if (l1Auth == 0) {
99
			if (l2Auth == 0) {
24500 govind 100
				this.setAssignment(ticket, ticketAssigned);
24439 govind 101
			} else {
24500 govind 102
				ticketAssigned.setAssineeId(l2Auth);
24439 govind 103
			}
104
		} else {
24500 govind 105
			ticketAssigned.setAssineeId(l1Auth);
24439 govind 106
			ticket.setL1AuthUser(l1Auth);
107
			ticket.setL2AuthUser(l2Auth);
108
		}
109
		ticket.setL3AuthUser(l3Auth);
110
		ticketRepository.persist(ticket);
24500 govind 111
		ticketAssigned.setTicketId(ticket.getId());
112
		ticketAssignedRepository.persist(ticketAssigned);
113
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
24383 amit.gupta 114
		try {
24439 govind 115
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
116
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
117
							retailerService.getFofoRetailer(fofoId).getBusinessName()),
118
					null);
24383 amit.gupta 119
		} catch (Exception e) {
24439 govind 120
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
121
					"Could not send ticket assignment mail");
24383 amit.gupta 122
		}
123
		Activity activity = this.createActivity(type, message, 0);
124
		this.addActivity(ticket.getId(), activity);
125
	}
126
 
127
	private Activity createActivity(ActivityType activityType, String message, int createdBy) {
128
		Activity activity = new Activity();
129
		activity.setMessage(message);
130
		activity.setCreatedBy(createdBy);
131
		activity.setType(activityType);
24417 govind 132
		activity.setCreateTimestamp(LocalDateTime.now());
24383 amit.gupta 133
		return activity;
134
	}
24417 govind 135
 
24439 govind 136
	private int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
24417 govind 137
 
24439 govind 138
		List<Position> positions = null;
139
		if (escalationType == EscalationType.L3) {
140
			positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
141
		} else {
142
			List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
143
			LOGGER.info("regions=" + regions);
24500 govind 144
			if (regions.size() == 0) {
145
				regions = partnerRegionRepository.selectByfofoId(0);
24452 govind 146
				LOGGER.info("regions=" + regions);
147
			}
24439 govind 148
			for (PartnerRegion region : regions) {
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) {
158
				return 0;
159
			}
160
		}
161
		return positions.get(0).getAuthUserId();
24417 govind 162
		/*
163
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
164
		 * //escalationMatrix } else { throw new
165
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
166
		 * "Could not find Assignee for "); }
167
		 */
24439 govind 168
 
24383 amit.gupta 169
	}
170
 
171
	@Override
172
	public void addActivity(int ticketId, Activity activity) {
173
		activity.setTicketId(ticketId);
24417 govind 174
		activityRepository.persist(activity);
24383 amit.gupta 175
	}
176
 
24439 govind 177
	private String getRandomString() {
178
		int length = 4;
179
		boolean useLetters = false;
180
		boolean useNumbers = true;
181
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
182
		return generatedString;
183
	}
184
 
24417 govind 185
	@Override
186
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
187
 
188
		for (Integer fofoId : fofoIds) {
24439 govind 189
			PartnerRegion partnerRegion = partnerRegionRepository.selectByRegionIdAndFofoId(regionId, fofoId);
24417 govind 190
			if (partnerRegion == null) {
191
				partnerRegion = new PartnerRegion();
192
				partnerRegion.setFofoId(fofoId);
193
				partnerRegion.setRegionId(regionId);
24439 govind 194
				partnerRegionRepository.persist(partnerRegion);
24417 govind 195
			} else {
196
				continue;
197
			}
198
		}
199
	}
200
 
201
	@Override
24500 govind 202
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<TicketAssigned> ticketAssigneds) {
24417 govind 203
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
24500 govind 204
 
205
		for (TicketAssigned ticketAssigned : ticketAssigneds) {
206
			AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
207
			authUserIdAndAuthUserMap.put(ticketAssigned.getTicketId(), authUser);
208
		}
209
		return authUserIdAndAuthUserMap;
210
	}
211
 
212
	@Override
213
	public Map<Integer, AuthUser> getTicketIdAndAuthUserMapUsingTickets(List<Ticket> tickets) {
214
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
215
 
24417 govind 216
		for (Ticket ticket : tickets) {
24500 govind 217
			AuthUser authUser = authRepository.selectById(ticket.getL1AuthUser());
218
			authUserIdAndAuthUserMap.put(ticket.getId(), authUser);
24417 govind 219
		}
220
		return authUserIdAndAuthUserMap;
221
	}
222
 
223
	@Override
224
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
225
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
226
		for (Ticket ticket : tickets) {
227
			TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
228
			subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
229
		}
230
		return subCategoryIdAndSubCategoryMap;
231
	}
232
 
233
	@Override
234
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
235
		List<Integer> fofoIds = new ArrayList<>();
236
		for (Ticket ticket : tickets) {
237
			fofoIds.add(ticket.getFofoId());
238
		}
239
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
240
		return fofoIdsAndCustomRetailer;
241
	}
242
 
24439 govind 243
	@Override
244
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
245
 
246
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
247
		HashSet<Integer> categoryIds = new HashSet<>();
248
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
249
			categoryIds.add(ticketSubcategory.getcategoryId());
250
		}
251
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
252
		return ticketcategories;
253
	}
254
 
24500 govind 255
	private Ticket setAssignment(Ticket ticket, TicketAssigned ticketAssigned) {
24439 govind 256
		TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
257
		List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
258
		for (Position position : positions) {
259
			if (position.getEscalationType().equals(EscalationType.L1)) {
24500 govind 260
				ticketAssigned.setAssineeId(position.getAuthUserId());
24439 govind 261
				ticket.setL1AuthUser(position.getAuthUserId());
262
			} else {
263
				ticket.setL2AuthUser(position.getAuthUserId());
264
			}
265
		}
266
		return ticket;
267
	}
24500 govind 268
 
24471 govind 269
	@Override
270
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
271
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
272
		for (Position position : positions) {
273
			AuthUser authUser = authRepository.selectById(position.getAuthUserId());
274
			authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
275
		}
276
		return authUserIdAndAuthUserMap;
277
	}
24500 govind 278
 
24471 govind 279
	@Override
280
	public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
281
		Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
282
		for (Position position : positions) {
24500 govind 283
			TicketCategory ticketCategory = ticketCategoryRepository.selectById(position.getCategoryId());
24471 govind 284
			categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
285
		}
286
		return categoryIdAndCategoryMap;
287
	}
24439 govind 288
 
24471 govind 289
	@Override
290
	public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
291
		Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
292
		for (Position position : positions) {
24500 govind 293
			Region region = regionRepository.selectById(position.getRegionId());
24471 govind 294
			regionIdAndRegionMap.put(position.getRegionId(), region);
295
		}
296
		return regionIdAndRegionMap;
297
	}
24500 govind 298
 
299
	@Override
300
	public Map<Integer, List<AuthUser>> getAuthUserList(List<Ticket> tickets, EscalationType escalationType) {
301
		Map<Integer, List<AuthUser>> authUserListMap = new HashMap<>();
302
		for (Ticket ticket : tickets) {
303
			if (escalationType == EscalationType.L2) {
304
				List<AuthUser> authUsers = new ArrayList<>();
305
				authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
306
				authUserListMap.put(ticket.getId(), authUsers);
307
 
308
			} else if (escalationType == EscalationType.L3) {
309
				TicketAssigned ticketAssigned = ticketAssignedRepository
310
						.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(), ticket.getId());
311
				if (ticketAssigned == null) {
312
					List<AuthUser> authUsers = new ArrayList<>();
313
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
314
					authUserListMap.put(ticket.getId(), authUsers);
315
				} else {
316
					List<AuthUser> authUsers = new ArrayList<>();
317
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
318
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
319
					authUserListMap.put(ticket.getId(), authUsers);
320
				}
321
			} else {
322
				TicketAssigned ticketAssigned = ticketAssignedRepository
323
						.selectByAssigneeIdAndTicketId(ticket.getL3AuthUser(), ticket.getId());
324
				if (ticketAssigned == null) {
325
					ticketAssigned = ticketAssignedRepository.selectByAssigneeIdAndTicketId(ticket.getL2AuthUser(),
326
							ticket.getId());
327
					if (ticketAssigned == null) {
328
						List<AuthUser> authUsers = new ArrayList<>();
329
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
330
						authUserListMap.put(ticket.getId(), authUsers);
331
					} else {
332
						List<AuthUser> authUsers = new ArrayList<>();
333
						authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
334
						authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
335
						authUserListMap.put(ticket.getId(), authUsers);
336
					}
337
				} else {
338
					List<AuthUser> authUsers = new ArrayList<>();
339
					authUsers.add(authRepository.selectById(ticket.getL1AuthUser()));
340
					authUsers.add(authRepository.selectById(ticket.getL2AuthUser()));
341
					authUsers.add(authRepository.selectById(ticket.getL3AuthUser()));
342
					authUserListMap.put(ticket.getId(), authUsers);
343
 
344
				}
345
			}
346
		}
347
		return authUserListMap;
348
	}
349
	@Override
350
	public void updateTicket(int categoryId,int subCategoryId,Ticket ticket) throws ProfitMandiBusinessException
351
	{
352
		TicketAssigned ticketAssigned=new TicketAssigned();
353
		ticket.setSubCategoryId(subCategoryId);
354
		ticket.setUpdateTimestamp(LocalDateTime.now());
355
		ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
356
		ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
357
		ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
358
		int l3Auth = this.getAuthUserId(categoryId, EscalationType.L3, ticket.getFofoId());
359
		int l1Auth = this.getAuthUserId(categoryId, EscalationType.L1, ticket.getFofoId());
360
		int l2Auth = this.getAuthUserId(categoryId, EscalationType.L2, ticket.getFofoId());
361
		LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
362
		if (l1Auth == 0) {
363
			if (l2Auth == 0) {
364
				this.setAssignment(ticket, ticketAssigned);
365
			} else {
366
				ticketAssigned.setAssineeId(l2Auth);
367
			}
368
		} else {
369
			ticketAssigned.setAssineeId(l1Auth);
370
			ticket.setL1AuthUser(l1Auth);
371
			ticket.setL2AuthUser(l2Auth);
372
		}
373
		ticket.setL3AuthUser(l3Auth);
374
		ticketRepository.persist(ticket);
375
		ticketAssigned.setTicketId(ticket.getId());
376
		ticketAssignedRepository.persist(ticketAssigned);
377
		AuthUser authUser = authRepository.selectById(ticketAssigned.getAssineeId());
378
		try {
379
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
380
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
381
							retailerService.getFofoRetailer(ticket.getFofoId()).getBusinessName()),
382
					null);
383
		} catch (Exception e) {
384
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
385
					"Could not send ticket assignment mail");
386
		}
387
	}
24471 govind 388
 
24383 amit.gupta 389
}