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.

214 lines
8.4 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package in.gov.chennaicorporation.gccoffice.callcenter.service;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.sql.DataSource;
  7. import javax.transaction.Transactional;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Qualifier;
  10. import org.springframework.dao.DataAccessException;
  11. import org.springframework.jdbc.core.JdbcTemplate;
  12. import org.springframework.stereotype.Service;
  13. @Service
  14. public class AgentAssignService
  15. {
  16. private JdbcTemplate jdbcTemplate;
  17. @Autowired
  18. public void setDataSource(@Qualifier("mysql1913CampaignDataSource") DataSource dataSource) {
  19. this.jdbcTemplate = new JdbcTemplate(dataSource);
  20. }
  21. @Autowired
  22. public void setDataSource2(@Qualifier("mysql1913QAQCDataSource") DataSource dataSource) {
  23. this.jdbcTemplate = new JdbcTemplate(dataSource);
  24. }
  25. @Autowired
  26. private JdbcTemplate jdbcTemplate2;
  27. @Transactional
  28. public List<Map<String, Object>> getcategory() {
  29. String sqlQuery = "SELECT category_id,category_name FROM gcc_1913_campaign.category where isactive='1' and isdelete='0'";
  30. return jdbcTemplate.queryForList(sqlQuery);
  31. }
  32. @Transactional
  33. public List<Map<String, Object>> getcampaignbycategory(int id) {
  34. String sqlQuery = "SELECT cr.campaign_id, cr.campaign_name " +
  35. "FROM gcc_1913_campaign.campaign_request cr " +
  36. "JOIN gcc_1913_campaign.category c ON cr.category_id = c.category_id " +
  37. "WHERE cr.category_id = ? AND campaign_status='PENDING' " +
  38. "AND cr.isactive = '1' ";
  39. return jdbcTemplate.queryForList(sqlQuery,id);
  40. }
  41. @Transactional
  42. public List<Map<String, Object>> getcampaignbyid(int id) {
  43. String sqlQuery = "SELECT * FROM gcc_1913_campaign.campaign_request where isactive='1' and campaign_id=?";
  44. return jdbcTemplate.queryForList(sqlQuery,id);
  45. }
  46. @Transactional
  47. public List<Map<String, Object>> getagentsfortask(int id) {
  48. String sqlQuery = "SELECT al.agent_id,al.agent_name\r\n"
  49. + "FROM gcc_1913_qaqc.agents_list al\r\n"
  50. + "LEFT JOIN gcc_1913_campaign.agent_assign aa ON al.agent_id = aa.agent_id AND aa.isactive = '1' AND aa.campaign_id = ?\r\n"
  51. + "WHERE aa.agent_id IS NULL AND al.calling_type='OUTBOUND'AND al.isactive='1' AND al.isdelete='0'";
  52. return jdbcTemplate.queryForList(sqlQuery,id);
  53. }
  54. @Transactional
  55. public int addagentsfortask(int agent_id, int category_id, int campaign_id) {
  56. String insertSql = "INSERT INTO gcc_1913_campaign.agent_assign (agent_id, category_id, campaign_id) VALUES (?, ?, ?)";
  57. int rowsInserted = jdbcTemplate.update(insertSql, agent_id, category_id, campaign_id);
  58. return rowsInserted;
  59. }
  60. @Transactional
  61. public void addAgentsForCampaign(int selectedAgentId, int categoryId, int campaignId) {
  62. String insertSql = "INSERT INTO gcc_1913_campaign.agent_assign (agent_id, category_id, campaign_id) VALUES (?, ?, ?)";
  63. try {
  64. int rowsInserted = jdbcTemplate.update(insertSql, selectedAgentId, categoryId, campaignId);
  65. if (rowsInserted > 0) {
  66. System.out.println("Successfully inserted agent ID in agent_assign== " + selectedAgentId + " into campaign ID " + campaignId);
  67. } else {
  68. System.out.println("No rows inserted for agent ID " + selectedAgentId);
  69. }
  70. } catch (DataAccessException | NumberFormatException ex) {
  71. System.err.println("Error inserting data: " + ex.getMessage());
  72. throw new RuntimeException("Failed to insert agents for campaign. Please try again.", ex);
  73. }
  74. }
  75. @Transactional
  76. public void updateAgentForCampaign(int selectedAgentId, int categoryId, int campaignId,int DataId) {
  77. String updateSql = "UPDATE gcc_1913_campaign.upload_data SET agent_id = ? WHERE campaign_id = ? AND category_id = ? AND data_id=?";
  78. try {
  79. int rowsUpdated = jdbcTemplate.update(updateSql, selectedAgentId,campaignId,categoryId,DataId);
  80. /* if (rowsUpdated > 0) {
  81. System.out.println("Successfully updated agent ID in upload_data== " + selectedAgentId + " for campaign ID " + campaignId);
  82. } else {
  83. System.out.println("No rows updated for agent ID " + selectedAgentId);
  84. }*/
  85. } catch (DataAccessException | NumberFormatException ex) {
  86. System.err.println("Error updating data: " + ex.getMessage());
  87. throw new RuntimeException("Failed to update agents for campaign. Please try again.", ex);
  88. }
  89. }
  90. @Transactional
  91. public List<Integer> getDataIdForInitialAssign(int categoryId, int campaignId) {
  92. String sql = "SELECT data_id FROM gcc_1913_campaign.upload_data "
  93. + "WHERE category_id = ? AND campaign_id = ? "
  94. + "AND agent_id IS NULL AND is_processed = 0 ORDER BY data_id";
  95. // Correctly specify the return type for jdbcTemplate.queryForList
  96. return jdbcTemplate.queryForList(sql, Integer.class, categoryId, campaignId);
  97. }
  98. @Transactional
  99. public List<Map<String, Object>> checkcampaigninassignedtable(int id)
  100. {
  101. String sqlQuery = "SELECT al.agent_id,al.agent_name,aa.assigned_id\r\n"
  102. + "FROM gcc_1913_qaqc.agents_list al\r\n"
  103. + "JOIN gcc_1913_campaign.agent_assign aa ON al.agent_id = aa.agent_id\r\n"
  104. + "JOIN gcc_1913_campaign.campaign_request c ON aa.campaign_id=c.campaign_id\r\n"
  105. + "WHERE aa.campaign_id = ? AND aa.isactive = '1' AND al.calling_type='OUTBOUND'AND al.isactive='1' AND al.isdelete='0'";
  106. return jdbcTemplate.queryForList(sqlQuery,id);
  107. }
  108. @Transactional
  109. public int editagentsfortask(int assigned_id) {
  110. String updateSql = "UPDATE gcc_1913_campaign.agent_assign SET isactive='0', isdelete='1',status='NOT_ASSIGNED' WHERE assigned_id=?";
  111. int rowsInserted = jdbcTemplate.update(updateSql,assigned_id);
  112. return rowsInserted;
  113. }
  114. @Transactional
  115. public List<Map<String, Object>> getcampaignquestionbyid(int id) {
  116. String sqlQuery = " SELECT " +
  117. " cr.campaign_id, " +
  118. " cr.campaign_name, " +
  119. " cr.category_id, " +
  120. " cr.start_date, " +
  121. " cr.end_date, " +
  122. " cr.no_of_agents, " +
  123. " cr.calls_per_agent, " +
  124. " cr.description, " +
  125. " cr.created_date, " +
  126. " cr.excel_url, " +
  127. " cr.isactive, " +
  128. " cr.campaign_status, " +
  129. " cq.questions, " +
  130. " cq.question_id, " +
  131. " cq.answer_type_id, " +
  132. " cat.type_name, " +
  133. " ca.campaign_answer " + // Campaign Answer can be NULL
  134. "FROM gcc_1913_campaign.campaign_request cr " +
  135. "JOIN gcc_1913_campaign.campaign_questions cq " +
  136. " ON cr.campaign_id = cq.campaign_id " +
  137. "LEFT JOIN gcc_1913_campaign.campaign_answers ca " + // LEFT JOIN to include questions with no answers
  138. " ON cq.question_id = ca.question_id " +
  139. "JOIN gcc_1913_campaign.answer_type cat " +
  140. " ON cat.answer_type_id = cq.answer_type_id " +
  141. "WHERE cr.isactive = 1 AND cr.campaign_id = ?";
  142. List<Map<String, Object>> queryResult = jdbcTemplate.queryForList(sqlQuery, id);
  143. // Map to group answers by question_id
  144. Map<Integer, Map<String, Object>> groupedData = new LinkedHashMap();
  145. for (Map<String, Object> row : queryResult) {
  146. Integer questionId = (Integer) row.get("question_id");
  147. String campaignAnswer = (String) row.get("campaign_answer"); // Can be null
  148. // Check if this question_id already exists in the map
  149. if (!groupedData.containsKey(questionId)) {
  150. // Copy the row data (excluding campaign_answer) and initialize answers list
  151. Map<String, Object> newRow = new LinkedHashMap<>(row);
  152. newRow.remove("campaign_answer"); // Remove individual answer
  153. newRow.put("campaign_answers", new ArrayList<String>()); // Initialize answer list
  154. groupedData.put(questionId, newRow);
  155. }
  156. // Only add answers if they are not null
  157. if (campaignAnswer != null) {
  158. ((List<String>) groupedData.get(questionId).get("campaign_answers")).add(campaignAnswer);
  159. }
  160. }
  161. // Convert grouped data to a list
  162. return new ArrayList<>(groupedData.values());
  163. }
  164. }