| 35867 |
ranu |
1 |
package com.spice.profitmandi.web.service;
|
|
|
2 |
|
| 35879 |
ranu |
3 |
import org.hibernate.Session;
|
|
|
4 |
import org.hibernate.SessionFactory;
|
|
|
5 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 35867 |
ranu |
6 |
import org.springframework.stereotype.Service;
|
|
|
7 |
|
| 35879 |
ranu |
8 |
import javax.annotation.PostConstruct;
|
|
|
9 |
import java.time.LocalDateTime;
|
|
|
10 |
import java.util.ArrayList;
|
|
|
11 |
import java.util.List;
|
|
|
12 |
import java.util.Map;
|
|
|
13 |
import java.util.concurrent.ConcurrentHashMap;
|
|
|
14 |
|
| 35867 |
ranu |
15 |
@Service
|
|
|
16 |
public class AgentLiveStatusService {
|
| 35879 |
ranu |
17 |
|
| 35867 |
ranu |
18 |
// In-memory map to store agent status by authId
|
|
|
19 |
private final Map<Integer, AgentStatus> agentStatusMap = new ConcurrentHashMap<>();
|
|
|
20 |
// Caller ID (Knowlarity SR Number) to Auth ID mapping (loaded from sip_master)
|
|
|
21 |
private final Map<String, Integer> callerIdToAuthIdMap = new ConcurrentHashMap<>();
|
|
|
22 |
// SR Number to Auth ID mapping (loaded from sip_master)
|
|
|
23 |
private final Map<String, Integer> srNumberToAuthIdMap = new ConcurrentHashMap<>();
|
|
|
24 |
// Agent name to Auth ID mapping (fallback)
|
|
|
25 |
private final Map<String, Integer> agentNameToAuthIdMap = new ConcurrentHashMap<>();
|
|
|
26 |
@Autowired
|
|
|
27 |
private SessionFactory sessionFactory;
|
|
|
28 |
private String lastError = null;
|
|
|
29 |
private int lastQueryCount = 0;
|
|
|
30 |
|
| 35879 |
ranu |
31 |
/**
|
| 35867 |
ranu |
32 |
* Load mapping from sip_master table on startup
|
|
|
33 |
* caller_id in sip_master = SR Number on Knowlarity dashboard
|
|
|
34 |
* sip_master has auth_id directly
|
| 35879 |
ranu |
35 |
*/
|
| 35867 |
ranu |
36 |
@PostConstruct
|
|
|
37 |
public void loadMappingFromDatabase() {
|
|
|
38 |
try {
|
|
|
39 |
Session session = sessionFactory.openSession();
|
|
|
40 |
|
|
|
41 |
// Query to get caller_id, auth_user.id, first_name from sip_master
|
|
|
42 |
// Join with auth_user to get correct auth_id used by rbm_call_target page
|
|
|
43 |
String sql = "SELECT sm.caller_id, au.id, sm.first_name " +
|
|
|
44 |
"FROM auth.sip_master sm " +
|
|
|
45 |
"JOIN auth.auth_user au ON sm.email_id = au.email_id " +
|
|
|
46 |
"WHERE au.id IS NOT NULL";
|
|
|
47 |
|
|
|
48 |
List<Object[]> results = session.createNativeQuery(sql).getResultList();
|
|
|
49 |
lastQueryCount = results.size();
|
|
|
50 |
|
|
|
51 |
for (Object[] row : results) {
|
|
|
52 |
String callerId = row[0] != null ? row[0].toString().trim() : "";
|
|
|
53 |
int authId = row[1] != null ? ((Number) row[1]).intValue() : 0;
|
|
|
54 |
String firstName = row[2] != null ? row[2].toString().trim() : "";
|
|
|
55 |
|
|
|
56 |
if (authId > 0) {
|
|
|
57 |
// Map by caller_id if available
|
|
|
58 |
if (!callerId.isEmpty()) {
|
|
|
59 |
callerIdToAuthIdMap.put(callerId, authId);
|
|
|
60 |
}
|
|
|
61 |
// Map by first_name for name-based matching
|
|
|
62 |
if (!firstName.isEmpty()) {
|
|
|
63 |
agentNameToAuthIdMap.put(firstName.toLowerCase(), authId);
|
|
|
64 |
System.out.println("Mapping: '" + firstName + "' -> authId=" + authId + " (caller_id=" + callerId + ")");
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
session.close();
|
|
|
70 |
System.out.println("Loaded " + callerIdToAuthIdMap.size() + " caller_id -> auth_id mappings from sip_master");
|
|
|
71 |
System.out.println("Loaded " + agentNameToAuthIdMap.size() + " name -> auth_id mappings");
|
|
|
72 |
|
|
|
73 |
} catch (Exception e) {
|
|
|
74 |
lastError = e.getClass().getSimpleName() + ": " + e.getMessage();
|
|
|
75 |
System.err.println("Error loading sip_master mapping: " + e.getMessage());
|
|
|
76 |
e.printStackTrace();
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
public String getLastError() {
|
|
|
81 |
return lastError;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public int getLastQueryCount() {
|
|
|
85 |
return lastQueryCount;
|
|
|
86 |
}
|
|
|
87 |
|
| 35879 |
ranu |
88 |
/**
|
| 35867 |
ranu |
89 |
* Reload mapping from database (can be called via API)
|
| 35879 |
ranu |
90 |
*/
|
| 35867 |
ranu |
91 |
public void reloadMapping() {
|
|
|
92 |
callerIdToAuthIdMap.clear();
|
|
|
93 |
srNumberToAuthIdMap.clear();
|
|
|
94 |
agentNameToAuthIdMap.clear();
|
|
|
95 |
lastError = null;
|
|
|
96 |
lastQueryCount = 0;
|
|
|
97 |
loadMappingFromDatabase();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
public Integer getAuthIdByCallerId(String callerId) {
|
|
|
101 |
return callerIdToAuthIdMap.get(callerId);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
public void setSrNumberMapping(String srNumber, int authId) {
|
|
|
105 |
srNumberToAuthIdMap.put(srNumber, authId);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public void setAgentNameMapping(String agentName, int authId) {
|
|
|
109 |
agentNameToAuthIdMap.put(agentName.toLowerCase(), authId);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public Integer getAuthIdBySrNumber(String srNumber) {
|
|
|
113 |
return srNumberToAuthIdMap.get(srNumber);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public Integer getAuthIdByAgentName(String agentName) {
|
|
|
117 |
// Try exact match
|
|
|
118 |
Integer authId = agentNameToAuthIdMap.get(agentName.toLowerCase());
|
|
|
119 |
if (authId != null) return authId;
|
|
|
120 |
|
|
|
121 |
// Try partial match
|
|
|
122 |
for (Map.Entry<String, Integer> entry : agentNameToAuthIdMap.entrySet()) {
|
|
|
123 |
if (agentName.toLowerCase().contains(entry.getKey()) || entry.getKey().contains(agentName.toLowerCase())) {
|
|
|
124 |
return entry.getValue();
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
return null;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public Map<String, Integer> getSrNumberMapping() {
|
|
|
131 |
return srNumberToAuthIdMap;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public Map<String, Integer> getAgentNameMapping() {
|
|
|
135 |
return agentNameToAuthIdMap;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public Map<String, Integer> getCallerIdMapping() {
|
|
|
139 |
return callerIdToAuthIdMap;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public void clearMappings() {
|
|
|
143 |
srNumberToAuthIdMap.clear();
|
|
|
144 |
agentNameToAuthIdMap.clear();
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
public void updateStatus(int authId, String srNumber, String agentName, String agentStatus, String timeInStatus, String customerNumber) {
|
|
|
148 |
AgentStatus status = new AgentStatus(authId, srNumber, agentName, agentStatus, timeInStatus, customerNumber);
|
|
|
149 |
agentStatusMap.put(authId, status);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
public AgentStatus getStatusByAuthId(int authId) {
|
|
|
153 |
return agentStatusMap.get(authId);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
public Map<Integer, AgentStatus> getAllStatusMap() {
|
|
|
157 |
return agentStatusMap;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
public List<AgentStatus> getAllStatuses() {
|
|
|
161 |
return new ArrayList<>(agentStatusMap.values());
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
public void clearAll() {
|
|
|
165 |
agentStatusMap.clear();
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
// Inner class to hold agent status
|
|
|
169 |
public static class AgentStatus {
|
|
|
170 |
private int authId;
|
|
|
171 |
private String srNumber;
|
|
|
172 |
private String agentName;
|
|
|
173 |
private String agentStatus;
|
|
|
174 |
private String timeInStatus;
|
|
|
175 |
private String customerNumber;
|
|
|
176 |
private LocalDateTime lastUpdated;
|
|
|
177 |
|
|
|
178 |
public AgentStatus() {
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
public AgentStatus(int authId, String srNumber, String agentName, String agentStatus, String timeInStatus, String customerNumber) {
|
|
|
182 |
this.authId = authId;
|
|
|
183 |
this.srNumber = srNumber;
|
|
|
184 |
this.agentName = agentName;
|
|
|
185 |
this.agentStatus = agentStatus;
|
|
|
186 |
this.timeInStatus = timeInStatus;
|
|
|
187 |
this.customerNumber = customerNumber;
|
|
|
188 |
this.lastUpdated = LocalDateTime.now();
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
public int getAuthId() {
|
|
|
192 |
return authId;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
public void setAuthId(int authId) {
|
|
|
196 |
this.authId = authId;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
public String getSrNumber() {
|
|
|
200 |
return srNumber;
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
public void setSrNumber(String srNumber) {
|
|
|
204 |
this.srNumber = srNumber;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
public String getAgentName() {
|
|
|
208 |
return agentName;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
public void setAgentName(String agentName) {
|
|
|
212 |
this.agentName = agentName;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
public String getAgentStatus() {
|
|
|
216 |
return agentStatus;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
public void setAgentStatus(String agentStatus) {
|
|
|
220 |
this.agentStatus = agentStatus;
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
public String getTimeInStatus() {
|
|
|
224 |
return timeInStatus;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
public void setTimeInStatus(String timeInStatus) {
|
|
|
228 |
this.timeInStatus = timeInStatus;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
public String getCustomerNumber() {
|
|
|
232 |
return customerNumber;
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
public void setCustomerNumber(String customerNumber) {
|
|
|
236 |
this.customerNumber = customerNumber;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
public LocalDateTime getLastUpdated() {
|
|
|
240 |
return lastUpdated;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
public void setLastUpdated(LocalDateTime lastUpdated) {
|
|
|
244 |
this.lastUpdated = lastUpdated;
|
|
|
245 |
}
|
| 35879 |
ranu |
246 |
}
|
| 35867 |
ranu |
247 |
}
|