package com.mooctest.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.mooctest.service.ExcelInputService; import com.mooctest.service.PaperService; import com.mooctest.util.ImportDataMap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Timestamp; import java.util.Map; @Controller public class PaperController { @Value("${report.host}") String reportHost ; @Autowired PaperService paperService; @Autowired ExcelInputService excelInputService; @Autowired ImportDataMap importDataMap; private String ITEM_GROUP_NAME = "item_group_list"; @GetMapping("/addPaper") public String addPaper (Model model){ model.addAttribute("reportHost",reportHost); return "add_paper"; } @GetMapping("/addExcel") public String addExcel (Model model){ model.addAttribute("type_map", importDataMap.typeMap); return "add_excel_pro"; } @PostMapping("/add_excel_pro") @ResponseBody public String excelToMongo (@RequestParam("name")String name, @RequestParam("excelFile") MultipartFile multipartFile, @RequestParam("come") Integer comeFrom) throws IOException { // 从前端接收Excel文件,并返回文件保存的全路径 String fileFullPath = excelInputService.saveMultipartFile(multipartFile, name); // 拿取文件,并将文件内容插入到Mongo数据库中 Map is_success = excelInputService.excelToMongo(fileFullPath, name, comeFrom); String insert_success_json= JSON.toJSONString(is_success); return insert_success_json; } @DeleteMapping("/dpaper") @ResponseBody public JSONObject deletePaper (Model model , @RequestParam("id") String id ){ return paperService.deletePaper(id); } @GetMapping("/papers") public String auditTask ( Model model){ JSONArray jsonArray = paperService.getPaperData(); // 所有的数据。 for (Object single : jsonArray){ single = (JSONObject) single; changeTime2Long(single); // 修改创建时间的格式 以便前段展示 } model.addAttribute("tasks",jsonArray); model.addAttribute("reportHost",reportHost); return "paper_list"; } @GetMapping("/changePaper") public String changeTask ( Model model ,@RequestParam("id") String id ){ JSONObject data = paperService.getSinglePaper(id); JSONArray arrary = data.getJSONArray(ITEM_GROUP_NAME); model.addAttribute("data",data); model.addAttribute("ruleLength",arrary.size()); model.addAttribute("reportHost",reportHost); return "changePaper"; } @GetMapping("/paperDetail") public String paperDetail ( Model model , @RequestParam("id") String id){ JSONObject data = paperService.getSinglePaper(id); JSONArray jobs = paperService.getJobsByPaperId(id); for(Object o : jobs){ changeTime2Long(o); } model.addAttribute("data",data); model.addAttribute("reportHost",reportHost); model.addAttribute("jobs",jobs); return "paperDetail"; } private void changeTime2Long ( Object input){ JSONObject temp = (JSONObject) input; temp.put("create_time",new Timestamp(Long.parseLong(temp.get("create_time").toString()))); // 修改创建时间的格式 以便前段展示 } }