陈昶聿
3 天以前 a2101aa52a00ae103a26a950b54f985f46769239
smartor/src/main/java/com/smartor/service/impl/PatMaternityArchiveServiceImpl.java
@@ -13,7 +13,7 @@
import com.smartor.mapper.PatMaternityArchiveMapper;
import com.smartor.service.IPatMaternityArchiveDetailService;
import com.smartor.service.IPatMaternityArchiveService;
import com.smartor.service.mch.IMchFollowupPlanService;
import com.smartor.service.mch.IMchFollowupSubtaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -41,7 +41,7 @@
    private PatMaternityArchiveDetailMapper patMaternityArchiveDetailMapper;
    @Autowired
    private IMchFollowupPlanService mchFollowupPlanService;
    private IMchFollowupSubtaskService mchFollowupSubtaskService;
    @Override
    public PatMaternityArchive selectPatMaternityArchiveById(Long id)
@@ -94,6 +94,13 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateMaternityArchive(PatMaternityArchiveDetail patMaternityArchiveDetail) {
        return insertOrUpdateMaternityArchive(patMaternityArchiveDetail, true);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateMaternityArchive(PatMaternityArchiveDetail patMaternityArchiveDetail,
                                              boolean confirmFollowup) {
        if (patMaternityArchiveDetail == null) {
            log.error("insertOrUpdateMaternityArchive入参为空,请检查入参");
            throw new BaseException("妇科档案入参为空,请检查入参");
@@ -130,10 +137,15 @@
        Long existArchiveId = null;
        Long existDetailId = null;
        PatMaternityArchiveDetail 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(patMaternityArchiveDetail, existingDetail, confirmFollowup);
        // 详情没查到,再兜底按同样键查主表,避免主表重复
        if (existArchiveId == null) {
@@ -220,19 +232,73 @@
            patMaternityArchiveDetailService.insertPatMaternityArchiveDetail(patMaternityArchiveDetail);
        }
        // 妇幼随访计划:档案新增/修改后按 plannedFollowTime + followupPeriod 重建
        // 妇幼随访任务:只有人工确认(followup_status=1)的档案才生成/重建 service_subtask;
        // 自动建档为待确认,rebuildForArchive 内部会直接跳过。
        try {
            if (mchFollowupPlanService != null) {
                mchFollowupPlanService.rebuildForArchive(
            if (mchFollowupSubtaskService != null) {
                mchFollowupSubtaskService.rebuildForArchive(
                        MchFollowupConstants.ARCHIVE_TYPE_MATERNITY,
                        patMaternityArchive.getId(), patMaternityArchiveDetail.getId());
            }
        } catch (Exception e) {
            log.error("妇幼随访计划重建失败,archiveId={},detailId={}",
            log.error("妇幼随访任务生成失败,archiveId={},detailId={}",
                    patMaternityArchive.getId(), patMaternityArchiveDetail.getId(), e);
        }
        log.info("insertOrUpdateMaternityArchive处理完成,主表id:{},详情id:{}", patMaternityArchive.getId(), patMaternityArchiveDetail.getId());
        log.info("insertOrUpdateMaternityArchive处理完成,主表id:{},详情id:{},随访状态:{}",
                patMaternityArchive.getId(), patMaternityArchiveDetail.getId(),
                patMaternityArchiveDetail.getFollowupStatus());
        return patMaternityArchive.getId().intValue();
    }
    /**
     * 处理档案随访状态。
     *
     * <p>人工保存/确认:档案进入随访中,并记录首次确认时间/确认人;
     * HIS 自动建档:新档案进入待确认,更新时不改动已有状态,也不覆盖人工维护的
     * plannedFollowTime / followupPeriod / followupCycleTimes。</p>
     */
    private void applyFollowupState(PatMaternityArchiveDetail detail,
                                    PatMaternityArchiveDetail 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);
            detail.setFollowupCycleTimes(null);
        }
    }
}