Subversion Repositories SmartDukaan

Rev

Rev 35867 | Rev 35879 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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