You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

656 lines
29 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package in.gov.chennaicorporation.gccoffice.callcenter.service;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12. import javax.sql.DataSource;
  13. import javax.transaction.Transactional;
  14. import org.apache.poi.ss.usermodel.Cell;
  15. import org.apache.poi.ss.usermodel.DateUtil;
  16. import org.apache.poi.ss.usermodel.Row;
  17. import org.apache.poi.ss.usermodel.Sheet;
  18. import org.apache.poi.ss.usermodel.Workbook;
  19. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Qualifier;
  22. import org.springframework.dao.DataAccessException;
  23. import org.springframework.jdbc.core.JdbcTemplate;
  24. import org.springframework.stereotype.Service;
  25. @Service
  26. public class AgentsService {
  27. private JdbcTemplate jdbcTemplate;
  28. @Autowired
  29. public void setDataSource(@Qualifier("mysql1913CampaignDataSource") DataSource dataSource) {
  30. this.jdbcTemplate = new JdbcTemplate(dataSource);
  31. }
  32. @Autowired
  33. public void setDataSource2(@Qualifier("mysql1913QAQCDataSource") DataSource dataSource) {
  34. this.jdbcTemplate = new JdbcTemplate(dataSource);
  35. }
  36. @Autowired
  37. private JdbcTemplate jdbcTemplate2;
  38. // Utility method to get cell value based on type
  39. private String getCellValue(Cell cell) {
  40. if (cell == null) {
  41. return "";
  42. }
  43. switch (cell.getCellType()) {
  44. case STRING:
  45. return cell.getStringCellValue();
  46. case NUMERIC:
  47. // Check if the numeric cell contains a date
  48. if (DateUtil.isCellDateFormatted(cell)) {
  49. return new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
  50. }
  51. return String.valueOf((long) cell.getNumericCellValue()); // Cast to long to avoid scientific notation
  52. case BOOLEAN:
  53. return String.valueOf(cell.getBooleanCellValue());
  54. case FORMULA:
  55. return cell.getCellFormula();
  56. default:
  57. return "";
  58. }
  59. }
  60. // Parse integer values (for ID)
  61. private Integer parseInteger(String value) {
  62. try {
  63. return Integer.parseInt(value);
  64. } catch (NumberFormatException e) {
  65. return null; // return null if parsing fails
  66. }
  67. }
  68. // Parse long values (for Mobile number)
  69. private Long parseLong(String value) {
  70. try {
  71. return Long.parseLong(value);
  72. } catch (NumberFormatException e) {
  73. return null; // return null if parsing fails
  74. }
  75. }
  76. // Example fetch method
  77. public List<Map<String, Object>> fetchDataFromExcel(int categoryId,Long campaignId) throws IOException {
  78. String excelUrl = getExcelUrlByCampaignId(campaignId);
  79. List<Map<String, Object>>data=getExcelData(categoryId,campaignId, excelUrl);
  80. return saveExcelData(data);
  81. }
  82. public String getExcelUrlByCampaignId(Long campaignId) {
  83. String sql = "SELECT excel_url\r\n"
  84. + "FROM gcc_1913_campaign.campaign_request\r\n"
  85. + "WHERE campaign_id = ?";
  86. return jdbcTemplate.queryForObject(sql, String.class, campaignId);
  87. }
  88. public List<Map<String, Object>> getExcelData(int categoryId,Long campaignId,String filePath) throws IOException {
  89. List<Map<String, Object>> data = new ArrayList<>();
  90. File file = new File(filePath);
  91. try (FileInputStream fis = new FileInputStream(file);
  92. Workbook workbook = new XSSFWorkbook(fis)) {
  93. Sheet sheet = workbook.getSheetAt(0);
  94. for (Row row : sheet) {
  95. if (row.getRowNum() == 0) continue; // skip header row
  96. Map<String, Object> rowData = new HashMap<>();
  97. // Add the campaign_id and category_id to each row
  98. rowData.put("category_id", categoryId);
  99. rowData.put("campaign_id", campaignId);
  100. // Convert ID to Integer
  101. rowData.put("ID", getCellValue(row.getCell(0)));
  102. // Name remains String
  103. rowData.put("Name", getCellValue(row.getCell(1)));
  104. // Address remains String
  105. rowData.put("Address", getCellValue(row.getCell(2)));
  106. // Dues remains String (you can convert if necessary)
  107. rowData.put("Balance amount", parseLong(getCellValue(row.getCell(3))));
  108. // Convert Mobile number to Long
  109. rowData.put("Mobile number", parseLong(getCellValue(row.getCell(4))));
  110. rowData.put("Tiny url", getCellValue(row.getCell(5)));
  111. data.add(rowData);
  112. }
  113. }
  114. return data;
  115. }
  116. public List<Map<String, Object>> saveExcelData(List<Map<String, Object>> data) {
  117. // Prepare SQL query for inserting the specified columns
  118. String sql = "INSERT INTO gcc_1913_campaign.upload_data (category_id, campaign_id, field_1, field_2, field_3, field_4, field_5,field_6) " +
  119. "VALUES (?, ?, ?, ?, ?, ?, ?,?)";
  120. // Loop through the data list and insert each row into the database
  121. for (Map<String, Object> row : data) {
  122. jdbcTemplate.update(sql,
  123. row.get("category_id"), // Category ID
  124. row.get("campaign_id"), // Campaign ID
  125. row.get("ID"), // ID (String)
  126. row.get("Name"), // Name (String)
  127. row.get("Address"), // Address (String)
  128. row.get("Balance amount"), // Dues if any (String)
  129. row.get("Mobile number"), // Mobile number (Long)
  130. row.get("Tiny url")
  131. );
  132. }
  133. // Return the inserted data (if needed)
  134. return data; // Optionally return the inserted data or change the return type as per your use case
  135. }
  136. //For Assigned tasks
  137. // @Transactional
  138. // public List<Map<String, Object>> getAgentsALLCampaigns(int agent_id) {
  139. // String SqlQuery = "SELECT \r\n"
  140. // + " c.category_name,\r\n"
  141. // + " cr.campaign_name,\r\n"
  142. // + " cr.calls_per_agent,\r\n"
  143. // + " aa.campaign_id,\r\n"
  144. // + " aa.agent_id,\r\n"
  145. // + " (SELECT COUNT(data_id) \r\n"
  146. // + " FROM gcc_1913_campaign.upload_data up \r\n"
  147. // + " WHERE up.agent_id = aa.agent_id \r\n"
  148. // + " AND up.campaign_id = cr.campaign_id\r\n"
  149. // + " ) AS calls_assigned,\r\n"
  150. // + " (SELECT COUNT(CASE WHEN up.field_14 IS NULL THEN 1 END) \r\n"
  151. // + " FROM gcc_1913_campaign.upload_data up \r\n"
  152. // + " WHERE up.agent_id = aa.agent_id \r\n"
  153. // + " AND up.campaign_id = cr.campaign_id\r\n"
  154. // + " ) AS pending_count,\r\n"
  155. // + " (SELECT COUNT(CASE WHEN up.field_14 IS NOT NULL THEN 1 END) \r\n"
  156. // + " FROM gcc_1913_campaign.upload_data up \r\n"
  157. // + " WHERE up.agent_id = aa.agent_id \r\n"
  158. // + " AND up.campaign_id = cr.campaign_id\r\n"
  159. // + " ) AS complete_count,\r\n"
  160. // + " (SELECT COUNT(CASE WHEN up.field_14 ='CALL_BACK_LATER' OR up.field_14 = 'UNATTENDED' THEN 1 END) \r\n"
  161. // + " FROM gcc_1913_campaign.upload_data up \r\n"
  162. // + " WHERE up.agent_id = aa.agent_id \r\n"
  163. // + " AND up.campaign_id = cr.campaign_id\r\n"
  164. // + " ) AS followup_count\r\n"
  165. // + " \r\n"
  166. // + "FROM \r\n"
  167. // + " gcc_1913_campaign.agent_assign aa\r\n"
  168. // + "JOIN \r\n"
  169. // + " gcc_1913_qaqc.agents_list al ON aa.agent_id = al.agent_id\r\n"
  170. // + "JOIN \r\n"
  171. // + " gcc_1913_campaign.campaign_request cr ON cr.campaign_id = aa.campaign_id\r\n"
  172. // + "JOIN \r\n"
  173. // + " gcc_1913_campaign.category c ON cr.category_id = c.category_id\r\n"
  174. // + "WHERE \r\n"
  175. // + " aa.agent_id = ? \r\n"
  176. // + " AND aa.isactive = '1'\r\n"
  177. // + " AND cr.isactive = '0'";
  178. //
  179. // return jdbcTemplate.queryForList(SqlQuery,agent_id);
  180. // }
  181. @Transactional
  182. public List<Map<String, Object>> getdataByIds(List<Integer> ids) {
  183. // Construct the SQL query with placeholders for the number of IDs
  184. String inSql = String.join(",", Collections.nCopies(ids.size(), "?"));
  185. String SqlQuery = "SELECT data_id, field_1 as id, field_2 as name, field_3 as address, field_4 as dues, field_5 as cellnumber,field_6 as viewdetails,field_14 as call_status,field_15 as remarks " +
  186. "FROM gcc_1913_campaign.upload_data " +
  187. "WHERE data_id IN (" + inSql + ")";
  188. // Convert the list of ids to an Object array
  189. return jdbcTemplate.queryForList(SqlQuery, ids.toArray());
  190. }
  191. @Transactional
  192. public void saveCallLogs(String call_status,String remarks ,int data_id,int agent_id)
  193. {
  194. String SqlQuery = "UPDATE gcc_1913_campaign.upload_data SET call_status = ?, remarks=?, updated_date = NOW() ,updated_agent_id=?, is_processed=1 WHERE (data_id = ? )";
  195. jdbcTemplate.update(SqlQuery,call_status, remarks,agent_id,data_id);
  196. }
  197. @Transactional
  198. public int saveInLogsTable(int campaignId, int agentId, int dataId, String call_status,String remarks) {
  199. String sqlQuery = "INSERT INTO gcc_1913_campaign.call_logs (campaign_id,agent_id,data_id,call_status,remarks) VALUES (?, ?, ?,?,?)";
  200. int rowsAffected = 0;
  201. try {
  202. rowsAffected = jdbcTemplate.update(sqlQuery, campaignId, agentId, dataId,call_status,remarks);
  203. //System.out.println("Inserted into call_logs successfully. Rows affected: " + rowsAffected);
  204. } catch (Exception e) {
  205. System.err.println("Error inserting into call_logs: " + e.getMessage());
  206. // Log the exception or rethrow it based on your error-handling strategy
  207. }
  208. return rowsAffected;
  209. }
  210. @Transactional
  211. public List<Map<String, Object>> getPendingDataByIds(List<Integer> ids) {
  212. // Construct the SQL query with placeholders for the number of IDs
  213. String inSql = String.join(",", Collections.nCopies(ids.size(), "?"));
  214. String SqlQuery = "SELECT data_id, field_1 as id, field_2 as name, field_3 as address, field_4 as dues, field_5 as cellnumber,field_6 as viewdetails,field_14 as call_status,field_15 as remarks " +
  215. "FROM gcc_1913_campaign.upload_data " +
  216. "WHERE data_id IN (" + inSql + ") " +
  217. "AND field_14 IN ('UNATTENDED', 'CALL_BACK_LATER')";
  218. // Convert the list of ids to an Object array
  219. return jdbcTemplate.queryForList(SqlQuery, ids.toArray());
  220. }
  221. @Transactional
  222. public List<Map<String, Object>> getAgentsNameById(List<Integer> agentIds)
  223. {
  224. String inSql = String.join(",", Collections.nCopies(agentIds.size(), "?"));
  225. String SqlQuery ="select agent_name,agent_id from gcc_1913_qaqc.agents_list where agent_id IN (" + inSql + ")";
  226. return jdbcTemplate.queryForList(SqlQuery, agentIds.toArray());
  227. }
  228. //agents performance
  229. @Transactional
  230. public List<Map<String, Object>> getDataDetailsById(List<Integer> dataIds)
  231. {
  232. if (dataIds.isEmpty()) {
  233. // Handle the case where dataIds is empty to avoid SQL syntax error.
  234. throw new IllegalArgumentException("The dataIds list is empty, cannot execute query.");
  235. }
  236. String inSql = String.join(",", Collections.nCopies(dataIds.size(), "?"));
  237. String SqlQuery ="SELECT\r\n"
  238. + " COUNT(*) AS Assigned_count,\r\n"
  239. + " COUNT(CASE WHEN field_14 IS NOT NULL THEN 1 END) AS Complete_count,\r\n"
  240. + " COUNT(CASE WHEN field_14 IS NULL THEN 1 END) AS Pending_count ,\r\n"
  241. + " COUNT(CASE WHEN field_14 = 'INFORMATION_GIVEN' THEN 1 END) AS Information_Given_count,\r\n"
  242. + " COUNT(CASE WHEN field_14 = 'UNATTENDED' THEN 1 END) AS Unattended_count,\r\n"
  243. + " COUNT(CASE WHEN field_14 = 'CALL_BACK_LATER' THEN 1 END) AS Call_back_later_count,\r\n"
  244. + " COUNT(CASE WHEN field_14 = 'SWITCH_OFF' THEN 1 END) AS Switch_off_count,\r\n"
  245. + " COUNT(CASE WHEN field_14 = 'WRONG_NUMBER' THEN 1 END) AS Wrong_number_count,\r\n"
  246. + " CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((COUNT(CASE WHEN field_14 IS NOT NULL THEN 1 END) * 100.0 / COUNT(*)), 0)END AS Percentage\r\n"
  247. + "FROM gcc_1913_campaign.upload_data\r\n"
  248. + "WHERE data_id IN (" + inSql + ")";
  249. List<Map<String, Object>> results = jdbcTemplate.queryForList(SqlQuery, dataIds.toArray());
  250. return results;
  251. }
  252. @Transactional
  253. public List<Map<String, Object>> getCompletedDataDetailsById(List<Integer> CdataIds)
  254. {
  255. if (CdataIds.isEmpty()) {
  256. // Handle the case where dataIds is empty to avoid SQL syntax error.
  257. throw new IllegalArgumentException("The CdataIds list is empty, cannot execute query.");
  258. }
  259. String inSql = String.join(",", Collections.nCopies(CdataIds.size(), "?"));
  260. String SqlQuery ="SELECT\r\n"
  261. + " data_id,\r\n" // Add data_id to the result
  262. + " COUNT(*) AS Assigned_count,\r\n"
  263. + " COUNT(CASE WHEN field_14 IS NOT NULL THEN 1 END) AS Complete_count,\r\n"
  264. + " COUNT(CASE WHEN field_14 IS NULL THEN 1 END) AS Pending_count,\r\n"
  265. + " COUNT(CASE WHEN field_14 = 'INFORMATION_GIVEN' THEN 1 END) AS Information_Given_count,\r\n"
  266. + " COUNT(CASE WHEN field_14 = 'UNATTENDED' THEN 1 END) AS Unattended_count,\r\n"
  267. + " COUNT(CASE WHEN field_14 = 'CALL_BACK_LATER' THEN 1 END) AS Call_back_later_count,\r\n"
  268. + " COUNT(CASE WHEN field_14 = 'SWITCH_OFF' THEN 1 END) AS Switch_off_count,\r\n"
  269. + " COUNT(CASE WHEN field_14 = 'WRONG_NUMBER' THEN 1 END) AS Wrong_number_count,\r\n"
  270. + " CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((COUNT(CASE WHEN field_14 IS NOT NULL THEN 1 END) * 100.0 / COUNT(*)), 0) END AS Percentage\r\n"
  271. + "FROM gcc_1913_campaign.upload_data\r\n"
  272. + "WHERE data_id IN (" + inSql + ")\r\n"
  273. + "GROUP BY data_id"; // Group by data_id
  274. return jdbcTemplate.queryForList(SqlQuery, CdataIds.toArray());
  275. }
  276. @Transactional
  277. public List<Map<String,Object>> getCompletedDataById(List<Integer> completedDataIds)
  278. {
  279. if (completedDataIds.isEmpty()) {
  280. // Handle the case where dataIds is empty to avoid SQL syntax error.
  281. throw new IllegalArgumentException("The completedDataIds list is empty, cannot execute query.");
  282. }
  283. String inSql = String.join(",", Collections.nCopies(completedDataIds.size(), "?"));
  284. String SqlQuery = "SELECT field_1 as id, field_2 as name, field_3 as address, " +
  285. "field_5 as cellnumber, field_14 as call_status, updated_date as updated_date,field_15 as remarks, " +
  286. "(SELECT COUNT(data_id) FROM call_logs WHERE data_id = upload_data.data_id) as call_count " +
  287. "FROM gcc_1913_campaign.upload_data " +
  288. "WHERE data_id IN (" + inSql + ")";
  289. return jdbcTemplate.queryForList(SqlQuery, completedDataIds.toArray());
  290. }
  291. @Transactional
  292. public void addAgentsForCampaign(int selectedAgentId, int categoryId, int campaignId) {
  293. String checkSql = "SELECT COUNT(*) FROM gcc_1913_campaign.agent_assign "
  294. + "WHERE agent_id = ? AND category_id = ? AND campaign_id = ?";
  295. String insertSql = "INSERT INTO gcc_1913_campaign.agent_assign (agent_id, category_id, campaign_id) VALUES (?, ?, ?)";
  296. try {
  297. // Check if the record already exists
  298. int count = jdbcTemplate.queryForObject(checkSql, Integer.class, selectedAgentId, categoryId, campaignId);
  299. if (count == 0) {
  300. // Insert only if no existing record found
  301. int rowsInserted = jdbcTemplate.update(insertSql, selectedAgentId, categoryId, campaignId);
  302. if (rowsInserted > 0) {
  303. System.out.println("Successfully inserted agent ID " + selectedAgentId + " into campaign ID " + campaignId);
  304. } else {
  305. System.out.println("No rows inserted for agent ID " + selectedAgentId);
  306. }
  307. } else {
  308. System.out.println("Agent ID " + selectedAgentId + " is already assigned to campaign ID " + campaignId);
  309. }
  310. } catch (DataAccessException | NumberFormatException ex) {
  311. System.err.println("Error inserting data: " + ex.getMessage());
  312. throw new RuntimeException("Failed to insert agents for campaign. Please try again.", ex);
  313. }
  314. }
  315. @Transactional
  316. public void updateAgentForCampaign(int selectedAgentId, int categoryId, int campaignId,int DataId) {
  317. String updateSql = "UPDATE gcc_1913_campaign.upload_data SET agent_id = ? WHERE campaign_id = ? AND category_id = ? AND data_id=?";
  318. try {
  319. int rowsUpdated = jdbcTemplate.update(updateSql, selectedAgentId,campaignId,categoryId,DataId);
  320. /* if (rowsUpdated > 0) {
  321. System.out.println("Successfully updated agent ID in upload_data== " + selectedAgentId + " for campaign ID " + campaignId);
  322. } else {
  323. System.out.println("No rows updated for agent ID " + selectedAgentId);
  324. }*/
  325. } catch (DataAccessException | NumberFormatException ex) {
  326. System.err.println("Error updating data: " + ex.getMessage());
  327. throw new RuntimeException("Failed to update agents for campaign. Please try again.", ex);
  328. }
  329. }
  330. @Transactional
  331. public List<Integer> getDataIdForFurtherAssign(int categoryId, int campaignId) {
  332. String sql = "SELECT data_id FROM gcc_1913_campaign.upload_data "
  333. + "WHERE category_id = ? "
  334. + "AND campaign_id = ? "
  335. + "AND is_processed = 0 "
  336. + "ORDER BY data_id";
  337. return jdbcTemplate.queryForList(sql, Integer.class, categoryId, campaignId);
  338. }
  339. @Transactional
  340. public void resetAgentAssignmentForNextDay(int categoryId, int campaignId) {
  341. String sql = "UPDATE gcc_1913_campaign.upload_data "
  342. + "SET agent_id = NULL, "
  343. + " call_status = NULL, "
  344. + " remarks = NULL, "
  345. + " updated_agent_id = NULL, "
  346. + " updated_date = NULL, "
  347. + " is_processed = 0 "
  348. + "WHERE category_id = ? "
  349. + " AND campaign_id = ? "
  350. + " AND call_status IN ('CALL BACK LATER', 'UNATTENDED')";
  351. jdbcTemplate.update(sql, categoryId, campaignId);
  352. }
  353. //////////////////////Ajith code///////////////////
  354. @Transactional
  355. public List<Map<String, Object>> getFilteredFields(int categoryId, int agentId, int dataId) {
  356. List<Map<String, Object>> resultList = new ArrayList<>();
  357. // Fetch ordered field column names
  358. String fetchColumnsSQL = "SELECT COLUMN_NAME " +
  359. "FROM INFORMATION_SCHEMA.COLUMNS " +
  360. "WHERE TABLE_SCHEMA = 'gcc_1913_campaign' " +
  361. "AND TABLE_NAME = 'upload_data' " +
  362. "AND COLUMN_NAME LIKE 'field_%' " +
  363. "ORDER BY CAST(SUBSTRING(COLUMN_NAME, 7) AS UNSIGNED)";
  364. List<String> fieldColumns = jdbcTemplate.queryForList(fetchColumnsSQL, String.class);
  365. if (fieldColumns.isEmpty()) {
  366. throw new RuntimeException("No matching columns found");
  367. }
  368. // Fetch ordered fields data
  369. String query1 = "SELECT " + String.join(", ", fieldColumns) + " FROM gcc_1913_campaign.upload_data WHERE data_id = ?";
  370. List<Map<String, Object>> orderedFieldsList = jdbcTemplate.queryForList(query1, dataId);
  371. if (orderedFieldsList.isEmpty()) {
  372. throw new RuntimeException("No data found for data_id: " + dataId);
  373. }
  374. Map<String, Object> orderedFields = orderedFieldsList.get(0);
  375. // Fetch category columns
  376. String query2 = "SELECT column_name FROM gcc_1913_campaign.category_columns WHERE category_id = ? and isactive=1 ";
  377. List<String> categoryColumns = new ArrayList<>(jdbcTemplate.queryForList(query2, String.class, categoryId));
  378. //System.out.println("service===="+categoryColumns);
  379. if (categoryColumns.isEmpty()) {
  380. throw new RuntimeException("No category columns found for category_id: " + categoryId);
  381. }
  382. int categoryCount = categoryColumns.size();
  383. // Collect first 'categoryCount' non-null values from orderedFields
  384. List<Object> nonNullValues = new ArrayList<>();
  385. for (String field : fieldColumns) {
  386. Object value = orderedFields.get(field);
  387. if (value != null) {
  388. nonNullValues.add(value);
  389. if (nonNullValues.size() == categoryCount) {
  390. break; // Stop after collecting required count
  391. }
  392. }
  393. }
  394. if (nonNullValues.size() < categoryCount) {
  395. throw new RuntimeException("Not enough non-null values to match category columns");
  396. }
  397. // Map category columns to non-null ordered field values
  398. Map<String, Object> finalMapping = new HashMap<>();
  399. for (int i = 0; i < categoryCount; i++) {
  400. finalMapping.put(categoryColumns.get(i), nonNullValues.get(i));
  401. }
  402. resultList.add(finalMapping);
  403. return resultList;
  404. }
  405. @Transactional
  406. public List<Map<String, Object>> getCampaignQuestions(int categoryId, int campaignId) {
  407. // Fetch questions along with answer type
  408. // String questionQuery = "SELECT cq.*, at.type_name " +
  409. // "FROM gcc_1913_campaign.campaign_questions cq " +
  410. // "JOIN gcc_1913_campaign.answer_type at " +
  411. // "ON cq.answer_type_id = at.answer_type_id " +
  412. //
  413. // "WHERE cq.category_id = ? AND cq.campaign_id = ? ";
  414. String questionQuery="SELECT cq.questions AS questions, \r\n"
  415. + " MIN(cq.question_id) AS question_id, \r\n"
  416. + " MIN(cq.category_id) AS category_id, \r\n"
  417. + " MIN(cq.campaign_id) AS campaign_id, \r\n"
  418. + " MIN(cq.answer_type_id) AS answer_type_id, \r\n"
  419. + " MIN(cat.type_name) AS type_name \r\n"
  420. + "FROM gcc_1913_campaign.campaign_questions cq \r\n"
  421. + "JOIN gcc_1913_campaign.answer_type cat \r\n"
  422. + " ON cq.answer_type_id = cat.answer_type_id \r\n"
  423. + "WHERE cq.category_id = ? \r\n"
  424. + "AND cq.campaign_id = ? \r\n"
  425. + "AND cq.isactive = 1\r\n"
  426. + "GROUP BY cq.questions\r\n"
  427. + "ORDER BY MIN(cq.question_id)";
  428. List<Map<String, Object>> questions = jdbcTemplate.queryForList(questionQuery, categoryId, campaignId);
  429. // Iterate over each question and fetch answers
  430. for (Map<String, Object> question : questions) {
  431. int questionId = (int) question.get("question_id");
  432. // String answerQuery = "SELECT campaign_answer FROM gcc_1913_campaign.campaign_answers " +
  433. // "WHERE question_id = ?";
  434. String answerQuery="SELECT distinct(question_id) as qid, campaign_answer, created_date FROM gcc_1913_campaign.campaign_answers\r\n"
  435. + "WHERE question_id = ?";
  436. List<Map<String, Object>> answers = jdbcTemplate.queryForList(answerQuery, questionId);
  437. // Extract answer values and add to the question map
  438. List<String> answerList = answers.stream()
  439. .map(a -> (String) a.get("campaign_answer"))
  440. .collect(Collectors.toList());
  441. question.put("answers", answerList);
  442. }
  443. return questions;
  444. }
  445. @Transactional
  446. public List<Map<String, Object>> FetchAnswer(int campaignId,int agentId,int questionId,int dataId) {
  447. String sqlQuery = "SELECT distinct(campaign_answer) FROM gcc_1913_campaign.questions_answers where campaign_id=? and agent_id=? and question_id=? and data_id=?; ";
  448. //System.out.println("dropdown l");
  449. return jdbcTemplate.queryForList(sqlQuery,campaignId,agentId,questionId,dataId);
  450. }
  451. @Transactional
  452. public void saveAnswers(List<Map<String, Object>> answers) {
  453. String sql = "INSERT INTO gcc_1913_campaign.questions_answers " +
  454. "(campaign_id, data_id, agent_id, question_id, campaign_answer, created_date) " +
  455. "VALUES (?, ?, ?, ?, ?, NOW())";
  456. for (Map<String, Object> answer : answers) {
  457. jdbcTemplate.update(sql,
  458. answer.get("campaign_id"),
  459. answer.get("data_id"),
  460. answer.get("agent_id"),
  461. answer.get("question_id"),
  462. answer.get("campaign_answer")
  463. );
  464. }
  465. }
  466. @Transactional
  467. public List<Map<String, Object>> getCallStatus() {
  468. String sqlQuery = "SELECT * FROM gcc_1913_campaign.calls_status ";
  469. //System.out.println("dropdown l");
  470. return jdbcTemplate.queryForList(sqlQuery);
  471. }
  472. @Transactional
  473. public List<Map<String, Object>> checkCounts(int campaignId, int dataId) {
  474. // Query to get the count of answered questions for a given dataId
  475. String answersCountQuery = "SELECT COUNT(distinct(question_id)) AS answersCount FROM gcc_1913_campaign.questions_answers WHERE data_id = ?";
  476. // Query to get the count of required questions for a given campaignId
  477. String questionsCountQuery = "SELECT COUNT(distinct(questions)) AS questionsCount FROM gcc_1913_campaign.campaign_questions WHERE campaign_id = ?";
  478. // Execute queries
  479. int answersCount = jdbcTemplate.queryForObject(answersCountQuery, Integer.class, dataId);
  480. int questionsCount = jdbcTemplate.queryForObject(questionsCountQuery, Integer.class, campaignId);
  481. // Construct response as a list of maps
  482. return List.of(Map.of(
  483. "answersCount", answersCount,
  484. "questionsCount", questionsCount
  485. ));
  486. }
  487. @Transactional
  488. public List<Map<String, Object>> getAgentsALLCampaignsList(int agent_id) {
  489. String SqlQuery = "SELECT * FROM gcc_1913_campaign.upload_data ud inner join gcc_1913_campaign.campaign_request cr on ud.campaign_id=cr.campaign_id where cr.campaign_status='ONGOING'"+
  490. " and ud.agent_id = ? and ud.is_processed=0 ";
  491. //System.out.println("text");
  492. return jdbcTemplate.queryForList(SqlQuery,agent_id);
  493. }
  494. @Transactional
  495. public List<Map<String, Object>> getAgentsALLCampaignsList1(int agent_id) {
  496. String SqlQuery = "SELECT * FROM gcc_1913_campaign.upload_data ud inner join gcc_1913_campaign.campaign_request cr on ud.campaign_id=cr.campaign_id where cr.campaign_status='ONGOING'"+
  497. " and ud.agent_id = ? and ud.is_processed=1";
  498. // System.out.println("text");
  499. return jdbcTemplate.queryForList(SqlQuery,agent_id);
  500. }
  501. @Transactional
  502. public List<Map<String, Object>> getAgentsALLCampaigns(int agent_id) {
  503. String SqlQuery = "SELECT c.category_name,cr.*,\r\n"
  504. + "(SELECT COUNT(data_id)\r\n"
  505. + "FROM gcc_1913_campaign.upload_data up\r\n"
  506. + "WHERE up.agent_id = aa.agent_id \r\n"
  507. + "AND up.campaign_id = cr.campaign_id\r\n"
  508. + ") AS calls_assigned,\r\n"
  509. + "(SELECT COUNT(CASE WHEN up.call_status IS NOT NULL THEN 1 END) \r\n"
  510. + "FROM gcc_1913_campaign.upload_data up \r\n"
  511. + "WHERE up.agent_id = aa.agent_id \r\n"
  512. + "AND up.campaign_id = cr.campaign_id\r\n"
  513. + ") AS complete_count,\r\n"
  514. + "(SELECT COUNT(CASE WHEN up.call_status IS NULL THEN 1 END) \r\n"
  515. + "FROM gcc_1913_campaign.upload_data up \r\n"
  516. + "WHERE up.agent_id = aa.agent_id \r\n"
  517. + "AND up.campaign_id = cr.campaign_id\r\n"
  518. + ") AS pending_count\r\n"
  519. + "FROM gcc_1913_campaign.agent_assign aa\r\n"
  520. + "JOIN gcc_1913_campaign.campaign_request cr ON cr.campaign_id=aa.campaign_id\r\n"
  521. + "JOIN gcc_1913_campaign.category c ON c.category_id=cr.category_id\r\n"
  522. + "WHERE cr.campaign_status='ONGOING' AND aa.isactive=1 AND cr.isactive=1 AND aa.agent_id=?";
  523. return jdbcTemplate.queryForList(SqlQuery,agent_id);
  524. }
  525. // public List<Map<String, Object>> getCallCategories()
  526. // {
  527. // String query = "SELECT id,call_status FROM gcc_1913_campaign.calls_status WHERE isactive=1 AND isdelete=0";
  528. // return jdbcTemplate.queryForList(query);
  529. // }
  530. }