| | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.core.service.IConfigService; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.common.utils.DtoConversionUtils; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.smartor.common.ScheduleSubtaskBuilder; |
| | | import com.smartor.domain.*; |
| | | import com.smartor.domain.DTO.ManualVisitDTO; |
| | | import com.smartor.domain.DTO.PatMedInhospDTO; |
| | | import com.smartor.domain.entity.ServiceSubtaskEntity; |
| | | import com.smartor.mapper.*; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.io.IOException; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 人工随访:删除患者出院记录对应的未完成随访任务,并按指定任务重新生成。 |
| | | * 未完成状态口径:sendstate in (1,2,3,7)。 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Map<String, Object> addManualVisit(ManualVisitDTO manualVisitDTO) { |
| | | if (manualVisitDTO == null) { |
| | | throw new BaseException("入参不能为空"); |
| | | } |
| | | if (CollectionUtils.isEmpty(manualVisitDTO.getPatids())) { |
| | | throw new BaseException("患者patid集合不能为空"); |
| | | } |
| | | if (manualVisitDTO.getTaskid() == null) { |
| | | throw new BaseException("人工随访任务taskid不能为空"); |
| | | } |
| | | |
| | | ServiceTask serviceTask = serviceTaskMapper.selectServiceTaskByTaskid(manualVisitDTO.getTaskid()); |
| | | if (Objects.isNull(serviceTask)) { |
| | | throw new BaseException("人工随访任务不存在,taskid:" + manualVisitDTO.getTaskid()); |
| | | } |
| | | |
| | | LinkedHashSet<Long> patids = manualVisitDTO.getPatids().stream() |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toCollection(LinkedHashSet::new)); |
| | | if (patids.isEmpty()) { |
| | | throw new BaseException("患者patid集合不能为空"); |
| | | } |
| | | |
| | | String config = iSysConfigService.selectConfigByKey("visit.early.day"); |
| | | List<Long> unfinishedSendstates = Arrays.asList(1L, 2L, 3L, 7L); |
| | | int deletedCount = 0; |
| | | int generatedCount = 0; |
| | | List<Long> noDischargeRecordPatids = new ArrayList<>(); |
| | | List<Long> noArchivePatids = new ArrayList<>(); |
| | | List<Long> generateFailedPatids = new ArrayList<>(); |
| | | |
| | | for (Long patid : patids) { |
| | | // 查询患者全部出院记录,SQL 按出院时间倒序,第一条作为本次生成任务的出院记录 |
| | | PatMedInhosp query = new PatMedInhosp(); |
| | | query.setPatid(patid); |
| | | query.setCry(1); |
| | | if (StringUtils.isNotEmpty(serviceTask.getOrgid())) { |
| | | query.setOrgid(serviceTask.getOrgid()); |
| | | } |
| | | List<PatMedInhosp> inhospList = patMedInhospMapper.selectPatMedInhospList(query); |
| | | if (CollectionUtils.isEmpty(inhospList)) { |
| | | noDischargeRecordPatids.add(patid); |
| | | continue; |
| | | } |
| | | |
| | | PatMedInhosp latestInhosp = inhospList.get(0); |
| | | if (Objects.isNull(latestInhosp.getEndtime())) { |
| | | noDischargeRecordPatids.add(patid); |
| | | continue; |
| | | } |
| | | |
| | | PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid); |
| | | if (Objects.isNull(patArchive)) { |
| | | noArchivePatids.add(patid); |
| | | continue; |
| | | } |
| | | |
| | | // 先构建新任务,构建失败时不删除原任务,避免患者丢失待随访任务 |
| | | ServiceSubtask serviceSubtask = boxedServiceSubtask(serviceTask, latestInhosp, patArchive, config, 1); |
| | | if (Objects.isNull(serviceSubtask) || Objects.isNull(serviceSubtask.getTaskid())) { |
| | | generateFailedPatids.add(patid); |
| | | continue; |
| | | } |
| | | |
| | | List<Long> inhospids = inhospList.stream() |
| | | .map(PatMedInhosp::getInhospid) |
| | | .filter(Objects::nonNull) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | if (CollectionUtils.isNotEmpty(inhospids)) { |
| | | deletedCount += serviceSubtaskMapper.deleteUnfinishedServiceSubtaskByInhospids( |
| | | patid, inhospids, unfinishedSendstates); |
| | | } |
| | | |
| | | serviceSubtask.setId(null); |
| | | serviceSubtask.setUpid(null); |
| | | serviceSubtask.setVisitDeptCode(StringUtils.isNotEmpty(latestInhosp.getLeaveldeptcode()) |
| | | ? latestInhosp.getLeaveldeptcode() : latestInhosp.getLeavehospitaldistrictcode()); |
| | | serviceSubtask.setVisitDeptName(StringUtils.isNotEmpty(latestInhosp.getLeaveldeptname()) |
| | | ? latestInhosp.getLeaveldeptname() : latestInhosp.getLeavehospitaldistrictname()); |
| | | // 与常规生成逻辑保持一致:手机号为空时生成任务但不执行 |
| | | if (StringUtils.isEmpty(serviceSubtask.getPhone())) { |
| | | serviceSubtask.setRemark(StringUtils.isNotEmpty(serviceSubtask.getRemark()) |
| | | ? serviceSubtask.getRemark() + ",手机号为空" : "手机号为空"); |
| | | serviceSubtask.setSendstate(4L); |
| | | serviceSubtask.setResult("error"); |
| | | } |
| | | |
| | | int inserted = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask); |
| | | if (inserted != 1) { |
| | | throw new BaseException("生成随访任务失败,patid:" + patid + ",taskid:" + serviceTask.getTaskid()); |
| | | } |
| | | addServiceSubtaskPreachform(serviceSubtask, null); |
| | | generatedCount++; |
| | | log.info("addManualVisit 生成随访任务成功,patid:{},taskid:{},subtaskId:{}", |
| | | patid, serviceTask.getTaskid(), serviceSubtask.getId()); |
| | | } |
| | | |
| | | Map<String, Object> result = new HashMap<>(); |
| | | result.put("deletedCount", deletedCount); |
| | | result.put("generatedCount", generatedCount); |
| | | result.put("noDischargeRecordPatids", noDischargeRecordPatids); |
| | | result.put("noArchivePatids", noArchivePatids); |
| | | result.put("generateFailedPatids", generateFailedPatids); |
| | | log.info("addManualVisit 处理完成,patid数:{},taskid:{},删除未完成任务数:{},生成任务数:{}", |
| | | patids.size(), serviceTask.getTaskid(), deletedCount, generatedCount); |
| | | return result; |
| | | } |
| | | |
| | | } |