liusheng
昨天 21ce122193e2fb57d206dbfca59e8d2ab2114fd0
smartor/src/main/java/com/smartor/service/impl/PatMedOuthospServiceImpl.java
@@ -1,27 +1,26 @@
package com.smartor.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.smartor.domain.*;
import com.smartor.domain.entity.ServiceSubtaskEntity;
import com.smartor.mapper.*;
import com.smartor.service.IPatMedOuthospService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUserDept;
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.domain.*;
import com.smartor.domain.entity.ServiceSubtaskEntity;
import com.smartor.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.smartor.service.IPatMedOuthospService;
import org.springframework.util.CollectionUtils;
/**
 * 患者门诊记录Service业务层处理
@@ -36,7 +35,7 @@
    private PatMedOuthospMapper patMedOuthospMapper;
    @Autowired
    private SysUserDeptMapper sysUserDeptMapper;
    private DataSource dataSource;
    @Autowired
    private ServiceSubtaskMapper serviceSubtaskMapper;
@@ -62,7 +61,8 @@
     */
    @Override
    public PatMedOuthosp selectPatMedOuthospById(Long id) {
        return patMedOuthospMapper.selectPatMedOuthospById(id);
//        return patMedOuthospMapper.selectPatMedOuthospById(id);
        return null;
    }
    /**
@@ -73,9 +73,10 @@
     */
    @Override
    public List<PatMedOuthosp> selectPatMedOuthospList(PatMedOuthosp patMedOuthosp) {
        List<PatMedOuthosp> patMedOuthosps = patMedOuthospMapper.selectPatMedOuthospList(patMedOuthosp);
        PatMedOuthospQueryReq patMedOuthospQueryReq = DtoConversionUtils.sourceToTarget(patMedOuthosp, PatMedOuthospQueryReq.class);
        List<PatMedOuthosp> patMedOuthospQueryResps = patMedOuthospMapper.callSpQueryOuthosp(patMedOuthospQueryReq);
        if (patMedOuthosp.getPageNum() != null) {
            for (PatMedOuthosp patMedOuthosp1 : patMedOuthosps) {
            for (PatMedOuthosp patMedOuthosp1 : patMedOuthospQueryResps) {
                PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patMedOuthosp1.getPatid());
                if (patArchive.getBirthdate() != null) {
                    Map<String, String> map = calculateAge(patArchive.getBirthdate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), LocalDate.now());
@@ -101,7 +102,7 @@
                if (!CollectionUtils.isEmpty(serviceSubtaskList)) patMedOuthosp1.setServerState("1");
            }
        }
        return patMedOuthosps;
        return patMedOuthospQueryResps;
    }
    public Map<String, String> calculateAge(LocalDate birthdate, LocalDate today) {
@@ -158,27 +159,109 @@
    /**
     * 新增患者门诊记录
     * 根据 admitdate 路由到对应分表;若分表不存在则写入主表
     *
     * @param patMedOuthosp 患者门诊记录
     * @return 结果
     */
    @Override
    public int insertPatMedOuthosp(PatMedOuthosp patMedOuthosp) {
        if (patMedOuthosp.getAdmitdate() != null) {
            log.error("门急就诊时间不能为空");
            return 0;
        }
        patMedOuthosp.setCreateTime(DateUtils.getNowDate());
        patMedOuthosp.setUpdateTime(DateUtils.getNowDate());
        return patMedOuthospMapper.insertPatMedOuthosp(patMedOuthosp);
        patMedOuthosp.setGuid(UUID.randomUUID().toString());
        //需要插入到哪个表,根据就诊时间的日期来定,如果根据就诊时间来确定的表不存在,则存在pat_med_outhosp表中
        String targetTable = resolveTargetTable(patMedOuthosp.getAdmitdate());
        if (targetTable != null) {
            log.info("[insert] 路由到分表: {}", targetTable);
            return patMedOuthospMapper.insertIntoTable(targetTable, patMedOuthosp);
        }
        return 0;
    }
    /**
     * 修改患者门诊记录
     * 根据 admitdate 路由到对应分表;若分表不存在则改主表
     *
     * @param patMedOuthosp 患者门诊记录
     * @return 结果
     */
    @Override
    public int updatePatMedOuthosp(PatMedOuthosp patMedOuthosp) {
        patMedOuthosp.setUpdateTime(DateUtils.getNowDate());
        return patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp);
        if (patMedOuthosp.getSerialnum() == null) {
            log.error("流水号不能为空");
            return 0;
        }
        PatMedOuthospQueryReq patMedOuthospQueryReq = new PatMedOuthospQueryReq();
        patMedOuthospQueryReq.setSerialnum(patMedOuthosp.getSerialnum());
        patMedOuthospQueryReq.setGuid(patMedOuthosp.getGuid());
        patMedOuthospQueryReq.setOuthospno(patMedOuthosp.getOuthospno());
        List<PatMedOuthosp> patMedOuthosps = patMedOuthospMapper.callSpQueryOuthosp(patMedOuthospQueryReq);
        if (patMedOuthosps == null || patMedOuthosps.size() == 0) {
            insertPatMedOuthosp(patMedOuthosp);
        } else {
            patMedOuthosp.setUpdateTime(DateUtils.getNowDate());
            String targetTable = resolveTargetTable(patMedOuthosp.getAdmitdate());
            if (targetTable != null) {
                log.info("[update] 路由到分表: {}", targetTable);
                return patMedOuthospMapper.updateInTable(targetTable, patMedOuthosp);
            }
        }
        return 0;
    }
    /**
     * 根据 create_time 推算目标分表名,与建表策略保持一致
     * <ul>
     *   <li>6位后缀(YYYYMM)→ 按月分表</li>
     *   <li>8位后缀(YYYYMMdd)→ 按季度分表,季度后缀固定为 0103/0406/0709/1012</li>
     *   <li>4位后缀(YYYY)    → 按年分表</li>
     * </ul>
     * 推算出表名后校验表是否存在,不存在返回 null(回落主表)
     *
     * @param createTime 创建时间,为 null 时返回 null
     * @return 目标分表名,或 null
     */
    private String resolveTargetTable(java.util.Date createTime) {
        if (createTime == null) return null;
        List<String> allTables = patMedOuthospMapper.getAllOuthospTableNames();
        if (allTables == null || allTables.isEmpty()) return null;
        // 判断当前分表后缀长度(以第一张表为准)
        String sample = allTables.get(0);
        String suffix = sample.replaceFirst("pat_med_outhosp_", "");
        int suffixLen = suffix.length();
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.setTime(createTime);
        int year = cal.get(java.util.Calendar.YEAR);
        int month = cal.get(java.util.Calendar.MONTH) + 1; // 1-based
        String targetTable;
        if (suffixLen == 4) {
            // 按年
            targetTable = "pat_med_outhosp_" + year;
        } else if (suffixLen == 6) {
            // 按月
            targetTable = String.format("pat_med_outhosp_%d%02d", year, month);
        } else {
            // 按季度(8位后缀,如 20260103)
            String quarterSuffix;
            if (month <= 3) quarterSuffix = "0103";
            else if (month <= 6) quarterSuffix = "0406";
            else if (month <= 9) quarterSuffix = "0709";
            else quarterSuffix = "1012";
            targetTable = "pat_med_outhosp_" + year + quarterSuffix;
        }
        boolean exists = allTables.contains(targetTable);
        log.info("[路由] createTime={} → 目标表={} exists={}", createTime, targetTable, exists);
        return exists ? targetTable : "pat_med_outhosp";
    }
    /**
@@ -189,7 +272,8 @@
     */
    @Override
    public int deletePatMedOuthospByIds(Long[] ids) {
        return patMedOuthospMapper.deletePatMedOuthospByIds(ids);
//        return patMedOuthospMapper.deletePatMedOuthospByIds(ids);
        return 0;
    }
    /**
@@ -200,22 +284,26 @@
     */
    @Override
    public int deletePatMedOuthospById(Long id) {
        return patMedOuthospMapper.deletePatMedOuthospById(id);
//        return patMedOuthospMapper.deletePatMedOuthospById(id);
        return 0;
    }
    @Override
    public PatMedRes selectPatMedOuthospCount(PatMedReq patMedReq) {
//        // 获取当前登陆人的部门权限
//        if (CollectionUtils.isEmpty(patMedReq.getDeptcodeList())) {
//            Long userId = SecurityUtils.getUserId();
//            List<SysDept> sysDepts = sysUserDeptMapper.selectDeptListByUserId(userId);
//            List<String> deptCode = new ArrayList<>();
//            for (SysDept sysDept : sysDepts) {
//                deptCode.add(sysDept.getDeptId().toString());
//            }
//            patMedReq.setDeptcodeList(deptCode);
//        }
        return patMedOuthospMapper.selectPatMedOuthospCount(patMedReq);
        //获取门诊病人信息,并统计人数和人次
        PatMedOuthospQueryReq req = new PatMedOuthospQueryReq();
        String deptcodes = CollectionUtils.isEmpty(patMedReq.getDeptcodeList()) ? null : String.join(",", patMedReq.getDeptcodeList());
        req.setBeginAdmitdate(patMedReq.getStartDate());
        req.setEndAdmitdate(patMedReq.getEndDate());
        req.setDeptcode(deptcodes);
        req.setOrgid(patMedReq.getOrgid());
//        req.setCampusid(patMedReq.getCampusid());
        Long count = patMedOuthospMapper.callSpQueryOuthospCount(req);
        //查询随访人次和人数
        PatMedRes patMedRes = serviceSubtaskMapper.selectVisitCount(patMedReq);
        patMedRes.setRc(count == null ? 0 : count.intValue());
        return patMedRes;
    }
    @Override
@@ -249,7 +337,7 @@
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(serviceTaskdeptList) || serviceTaskdeptList.size() == 0) {
                patMedOuthosp1.setDeptcheckFlag("2");
                patMedOuthosp1.setRemark("通过部门,没有找到门诊随访任务ID");
                patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp1);
                updatePatMedOuthosp(patMedOuthosp1);
            } else {
                for (ServiceTaskdept serviceTaskdept1 : serviceTaskdeptList) {
                    writeInSubTask(serviceTaskdept1.getTaskId(), true, patMedOuthosp1, patArchive, 1);
@@ -265,7 +353,7 @@
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(serviceTaskdiags) || serviceTaskdiags.size() == 0) {
                patMedOuthosp1.setDiagcheckFlag("2");
                patMedOuthosp1.setRemark("通过icd10,没有找到门诊随访任务ID");
                patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp1);
                updatePatMedOuthosp(patMedOuthosp1);
            } else {
                for (ServiceTaskdiag serviceTaskdept1 : serviceTaskdiags) {
                    writeInSubTask(serviceTaskdept1.getTaskId(), true, patMedOuthosp1, patArchive, 2);
@@ -294,7 +382,7 @@
            if (type == 1) patMedOuthosp.setDiagcheckFlag("2");
            if (type == 2) patMedOuthosp.setDeptcheckFlag("2");
            patMedOuthosp.setRemark("该患者门诊随访长期任务不存在,任务ID为:" + taskid);
            patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp);
            updatePatMedOuthosp(patMedOuthosp);
            return;
        }
        ServiceTask serviceTask = serviceTasks.get(0);
@@ -356,16 +444,17 @@
            patMedOuthosp1.setId(patMedOuthosp.getId());
            if (type == 1) patMedOuthosp1.setDiagcheckFlag("2");
            if (type == 2) patMedOuthosp1.setDeptcheckFlag("2");
            patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp1);
            updatePatMedOuthosp(patMedOuthosp1);
        } else {
            //生成子任务失败,
            log.info("生成子任务失败serviceSubtask的taskid为:{},patid为:{}", serviceSubtask.getTaskid(), serviceSubtask.getPatid());
            PatMedOuthosp pmo = new PatMedOuthosp();
            pmo.setId(patMedOuthosp.getId());
            pmo.setGuid(patMedOuthosp.getGuid());
            pmo.setSerialnum(patMedOuthosp.getSerialnum());
            if (type == 1) pmo.setDiagcheckFlag("2");
            if (type == 2) pmo.setDeptcheckFlag("2");
            pmo.setRemark("生成子任务失败");
            patMedOuthospMapper.updatePatMedOuthosp(pmo);
            updatePatMedOuthosp(pmo);
        }
    }