package com.epmet.controller; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.annotation.MaskResponse; import com.epmet.commons.tools.aop.NoRepeatSubmit; import com.epmet.commons.tools.constant.AppClientConstant; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.ServiceConstant; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetException; import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.feign.ResultDataResolver; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.*; import com.epmet.commons.tools.utils.poi.excel.handler.FreezeAndFilter; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.constants.ImportTaskConstants; import com.epmet.dto.IcVaccineDTO; import com.epmet.dto.form.AddIcVaccineFormDTO; import com.epmet.dto.form.IcVaccineFormDTO; import com.epmet.dto.form.VaccineListFormDTO; import com.epmet.dto.result.IcVaccineListCommonExcelResultDTO; import com.epmet.dto.result.IcVaccineListResultDTO; import com.epmet.dto.result.ImportTaskCommonResultDTO; import com.epmet.feign.EpmetCommonServiceOpenFeignClient; import com.epmet.service.IcVaccineService; import com.epmet.utils.ImportTaskUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.nio.file.Path; import java.util.List; import java.util.UUID; /** * 疫苗接种记录 * * @author generator generator@elink-cn.com * @since v1.0.0 2022-04-06 */ @Slf4j @RestController @RequestMapping("icVaccine") public class IcVaccineController implements ResultDataResolver { @Autowired private IcVaccineService icVaccineService; @Autowired private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; /** * @Author sun * @Description 【疫苗】疫苗接种信息列表 **/ @NoRepeatSubmit @PostMapping("vaccine-list") @MaskResponse(fieldNames = { "mobile", "idCard" }, fieldsMaskType = { MaskResponse.MASK_TYPE_MOBILE, MaskResponse.MASK_TYPE_ID_CARD }) public Result> vaccineList(@LoginUser TokenDto tokenDto, @RequestBody VaccineListFormDTO formDTO) { formDTO.setCustomerId(tokenDto.getCustomerId()); formDTO.setUserId(tokenDto.getUserId()); return new Result>().ok(icVaccineService.icVaccineList(formDTO)); } /** * @Author zxc * @Description 【疫苗】疫苗接种信息同步 **/ @NoRepeatSubmit @PostMapping("synchro") public Result synchro(@LoginUser TokenDto tokenDto, @RequestBody VaccineListFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, VaccineListFormDTO.Synchro.class); formDTO.setCustomerId(tokenDto.getCustomerId()); formDTO.setUserId(tokenDto.getUserId()); icVaccineService.synchro(formDTO); return new Result(); } /** * @Author zxc * @Description 【疫苗】疫苗接种信息取消同步 **/ @NoRepeatSubmit @PostMapping("cancelsynchro") public Result cancelSynchro(@LoginUser TokenDto tokenDto, @RequestBody VaccineListFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, VaccineListFormDTO.Synchro.class); formDTO.setCustomerId(tokenDto.getCustomerId()); formDTO.setUserId(tokenDto.getUserId()); icVaccineService.cancelSynchro(formDTO); return new Result<>(); } /** * 导入excel * @return */ @PostMapping("import") public Result importExcel(MultipartFile file) { String userId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID); // 1.暂存文件 String originalFilename = file.getOriginalFilename(); String extName = originalFilename.substring(originalFilename.lastIndexOf(".")); Path fileSavePath; try { Path importPath = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_vaccine", "import"); fileSavePath = importPath.resolve(UUID.randomUUID().toString().concat(extName)); } catch (IOException e) { String errorMsg = ExceptionUtils.getErrorStackTrace(e); log.error("【疫苗接种导入】创建临时存储文件失败:{}", errorMsg); throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "文件上传失败", "文件上传失败"); } InputStream is = null; FileOutputStream os = null; try { is = file.getInputStream(); os = new FileOutputStream(fileSavePath.toString()); IOUtils.copy(is, os); } catch (Exception e) { log.error("importExcel exception", e); } finally { org.apache.poi.util.IOUtils.closeQuietly(is); org.apache.poi.util.IOUtils.closeQuietly(os); } // 2.生成导入任务记录 ImportTaskCommonResultDTO rstData = getResultDataOrThrowsException( ImportTaskUtils.createImportTask(originalFilename, ImportTaskConstants.BIZ_TYPE_IC_VACCINE), ServiceConstant.EPMET_COMMON_SERVICE, EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "excel导入疫苗接种信息错误", "导入居民疫苗接种信息失败"); // 3.执行导入 icVaccineService.execAsyncExcelImport(fileSavePath, rstData.getTaskId()); return new Result(); } /** * @Author zxc * @Description 【疫苗】疫苗接种信息下载模板 **/ @RequestMapping(value = "import-template-download", method = {RequestMethod.GET, RequestMethod.POST}) public void downloadTemplate(HttpServletResponse response) throws IOException { response.setCharacterEncoding("UTF-8"); response.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode("疫苗接种导入模板", "UTF-8") + ".xlsx"); InputStream is = this.getClass().getClassLoader().getResourceAsStream("excel/ic_vaccine.xlsx"); try { ServletOutputStream os = response.getOutputStream(); IOUtils.copy(is, os); } finally { if (is != null) { is.close(); } } } /** * @Author sun * @Description 【疫苗】疫苗接种信息列表 **/ @NoRepeatSubmit @PostMapping("export") public void export(@LoginUser TokenDto tokenDto, @RequestBody VaccineListFormDTO formDTO, HttpServletResponse response) { formDTO.setCustomerId(tokenDto.getCustomerId()); formDTO.setUserId(tokenDto.getUserId()); formDTO.setIsPage(false); ExcelWriter excelWriter = null; formDTO.setPageSize(NumConstant.TEN_THOUSAND); int pageNo = formDTO.getPageNo(); try { // 这里 需要指定写用哪个class去写 String fileName = "疫苗接种信息.xlsx"; if ("all".equals(formDTO.getSearchType())) { excelWriter = EasyExcel.write(ExcelUtils.getOutputStreamForExcel(fileName, response), IcVaccineListResultDTO.class).build(); }else { excelWriter = EasyExcel.write(ExcelUtils.getOutputStreamForExcel(fileName, response), IcVaccineListCommonExcelResultDTO.class).build(); } WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").registerWriteHandler(new FreezeAndFilter()).build(); PageData data = null; do { data = icVaccineService.icVaccineList(formDTO); data.getList().forEach(o-> { o.setIsResiUser(NumConstant.ONE_STR.equals(o.getIsResiUser()) ? "是" : "否"); }); formDTO.setPageNo(++pageNo); if ("current".equals(formDTO.getSearchType())) { List list = ConvertUtils.sourceToTarget(data.getList(), IcVaccineListResultDTO.class); excelWriter.write(list, writeSheet); }else{ excelWriter.write(data.getList(), writeSheet); } } while (CollectionUtils.isNotEmpty(data.getList()) && data.getList().size() == formDTO.getPageSize()); }catch (Exception e){ log.error("export exception", e); }finally { // 千万别忘记finish 会帮忙关闭流 if (excelWriter != null) { excelWriter.finish(); } } } /** * @Author sun * @Description 疫苗接种情况-列表点击查询详情 **/ @MaskResponse(fieldNames = { "showMobile", "showIdCard" }, fieldsMaskType = { MaskResponse.MASK_TYPE_MOBILE, MaskResponse.MASK_TYPE_ID_CARD }) @NoRepeatSubmit @PostMapping("detail") public Result detail(@RequestBody IcVaccineFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, IcVaccineFormDTO.Detail.class); return new Result().ok(icVaccineService.detail(formDTO.getVaccineId())); } /** * @Author sun * @Description 【疫苗】疫苗接种信息新增 **/ @PostMapping("add") public Result add(@LoginUser TokenDto tokenDto, @RequestBody AddIcVaccineFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, AddIcVaccineFormDTO.Vaccine.class); formDTO.setCustomerId(tokenDto.getCustomerId()); formDTO.setStaffId(tokenDto.getUserId()); formDTO.setClient(tokenDto.getClient()); icVaccineService.add(formDTO); return new Result(); } /** * @Author sun * @Description 【疫苗】疫苗接种信息修改 **/ @PostMapping("edit") public Result edit(@LoginUser TokenDto tokenDto, @RequestBody AddIcVaccineFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, AddIcVaccineFormDTO.Edit.class); formDTO.setCustomerId(tokenDto.getCustomerId()); formDTO.setStaffId(tokenDto.getUserId()); icVaccineService.edit(formDTO); return new Result(); } /** * @Author sun * @Description 【疫苗】疫苗接种信息删除 **/ @PostMapping("del") public Result del(@RequestBody IcVaccineFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, IcVaccineFormDTO.Detail.class); icVaccineService.del(formDTO); return new Result<>(); } @PostMapping("initVaccineLocal") public Result initVaccineLocal(){ icVaccineService.initVaccineLocal(); return new Result(); } }