Subversion Repositories SmartDukaan

Rev

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