Subversion Repositories SmartDukaan

Rev

Rev 24417 | Rev 24452 | 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());
24439 govind 78
		ticket.setHappyCode(getRandomString());
79
		int l3Auth = this.getAuthUserId(categoryId, escalationTypeL3, fofoId);
80
		int l1Auth = this.getAuthUserId(categoryId, escalationTypeL1, fofoId);
81
		int l2Auth = this.getAuthUserId(categoryId, escalationTypeL2, fofoId);
82
		LOGGER.info(l1Auth + "-" + l2Auth + "-" + l3Auth);
83
		if (l1Auth == 0) {
84
			if (l2Auth == 0) {
85
				this.setAssignment(ticket);
86
			} else {
87
				ticket.setAssigneeId(l2Auth);
88
			}
89
		} else {
90
			ticket.setAssigneeId(l1Auth);
91
			ticket.setL1AuthUser(l1Auth);
92
			ticket.setL2AuthUser(l2Auth);
93
		}
94
		ticket.setL3AuthUser(l3Auth);
95
		ticketRepository.persist(ticket);
96
		AuthUser authUser = authRepository.selectById(ticket.getAssigneeId());
24383 amit.gupta 97
		try {
24439 govind 98
			Utils.sendMailWithAttachments(mailSender, authUser.getEmailId(), null, ASSINMENT_SUBJECT,
99
					String.format(ASSIGNED_TICKET, authUser.getFirstName(),
100
							retailerService.getFofoRetailer(fofoId).getBusinessName()),
101
					null);
24383 amit.gupta 102
		} catch (Exception e) {
24439 govind 103
			throw new ProfitMandiBusinessException("Ticket Assingment", authUser.getEmailId(),
104
					"Could not send ticket assignment mail");
24383 amit.gupta 105
		}
106
		Activity activity = this.createActivity(type, message, 0);
107
		this.addActivity(ticket.getId(), activity);
108
	}
109
 
110
	private Activity createActivity(ActivityType activityType, String message, int createdBy) {
111
		Activity activity = new Activity();
112
		activity.setMessage(message);
113
		activity.setCreatedBy(createdBy);
114
		activity.setType(activityType);
24417 govind 115
		activity.setCreateTimestamp(LocalDateTime.now());
24383 amit.gupta 116
		return activity;
117
	}
24417 govind 118
 
24439 govind 119
	private int getAuthUserId(int categoryId, EscalationType escalationType, int fofoId) {
24417 govind 120
 
24439 govind 121
		List<Position> positions = null;
122
		if (escalationType == EscalationType.L3) {
123
			positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(0, escalationType, 0);
124
		} else {
125
			List<PartnerRegion> regions = partnerRegionRepository.selectByfofoId(fofoId);
126
			LOGGER.info("regions=" + regions);
127
			for (PartnerRegion region : regions) {
128
				positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
129
						region.getRegionId());
130
				LOGGER.info("positions:-" + positions);
131
				if (positions != null) {
132
					break;
133
				}
134
 
135
			}
136
			if (positions.size() == 0) {
137
				return 0;
138
			}
139
		}
140
		return positions.get(0).getAuthUserId();
24417 govind 141
		/*
142
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
143
		 * //escalationMatrix } else { throw new
144
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
145
		 * "Could not find Assignee for "); }
146
		 */
24439 govind 147
 
24383 amit.gupta 148
	}
149
 
150
	@Override
151
	public void addActivity(int ticketId, Activity activity) {
152
		activity.setTicketId(ticketId);
24417 govind 153
		activityRepository.persist(activity);
24383 amit.gupta 154
	}
155
 
24439 govind 156
	private String getRandomString() {
157
		int length = 4;
158
		boolean useLetters = false;
159
		boolean useNumbers = true;
160
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
161
		return generatedString;
162
	}
163
 
24417 govind 164
	@Override
165
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
166
 
167
		for (Integer fofoId : fofoIds) {
24439 govind 168
			PartnerRegion partnerRegion = partnerRegionRepository.selectByRegionIdAndFofoId(regionId, fofoId);
24417 govind 169
			if (partnerRegion == null) {
170
				partnerRegion = new PartnerRegion();
171
				partnerRegion.setFofoId(fofoId);
172
				partnerRegion.setRegionId(regionId);
24439 govind 173
				partnerRegionRepository.persist(partnerRegion);
24417 govind 174
			} else {
175
				continue;
176
			}
177
		}
178
	}
179
 
180
	@Override
181
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<Ticket> tickets) {
182
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
183
		for (Ticket ticket : tickets) {
184
			AuthUser authUser = authRepository.selectById(ticket.getAssigneeId());
185
			authUserIdAndAuthUserMap.put(ticket.getAssigneeId(), authUser);
186
		}
187
		return authUserIdAndAuthUserMap;
188
	}
189
 
190
	@Override
191
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
192
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
193
		for (Ticket ticket : tickets) {
194
			TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
195
			subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
196
		}
197
		return subCategoryIdAndSubCategoryMap;
198
	}
199
 
200
	@Override
201
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
202
		List<Integer> fofoIds = new ArrayList<>();
203
		for (Ticket ticket : tickets) {
204
			fofoIds.add(ticket.getFofoId());
205
		}
206
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
207
		return fofoIdsAndCustomRetailer;
208
	}
209
 
24439 govind 210
	@Override
211
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
212
 
213
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
214
		HashSet<Integer> categoryIds = new HashSet<>();
215
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
216
			categoryIds.add(ticketSubcategory.getcategoryId());
217
		}
218
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
219
		return ticketcategories;
220
	}
221
 
222
	private Ticket setAssignment(Ticket ticket) {
223
		TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
224
		List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
225
		for (Position position : positions) {
226
			if (position.getEscalationType().equals(EscalationType.L1)) {
227
				ticket.setAssigneeId(position.getAuthUserId());
228
				ticket.setL1AuthUser(position.getAuthUserId());
229
			} else {
230
				ticket.setL2AuthUser(position.getAuthUserId());
231
			}
232
		}
233
		return ticket;
234
	}
235
 
24383 amit.gupta 236
}