/** * 执行添加操作 * * @param caseId * @param expectData * @return */ @RequestMapping(value = "/INTERFACE_CASE/add/{caseId}", method = RequestMethod.POST) public String addBatch(@PathVariable Integer caseId, String expectData) { CaseBean caseBean = caseService.getCaseBean(caseId); InterfaceBatchDataBean interBatchDataBean = new InterfaceBatchDataBean(); interBatchDataBean.setCaseBean(caseBean); interBatchDataBean.setExpectData(expectData); inBatchDataService.addInBatch(interBatchDataBean); return "redirect:/data/INTERFACE_CASE/list/" + caseId; }
/** * 执行批量数据导入的操作,如果勾选了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(); }