package in.gov.chennaicorporation.gccoffice.callcenter.controller; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import in.gov.chennaicorporation.gccoffice.callcenter.service.AgentsService; @RequestMapping("/gcc/api/agents") @RestController public class AgentsAPIController { @Autowired private AgentsService agentsService; @GetMapping("/excel") public List> fetchDataFromExcel(@RequestParam int categoryId,@RequestParam Long campaignId) { try { return agentsService.fetchDataFromExcel(categoryId,campaignId); } catch (IOException e) { e.printStackTrace(); return null; // Handle the exception as needed } } @PostMapping("/saveExcelData") public String saveExcelData( @RequestParam int categoryId,@RequestParam Long campaignId,@RequestParam String filePath) { try { // Fetch data from Excel List> excelData = agentsService.getExcelData(categoryId,campaignId, filePath); // Save data into the database agentsService.saveExcelData(excelData); return "Data saved successfully"; } catch (IOException e) { e.printStackTrace(); return "Failed to save data"; } } @GetMapping("/getdataByIds") public List> getdataByIds(@RequestParam List ids) { return agentsService.getdataByIds(ids); } @GetMapping("/getPendingDataByIds") public List> getPendingDataByIds(@RequestParam List ids) { return agentsService.getPendingDataByIds(ids); } // @PostMapping("/updatecall") // public ResponseEntity updatecall(@RequestBody Map callData) // { // String call_status = (String) callData.get("call_status"); // int data_id = (int) callData.get("data_id"); // int agent_id=(int) callData.get("agent_id"); // int campaign_id=(int) callData.get("campaign_id"); // // if (call_status == null || call_status.isEmpty()) { // return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Call status is required."); // } // // agentsService.saveInLogsTable(campaign_id, agent_id, data_id,call_status); // // return ResponseEntity.ok("Data Inserted successfully."); // } @GetMapping("/getAgentNames") public List> getAgentsNameById(@RequestParam List agentIds){ return agentsService.getAgentsNameById(agentIds); } @GetMapping("/getDataFields") public List> getDataDetailsById( @RequestParam String originalDataIds) throws JsonProcessingException { // Parse originalDataIds from JSON string ObjectMapper objectMapper = new ObjectMapper(); List> originalDataIdsList = objectMapper.readValue(originalDataIds, new TypeReference>>() {}); // Prepare a list to hold the results for each sub-array List> groupedResults = new ArrayList<>(); // Iterate through each sub-array of originalDataIds for (List dataIds : originalDataIdsList) { List> results = agentsService.getDataDetailsById(dataIds); // Group results according to your business logic Map combinedResult = new HashMap<>(); for (Map result : results) { combinedResult.putAll(result); } groupedResults.add(combinedResult); } return groupedResults; } @GetMapping("/getCompletedDataFields") public Map getCompletedDataDetailsById( @RequestParam String originalCDataIds) throws JsonProcessingException { // Parse originalDataIds from JSON string ObjectMapper objectMapper = new ObjectMapper(); List originalCDataIdsList = objectMapper.readValue(originalCDataIds, new TypeReference>() {}); // Prepare a list to hold the results for each sub-array //List> groupedCompletedResults = new ArrayList<>(); List completedDataIds = new ArrayList<>(); // List to hold data_ids with Complete_count > 0 // Iterate through each sub-array of originalDataIds List> results = agentsService.getCompletedDataDetailsById(originalCDataIdsList); // Group results according to your business logic //Map CompletedcombinedResult = new HashMap<>(); for (Map result : results) { if (result.get("Complete_count") != null && ((Number) result.get("Complete_count")).intValue() > 0) { completedDataIds.add((Integer) result.get("data_id")); } //groupedCompletedResults.add(result); } // Prepare the final response containing grouped results and completed dataIds Map response = new HashMap<>(); //response.put("groupedCompletedResults", groupedCompletedResults); response.put("completedDataIds", completedDataIds); return response; } @GetMapping("/getCompletedDataById") public List> getCompletedDataById(@RequestParam List completedDataIds){ return agentsService.getCompletedDataById(completedDataIds); } @PostMapping("/furtherAssignedAgents") public ResponseEntity> updateAgentAssigndata( @RequestBody Map requestData) { String campaign_id = (String) requestData.get("campaign_id"); String category_id = (String) requestData.get("category_id"); String call_per_agent = (String) requestData.get("call_per_agent"); // Correct key name for selected agents List selectedAgentsList = (List) requestData.get("selected_agents"); //System.out.println("campaign_id=" + campaign_id); //System.out.println("category_id=" + category_id); //System.out.println("call_per_agent=" + call_per_agent); //System.out.println("selectedAgentsList=" + selectedAgentsList); int campaignId = Integer.parseInt(campaign_id); int categoryId = Integer.parseInt(category_id); int callperagent = Integer.parseInt(call_per_agent); agentsService.resetAgentAssignmentForNextDay(categoryId,campaignId); int calls_assign = callperagent * selectedAgentsList.size(); //System.out.println("calls_assign== " + calls_assign); List DataIds=agentsService.getDataIdForFurtherAssign(categoryId, campaignId); //System.out.println("dataids=== " + DataIds); if (!DataIds.isEmpty()) { int dataIndex = 0; // Initialize data index pointer for (String agentId : selectedAgentsList) { int selectedAgentId = Integer.parseInt(agentId); agentsService.addAgentsForCampaign(selectedAgentId, categoryId, campaignId); // Iterate for noOfCalls times and assign data IDs to the current agent for (int i = 0; i < callperagent && dataIndex < DataIds.size(); i++, dataIndex++) { int DataId = DataIds.get(dataIndex); agentsService.updateAgentForCampaign( selectedAgentId, categoryId, campaignId, DataId ); } } } else { return ResponseEntity.status(HttpStatus.NOT_FOUND) .body(Map.of("status", "error", "message", "No data available for Further assign.")); } return ResponseEntity.ok(Map.of("status", "success", "message", "Campaign details saved successfully")); } ////////////////////Ajith code////////////////////// @GetMapping("/Campaignview") public ResponseEntity getCampaignview(@RequestParam int categoryId, @RequestParam int agentId, @RequestParam int dataId) { try { List> response = agentsService.getFilteredFields(categoryId, agentId, dataId); if (response.isEmpty()) { return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No matching data found."); } return ResponseEntity.ok(response); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error: " + e.getMessage()); } } @GetMapping("/questions") public List> getCampaignQuestions( @RequestParam int categoryId, @RequestParam int campaignId) { return agentsService.getCampaignQuestions(categoryId, campaignId); } @GetMapping("/fetchAnswer") public List> fetchAnswer( @RequestParam int campaignId, @RequestParam int agentId, @RequestParam int questionId, @RequestParam int dataId) { return agentsService.FetchAnswer(campaignId,agentId,questionId,dataId); } @PostMapping("/submitAnswers") public ResponseEntity submitAnswers(@RequestBody Map>> requestData) { List> answers = requestData.get("answers"); if (answers == null || answers.isEmpty()) { return ResponseEntity.badRequest().body("No answers provided."); } agentsService.saveAnswers(answers); return ResponseEntity.ok("Answers submitted successfully"); } @GetMapping("/callstatus") public List> getCallStatus() { //System.out.println("dropdown"); return agentsService.getCallStatus(); } @GetMapping("/checkCounts") public List> checkCounts( @RequestParam int campaignId, @RequestParam int dataId) { return agentsService.checkCounts(campaignId,dataId); } @PostMapping("/savecall") public ResponseEntity savecall(@RequestBody Map callData) { String call_status = (String) callData.get("call_status"); String remarks = (String) callData.get("remarks"); int data_id = Integer.parseInt(callData.get("data_id").toString()); int agent_id = Integer.parseInt(callData.get("agent_id").toString()); int campaign_id = Integer.parseInt(callData.get("campaign_id").toString()); // System.out.println("controler"); //System.out.println(call_status); // Add a debug statement to check the received values // System.out.println("Received call_status: " + call_status); if (call_status == null || call_status.isEmpty()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Call status is required."); } agentsService.saveCallLogs(call_status, remarks,data_id,agent_id); agentsService.saveInLogsTable(campaign_id, agent_id, data_id,call_status,remarks); return ResponseEntity.ok("Data Inserted successfully."); } }