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.

614 lines
25 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package in.gov.chennaicorporation.gccoffice.callcenter.controller;
  2. import org.apache.poi.ss.usermodel.*;
  3. import org.springframework.web.multipart.MultipartFile;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.core.type.TypeReference;
  6. import com.fasterxml.jackson.databind.ObjectMapper;
  7. import java.io.*;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11. import java.text.SimpleDateFormat;
  12. import java.time.LocalDateTime;
  13. import java.time.format.DateTimeFormatter;
  14. import java.util.*;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.stream.Collectors;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.core.env.Environment;
  22. import org.springframework.http.HttpStatus;
  23. import org.springframework.http.ResponseEntity;
  24. import org.springframework.jdbc.core.JdbcTemplate;
  25. import org.springframework.web.bind.annotation.GetMapping;
  26. import org.springframework.web.bind.annotation.PostMapping;
  27. import org.springframework.web.bind.annotation.RequestBody;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.RequestParam;
  30. import org.springframework.web.bind.annotation.RequestPart;
  31. import org.springframework.web.bind.annotation.ResponseBody;
  32. import org.springframework.web.bind.annotation.RestController;
  33. import in.gov.chennaicorporation.gccoffice.callcenter.service.AddcategoryServices;
  34. import in.gov.chennaicorporation.gccoffice.callcenter.service.AgentAssignService;
  35. import in.gov.chennaicorporation.gccoffice.callcenter.service.CampaignService;
  36. @RestController("callcenterapicontroller")
  37. @RequestMapping("gcc/api/campaign")
  38. public class CampaignAPIController {
  39. @Autowired
  40. private CampaignService campaignService;
  41. @Autowired
  42. private AddcategoryServices addCategoryServices; // Injecting the service
  43. @Autowired
  44. private AgentAssignService agentsAssignService;
  45. @Autowired
  46. private JdbcTemplate jdbcTemplate;
  47. @Autowired
  48. private Environment environment;
  49. @GetMapping("/getCampaignDetails")
  50. public ResponseEntity<Map<String, Object>> getCampaignDetails() {
  51. Map<String, Object> response = new HashMap<>();
  52. List<Map<String, Object>> data = campaignService.getCampaignDetails();
  53. response.put("data", data);
  54. return ResponseEntity.ok(response);
  55. }
  56. // Endpoint to get campaign details by ID
  57. @GetMapping("/getCampaignDetailsById")
  58. public @ResponseBody List<Map<String, Object>> getCampaignDetailsById(@RequestParam("campaign_id") Long campaignId) {
  59. return campaignService.getCampaignDetailsById(campaignId);
  60. }
  61. @PostMapping("/updateCampaign")
  62. public ResponseEntity<String> updateCampaign(
  63. @RequestBody Map<String, Object> requestData) {
  64. Long campaign_id = ((Number) requestData.get("campaign_id")).longValue();
  65. String end_date = (String) requestData.get("end_date");
  66. List<Map<String, Object>> questionsData =
  67. (List<Map<String, Object>>) requestData.get("questionsData");
  68. if (campaign_id == null) {
  69. return ResponseEntity.badRequest().body("Campaign ID is required.");
  70. }
  71. try {
  72. // Update campaign details
  73. campaignService.updateCampaign(campaign_id, end_date);
  74. // Save questions and answers
  75. for (Map<String, Object> questionMap : questionsData) {
  76. String category = (String) questionMap.get("category");
  77. String question = (String) questionMap.get("question");
  78. String answerType = (String) questionMap.get("answerType");
  79. List<String> answers = (List<String>) questionMap.get("answers");
  80. if (category != null && question != null && answerType != null) {
  81. campaignService.saveQuestionsAndAnswers(
  82. category,
  83. String.valueOf(campaign_id), // Corrected data type
  84. question,
  85. answerType,
  86. answers
  87. );
  88. }
  89. }
  90. return ResponseEntity.ok("Campaign updated successfully.");
  91. } catch (Exception e) {
  92. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
  93. .body("Error updating campaign: " + e.getMessage());
  94. }
  95. }
  96. //save question and ansewers for campaign
  97. @PostMapping("/saveQuestionsAndAnswers")
  98. public ResponseEntity<String> saveQuestions(@RequestBody List<Map<String, Object>> requestData) {
  99. String response = campaignService.saveQuestionsAnswers(requestData);
  100. return ResponseEntity.ok(response);
  101. }
  102. //save file and get path
  103. private String saveFile(MultipartFile file) throws IOException {
  104. String uploadDirectory = environment.getProperty("file.upload.directory");
  105. String folderName = environment.getProperty("campaign.foldername");
  106. String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + "_" + file.getOriginalFilename();
  107. String folderPath = uploadDirectory + "/" + folderName + "/excel/";
  108. Path dirPath = Paths.get(folderPath);
  109. if (!Files.exists(dirPath)) {
  110. Files.createDirectories(dirPath);
  111. }
  112. Path filePath = dirPath.resolve(fileName);
  113. Files.write(filePath, file.getBytes());
  114. return filePath.toAbsolutePath().toString();
  115. }
  116. //read uploaded excel data and make condition for each category uploading same excel columns are other some other?
  117. private List<Map<String, Object>> readExcelData(InputStream inputStream, List<String> categoryColumns) throws IOException {
  118. List<Map<String, Object>> dataList = new ArrayList<>();
  119. try (Workbook workbook = new XSSFWorkbook(inputStream)) {
  120. Sheet sheet = workbook.getSheetAt(0);
  121. FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();
  122. // Read header row from Excel
  123. Row headerRow = sheet.getRow(0);
  124. List<String> excelHeaders = new ArrayList<>();
  125. for (Cell cell : headerRow) {
  126. excelHeaders.add(cell.getStringCellValue().trim());
  127. }
  128. // Compare Excel headers with category columns
  129. if (!excelHeaders.containsAll(categoryColumns)) {
  130. // throw new IllegalArgumentException("Excel data does not match the category columns.");
  131. }
  132. // Proceed with reading data only if headers match
  133. for (Row row : sheet) {
  134. if (row.getRowNum() == 0) continue; // Skip header row
  135. Map<String, Object> dataMap = new HashMap<>();
  136. for (Cell cell : row) {
  137. String cellValue = "";
  138. switch (formulaEvaluator.evaluateInCell(cell).getCellType()) {
  139. case STRING:
  140. cellValue = cell.getStringCellValue();
  141. break;
  142. case NUMERIC:
  143. if (DateUtil.isCellDateFormatted(cell)) {
  144. cellValue = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
  145. } else {
  146. // Check if value is an integer (whole number)
  147. if (cell.getNumericCellValue() % 1 == 0) {
  148. cellValue = String.valueOf((long) cell.getNumericCellValue()); // Format as integer
  149. } else {
  150. cellValue = String.valueOf(cell.getNumericCellValue()); // Keep decimal values as they are
  151. }
  152. }
  153. break;
  154. case BOOLEAN:
  155. cellValue = String.valueOf(cell.getBooleanCellValue());
  156. break;
  157. default:
  158. cellValue = "Unknown";
  159. break;
  160. }
  161. // Map cell data to correct column name
  162. dataMap.put(excelHeaders.get(cell.getColumnIndex()), cellValue);
  163. }
  164. if (!dataMap.isEmpty()) {
  165. dataList.add(dataMap);
  166. }
  167. }
  168. }
  169. return dataList;
  170. }
  171. @GetMapping("/getExcelData")
  172. public ResponseEntity<List<Map<String, Object>>> getExcelData(@RequestParam("filePath") String filePath) {
  173. try {
  174. // Read data from the uploaded Excel file
  175. List<Map<String, Object>> excelData = readExcelData(new FileInputStream(filePath), null);
  176. // Return data as JSON
  177. return ResponseEntity.ok(excelData);
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
  181. }
  182. }
  183. @GetMapping("/getlastCampaignid")
  184. public List<Map<String, Object>> getLastRow()
  185. {
  186. return campaignService.getLastRow();
  187. }
  188. @GetMapping("/startcampaign")
  189. public List<Map<String, Object>> getDataforAgents(@RequestParam int campaign_id,@RequestParam int calls_per_agent)
  190. {
  191. return campaignService.getDataforAgents(campaign_id, calls_per_agent);
  192. }
  193. @GetMapping("/viewcampaignstatus")
  194. public List<Map<String, Object>> viewcampaignstatus(@RequestParam int campaign_id)
  195. {
  196. return campaignService.viewcampaignstatus(campaign_id);
  197. }
  198. @GetMapping("/viewcompletedby")
  199. public List<Map<String, Object>> viewcompletedby(@RequestParam int campaign_id,@RequestParam int agent_id)
  200. {
  201. return campaignService.viewcompletedby(campaign_id,agent_id);
  202. }
  203. @PostMapping(value = "/savecampaign", consumes = {"multipart/form-data"})
  204. public ResponseEntity<Map<String, String>> saveCampaign(
  205. @RequestParam String campaignId,
  206. @RequestParam String categoryId,
  207. @RequestParam String eventStartDate,
  208. @RequestParam String eventEndDate,
  209. @RequestParam int noOfCalls,
  210. @RequestParam String description,
  211. @RequestParam(required = false) String questionsData, // JSON as String
  212. @RequestParam(required = false) String selectedAgents // JSON string
  213. ) {
  214. // Convert JSON strings to List objects
  215. List<Map<String, Object>> questionsDataList = new ArrayList<>();
  216. List<String> selectedAgentsList = new ArrayList<>();
  217. try {
  218. if (questionsData != null) {
  219. questionsDataList = new ObjectMapper().readValue(questionsData, new TypeReference<List<Map<String, Object>>>() {});
  220. }
  221. if (selectedAgents != null) {
  222. selectedAgentsList = new ObjectMapper().readValue(selectedAgents, new TypeReference<List<String>>() {});
  223. }
  224. } catch (JsonProcessingException e) {
  225. e.printStackTrace();
  226. return ResponseEntity.status(HttpStatus.BAD_REQUEST)
  227. .body(Map.of("status", "error", "message", "Invalid JSON format in request data"));
  228. }
  229. // Campaign Details Mapping
  230. Map<String, Object> campaignDetails = new HashMap<>();
  231. campaignDetails.put("campaign_id", campaignId);
  232. campaignDetails.put("category_id", categoryId);
  233. campaignDetails.put("start_date", eventStartDate);
  234. campaignDetails.put("end_date", eventEndDate);
  235. campaignDetails.put("calls_per_agent", noOfCalls);
  236. campaignDetails.put("description", description);
  237. try {
  238. campaignService.saveCampaignDetails(campaignDetails);
  239. // Step 3: Assign Agents for the Campaign
  240. if (!selectedAgentsList.isEmpty()) {
  241. int parsedCampaignId = Integer.parseInt(campaignId);
  242. int parsedCategoryId = Integer.parseInt(categoryId);
  243. System.out.println("selectedAgentsList====== " + selectedAgentsList);
  244. int calls_assign = noOfCalls * selectedAgentsList.size();
  245. System.out.println("calls_assign== " + calls_assign);
  246. List<Integer> DataIds=agentsAssignService.getDataIdForInitialAssign(parsedCategoryId, parsedCampaignId);
  247. for (String agentId : selectedAgentsList) {
  248. int selectedAgentId = Integer.parseInt(agentId);
  249. agentsAssignService.addAgentsForCampaign(selectedAgentId, parsedCategoryId, parsedCampaignId);
  250. }
  251. //System.out.println("dataids=== " + DataIds);
  252. if (!DataIds.isEmpty()) {
  253. int dataIndex = 0; // Initialize data index pointer
  254. for (String agentId : selectedAgentsList) {
  255. int selectedAgentId = Integer.parseInt(agentId);
  256. // Iterate for noOfCalls times and assign data IDs to the current agent
  257. for (int i = 0; i < noOfCalls && dataIndex < DataIds.size(); i++, dataIndex++) {
  258. int DataId = DataIds.get(dataIndex);
  259. agentsAssignService.updateAgentForCampaign(
  260. selectedAgentId,
  261. parsedCategoryId,
  262. parsedCampaignId,
  263. DataId
  264. );
  265. }
  266. }
  267. // Update campaign status to 'ONGOING' after successful assignment
  268. campaignService.updateCampaignStatus(parsedCampaignId, parsedCategoryId);
  269. }
  270. else {
  271. return ResponseEntity.status(HttpStatus.NOT_FOUND)
  272. .body(Map.of("status", "error", "message", "No data available for initial assignment."));
  273. }
  274. }
  275. // Step 4: Save Questions and Answers
  276. if (!questionsDataList.isEmpty()) {
  277. for (Map<String, Object> questionMap : questionsDataList) {
  278. String question = (String) questionMap.get("question");
  279. String answerType = (String) questionMap.get("answerType");
  280. List<String> answers = (List<String>) questionMap.get("answers");
  281. if (question != null && answerType != null) {
  282. campaignService.saveQuestionsAndAnswers(
  283. categoryId,
  284. campaignId,
  285. question,
  286. answerType,
  287. answers
  288. );
  289. } else {
  290. System.out.println("Skipping invalid question data: " + questionMap);
  291. }
  292. }
  293. }
  294. }
  295. catch (Exception e) {
  296. e.printStackTrace();
  297. return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
  298. .body(Map.of("status", "error", "message", "Failed to save campaign details: " + e.getMessage()));
  299. }
  300. return ResponseEntity.ok(Map.of("status", "success", "message", "Campaign details saved successfully"));
  301. }
  302. //save excel data separate method
  303. @PostMapping(value = "/saveexcelcampaign" ,consumes = {"multipart/form-data"})
  304. public Map<String, String> saveExcelCampaign(
  305. @RequestParam String name,
  306. @RequestParam String category,
  307. @RequestPart MultipartFile file) {
  308. //System.out.println("Received campaign data:");
  309. //System.out.println("Name: " + name);
  310. //System.out.println("Category: " + category);
  311. //System.out.println("File Name: " + file.getOriginalFilename());
  312. Map<String, Object> campaignDetails = new HashMap<>();
  313. campaignDetails.put("campaign_name", name);
  314. campaignDetails.put("category_id", category);
  315. try {
  316. // Get category-specific columns
  317. List<Map<String, Object>> categoryColumns = addCategoryServices.getCategoryColumns(Integer.parseInt(category));
  318. //System.out.println("categorycolmns: " + categoryColumns);
  319. List<String> expectedHeaders = categoryColumns.stream()
  320. .map(column -> column.get("column_name").toString())
  321. .collect(Collectors.toList());
  322. // System.out.println("expectedheaders: " + expectedHeaders);
  323. // Fetch field_ columns from upload_data table
  324. List<String> fieldColumns = campaignService.getUploadDataFieldColumns();
  325. //System.out.println("fieldColumns: " + fieldColumns);
  326. // Read uploaded Excel data and extract headers
  327. List<Map<String, Object>> excelData = readExcelData(file.getInputStream(), expectedHeaders);
  328. // System.out.println("excedata: " + excelData );
  329. List<String> uploadedHeaders = new ArrayList<>(excelData.get(0).keySet()); // Extract headers from first row
  330. //System.out.println("uploadeddata : " + uploadedHeaders);
  331. // Check column count mismatch with field_ columns
  332. if (uploadedHeaders.size() > fieldColumns.size()) {
  333. Map<String, String> response = new HashMap<>();
  334. response.put("status", "column_mismatch"); // Changed status to differentiate
  335. response.put("message", "Extend the columns in Database, Inform Admin.");
  336. return response;
  337. }
  338. // Validate headers
  339. if (!new HashSet<>(expectedHeaders).equals(new HashSet<>(uploadedHeaders))) {
  340. Map<String, String> response = new HashMap<>();
  341. response.put("status", "header_mismatch"); // Changed status to differentiate
  342. response.put("message", "Excel data does not match the category columns.");
  343. return response;
  344. }
  345. String fileUrl = saveFile(file);
  346. campaignDetails.put("excel_url", fileUrl);
  347. //System.out.println("Campaign Details: " + campaignDetails);
  348. //System.out.println("Excel Data: " + excelData);
  349. campaignService.saveCampaignDetailsExcel(campaignDetails);
  350. int lastId = campaignService.getLastInsertedCampaignId();
  351. String excelUrl = campaignService.getExcelUrlByCampaignId(lastId);
  352. //List<Map<String, Object>> excelDatas = readExcelDataupdated(new FileInputStream(excelUrl));
  353. List<Map<String, Object>> excelDatas = readExcelData(new FileInputStream(excelUrl), uploadedHeaders);
  354. //System.out.println("excelDatas:" + excelDatas);
  355. for (Map<String, Object> row : excelDatas) {
  356. campaignService.saveUploadData(category, lastId, row,expectedHeaders);
  357. //System.out.println("exceldatas:" + excelDatas);
  358. }
  359. Map<String, String> response = new HashMap<>();
  360. response.put("status", "success");
  361. response.put("message", "Campaign and Excel saved successfully");
  362. return response;
  363. }
  364. catch (IOException e) {
  365. e.printStackTrace();
  366. Map<String, String> response = new HashMap<>();
  367. response.put("status", "error");
  368. response.put("message", "Failed to save campaign details: "+ e.getMessage());
  369. return response;
  370. }
  371. }
  372. @GetMapping("/availableAgents")
  373. public ResponseEntity<List<Map<String, Object>>> getAvailableAgents() {
  374. // Query 1: Fetch OUTBOUND agents
  375. String outboundAgentsQuery = "SELECT agent_name, agent_id, calling_type " +
  376. "FROM gcc_1913_qaqc.agents_list " +
  377. "WHERE calling_type='OUTBOUND'";
  378. List<Map<String, Object>> outboundAgents = jdbcTemplate.queryForList(outboundAgentsQuery);
  379. //System.out.println("outboundagents" + outboundAgents);
  380. // Query 2: Fetch agents from ongoing campaigns
  381. String ongoingCampaignAgentsQuery = "SELECT ca.agent_id " +
  382. "FROM gcc_1913_campaign.campaign_request cr " +
  383. "JOIN gcc_1913_campaign.agent_assign ca " +
  384. "ON ca.campaign_id = cr.campaign_id " +
  385. "WHERE cr.campaign_status = 'ONGOING'";
  386. List<Map<String, Object>> ongoingAgents = jdbcTemplate.queryForList(ongoingCampaignAgentsQuery);
  387. //System.out.println("ongoingAgents" + ongoingAgents);
  388. // Extract agent IDs from both lists
  389. Set<Integer> ongoingAgentIds = ongoingAgents.stream()
  390. .map(agent -> (Integer) agent.get("agent_id"))
  391. .collect(Collectors.toSet());
  392. // Filter OUTBOUND agents who are NOT in ongoing agents
  393. List<Map<String, Object>> availableAgents = outboundAgents.stream()
  394. .filter(agent -> !ongoingAgentIds.contains(agent.get("agent_id")))
  395. .collect(Collectors.toList());
  396. return ResponseEntity.ok(availableAgents);
  397. }
  398. private Object getCellValue(Cell cell) {
  399. switch (cell.getCellType()) {
  400. case STRING:
  401. return cell.getStringCellValue().trim();
  402. case NUMERIC:
  403. if (DateUtil.isCellDateFormatted(cell)) {
  404. return cell.getDateCellValue(); // Return Date for date-formatted cells
  405. }
  406. double numericValue = cell.getNumericCellValue();
  407. // ✅ Convert numeric values that are likely mobile numbers to string
  408. if (String.valueOf((long) numericValue).length() >= 10) { // Assuming mobile numbers are at least 10 digits
  409. return String.valueOf((long) numericValue);
  410. }
  411. // ✅ Convert to int if no decimal part, else keep as double
  412. return (numericValue % 1 == 0) ? (int) numericValue : numericValue;
  413. case BOOLEAN:
  414. return cell.getBooleanCellValue();
  415. case FORMULA:
  416. return cell.getCellFormula(); // Return formula as string
  417. case BLANK:
  418. return "";
  419. default:
  420. return "";
  421. }
  422. }
  423. //question and answers report page api
  424. @GetMapping("/question-answers")
  425. public ResponseEntity<?> getQuestionAnswers(@RequestParam int campaignId) {
  426. List<Map<String, Object>> response = campaignService.getQuestionAnswers(campaignId);
  427. return ResponseEntity.ok(response);
  428. }
  429. //dizo campaignlist apicontroller
  430. // Query Parameter Approach
  431. @GetMapping("/statistics")
  432. public ResponseEntity<Map<String, Object>> getCampaignStatisticsByQuery(@RequestParam int campaignId) {
  433. Map<String, Object> statistics = campaignService.getCampaignStatistics(campaignId);
  434. return ResponseEntity.ok(statistics);
  435. }
  436. @GetMapping("/assignagentid")
  437. public ResponseEntity<List<Map<String, Object>>> getAllAgents() {
  438. List<Map<String, Object>> assignAgentId = campaignService.getAllAgents();
  439. return ResponseEntity.ok(assignAgentId);
  440. }
  441. @PostMapping("/close")
  442. public ResponseEntity<String> closeCampaign(@RequestParam("campaignId") int campaignId) {
  443. if (campaignId <= 0) {
  444. return ResponseEntity.badRequest().body("Invalid Campaign ID");
  445. }
  446. System.out.println("Received request to close campaign ID: " + campaignId); // Debugging log
  447. int rowsUpdated = campaignService.closeCampaign(campaignId);
  448. if (rowsUpdated > 0) {
  449. return ResponseEntity.ok("Campaign closed successfully");
  450. } else {
  451. return ResponseEntity.status(400).body("Failed to close campaign. Campaign ID " + campaignId + " may not exist.");
  452. }
  453. }
  454. @PostMapping("/auto-close")
  455. public ResponseEntity<String> autoCloseExpiredCampaigns() {
  456. int rowsUpdated = campaignService.autoCloseExpiredCampaigns();
  457. if (rowsUpdated > 0) {
  458. return ResponseEntity.ok(rowsUpdated + " expired campaigns closed successfully.");
  459. } else {
  460. return ResponseEntity.ok("No expired campaigns found.");
  461. }
  462. }
  463. }