package cn.com.goldenwater.dcproj.controller.ducha; import cn.com.goldenwater.core.web.BaseController; import cn.com.goldenwater.core.web.BaseResponse; import cn.com.goldenwater.dcproj.model.BisInspPlanChk; import cn.com.goldenwater.dcproj.param.BisInspPlanChkParam; import cn.com.goldenwater.dcproj.service.BisInspPlanChkService; import cn.com.goldenwater.dcproj.service.OlBisInspOrgService; import cn.com.goldenwater.dcproj.util.ReadExcelUtil; import cn.com.goldenwater.dcproj.utils.AdLevelUtil; import cn.com.goldenwater.dcproj.utils.impexcel.ExpAndImpUtil; import cn.com.goldenwater.id.util.UuidUtil; import com.github.pagehelper.PageInfo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; /** * @author lhc * @date 2021-4-1 */ @Api(value = "年度计划方案-年度督查检查计划管理", tags = "年度计划方案-年度督查检查计划管理") @RestController @RequestMapping("/bis/insp/plan/chk") public class BisInspPlanChkController extends BaseController { private Logger logger = LoggerFactory.getLogger(getClass()); @Value("${export.templatePath}") private String templatePath; @Autowired private BisInspPlanChkService bisInspPlanChkService; @Autowired private OlBisInspOrgService olBisInspOrgService; @ApiOperation(value = "修改年度计划方案-年度督查检查计划") @RequestMapping(value = "", method = RequestMethod.POST) public BaseResponse insert(@ApiParam(name = "bisInspPlanChk", value = "BisInspPlanChk", required = true) @RequestBody BisInspPlanChk bisInspPlanChk) { int ret = 0; if (StringUtils.isBlank(bisInspPlanChk.getId())) { bisInspPlanChk.setAdCode(olBisInspOrgService.getProvince(getCurrentOrgId())); bisInspPlanChk.setPersId(getCurrentPersId()); ret = bisInspPlanChkService.insert(bisInspPlanChk); } else { ret = bisInspPlanChkService.update(bisInspPlanChk); } return buildSuccessResponse(bisInspPlanChk); } @ApiOperation(value = "根据ID删除年度计划方案-年度督查检查计划") @RequestMapping(value = "delete/{id}", method = RequestMethod.POST) public BaseResponse delete(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) { int ret = bisInspPlanChkService.delete(id); return buildSuccessResponse(); } @ApiOperation(value = "根据ID获取年度计划方案-年度督查检查计划(单表)") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public BaseResponse get(@ApiParam(name = "id", value = "id", required = true) @PathVariable String id) { BisInspPlanChk bisInspPlanChk = bisInspPlanChkService.get(id); return buildSuccessResponse(bisInspPlanChk); } @ApiOperation(value = "根据ID获取年度计划方案-年度督查检查计划(单表)") @RequestMapping(value = "/listOfPage", method = RequestMethod.POST) public BaseResponse> listOfPage(@ApiParam(name = "bisInspPlanChkcc", value = "bisInspPlanChkcc", required = true) @RequestBody BisInspPlanChkParam bisInspPlanChk) { bisInspPlanChk.setAdCode(AdLevelUtil.getAddvcd(olBisInspOrgService.getProvince(getCurrentOrgId()))); return buildSuccessResponse(bisInspPlanChkService.findPageInfo(bisInspPlanChk)); } @ApiOperation(value = "导出检查表") @RequestMapping(value = "/export/checkList", method = RequestMethod.POST) public void exportCheckList(HttpServletResponse response, @RequestBody BisInspPlanChkParam bisInspPlanChkParam) { bisInspPlanChkService.exportCheckList(response, bisInspPlanChkParam); } @ApiOperation("获取导入模板") @GetMapping(value = "/getExl") public BaseResponse getExl(HttpServletResponse response) { String path = templatePath + File.separator + "chk_plan.xls"; try { ExpAndImpUtil.downloadFile(response, path, "年度检查表"); } catch (Exception e) { logger.error(e.getMessage(), e); } return buildSuccessResponse(); } @ApiOperation("上传 xls 导入数据") @RequestMapping(value = "/impExl", method = RequestMethod.POST) public BaseResponse> impExl(@RequestParam("file") MultipartFile multfile) throws IOException, InvalidFormatException { // 获取文件名 String fileName = multfile.getOriginalFilename(); // 获取文件后缀 String prefix = fileName.substring(fileName.lastIndexOf(".")); // 用uuid作为文件名,防止生成的临时文件重复 String uuid = UuidUtil.uuid(); File file = File.createTempFile(uuid, prefix); // MultipartFile to File multfile.transferTo(Paths.get(file.getPath())); Map map = new HashMap<>(2); int shu = 0; int success = 0; String[][] data = ReadExcelUtil.getData(file, 3); if (data.length > 0) { for (String[] datum : data) { if (StringUtils.isBlank(datum[1])) { continue; } try { BisInspPlanChk planChk = new BisInspPlanChk(); planChk.setChkYear(Long.parseLong(datum[1])); planChk.setChkItem(datum[2]); planChk.setChkMatter(datum[3]); planChk.setChkBatch(datum[4]); planChk.setChkTm(datum[5]); planChk.setChkCity(datum[6]); planChk.setChkCounty(datum[7]); planChk.setChkContent(datum[8]); planChk.setChkDep(datum[9]); planChk.setChkType(datum[10]); insert(planChk); success++; } catch (Exception e) { logger.error(e.getMessage(), e); shu++; } } } map.put("false", shu); map.put("success", success); return buildSuccessResponse(map); } }