Subversion Repositories SmartDukaan

Rev

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