Subversion Repositories SmartDukaan

Rev

Rev 24467 | Rev 24500 | 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;
2
import java.time.LocalDateTime;
24417 govind 3
import java.util.ArrayList;
4
import java.util.HashMap;
24439 govind 5
import java.util.HashSet;
24417 govind 6
import java.util.List;
7
import java.util.Map;
24439 govind 8
import org.apache.commons.lang.RandomStringUtils;
9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
24383 amit.gupta 11
import org.springframework.beans.factory.annotation.Autowired;
24439 govind 12
import org.springframework.mail.javamail.JavaMailSender;
24383 amit.gupta 13
import org.springframework.stereotype.Component;
14
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24417 govind 15
import com.spice.profitmandi.common.model.CustomRetailer;
24439 govind 16
import com.spice.profitmandi.common.model.ProfitMandiConstants;
17
import com.spice.profitmandi.common.util.Utils;
24417 govind 18
import com.spice.profitmandi.dao.entity.auth.AuthUser;
24383 amit.gupta 19
import com.spice.profitmandi.dao.entity.cs.Activity;
24417 govind 20
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
21
import com.spice.profitmandi.dao.entity.cs.Position;
24471 govind 22
import com.spice.profitmandi.dao.entity.cs.Region;
24383 amit.gupta 23
import com.spice.profitmandi.dao.entity.cs.Ticket;
24439 govind 24
import com.spice.profitmandi.dao.entity.cs.TicketCategory;
24417 govind 25
import com.spice.profitmandi.dao.entity.cs.TicketSubCategory;
24383 amit.gupta 26
import com.spice.profitmandi.dao.entity.fofo.ActivityType;
24417 govind 27
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
28
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
29
import com.spice.profitmandi.service.user.RetailerService;
24383 amit.gupta 30
 
31
@Component
32
public class CsServiceImpl implements CsService {
24417 govind 33
 
24439 govind 34
	private static final Logger LOGGER = LogManager.getLogger(CsServiceImpl.class);
35
 
36
	private static final String ASSIGNED_TICKET = "Dear %s,You have assigned a ticket by %s. Regards\nSmartdukaan";
37
	private static final String ASSINMENT_SUBJECT = "Assignment Ticket";
38
 
24383 amit.gupta 39
	@Autowired
40
	TicketRepository ticketRepository;
24417 govind 41
 
24383 amit.gupta 42
	@Autowired
24439 govind 43
	JavaMailSender mailSender;
44
 
45
	@Autowired
24383 amit.gupta 46
	TicketCategoryRepository ticketCategoryRepository;
24417 govind 47
 
24383 amit.gupta 48
	@Autowired
49
	TicketSubCategoryRepository ticketSubCategoryRepository;
24417 govind 50
 
51
	@Autowired
24388 amit.gupta 52
	ActivityRepository activityRepository;
24417 govind 53
 
54
	@Autowired
24439 govind 55
	PartnerRegionRepository partnerRegionRepository;
24417 govind 56
 
57
	@Autowired
58
	private PositionRepository positionRepository;
59
 
60
	@Autowired
61
	private AuthRepository authRepository;
62
 
63
	@Autowired
64
	private RetailerService retailerService;
24471 govind 65
 
66
	@Autowired
67
	private RegionRepository regionRepository;
24417 govind 68
 
24383 amit.gupta 69
	@Override
24439 govind 70
	public void createTicket(int fofoId, int categoryId, int subcategoryId, String message)
71
			throws ProfitMandiBusinessException {
24417 govind 72
 
24383 amit.gupta 73
		ActivityType type = ActivityType.OPENED;
24439 govind 74
		EscalationType escalationTypeL1 = EscalationType.L1;
75
		EscalationType escalationTypeL2 = EscalationType.L2;
76
		EscalationType escalationTypeL3 = EscalationType.L3;
24383 amit.gupta 77
		Ticket ticket = new Ticket();
78
		ticket.setSubCategoryId(subcategoryId);
24417 govind 79
		ticket.setFofoId(fofoId);
24383 amit.gupta 80
		ticket.setCreateTimestamp(LocalDateTime.now());
24417 govind 81
		ticket.setUpdateTimestamp(LocalDateTime.now());
24467 govind 82
		ticket.setL2EscalationTimestamp(ticket.getUpdateTimestamp().plusDays(2));
83
		ticket.setL3EscalationTimestamp(ticket.getL2EscalationTimestamp().plusDays(2));
84
		ticket.setLastEscalationTimestamp(ticket.getL3EscalationTimestamp().plusDays(2));
24439 govind 85
		ticket.setHappyCode(getRandomString());
86
		int l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
87
		int l1Auth = this.getAuthUserId(categoryId, escalationTypeL1, fofoId);
88
		int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
89
		LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
90
		if (l1Auth == 0) {
91
			if (l2Auth == 0) {
92
				this.setAssignment(ticket);
93
			} else {
94
				ticket.setAssigneeId(l2Auth);
95
			}
96
		} else {
97
			ticket.setAssigneeId(l1Auth);
98
			ticket.setL1AuthUser(l1Auth);
99
			ticket.setL2AuthUser(l2Auth);
100
		}
101
		ticket.setL3AuthUser(l3Auth);
102
		ticketRepository.persist(ticket);
103
		AuthUser authUser = authRepository.selectById(ticket.getAssigneeId());
24383 amit.gupta 104
		try {
24439 govind 105
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
106
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
107
							retailerService.getFofoRetailer(fofoId).getBusinessName()),
108
					null);
24383 amit.gupta 109
		} catch (Exception e) {
24439 govind 110
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
111
					"Could not send ticket assignment mail");
24383 amit.gupta 112
		}
113
		Activity activity = this.createActivity(type, message, 0);
114
		this.addActivity(ticket.getId(), activity);
115
	}
116
 
117
	private Activity createActivity(ActivityType activityType, String message, int createdBy) {
118
		Activity activity = new Activity();
119
		activity.setMessage(message);
120
		activity.setCreatedBy(createdBy);
121
		activity.setType(activityType);
24417 govind 122
		activity.setCreateTimestamp(LocalDateTime.now());
24383 amit.gupta 123
		return activity;
124
	}
24417 govind 125
 
24439 govind 126
	private int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
24417 govind 127
 
24439 govind 128
		List<Position> positions = null;
129
		if (escalationType == EscalationType.L3) {
130
			positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
131
		} else {
132
			List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
133
			LOGGER.info("regions=" + regions);
24452 govind 134
			if(regions.size()==0)
135
			{
136
				regions=partnerRegionRepository.selectByfofoId(0);
137
				LOGGER.info("regions=" + regions);
138
			}
24439 govind 139
			for (PartnerRegion region : regions) {
140
				positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
141
						region.getRegionId());
142
				LOGGER.info("positions:-" + positions);
143
				if (positions != null) {
144
					break;
145
				}
146
 
147
			}
148
			if (positions.size() == 0) {
149
				return 0;
150
			}
151
		}
152
		return positions.get(0).getAuthUserId();
24417 govind 153
		/*
154
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
155
		 * //escalationMatrix } else { throw new
156
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
157
		 * "Could not find Assignee for "); }
158
		 */
24439 govind 159
 
24383 amit.gupta 160
	}
161
 
162
	@Override
163
	public void addActivity(int ticketId, Activity activity) {
164
		activity.setTicketId(ticketId);
24417 govind 165
		activityRepository.persist(activity);
24383 amit.gupta 166
	}
167
 
24439 govind 168
	private String getRandomString() {
169
		int length = 4;
170
		boolean useLetters = false;
171
		boolean useNumbers = true;
172
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
173
		return generatedString;
174
	}
175
 
24417 govind 176
	@Override
177
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
178
 
179
		for (Integer fofoId : fofoIds) {
24439 govind 180
			PartnerRegion partnerRegion = partnerRegionRepository.selectByRegionIdAndFofoId(regionId, fofoId);
24417 govind 181
			if (partnerRegion == null) {
182
				partnerRegion = new PartnerRegion();
183
				partnerRegion.setFofoId(fofoId);
184
				partnerRegion.setRegionId(regionId);
24439 govind 185
				partnerRegionRepository.persist(partnerRegion);
24417 govind 186
			} else {
187
				continue;
188
			}
189
		}
190
	}
191
 
192
	@Override
193
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<Ticket> tickets) {
194
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
195
		for (Ticket ticket : tickets) {
196
			AuthUser authUser = authRepository.selectById(ticket.getAssigneeId());
197
			authUserIdAndAuthUserMap.put(ticket.getAssigneeId(), authUser);
198
		}
199
		return authUserIdAndAuthUserMap;
200
	}
201
 
202
	@Override
203
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
204
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
205
		for (Ticket ticket : tickets) {
206
			TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
207
			subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
208
		}
209
		return subCategoryIdAndSubCategoryMap;
210
	}
211
 
212
	@Override
213
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
214
		List<Integer> fofoIds = new ArrayList<>();
215
		for (Ticket ticket : tickets) {
216
			fofoIds.add(ticket.getFofoId());
217
		}
218
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
219
		return fofoIdsAndCustomRetailer;
220
	}
221
 
24439 govind 222
	@Override
223
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
224
 
225
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
226
		HashSet<Integer> categoryIds = new HashSet<>();
227
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
228
			categoryIds.add(ticketSubcategory.getcategoryId());
229
		}
230
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
231
		return ticketcategories;
232
	}
233
 
234
	private Ticket setAssignment(Ticket ticket) {
235
		TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
236
		List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
237
		for (Position position : positions) {
238
			if (position.getEscalationType().equals(EscalationType.L1)) {
239
				ticket.setAssigneeId(position.getAuthUserId());
240
				ticket.setL1AuthUser(position.getAuthUserId());
241
			} else {
242
				ticket.setL2AuthUser(position.getAuthUserId());
243
			}
244
		}
245
		return ticket;
246
	}
24471 govind 247
 
248
	@Override
249
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMapUsingPositions(List<Position> positions) {
250
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
251
		for (Position position : positions) {
252
			AuthUser authUser = authRepository.selectById(position.getAuthUserId());
253
			authUserIdAndAuthUserMap.put(position.getAuthUserId(), authUser);
254
		}
255
		return authUserIdAndAuthUserMap;
256
	}
257
 
258
	@Override
259
	public Map<Integer, TicketCategory> getCategoryIdAndCategoryUsingPositions(List<Position> positions) {
260
		Map<Integer, TicketCategory> categoryIdAndCategoryMap = new HashMap<>();
261
		for (Position position : positions) {
262
			TicketCategory ticketCategory=ticketCategoryRepository.selectById(position.getCategoryId());
263
			categoryIdAndCategoryMap.put(position.getCategoryId(), ticketCategory);
264
		}
265
		return categoryIdAndCategoryMap;
266
	}
24439 govind 267
 
24471 govind 268
	@Override
269
	public Map<Integer, Region> getRegionIdAndRegionMap(List<Position> positions) {
270
		Map<Integer, Region> regionIdAndRegionMap = new HashMap<>();
271
		for (Position position : positions) {
272
			Region region=regionRepository.selectById(position.getRegionId());
273
			regionIdAndRegionMap.put(position.getRegionId(), region);
274
		}
275
		return regionIdAndRegionMap;
276
	}
277
 
24383 amit.gupta 278
}