Subversion Repositories SmartDukaan

Rev

Rev 24439 | Rev 24467 | 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);
24452 govind 127
			if(regions.size()==0)
128
			{
129
				regions=partnerRegionRepository.selectByfofoId(0);
130
				LOGGER.info("regions=" + regions);
131
			}
24439 govind 132
			for (PartnerRegion region : regions) {
133
				positions = positionRepository.selectPositionbyCategoryIdAndEscalationType(categoryId, escalationType,
134
						region.getRegionId());
135
				LOGGER.info("positions:-" + positions);
136
				if (positions != null) {
137
					break;
138
				}
139
 
140
			}
141
			if (positions.size() == 0) {
142
				return 0;
143
			}
144
		}
145
		return positions.get(0).getAuthUserId();
24417 govind 146
		/*
147
		 * if(ActivityType.escalationTypes.contains(escalationType)) {
148
		 * //escalationMatrix } else { throw new
149
		 * ProfitMandiBusinessException("SubCategory", subcategoryId,
150
		 * "Could not find Assignee for "); }
151
		 */
24439 govind 152
 
24383 amit.gupta 153
	}
154
 
155
	@Override
156
	public void addActivity(int ticketId, Activity activity) {
157
		activity.setTicketId(ticketId);
24417 govind 158
		activityRepository.persist(activity);
24383 amit.gupta 159
	}
160
 
24439 govind 161
	private String getRandomString() {
162
		int length = 4;
163
		boolean useLetters = false;
164
		boolean useNumbers = true;
165
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
166
		return generatedString;
167
	}
168
 
24417 govind 169
	@Override
170
	public void addPartnerToRegion(int regionId, List<Integer> fofoIds) {
171
 
172
		for (Integer fofoId : fofoIds) {
24439 govind 173
			PartnerRegion partnerRegion = partnerRegionRepository.selectByRegionIdAndFofoId(regionId, fofoId);
24417 govind 174
			if (partnerRegion == null) {
175
				partnerRegion = new PartnerRegion();
176
				partnerRegion.setFofoId(fofoId);
177
				partnerRegion.setRegionId(regionId);
24439 govind 178
				partnerRegionRepository.persist(partnerRegion);
24417 govind 179
			} else {
180
				continue;
181
			}
182
		}
183
	}
184
 
185
	@Override
186
	public Map<Integer, AuthUser> getAuthUserIdAndAuthUserMap(List<Ticket> tickets) {
187
		Map<Integer, AuthUser> authUserIdAndAuthUserMap = new HashMap<>();
188
		for (Ticket ticket : tickets) {
189
			AuthUser authUser = authRepository.selectById(ticket.getAssigneeId());
190
			authUserIdAndAuthUserMap.put(ticket.getAssigneeId(), authUser);
191
		}
192
		return authUserIdAndAuthUserMap;
193
	}
194
 
195
	@Override
196
	public Map<Integer, TicketSubCategory> getSubCategoryIdAndSubCategoryMap(List<Ticket> tickets) {
197
		Map<Integer, TicketSubCategory> subCategoryIdAndSubCategoryMap = new HashMap<>();
198
		for (Ticket ticket : tickets) {
199
			TicketSubCategory ticketSubCategory = ticketSubCategoryRepository.selectById(ticket.getSubCategoryId());
200
			subCategoryIdAndSubCategoryMap.put(ticket.getSubCategoryId(), ticketSubCategory);
201
		}
202
		return subCategoryIdAndSubCategoryMap;
203
	}
204
 
205
	@Override
206
	public Map<Integer, CustomRetailer> getPartnerByFofoIds(List<Ticket> tickets) {
207
		List<Integer> fofoIds = new ArrayList<>();
208
		for (Ticket ticket : tickets) {
209
			fofoIds.add(ticket.getFofoId());
210
		}
211
		Map<Integer, CustomRetailer> fofoIdsAndCustomRetailer = retailerService.getFofoRetailers(fofoIds);
212
		return fofoIdsAndCustomRetailer;
213
	}
214
 
24439 govind 215
	@Override
216
	public List<TicketCategory> getAllTicketCategotyFromSubCategory() {
217
 
218
		List<TicketSubCategory> ticketSubCategories = ticketSubCategoryRepository.selectAll();
219
		HashSet<Integer> categoryIds = new HashSet<>();
220
		for (TicketSubCategory ticketSubcategory : ticketSubCategories) {
221
			categoryIds.add(ticketSubcategory.getcategoryId());
222
		}
223
		List<TicketCategory> ticketcategories = ticketCategoryRepository.selectAll(new ArrayList<>(categoryIds));
224
		return ticketcategories;
225
	}
226
 
227
	private Ticket setAssignment(Ticket ticket) {
228
		TicketCategory ticketCategory = ticketCategoryRepository.selectByName(ProfitMandiConstants.CRM);
229
		List<Position> positions = positionRepository.selectPositionByCategoryId(ticketCategory.getId());
230
		for (Position position : positions) {
231
			if (position.getEscalationType().equals(EscalationType.L1)) {
232
				ticket.setAssigneeId(position.getAuthUserId());
233
				ticket.setL1AuthUser(position.getAuthUserId());
234
			} else {
235
				ticket.setL2AuthUser(position.getAuthUserId());
236
			}
237
		}
238
		return ticket;
239
	}
240
 
24383 amit.gupta 241
}