From d69e26f1342b56c359cf8ce56121be29ca47c5f2 Mon Sep 17 00:00:00 2001 From: jianjun Date: Wed, 6 Apr 2022 16:09:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=96=AB=E8=8B=97=E6=8E=A5=E7=A7=8D=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/controller/IcVaccineController.java | 308 ++++++++- .../main/java/com/epmet/dao/IcVaccineDao.java | 56 +- .../excel/data/IcVaccineImportExcelData.java | 63 ++ .../handler/IcVaccineExcelImportListener.java | 148 +++++ .../com/epmet/service/IcVaccineService.java | 111 ++-- .../service/impl/IcVaccineServiceImpl.java | 627 ++++++++++++++++-- .../main/resources/mapper/IcVaccineDao.xml | 172 ++++- 7 files changed, 1366 insertions(+), 119 deletions(-) create mode 100644 epmet-user/epmet-user-server/src/main/java/com/epmet/excel/data/IcVaccineImportExcelData.java create mode 100644 epmet-user/epmet-user-server/src/main/java/com/epmet/excel/handler/IcVaccineExcelImportListener.java diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcVaccineController.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcVaccineController.java index 0a6638394e..65f19fc3df 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcVaccineController.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcVaccineController.java @@ -1,22 +1,49 @@ 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.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.utils.ExcelUtils; -import com.epmet.commons.tools.utils.Result; -import com.epmet.commons.tools.validator.AssertUtils; +import com.epmet.commons.tools.security.dto.TokenDto; +import com.epmet.commons.tools.utils.*; import com.epmet.commons.tools.validator.ValidatorUtils; -import com.epmet.commons.tools.validator.group.AddGroup; -import com.epmet.commons.tools.validator.group.UpdateGroup; -import com.epmet.commons.tools.validator.group.DefaultGroup; -import com.epmet.dto.IcVaccineDTO; +import com.epmet.constants.ImportTaskConstants; +import com.epmet.dto.IcNatDTO; +import com.epmet.dto.form.AddIcNatFormDTO; +import com.epmet.dto.form.ImportTaskCommonFormDTO; +import com.epmet.dto.form.MyNatListFormDTO; +import com.epmet.dto.result.ImportTaskCommonResultDTO; +import com.epmet.dto.result.MyNatListResultDTO; +import com.epmet.dto.result.NatListCommonExcelResultDTO; +import com.epmet.dto.result.NatListResultDTO; +import com.epmet.feign.EpmetCommonServiceOpenFeignClient; import com.epmet.service.IcVaccineService; +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.Map; +import java.util.UUID; /** @@ -25,51 +52,268 @@ import java.util.Map; * @author generator generator@elink-cn.com * @since v1.0.0 2022-04-06 */ +@Slf4j @RestController @RequestMapping("icVaccine") -public class IcVaccineController { +public class IcVaccineController implements ResultDataResolver { @Autowired private IcVaccineService icVaccineService; - @RequestMapping("page") - public Result> page(@RequestParam Map params){ - PageData page = icVaccineService.page(params); - return new Result>().ok(page); + @Autowired + private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; + + /** + * 记录一下核酸记录模块的业务程序逻辑,防止后续理不清 ic_nat:基础信息表 ic_nat_relation:关系表 + * 1.数字平台导入时,按身份证检测时间查询数据,查到数据不是导入的状态则记录下并跳过,是导入数据更新结果,未查到直接新增 + * 是否居民以及userId值查询数据平台居民,能查到就相应的赋值; + * 2.居民端录入可以录自己的或别人的,但是数据属于当前录入的组织; + * 3.数据平台录入可以为已有居民录入也可以录新数据,新数据则是否居民和userId无值; + * 4.导入数据不允许修改删除,录入数据可以修改删除,删除时相应的删除基础表和可能存在的所有关心表数据, + * 数据同步可多次重复,只是修改对应的关系表数据,基础信息表数据公用; + * 5.同一条数据被A街道和A社区都同步了,街道在看本辖区数据时会看到两条,但是只有A街道的那一条才能看到取消同步按钮; + * 6.数据库一个人一个检测时间只存在一条记录,以导入数据结果为准,已导入的各项值都不能修改,即使错误也只能通过导入修改; + * 7.新增、删除居民时会相应的修改基础信息表是否居民以及userId的值; + * 注:1.导入数据不允许修改是觉得导入数据是官方提供的且没有问题的数据,不需要在平台有维护操作,只要看就可以了; + * 2.是否居民的查询条件以及是否居民的列标题在看整个客户列表数据时这两个都隐藏 + * + */ + + /** + * @Author sun + * @Description 核酸检测-上报核酸记录 + **/ + //@NoRepeatSubmit + @PostMapping("add") + public Result add(@LoginUser TokenDto tokenDto, @RequestBody AddIcNatFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, AddIcNatFormDTO.Nat.class); + formDTO.setCustomerId(tokenDto.getCustomerId()); + formDTO.setStaffId(tokenDto.getUserId()); + icVaccineService.add(formDTO); + return new Result(); + } + + /** + * @Author sun + * @Description 核酸检测-居民端我的上报 + **/ + @NoRepeatSubmit + @PostMapping("mynatlist") + public Result> myNatList(@LoginUser TokenDto tokenDto, @RequestBody MyNatListFormDTO formDTO) { + formDTO.setCustomerId(tokenDto.getCustomerId()); + formDTO.setUserId(tokenDto.getUserId()); + return new Result>().ok(icVaccineService.myNatList(formDTO)); } - @RequestMapping(value = "{id}",method = {RequestMethod.POST,RequestMethod.GET}) - public Result get(@PathVariable("id") String id){ - IcVaccineDTO data = icVaccineService.get(id); - return new Result().ok(data); + /** + * @Author sun + * @Description 【核酸】核酸检测信息列表 + **/ + @NoRepeatSubmit + @PostMapping("natlist") + public Result> natList(@LoginUser TokenDto tokenDto, @RequestBody MyNatListFormDTO formDTO) { + formDTO.setCustomerId(tokenDto.getCustomerId()); + formDTO.setUserId(tokenDto.getUserId()); + return new Result>().ok(icVaccineService.natList(formDTO)); } + /** + * @Author sun + * @Description 【核酸】核酸检测信息详情 + **/ @NoRepeatSubmit - @PostMapping("save") - public Result save(@RequestBody IcVaccineDTO dto){ - //效验数据 - ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class); - icVaccineService.save(dto); + @PostMapping("detail") + public Result detail(@RequestBody MyNatListFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, MyNatListFormDTO.Detail.class); + return new Result().ok(icVaccineService.detail(formDTO)); + } + + /** + * @Author sun + * @Description 【核酸】核酸检测信息修改 + **/ + @NoRepeatSubmit + @PostMapping("edit") + public Result edit(@LoginUser TokenDto tokenDto, @RequestBody AddIcNatFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, AddIcNatFormDTO.Edit.class); + formDTO.setCustomerId(tokenDto.getCustomerId()); + formDTO.setStaffId(tokenDto.getUserId()); + icVaccineService.edit(formDTO); return new Result(); } + /** + * @Author sun + * @Description 【核酸】核酸检测信息删除/取消同步 + **/ @NoRepeatSubmit - @PostMapping("update") - public Result update(@RequestBody IcVaccineDTO dto){ - //效验数据 - ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class); - icVaccineService.update(dto); + @PostMapping("del") + public Result del(@RequestBody MyNatListFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, MyNatListFormDTO.Del.class); + icVaccineService.del(formDTO); + return new Result<>(); + } + + /** + * @Author sun + * @Description 【核酸】核酸检测信息同步 + **/ + @NoRepeatSubmit + @PostMapping("synchro") + public Result synchro(@LoginUser TokenDto tokenDto, @RequestBody MyNatListFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, MyNatListFormDTO.Synchro.class); + formDTO.setCustomerId(tokenDto.getCustomerId()); + formDTO.setUserId(tokenDto.getUserId()); + icVaccineService.synchro(formDTO); return new Result(); } - @PostMapping("delete") - public Result delete(@RequestBody String[] ids){ - //效验数据 - AssertUtils.isArrayEmpty(ids, "id"); - icVaccineService.delete(ids); + /** + * @Author sun + * @Description 【核酸】核酸检测信息取消同步 + **/ + @NoRepeatSubmit + @PostMapping("cancelsynchro") + public Result cancelSynchro(@LoginUser TokenDto tokenDto, @RequestBody MyNatListFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, MyNatListFormDTO.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_nat", "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) { + e.printStackTrace(); + } finally { + org.apache.poi.util.IOUtils.closeQuietly(is); + org.apache.poi.util.IOUtils.closeQuietly(os); + } + + // 2.生成导入任务记录 + ImportTaskCommonFormDTO importTaskForm = new ImportTaskCommonFormDTO(); + importTaskForm.setOperatorId(userId); + importTaskForm.setBizType(ImportTaskConstants.BIZ_TYPE_IC_NAT); + importTaskForm.setOriginFileName(originalFilename); + + ImportTaskCommonResultDTO rstData = getResultDataOrThrowsException(commonServiceOpenFeignClient.createImportTask(importTaskForm), + ServiceConstant.EPMET_COMMON_SERVICE, + EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), + "excel导入核酸检测信息错误", + "导入居民核酸检测信息失败"); + + // 3.执行导入 + icVaccineService.execAsyncExcelImport(fileSavePath, rstData.getTaskId()); + return new Result(); } + /** + * @Author sun + * @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.ms-excel"); + 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_nat.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 MyNatListFormDTO formDTO, HttpServletResponse response) { + formDTO.setCustomerId(tokenDto.getCustomerId()); + formDTO.setUserId(tokenDto.getUserId()); + + //formDTO.setCustomerId("45687aa479955f9d06204d415238f7cc"); + //formDTO.setUserId("73ae6280e46a6653a5605d51d5462725"); + + formDTO.setIsPage(false); + + ExcelWriter excelWriter = null; + formDTO.setPageSize(NumConstant.TEN_THOUSAND); + int pageNo = formDTO.getPageNo(); + try { + // 这里 需要指定写用哪个class去写 + String fileName = "核酸检测信息.xlsx"; + if ("all".equals(formDTO.getOrgType())) { + excelWriter = EasyExcel.write(ExcelUtils.getOutputStreamForExcel(fileName, response), NatListResultDTO.class).build(); + }else { + excelWriter = EasyExcel.write(ExcelUtils.getOutputStreamForExcel(fileName, response), NatListCommonExcelResultDTO.class).build(); + } + + WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build(); + PageData data = null; + do { + data = icVaccineService.natList(formDTO); + data.getList().forEach(o-> { + o.setNatResult(NumConstant.ONE_STR.equals(o.getNatResult()) ? "阳性" : "阴性"); + o.setIsResiUser(NumConstant.ONE_STR.equals(o.getIsResiUser()) ? "是" : "否"); + }); + formDTO.setPageNo(++pageNo); + if ("current".equals(formDTO.getOrgType())) { + List list = ConvertUtils.sourceToTarget(data.getList(), NatListResultDTO.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(); + } + } + } + } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/IcVaccineDao.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/IcVaccineDao.java index 9e2b136739..84f0be0178 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/IcVaccineDao.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/IcVaccineDao.java @@ -1,8 +1,16 @@ package com.epmet.dao; import com.epmet.commons.mybatis.dao.BaseDao; +import com.epmet.dto.IcNatDTO; +import com.epmet.dto.form.MyNatListFormDTO; +import com.epmet.dto.result.MyNatListResultDTO; +import com.epmet.dto.result.NatListResultDTO; +import com.epmet.entity.IcVaccineEntity; import com.epmet.entity.IcVaccineEntity; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; /** * 疫苗接种记录 @@ -12,5 +20,49 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface IcVaccineDao extends BaseDao { - -} \ No newline at end of file + /** + * @Author sun + * @Description 核酸检测-按条件查询核酸记录 + **/ + List getMyNatList(MyNatListFormDTO formDTO); + + /** + * @Author sun + * @Description 【核酸】本辖区核酸检测信息列表 + **/ + List getNatList(MyNatListFormDTO formDTO); + + /** + * @Author sun + * @Description 【核酸】客户下核酸检测信息列表 + **/ + List getCustomerNatList(MyNatListFormDTO formDTO); + + /** + * @Author sun + * @Description 删除操作--物理删除业务数据 + **/ + int delById(@Param("icNatId") String icNatId); + + /** + * 插入或者更新 + * + * @param e + */ + void insertOrUpdate(IcVaccineEntity e); + + /** + * @Author sun + * @Description 按条件查询业务数据 + **/ + IcNatDTO getNatDTO(@Param("customerId") String customerId, @Param("icNatId") String icNatId, @Param("idCard") String idCard, @Param("natTime") String natTime, @Param("natResult") String natResult); + + /** + * desc:根据客户id 更新是否居民状态 + * + * @param customerId + * @param icResiUserId 如果为空则更新全部 + * @return + */ + int updateIsResiFlag(@Param("customerId") String customerId, @Param("icResiUserId") String icResiUserId); +} diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/excel/data/IcVaccineImportExcelData.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/excel/data/IcVaccineImportExcelData.java new file mode 100644 index 0000000000..3c10ff69b7 --- /dev/null +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/excel/data/IcVaccineImportExcelData.java @@ -0,0 +1,63 @@ +package com.epmet.excel.data; + +import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import lombok.Data; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.Date; + +/** + * 核酸检测信息导入excel数据 + */ +@Data +public class IcVaccineImportExcelData implements Serializable { + + private static final long serialVersionUID = -700535059296633797L; + @NotBlank(message = "姓名为必填项") + @ExcelProperty("姓名") + private String name; + + @NotBlank(message = "手机号为必填项") + @ExcelProperty("手机号") + private String mobile; + + @NotBlank(message = "身份证号为必填项") + @ExcelProperty("身份证号") + @Length(max = 18, message = "身份证号长度不正确,应小于18位") + private String idCard; + + @NotNull(message = "检测时间为必填项") + @ExcelProperty("检测时间") + private Date natTime; + + @ExcelProperty("检测地点") + private String natAddress; + + @NotBlank(message = "检测结果为必填项") + @ExcelProperty("检测结果") + private String natResultZh; + + @Data + public static class RowRemarkMessage { + + @ExcelProperty("姓名") + @ColumnWidth(20) + private String name; + + @ExcelProperty("手机号") + @ColumnWidth(20) + private String mobile; + + @ColumnWidth(20) + @ExcelProperty("身份证号") + private String idCard; + + @ColumnWidth(60) + @ExcelProperty("错误信息") + private String errorInfo; + } +} diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/excel/handler/IcVaccineExcelImportListener.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/excel/handler/IcVaccineExcelImportListener.java new file mode 100644 index 0000000000..027040871e --- /dev/null +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/excel/handler/IcVaccineExcelImportListener.java @@ -0,0 +1,148 @@ +package com.epmet.excel.handler; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.read.listener.ReadListener; +import com.epmet.commons.tools.exception.ExceptionUtils; +import com.epmet.commons.tools.exception.ValidateException; +import com.epmet.commons.tools.utils.ConvertUtils; +import com.epmet.commons.tools.validator.ValidatorUtils; +import com.epmet.entity.IcVaccineEntity; +import com.epmet.excel.data.IcNatImportExcelData; +import com.epmet.excel.data.IcVaccineImportExcelData; +import com.epmet.service.impl.IcNatServiceImpl; +import com.epmet.service.impl.IcVaccineServiceImpl; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * 核酸检测excel导入监听器 + */ +@Data +@Slf4j +public class IcVaccineExcelImportListener implements ReadListener { + + /** + * 最大条数阈值 + */ + public static final int MAX_THRESHOLD = 200; + + private String currentUserId; + /** + * 当前组织ID + */ + private String currentAgencyId; + + private String currentAgencyPids; + + /** + * 数据 + */ + private List datas = new ArrayList<>(); + + /** + * 错误项列表 + */ + private List errorRows = new ArrayList<>(); + /** + * 其他被标记出来的列表列表 + */ + private List otherRows = new ArrayList<>(); + + private IcVaccineServiceImpl icVaccineService; + + public IcVaccineExcelImportListener(String currentUserId, String currentAgencyId, String currentAgencyPids, IcVaccineServiceImpl icVaccineService) { + this.currentUserId = currentUserId; + this.currentAgencyId = currentAgencyId; + this.currentAgencyPids = currentAgencyPids; + this.icVaccineService = icVaccineService; + } + + @Override + public void invoke(IcVaccineImportExcelData data, AnalysisContext context) { + + try { + // 先校验数据 + ValidatorUtils.validateEntity(data); + + if (StringUtils.isNotBlank(data.getName())) { + data.setName(data.getName().trim()); + } + if (StringUtils.isNotBlank(data.getMobile())) { + data.setMobile(data.getMobile().trim()); + } + if (StringUtils.isNotBlank(data.getNatAddress())) { + data.setNatAddress(data.getNatAddress().trim()); + } + if (StringUtils.isNotBlank(data.getIdCard())) { + data.setIdCard(data.getIdCard().trim()); + } + + // 默认空,没有结果 + String natResult = ""; + String natResultZh = data.getNatResultZh(); + if (StringUtils.isNotBlank(natResultZh)) { + natResult = natResultZh.equals("阴性") ? "0" : "1"; + } + + IcVaccineEntity icNatEntity = ConvertUtils.sourceToTarget(data, IcVaccineEntity.class); + //icNatEntity.setAgencyId(currentAgencyId); + //icNatEntity.setPids(currentAgencyPids); + icNatEntity.setUserId(currentUserId); + icNatEntity.setUserType("import"); + icNatEntity.setNatResult(natResult); + icNatEntity.setAttachmentType(""); + icNatEntity.setAttachmentUrl(""); + datas.add(icNatEntity); + + if (datas.size() == MAX_THRESHOLD) { + execPersist(); + } + } catch (Exception e) { + String errorMsg = null; + if (e instanceof ValidateException) { + errorMsg = ((ValidateException) e).getMsg(); + } else { + errorMsg = "未知错误"; + log.error("【核酸检测信息导入】出错:{}", ExceptionUtils.getErrorStackTrace(e)); + } + + IcNatImportExcelData.RowRemarkMessage errorRow = new IcNatImportExcelData.RowRemarkMessage(); + errorRow.setName(data.getName()); + errorRow.setMobile(data.getMobile()); + errorRow.setIdCard(data.getIdCard()); + errorRow.setErrorInfo(errorMsg); + errorRows.add(errorRow); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + // 最后几条达不到阈值,这里必须再调用一次 + execPersist(); + } + + /** + * 执行持久化 + */ + private void execPersist() { + try { + if (datas != null && datas.size() > 0) { + icVaccineService.batchPersist(datas, this); + } + } finally { + datas.clear(); + } + } + + /** + * 获取错误行 + * @return + */ + public List getErrorRows() { + return errorRows; + } +} diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/IcVaccineService.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/IcVaccineService.java index a983466956..1e9e5d71ba 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/IcVaccineService.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/IcVaccineService.java @@ -2,11 +2,16 @@ package com.epmet.service; import com.epmet.commons.mybatis.service.BaseService; import com.epmet.commons.tools.page.PageData; -import com.epmet.dto.IcVaccineDTO; +import com.epmet.dto.IcNatDTO; +import com.epmet.dto.form.AddIcNatFormDTO; +import com.epmet.dto.form.MyNatListFormDTO; +import com.epmet.dto.result.MyNatListResultDTO; +import com.epmet.dto.result.NatListDTO; +import com.epmet.dto.result.NatListResultDTO; import com.epmet.entity.IcVaccineEntity; +import java.nio.file.Path; import java.util.List; -import java.util.Map; /** * 疫苗接种记录 @@ -17,62 +22,74 @@ import java.util.Map; public interface IcVaccineService extends BaseService { /** - * 默认分页 - * - * @param params - * @return PageData - * @author generator - * @date 2022-04-06 - */ - PageData page(Map params); + * @Author sun + * @Description 核酸检测-上报核酸记录 + **/ + void add(AddIcNatFormDTO formDTO); /** - * 默认查询 - * - * @param params - * @return java.util.List - * @author generator - * @date 2022-04-06 - */ - List list(Map params); + * @Author sun + * @Description 核酸检测-居民端我的上报 + **/ + List myNatList(MyNatListFormDTO formDTO); /** - * 单条查询 + * @Author sun + * @Description 【核酸】核酸检测信息列表 * - * @param id - * @return IcVaccineDTO - * @author generator - * @date 2022-04-06 - */ - IcVaccineDTO get(String id); + * @return*/ + PageData natList(MyNatListFormDTO formDTO); /** - * 默认保存 - * - * @param dto - * @return void - * @author generator - * @date 2022-04-06 + * @Author sun + * @Description 【核酸】核酸检测信息详情 + **/ + IcNatDTO detail(MyNatListFormDTO formDTO); + + /** + * @Author sun + * @Description 【核酸】核酸检测信息修改 + **/ + void edit(AddIcNatFormDTO formDTO); + + /** + * @Author sun + * @Description 【核酸】核酸检测信息删除/取消同步 + **/ + void del(MyNatListFormDTO formDTO); + + /** + * @Author sun + * @Description 【核酸】核酸检测信息同步 + **/ + void synchro(MyNatListFormDTO formDTO); + + /** + * @Author sun + * @Description 【核酸】核酸检测信息取消同步 + **/ + void cancelSynchro(MyNatListFormDTO formDTO); + + /** + * 执行Excel导入 + * @param filePath */ - void save(IcVaccineDTO dto); + void execAsyncExcelImport(Path filePath, String importTaskId); /** - * 默认更新 - * - * @param dto - * @return void - * @author generator - * @date 2022-04-06 + * 核酸检测信息 + * @Param idCard + * @Return {@link List} + * @Author zhaoqifeng + * @Date 2022/3/29 16:29 */ - void update(IcVaccineDTO dto); + List getNatList(String customerId, String idCard); /** - * 批量删除 - * - * @param ids - * @return void - * @author generator - * @date 2022-04-06 + * desc:根据用户id 更新核酸中是否居民状态 + * @param customerId + * @param icResiUserId 如果为空则更新全部 + * @return */ - void delete(String[] ids); -} \ No newline at end of file + Integer updateIsResiFlag(String customerId, String icResiUserId); +} diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcVaccineServiceImpl.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcVaccineServiceImpl.java index 18348ebde7..f6cab4ae48 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcVaccineServiceImpl.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/IcVaccineServiceImpl.java @@ -1,22 +1,66 @@ package com.epmet.service.impl; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.baomidou.mybatisplus.core.metadata.IPage; +import com.alibaba.excel.EasyExcel; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; +import com.epmet.commons.tools.constant.AppClientConstant; +import com.epmet.commons.tools.constant.NumConstant; +import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult; +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.exception.RenException; +import com.epmet.commons.tools.feign.ResultDataResolver; import com.epmet.commons.tools.page.PageData; -import com.epmet.commons.tools.utils.ConvertUtils; -import com.epmet.commons.tools.constant.FieldConstant; +import com.epmet.commons.tools.redis.common.CustomerOrgRedis; +import com.epmet.commons.tools.redis.common.CustomerStaffRedis; +import com.epmet.commons.tools.redis.common.bean.AgencyInfoCache; +import com.epmet.commons.tools.utils.*; +import com.epmet.constants.ImportTaskConstants; +import com.epmet.dao.IcNatRelationDao; +import com.epmet.dao.IcResiUserDao; import com.epmet.dao.IcVaccineDao; -import com.epmet.dto.IcVaccineDTO; +import com.epmet.dao.UserBaseInfoDao; +import com.epmet.dto.IcNatDTO; +import com.epmet.dto.IcNoticeDTO; +import com.epmet.dto.form.AddIcNatFormDTO; +import com.epmet.dto.form.ImportTaskCommonFormDTO; +import com.epmet.dto.form.MyNatListFormDTO; +import com.epmet.dto.form.SendNoticeFormDTO; +import com.epmet.dto.result.*; +import com.epmet.entity.IcNatRelationEntity; +import com.epmet.entity.IcResiUserEntity; import com.epmet.entity.IcVaccineEntity; +import com.epmet.excel.data.IcNatImportExcelData; +import com.epmet.excel.handler.IcNatExcelImportListener; +import com.epmet.excel.handler.IcVaccineExcelImportListener; +import com.epmet.feign.EpmetCommonServiceOpenFeignClient; +import com.epmet.feign.EpmetUserOpenFeignClient; +import com.epmet.feign.OssFeignClient; +import com.epmet.service.IcNoticeService; import com.epmet.service.IcVaccineService; -import org.apache.commons.lang3.StringUtils; +import com.epmet.service.UserService; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.FileItemFactory; +import org.apache.commons.fileupload.disk.DiskFileItemFactory; +import org.apache.http.entity.ContentType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.commons.CommonsMultipartFile; -import java.util.Arrays; -import java.util.List; -import java.util.Map; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.*; +import java.util.stream.Collectors; /** * 疫苗接种记录 @@ -24,59 +68,568 @@ import java.util.Map; * @author generator generator@elink-cn.com * @since v1.0.0 2022-04-06 */ +@Slf4j @Service -public class IcVaccineServiceImpl extends BaseServiceImpl implements IcVaccineService { +public class IcVaccineServiceImpl extends BaseServiceImpl implements IcVaccineService, ResultDataResolver { + @Autowired + private EpmetCommonServiceOpenFeignClient commonServiceOpenFeignClient; + @Autowired + private EpmetUserOpenFeignClient epmetUserOpenFeignClient; + @Autowired + private IcNoticeService icNoticeService; + @Autowired + private OssFeignClient ossFeignClient; + @Autowired + private UserBaseInfoDao userBaseInfoDao; + @Autowired + private IcNatRelationDao icNatRelationDao; + @Autowired + private IcResiUserDao icResiUserDao; + @Autowired + private UserService userService; + + /** + * @Author sun + * @Description 核酸检测-上报核酸记录 + **/ @Override - public PageData page(Map params) { - IPage page = baseDao.selectPage( - getPage(params, FieldConstant.CREATED_TIME, false), - getWrapper(params) - ); - return getPageData(page, IcVaccineDTO.class); + @Transactional(rollbackFor = Exception.class) + public void add(AddIcNatFormDTO formDTO) { + //0.先根据身份证号和检查时间以及检测结果校验数据是否存在 + IcNatDTO icNatDTO = baseDao.getNatDTO(formDTO.getCustomerId(), null, formDTO.getIdCard(), DateUtils.format(formDTO.getNatTime(), DateUtils.DATE_TIME_PATTERN_END_WITH_MINUTE), null); + //按身份证号核酸时间存在记录的 核酸结果相同的提示已存在相同记录核酸结果不同的提示已存在去修改【业务要求的】 + if (null != icNatDTO && icNatDTO.getNatResult().equals(formDTO.getNatResult())) { + throw new RenException(EpmetErrorCode.IC_NAT_IDCARD_NATTIME.getCode(), EpmetErrorCode.IC_NAT_IDCARD_NATTIME.getMsg()); + } else if (null != icNatDTO && !icNatDTO.getNatResult().equals(formDTO.getNatResult())) { + throw new RenException(EpmetErrorCode.IC_NAT.getCode(), EpmetErrorCode.IC_NAT.getMsg()); + } + //1.获取所填居民所属组织缓存信息 + AgencyInfoCache agencyInfo = CustomerOrgRedis.getAgencyInfo(formDTO.getAgencyId()); + if (null == agencyInfo) { + throw new RenException(String.format("获取组织缓存信息失败%s", formDTO.getAgencyId())); + } + //2.新增核酸基础信息数据 + IcVaccineEntity entity = ConvertUtils.sourceToTarget(formDTO, IcVaccineEntity.class); + if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(formDTO.getUserId())) { + entity.setIsResiUser("1"); + } + insert(entity); + //3.核酸记录关系表新增与组织的关系数据 + IcNatRelationEntity relationEntity = ConvertUtils.sourceToTarget(formDTO, IcNatRelationEntity.class); + relationEntity.setIcNatId(entity.getId()); + relationEntity.setPids(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(agencyInfo.getPids()) ? agencyInfo.getPids() + ":" + formDTO.getAgencyId() : formDTO.getAgencyId()); + icNatRelationDao.insert(relationEntity); + + //3.新增通知表信息 + if (formDTO.getChannel().size() > NumConstant.ZERO) { + SendNoticeFormDTO dto = new SendNoticeFormDTO(); + List userList = new ArrayList<>(); + userList.add(ConvertUtils.sourceToTarget(formDTO, SendNoticeFormDTO.UserListBean.class)); + dto.setCustomerId(formDTO.getCustomerId()); + dto.setUserList(userList); + dto.setChannel(formDTO.getChannel()); + dto.setOrigin("2"); + dto.setContent(formDTO.getContent()); + dto.setStaffId(formDTO.getStaffId()); + icNoticeService.sendNotice(dto); + } + } + /** + * @Author sun + * @Description 核酸检测-居民端我的上报 + **/ @Override - public List list(Map params) { - List entityList = baseDao.selectList(getWrapper(params)); + public List myNatList(MyNatListFormDTO formDTO) { + //1.根据token信息查询居民身份证号 + UserBaseInfoResultDTO dto = userBaseInfoDao.selectListByUserIdList(formDTO.getUserId()); + if (null != dto) { + formDTO.setIdCard(dto.getIdNum()); + } + //2.查询当前人员创建的或该身份证号录入的核算检测数据(居民端录入、数字平台录入、数字平台导入、数字平台同步的) + List resultList = baseDao.getMyNatList(formDTO); + return resultList; + } + + /** + * @Author sun + * @Description 【核酸】本组织及下级核酸检测信息列表 + * + * @return*/ + @Override + public PageData natList(MyNatListFormDTO formDTO) { + //1.根据是查询客户数据还是本辖区数据走不同逻辑 + if ("current".equals(formDTO.getOrgType())) { + //本辖区数据 + //2.获取工作人员缓存信息 + CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(formDTO.getCustomerId(), formDTO.getUserId()); + if (null == staffInfo) { + throw new EpmetException(String.format("查询工作人员%s缓存信息失败...", formDTO.getUserId())); + } + formDTO.setAgencyId(staffInfo.getAgencyId()); + //3.按条件查询业务数据 + PageInfo data = PageHelper.startPage(formDTO.getPageNo(), formDTO.getPageSize(), formDTO.getIsPage()) + .doSelectPageInfo(() -> baseDao.getNatList(formDTO)); + return new PageData(data.getList(), data.getTotal()); + } else if ("all".equals(formDTO.getOrgType())) { + //客户下数据 + PageInfo data = PageHelper.startPage(formDTO.getPageNo(), formDTO.getPageSize(), formDTO.getIsPage()) + .doSelectPageInfo(() -> baseDao.getCustomerNatList(formDTO)); + return new PageData(data.getList(), data.getTotal()); + } - return ConvertUtils.sourceToTarget(entityList, IcVaccineDTO.class); + return new PageData(new ArrayList(), 0); } - private QueryWrapper getWrapper(Map params){ - String id = (String)params.get(FieldConstant.ID_HUMP); + /** + * @Author sun + * @Description 【核酸】核酸检测信息详情 + **/ + @Override + public IcNatDTO detail(MyNatListFormDTO formDTO) { + IcNatDTO resultDTO = new IcNatDTO(); + //1.查询核酸记录信息 + IcVaccineEntity entity = baseDao.selectById(formDTO.getIcNatId()); + if (null == resultDTO) { + return resultDTO; + } + resultDTO = ConvertUtils.sourceToTarget(entity, IcNatDTO.class); + + //2.查询对应的通知记录信息 + IcNoticeDTO dto = icNoticeService.getNotice(entity.getCustomerId(), entity.getIdCard()); + if (null != dto) { + resultDTO.setChannel(dto.getChannelList()); + resultDTO.setContent(dto.getContent()); + } + + return resultDTO; + } - QueryWrapper wrapper = new QueryWrapper<>(); - wrapper.eq(StringUtils.isNotBlank(id), FieldConstant.ID, id); + /** + * @Author sun + * @Description 【核酸】核酸检测信息修改 + **/ + @Override + @Transactional(rollbackFor = Exception.class) + public void edit(AddIcNatFormDTO formDTO) { + //0.先根据身份证号和检测时间以及检测结果校验除当前数据是否还存在相同数据 + IcNatDTO icNatDTO = baseDao.getNatDTO(formDTO.getCustomerId(), formDTO.getIcNatId(), formDTO.getIdCard(), DateUtils.format(formDTO.getNatTime(), DateUtils.DATE_TIME_PATTERN_END_WITH_MINUTE), formDTO.getNatResult()); + if (null != icNatDTO) { + throw new RenException(EpmetErrorCode.IC_NAT.getCode(), EpmetErrorCode.IC_NAT.getMsg()); + } + //1.更新核酸记录基础信息表数据 + IcVaccineEntity entity = ConvertUtils.sourceToTarget(formDTO, IcVaccineEntity.class); + entity.setId(formDTO.getIcNatId()); + if (!updateById(entity)) { + log.error(String.format("数据修改失败,核酸记录Id->%s", formDTO.getIcNatId())); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "核酸记录修改失败"); + } + //修改只涉及到基础信息的修改 不会修改关系数据 关系表不涉及更新 - return wrapper; + //3.新增通知表信息 + if (CollectionUtils.isNotEmpty(formDTO.getChannel())) { + SendNoticeFormDTO dto = new SendNoticeFormDTO(); + List userList = new ArrayList<>(); + userList.add(ConvertUtils.sourceToTarget(formDTO, SendNoticeFormDTO.UserListBean.class)); + dto.setCustomerId(formDTO.getCustomerId()); + dto.setUserList(userList); + dto.setChannel(formDTO.getChannel()); + dto.setOrigin("2"); + dto.setContent(formDTO.getContent()); + dto.setStaffId(formDTO.getStaffId()); + icNoticeService.sendNotice(dto); + } } + /** + * @Author sun + * @Description 【核酸】核酸检测信息删除/取消同步 + **/ @Override - public IcVaccineDTO get(String id) { - IcVaccineEntity entity = baseDao.selectById(id); - return ConvertUtils.sourceToTarget(entity, IcVaccineDTO.class); + @Transactional(rollbackFor = Exception.class) + public void del(MyNatListFormDTO formDTO) { + //1.确定删除则需要删除基础数据以及所有可能存在的关系数据,物理删除业务数据 + if (baseDao.delById(formDTO.getIcNatId()) < NumConstant.ONE) { + log.error(String.format("数据删除失败,核酸记录Id->%s", formDTO.getIcNatId())); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "数据删除失败"); + } + //关系数据删除 + icNatRelationDao.delRelation(formDTO.getIcNatId(), null); } + /** + * @Author sun + * @Description 【核酸】核酸检测信息同步 + **/ @Override @Transactional(rollbackFor = Exception.class) - public void save(IcVaccineDTO dto) { - IcVaccineEntity entity = ConvertUtils.sourceToTarget(dto, IcVaccineEntity.class); - insert(entity); + public void synchro(MyNatListFormDTO formDTO) { + //1.检验核酸基础信息是否存在 + IcVaccineEntity entity = baseDao.selectById(formDTO.getIcNatId()); + if (null == entity) { + log.error(String.format("核酸记录数据不存在同步失败,核酸记录Id->%s", formDTO.getIcNatId())); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "数据同步失败,核酸记录数据不存在"); + } + //获取工作人员缓存数据 + CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(formDTO.getCustomerId(), formDTO.getUserId()); + if (null == staffInfo) { + throw new EpmetException(String.format("查询工作人员%s缓存信息失败...", formDTO.getUserId())); + } + //2.不管是否已存在关系数据都是先删后增 + icNatRelationDao.delRelation(formDTO.getIcNatId(), staffInfo.getAgencyId()); + //3.新增关系数据 + IcNatRelationEntity relationEntity = new IcNatRelationEntity(); + relationEntity.setCustomerId(formDTO.getCustomerId()); + relationEntity.setIcNatId(formDTO.getIcNatId()); + relationEntity.setAgencyId(staffInfo.getAgencyId()); + relationEntity.setPids(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(staffInfo.getAgencyPIds())?staffInfo.getAgencyPIds()+":"+staffInfo.getAgencyId():staffInfo.getAgencyId()); + relationEntity.setUserType("synchro"); + icNatRelationDao.insert(relationEntity); } + /** + * @Author sun + * @Description 【核酸】核酸检测信息取消同步 + **/ @Override @Transactional(rollbackFor = Exception.class) - public void update(IcVaccineDTO dto) { - IcVaccineEntity entity = ConvertUtils.sourceToTarget(dto, IcVaccineEntity.class); - updateById(entity); + public void cancelSynchro(MyNatListFormDTO formDTO) { + //1.获取工作人员缓存数据 + CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(formDTO.getCustomerId(), formDTO.getUserId()); + if (null == staffInfo) { + throw new EpmetException(String.format("查询工作人员%s缓存信息失败...", formDTO.getUserId())); + } + //1.物理删除关系数据 + if (icNatRelationDao.delRelation(formDTO.getIcNatId(), staffInfo.getAgencyId()) < NumConstant.ONE) { + log.error(String.format("数据取消同步失败,核酸记录Id->%s", formDTO.getIcNatId())); + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "取消同步操作失败"); + } + } + + @Async + @Override + public void execAsyncExcelImport(Path filePath, String importTaskId) { + String userId = null; + + try { + userId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID); + String app = EpmetRequestHolder.getHeader(AppClientConstant.APP); + String client = EpmetRequestHolder.getHeader(AppClientConstant.CLIENT); + + LoginUserDetailsResultDTO loginUserDetails = userService.getLoginUserDetails(app, client, userId); + + String agencyId = loginUserDetails.getAgencyId(); + + IcNatExcelImportListener listener = new IcNatExcelImportListener(userId, agencyId, loginUserDetails.getOrgIdPath().replace(":".concat(agencyId), ""), this); + + EasyExcel.read(filePath.toFile(), IcNatImportExcelData.class, listener).headRowNumber(2).sheet(0).doRead(); + + Path errorDescFile = null; + String errorDesFileUrl = null; + + List errorRows = listener.getErrorRows(); + List otherRows = listener.getOtherRows(); + + boolean failed = errorRows.size() > 0; + + // 合并到一起写入 + errorRows.addAll(otherRows); + + // 生成并上传描述文件 + if (errorRows.size() > 0) { + try { + // 文件生成 + Path errorDescDir = FileUtils.getAndCreateDirUnderEpmetFilesDir("ic_nat", "import", "error_des"); + String fileName = UUID.randomUUID().toString().concat(".xlsx"); + errorDescFile = errorDescDir.resolve(fileName); + + FileItemFactory factory = new DiskFileItemFactory(16, errorDescDir.toFile()); + FileItem fileItem = factory.createItem("file", ContentType.APPLICATION_OCTET_STREAM.toString(), true, fileName); + OutputStream os = fileItem.getOutputStream(); + + EasyExcel.write(os, IcNatImportExcelData.RowRemarkMessage.class).sheet("信息列表").doWrite(errorRows); + // 文件上传oss + Result errorDesFileUploadResult = ossFeignClient.uploadImportTaskDescFile(new CommonsMultipartFile(fileItem)); + if (errorDesFileUploadResult.success()) { + errorDesFileUrl = errorDesFileUploadResult.getData().getUrl(); + } + + } finally { + if (Files.exists(errorDescFile)) { + Files.delete(errorDescFile); + } + } + } + + ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO(); + importFinishTaskForm.setTaskId(importTaskId); + importFinishTaskForm.setProcessStatus(failed ? ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL : ImportTaskConstants.PROCESS_STATUS_FINISHED_SUCCESS); + importFinishTaskForm.setOperatorId(userId); + importFinishTaskForm.setResultDesc(""); + importFinishTaskForm.setResultDescFilePath(errorDesFileUrl); + + Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm); + if (!result.success()) { + log.error("【核酸检测导入】finishImportTask失败"); + } + } catch (Exception e) { + String errorMsg = ExceptionUtils.getErrorStackTrace(e); + log.error("【核酸检测导入】出错:{}", errorMsg); + + ImportTaskCommonFormDTO importFinishTaskForm = new ImportTaskCommonFormDTO(); + importFinishTaskForm.setTaskId(importTaskId); + importFinishTaskForm.setProcessStatus(ImportTaskConstants.PROCESS_STATUS_FINISHED_FAIL); + importFinishTaskForm.setOperatorId(userId); + importFinishTaskForm.setResultDesc("导入失败"); + + Result result = commonServiceOpenFeignClient.finishImportTask(importFinishTaskForm); + if (!result.success()) { + log.error("【核酸检测导入】导入记录状态修改为'完成'失败"); + } + } finally { + // 删除临时文件 + if (Files.exists(filePath)) { + try { + Files.delete(filePath); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + } + + /** + * 核酸检测信息 + * + * @param idCard + * @Param idCard + * @Return {@link List< NatListDTO >} + * @Author zhaoqifeng + * @Date 2022/3/29 16:29 + */ + @Override + public List getNatList(String customerId, String idCard) { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + wrapper.eq(IcVaccineEntity::getCustomerId, customerId); + wrapper.eq(IcVaccineEntity::getIdCard, idCard); + wrapper.orderByDesc(IcVaccineEntity::getNatTime); + List list = baseDao.selectList(wrapper); + if (CollectionUtils.isEmpty(list)) { + return Collections.emptyList(); + } + return list.stream().map(item -> { + NatListDTO dto = new NatListDTO(); + dto.setAddress(item.getNatAddress()); + dto.setTestTime(DateUtils.format(item.getNatTime(), DateUtils.DATE_TIME_PATTERN_END_WITH_MINUTE)); + switch (item.getNatResult()) + { + case NumConstant.ZERO_STR: + dto.setResult("阴性"); + break; + case NumConstant.ONE_STR: + dto.setResult("阳性"); + break; + default: + dto.setResult("未知"); + } + return dto; + }).collect(Collectors.toList()); } @Override + public Integer updateIsResiFlag(String customerId, String icResiUserId) { + return baseDao.updateIsResiFlag(customerId,icResiUserId); + } + + /** + * 批量持久化 + * @param entities + */ + public void batchPersist(List entities, IcVaccineExcelImportListener listener) { + String customerId = EpmetRequestHolder.getHeader(AppClientConstant.CUSTOMER_ID); + String currentUserId = EpmetRequestHolder.getHeader(AppClientConstant.USER_ID); + + entities.forEach(e -> { + try { + persisVaccine(e, customerId, currentUserId, listener); + } catch (Exception exception) { + String errorMsg = ExceptionUtils.getErrorStackTrace(exception); + log.error(errorMsg); + + IcNatImportExcelData.RowRemarkMessage errorRow = new IcNatImportExcelData.RowRemarkMessage(); + errorRow.setName(e.getName()); + errorRow.setMobile(e.getMobile()); + errorRow.setIdCard(e.getIdCard()); + errorRow.setErrorInfo("未知系统错误"); + listener.getErrorRows().add(errorRow); + } + }); + } + + /** + * 单条持久化 + * @param e + * @param customerId + * @param currentUserId + * @param listener + */ @Transactional(rollbackFor = Exception.class) - public void delete(String[] ids) { - // 逻辑删除(@TableLogic 注解) - baseDao.deleteBatchIds(Arrays.asList(ids)); + public void persisVaccine(IcVaccineEntity e, String customerId, String currentUserId, IcNatExcelImportListener listener) { + List otherRows = listener.getOtherRows(); + String agencyPids = listener.getCurrentAgencyPids(); + String agencyId = listener.getCurrentAgencyId(); + + Date natTime = e.getNatTime(); + String idCard = e.getIdCard(); + String name = e.getName(); + String mobile = e.getMobile(); + String natResult = e.getNatResult(); + String natAddress = e.getNatAddress(); + + //1.先看客户下有没有这个人 + IcVaccineEntity resiNat = getResiNat(customerId, idCard, natTime); + if (resiNat != null && !"import".equals(resiNat.getUserType())) { + // 有这个人,并且不是导入的 + String message = "已存在该次核酸检测录入记录,请到系统中修改"; + + IcNatImportExcelData.RowRemarkMessage errorRow = new IcNatImportExcelData.RowRemarkMessage(); + errorRow.setName(name); + errorRow.setMobile(mobile); + errorRow.setIdCard(idCard); + errorRow.setErrorInfo(message); + otherRows.add(errorRow); + return; + } + + if (resiNat != null) { + boolean needUpdate = false; + // 有这个人,也是导入的,那就要更新le + ArrayList changedFieldNames = new ArrayList<>(); + if (!name.equals(resiNat.getName())) { + changedFieldNames.add("姓名"); + resiNat.setName(name); + needUpdate = true; + } + + if (!natResult.equals(resiNat.getNatResult())) { + changedFieldNames.add("检测结果"); + resiNat.setNatResult(natResult); + needUpdate = true; + } + + // 检测地点和手机号先不提示,说需要提示再提示。需要考虑两个都不为空&&不相等 || 两个中一个为空一个不为空 + if ((com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(natAddress) && com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(resiNat.getNatAddress())) + || (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(natAddress) && com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(resiNat.getNatAddress())) + || (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(natAddress) && com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(resiNat.getNatAddress()) && !natAddress.equals(resiNat.getNatAddress()))) { + resiNat.setNatAddress(natAddress); + needUpdate = true; + } + + if (!mobile.equals(resiNat.getMobile())) { + resiNat.setMobile(mobile); + needUpdate = true; + } + + if (changedFieldNames.size() > 0) { + String fieldsStr = String.join(",", changedFieldNames); + String message = "该次核酸检测记录已存在,执行更新动作," + fieldsStr + "已成功更新"; + IcNatImportExcelData.RowRemarkMessage errorRow = new IcNatImportExcelData.RowRemarkMessage(); + errorRow.setName(name); + errorRow.setMobile(mobile); + errorRow.setIdCard(idCard); + errorRow.setErrorInfo(message); + otherRows.add(errorRow); + } + + if (needUpdate) { + resiNat.setUpdatedBy(currentUserId); + resiNat.setUpdatedTime(new Date()); + + IcResiUserEntity resi = getResi(customerId, idCard, null); + resiNat.setIsResiUser(resi != null ? "1" : "0"); + resiNat.setUserId(resi != null ? resi.getId() : ""); + + baseDao.updateById(resiNat); + } + + // 还要创建关系。只有本辖区及下级居民,才建立关系 + if (getResi(customerId, idCard, String.join(":", Arrays.asList(agencyPids, agencyId))) != null) { + createNatRelation(resiNat.getId(), listener.getCurrentAgencyId(), listener.getCurrentAgencyPids()); + } + + return; + } + + // 执行新增操作 + IcResiUserEntity resi = getResi(customerId, idCard, null); + e.setIsResiUser(resi != null ? "1" : "0"); + e.setUserId(resi != null ? resi.getId() : ""); + e.setUserType("import"); + + baseDao.insert(e); + + // 还要创建关系。只有本辖区及下级居民,才建立关系 + if (getResi(customerId, idCard, String.join(":", Arrays.asList(agencyPids, agencyId))) != null) { + createNatRelation(e.getId(), listener.getCurrentAgencyId(), listener.getCurrentAgencyPids()); + } + } + + /** + * 是否是指定条件下的居民; + * 0:否 + * 1:是 + * @param customerId + * @param idCard + * @return + */ + public IcResiUserEntity getResi(String customerId, String idCard, String pids) { + LambdaQueryWrapper query = new LambdaQueryWrapper(); + query.eq(IcResiUserEntity::getCustomerId, customerId); + query.eq(IcResiUserEntity::getIdCard, idCard); + if (StringUtils.isNotBlank(pids)) { + query.likeRight(IcResiUserEntity::getPids, pids); + } + return icResiUserDao.selectOne(query); + } + + /** + * + * @param customerId + * @param idCard + * @return + */ + public IcVaccineEntity getResiNat(String customerId, String idCard, Date natTime) { + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.eq(IcVaccineEntity::getCustomerId, customerId); + query.eq(IcVaccineEntity::getIdCard, idCard); + query.eq(IcVaccineEntity::getNatTime, natTime); + return baseDao.selectOne(query); + } + + /** + * 创建nat关系 + * @param natId + * @param currentUserAgencyId + */ + private void createNatRelation(String natId, String currentUserAgencyId, String agencyPids) { + + // 没有关系创建关系,有关系就跳过 + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.eq(IcNatRelationEntity::getIcNatId, natId); + query.eq(IcNatRelationEntity::getAgencyId, currentUserAgencyId); + if (icNatRelationDao.selectCount(query) > 0) { + return; + } + + IcNatRelationEntity relation = new IcNatRelationEntity(); + relation.setAgencyId(currentUserAgencyId); + relation.setPids(String.join(":", Arrays.asList(agencyPids, currentUserAgencyId))); + relation.setIcNatId(natId); + relation.setUserType("import"); + icNatRelationDao.insert(relation); } -} \ No newline at end of file +} diff --git a/epmet-user/epmet-user-server/src/main/resources/mapper/IcVaccineDao.xml b/epmet-user/epmet-user-server/src/main/resources/mapper/IcVaccineDao.xml index 49fdaa2fe4..fcb5e56580 100644 --- a/epmet-user/epmet-user-server/src/main/resources/mapper/IcVaccineDao.xml +++ b/epmet-user/epmet-user-server/src/main/resources/mapper/IcVaccineDao.xml @@ -2,5 +2,175 @@ + - \ No newline at end of file + + + + + + + + DELETE FROM ic_nat WHERE id = #{icNatId} + + + + INSERT INTO epmet_user.ic_nat + (ID, CUSTOMER_ID, AGENCY_ID, PIDS, USER_ID, + USER_TYPE, NAME, MOBILE, ID_CARD, NAT_TIME, NAT_RESULT, NAT_ADDRESS, FILE_NAME, ATTACHMENT_TYPE, ATTACHMENT_URL, + DEL_FLAG, REVISION, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME) + VALUES (#{id}, #{customerId}, #{agencyId}, #{pids}, #{userId}, + #{userType}, #{name}, #{mobile}, #{idCard}, #{natTime}, + #{natResult}, #{natAddress}, #{fileName}, #{attachmentType}, #{attachmentUrl}, + #{delFlag}, #{revision}, #{createdBy}, #{createdTime}, #{updatedBy}, + #{updatedTime}) + ON DUPLICATE KEY update + NAME=#{name}, + MOBILE=#{mobile}, + NAT_ADDRESS=#{natAddress}, + NAT_RESULT=#{natResult}, + UPDATED_BY=#{updatedBy}, + UPDATED_TIME=NOW() + + + UPDATE ic_nat m, + ( + SELECT if(DEL_FLAG,0,1) resiFlag, ID_CARD FROM ic_resi_user + WHERE + 1=1 + + AND ID = #{icResiUserId} + + AND CUSTOMER_ID = #{customerId} + AND DEL_FLAG = '0' + ) t + SET m.IS_RESI_USER = t.resiFlag + WHERE + m.ID_CARD = t.ID_CARD + AND m.DEL_FLAG = '0' + +