/**
  * 执行批量数据导入的操作,如果勾选了isRewrite,则会先清除表中原有的数据,然后再重新插入,<br>
  * 若没有勾选,则直接执行插入操作,与原有的数据共同保存在数据库中。
  *
  * @param sheetContentDTO
  * @param br
  * @param attach
  * @param req
  * @param model
  * @return
  * @throws IOException
  */
 @RequestMapping(value = "/import/{cid}", method = RequestMethod.POST)
 public String importDatas(
     @Valid SheetContentDTO sheetContentDTO,
     BindingResult br,
     @RequestParam("attachs") MultipartFile attach,
     HttpServletRequest req,
     Model model) {
   model.addAttribute("casename", sheetContentDTO.getCaseName());
   model.addAttribute("caseid", sheetContentDTO.getCaseId());
   if (br.hasErrors()) return "data/importData";
   ExcelUtil excelUtil = new ExcelUtil();
   String realpath = req.getSession().getServletContext().getRealPath("/resources/upload");
   String errorInfo = null;
   if (attach.isEmpty()) errorInfo = "文件不能为空";
   if (!attach.getOriginalFilename().endsWith(".xlsx")
       && !attach.getOriginalFilename().endsWith(".xls")) errorInfo = "请选择[xlsx、xls]格式的数据文件";
   if (errorInfo != null) {
     model.addAttribute("errorInfo", errorInfo);
     return "data/importData";
   }
   String filePath =
       realpath
           + "/"
           + req.getSession().getId()
           + "."
           + Files.getFileExtension(attach.getOriginalFilename());
   ;
   File f = new File(filePath);
   SheetContentDTO sheetContent = null;
   try {
     FileUtils.copyInputStreamToFile(attach.getInputStream(), f);
     excelUtil.setImportExcelPath(filePath);
     sheetContent = excelUtil.getExcelContent(sheetContentDTO.getSheetName());
   } catch (Exception e) {
     errorInfo =
         "发生异常,请检查数据表的数据格式和指定的Sheet页名称是否正确," + e.getClass().getSimpleName() + "," + e.getMessage();
     model.addAttribute("errorInfo", errorInfo);
     f.delete();
     return "data/importData";
   }
   CaseBean caseBean = null;
   if (sheetContent.getCaseId() != null)
     caseBean = caseService.getCaseBean(Integer.parseInt(sheetContent.getCaseId()));
   else if (sheetContent.getCaseName() != null)
     caseBean = caseService.getCaseBeanByName(sheetContent.getCaseName());
   if (!sheetContentDTO.getCaseId().equals(caseBean.getId() + "")) {
     errorInfo = "当前用例是:[ " + sheetContentDTO.getCaseName() + " ] ,数据表中的数据不属于当前用例";
     model.addAttribute("errorInfo", errorInfo);
     f.delete();
     return "data/importData";
   }
   if (sheetContentDTO.getIsRewrite()) {
     inBatchDataService.deleteInBatchByCaseId(caseBean.getId());
     for (String[] params : sheetContent.getParameters()) {
       InterfaceBatchDataBean tempBatchBean =
           inBatchDataService.addInBatch(new InterfaceBatchDataBean(caseBean, params[0]));
       for (int i = 1; i < sheetContent.getParamNames().length; i++) {
         inDataBeanService.addDataBean(
             new InterfaceDataBean(tempBatchBean, sheetContent.getParamNames()[i], params[i]));
       }
     }
   } else {
     for (String[] params : sheetContent.getParameters()) {
       InterfaceBatchDataBean tempBatchBean =
           inBatchDataService.addInBatch(new InterfaceBatchDataBean(caseBean, params[0]));
       for (int i = 1; i < sheetContent.getParamNames().length; i++) {
         inDataBeanService.addDataBean(
             new InterfaceDataBean(tempBatchBean, sheetContent.getParamNames()[i], params[i]));
       }
     }
   }
   f.delete();
   return "redirect:/data/INTERFACE_CASE/list/" + caseBean.getId();
 }
 @RequestMapping(
     value = "/export/{id}",
     method = RequestMethod.POST,
     produces = "text/plain;charset=UTF-8")
 public @ResponseBody String exportData(
     @PathVariable Integer id, HttpServletRequest req, Model model) {
   CaseBean caseBean = caseService.getCaseBean(id);
   String fileName = GetNow.getCurrentTime("yyyyMMddHHmmss") + "_" + caseBean.getCaseName();
   String filePath =
       req.getSession().getServletContext().getRealPath("/resources/upload/")
           + "/"
           + fileName
           + ".xlsx";
   SheetContentDTO sheetDTO = new SheetContentDTO();
   ExcelUtil excelUtil = new ExcelUtil();
   List<String[]> parameters = new ArrayList<String[]>();
   List<InterfaceBatchDataBean> ibatchDataList = inBatchDataService.getInBatchList(id);
   sheetDTO.setCaseName(caseBean.getCaseName());
   sheetDTO.setCaseId(id + "");
   String[] paramName = null;
   for (InterfaceBatchDataBean batchData : ibatchDataList) {
     List<InterfaceDataBean> idataBeanList = inDataBeanService.getDataBeans(batchData.getId());
     String[] paramContent = new String[idataBeanList.size() + 1];
     paramName = new String[idataBeanList.size() + 1];
     paramName[0] = "期望值\\参数名";
     paramContent[0] = batchData.getExpectData();
     for (int i = 0; i < idataBeanList.size(); i++) {
       paramName[i + 1] = idataBeanList.get(i).getDataName();
       paramContent[i + 1] = idataBeanList.get(i).getDataContent();
     }
     parameters.add(paramContent);
   }
   sheetDTO.setParamNames(paramName);
   sheetDTO.setParameters(parameters);
   excelUtil.setExportExcelPath(filePath);
   try {
     excelUtil.exportExcel(sheetDTO);
     String fileUrl =
         "http://"
             + req.getServerName()
             + ":"
             + req.getServerPort()
             + "/phoenix_web/resources/upload/"
             + fileName
             + ".xlsx";
     return JSON.toJSONString(
         new AjaxObj(
             1,
             "导出接口用例[ "
                 + caseBean.getCaseName()
                 + " ]的数据成功!<br>点击下载:<a href='"
                 + fileUrl
                 + "'>"
                 + fileUrl
                 + "</a>",
             fileName));
   } catch (Exception e) {
     return JSON.toJSONString(
         new AjaxObj(0, "导出接口用例[ " + caseBean.getCaseName() + " ]的数据失败!<br>" + e.getCause()));
   }
 }