陈昶聿
3 天以前 3acf9415fce2dd21c0fd5d67805b72a8bde24a81
smartor/src/main/java/com/smartor/service/impl/PatChildArchiveServiceImpl.java
@@ -6,9 +6,11 @@
import com.ruoyi.common.utils.StringUtils;
import com.smartor.domain.PatChildArchive;
import com.smartor.domain.PatChildArchiveDetail;
import com.smartor.common.MchFollowupConstants;
import com.smartor.mapper.PatChildArchiveMapper;
import com.smartor.service.IPatChildArchiveDetailService;
import com.smartor.service.IPatChildArchiveService;
import com.smartor.service.mch.IMchFollowupPlanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,6 +34,9 @@
    @Autowired
    private IPatChildArchiveDetailService patChildArchiveDetailService;
    @Autowired
    private IMchFollowupPlanService mchFollowupPlanService;
    @Override
    public PatChildArchive selectPatChildArchiveById(Long id)
@@ -84,6 +89,13 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateChildArchive(PatChildArchiveDetail patChildArchiveDetail) {
        return insertOrUpdateChildArchive(patChildArchiveDetail, true);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateChildArchive(PatChildArchiveDetail patChildArchiveDetail,
                                          boolean confirmFollowup) {
        if (patChildArchiveDetail == null) {
            log.error("insertOrUpdateChildArchive入参为空,请检查入参");
            throw new BaseException("儿科档案入参为空,请检查入参");
@@ -108,10 +120,15 @@
        Long existArchiveId = null;
        Long existDetailId = null;
        PatChildArchiveDetail existingDetail = null;
        if (CollectionUtils.isNotEmpty(olds)) {
            existArchiveId = olds.get(0).getArchiveid();
            existDetailId = olds.get(0).getId();
            existingDetail = olds.get(0);
            existArchiveId = existingDetail.getArchiveid();
            existDetailId = existingDetail.getId();
        }
        // 人工保存/确认时进入随访中;HIS 自动建档只写待确认,不生成随访计划
        applyFollowupState(patChildArchiveDetail, existingDetail, confirmFollowup);
        // 详情没查到,再兜底按同样键查主表,避免主表重复
        if (existArchiveId == null) {
@@ -191,7 +208,72 @@
            patChildArchiveDetailService.insertPatChildArchiveDetail(patChildArchiveDetail);
        }
        log.info("insertOrUpdateChildArchive处理完成,主表id:{},详情id:{}", patChildArchive.getId(), patChildArchiveDetail.getId());
        // 妇幼随访计划:只有人工确认(followup_status=1)的档案才生成/重建;
        // 自动建档为待确认,rebuildForArchive 内部会直接跳过。
        try {
            if (mchFollowupPlanService != null) {
                mchFollowupPlanService.rebuildForArchive(
                        MchFollowupConstants.ARCHIVE_TYPE_CHILD,
                        patChildArchive.getId(), patChildArchiveDetail.getId());
            }
        } catch (Exception e) {
            log.error("妇幼随访计划重建失败,archiveId={},detailId={}",
                    patChildArchive.getId(), patChildArchiveDetail.getId(), e);
        }
        log.info("insertOrUpdateChildArchive处理完成,主表id:{},详情id:{},随访状态:{}",
                patChildArchive.getId(), patChildArchiveDetail.getId(),
                patChildArchiveDetail.getFollowupStatus());
        return patChildArchive.getId().intValue();
    }
    /**
     * 处理档案随访状态。
     *
     * <p>人工保存/确认:档案进入随访中,并记录首次确认时间/确认人;
     * HIS 自动建档:新档案进入待确认,更新时不改动已有状态,也不覆盖人工维护的
     * plannedFollowTime / followupPeriod。</p>
     */
    private void applyFollowupState(PatChildArchiveDetail detail,
                                    PatChildArchiveDetail existing,
                                    boolean confirmFollowup) {
        if (confirmFollowup) {
            boolean alreadyActive = existing != null
                    && MchFollowupConstants.FOLLOWUP_STATUS_ACTIVE.equals(existing.getFollowupStatus())
                    && existing.getFollowupConfirmTime() != null;
            detail.setFollowupStatus(MchFollowupConstants.FOLLOWUP_STATUS_ACTIVE);
            if (alreadyActive) {
                // 已确认过的档案重复保存时,保留首次确认时间/确认人
                detail.setFollowupConfirmTime(null);
                detail.setFollowupConfirmBy(null);
            } else {
                detail.setFollowupConfirmTime(DateUtils.getNowDate());
                String confirmBy = detail.getUpdateBy();
                if (StringUtils.isEmpty(confirmBy)) {
                    confirmBy = detail.getCreateBy();
                }
                detail.setFollowupConfirmBy(confirmBy);
            }
            return;
        }
        // HIS 自动建档:新增只进入待确认,不生成计划
        if (existing == null) {
            detail.setFollowupStatus(MchFollowupConstants.FOLLOWUP_STATUS_PENDING);
            detail.setFollowupConfirmTime(null);
            detail.setFollowupConfirmBy(null);
            return;
        }
        // 更新时保持原状态,避免 HIS 重复推送把已确认档案退回待确认
        detail.setFollowupStatus(null);
        detail.setFollowupConfirmTime(null);
        detail.setFollowupConfirmBy(null);
        // 已确认/历史已生效档案的随访日期与周期由人工维护,HIS 同步不覆盖
        if (StringUtils.isEmpty(existing.getFollowupStatus())
                || MchFollowupConstants.FOLLOWUP_STATUS_ACTIVE.equals(existing.getFollowupStatus())) {
            detail.setPlannedFollowTime(null);
            detail.setFollowupPeriod(null);
        }
    }
}