package com.smartor.service.mch;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.smartor.common.MchFollowupConstants;
import com.smartor.common.SpecificArchiveConstants;
import com.smartor.domain.MchFollowupArchiveCandidate;
import com.smartor.domain.MchFollowupNode;
import com.smartor.domain.MchFollowupPlan;
import com.smartor.domain.MchFollowupPlanItem;
import com.smartor.domain.PatChildArchiveDetail;
import com.smartor.domain.PatMaternityArchiveDetail;
import com.smartor.mapper.MchFollowupPlanItemMapper;
import com.smartor.mapper.MchFollowupPlanMapper;
import com.smartor.mapper.PatChildArchiveDetailMapper;
import com.smartor.mapper.PatMaternityArchiveDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* 妇幼随访计划服务实现。
*
*
只处理丽水市妇幼保健院。计划生成不发送消息,发送由
* {@link IMchFollowupSendService} 独立完成。
*/
@Slf4j
@Service
public class MchFollowupPlanServiceImpl implements IMchFollowupPlanService {
@Autowired
private MchFollowupPlanMapper planMapper;
@Autowired
private MchFollowupPlanItemMapper itemMapper;
@Autowired
private PatMaternityArchiveDetailMapper maternityDetailMapper;
@Autowired
private PatChildArchiveDetailMapper childDetailMapper;
@Autowired
private MchFollowupPeriodParser periodParser;
@Autowired
private MchFollowupPlanAssembler planAssembler;
@Autowired
private ISysConfigService configService;
@Override
@Transactional(rollbackFor = Exception.class)
public int rebuildForArchive(String archiveType, Long archiveId, Long archiveDetailId) {
try {
return doRebuildForArchive(archiveType, archiveId, archiveDetailId);
} catch (Exception e) {
// 计划生成失败不能影响档案主流程,异常在事务边界内消化
log.error("【妇幼随访】计划重建异常,archiveType={},archiveId={},detailId={}",
archiveType, archiveId, archiveDetailId, e);
return 0;
}
}
private int doRebuildForArchive(String archiveType, Long archiveId, Long archiveDetailId) {
if (!isEnabled()) {
return 0;
}
ArchiveSnapshot snapshot = loadSnapshot(archiveType, archiveId, archiveDetailId);
if (snapshot == null) {
return 0;
}
if (snapshot.archiveId == null || snapshot.archiveDetailId == null) {
log.warn("【妇幼随访】档案主键或详情主键为空,跳过计划重建,archiveType={},archiveId={},detailId={}",
archiveType, archiveId, archiveDetailId);
return 0;
}
if (!SpecificArchiveConstants.LISHUI_MCH_ORG_ID.equals(snapshot.orgid)) {
return 0;
}
List actives = planMapper.selectActiveByArchive(
snapshot.archiveType, snapshot.archiveId, snapshot.archiveDetailId);
if (actives == null) {
actives = Collections.emptyList();
}
for (MchFollowupPlan active : actives) {
if (sameTime(active.getPlannedFollowTime(), snapshot.plannedFollowTime)
&& StringUtils.equals(active.getFollowupPeriod(), snapshot.followupPeriod)) {
MchFollowupPlan contactUpdate = new MchFollowupPlan();
contactUpdate.setId(active.getId());
contactUpdate.setCampusid(snapshot.campusid);
contactUpdate.setSourceBizNo(snapshot.sourceBizNo);
contactUpdate.setPatname(snapshot.patname);
contactUpdate.setPhone(snapshot.phone);
contactUpdate.setIdcardno(snapshot.idcardno);
contactUpdate.setUpdateTime(DateUtils.getNowDate());
planMapper.updateMchFollowupPlan(contactUpdate);
log.debug("【妇幼随访】计划未变化,仅刷新联系人信息,archiveType={},archiveId={},detailId={}",
snapshot.archiveType, snapshot.archiveId, snapshot.archiveDetailId);
return 0;
}
}
Date now = DateUtils.getNowDate();
for (MchFollowupPlan active : actives) {
itemMapper.cancelPendingByPlanId(active.getId(), now);
planMapper.cancelById(active.getId(), now);
}
MchFollowupPlan plan = new MchFollowupPlan();
plan.setOrgid(snapshot.orgid);
plan.setCampusid(snapshot.campusid);
plan.setArchiveType(snapshot.archiveType);
plan.setArchiveId(snapshot.archiveId);
plan.setArchiveDetailId(snapshot.archiveDetailId);
plan.setSourceType(snapshot.sourceType);
plan.setSourceBizNo(snapshot.sourceBizNo);
plan.setPatid(snapshot.patid);
plan.setPatname(snapshot.patname);
plan.setPhone(snapshot.phone);
plan.setIdcardno(snapshot.idcardno);
plan.setPlannedFollowTime(snapshot.plannedFollowTime);
plan.setFollowupPeriod(snapshot.followupPeriod);
plan.setCreateTime(now);
plan.setUpdateTime(now);
if (snapshot.plannedFollowTime == null) {
plan.setPlanStatus(MchFollowupConstants.PLAN_STATUS_WAITING);
plan.setRemark("拟随访时间为空,待补充");
planMapper.insertMchFollowupPlan(plan);
log.warn("【妇幼随访】拟随访时间为空,生成待补充计划,archiveType={},detailId={}",
snapshot.archiveType, snapshot.archiveDetailId);
return 0;
}
List nodes;
try {
nodes = periodParser.parse(snapshot.followupPeriod, snapshot.followupCycleTimes);
} catch (Exception e) {
plan.setPlanStatus(MchFollowupConstants.PLAN_STATUS_WAITING);
plan.setRemark("随访周期JSON解析失败:" + truncate(e.getMessage(), 200));
planMapper.insertMchFollowupPlan(plan);
log.error("【妇幼随访】随访周期解析失败,archiveType={},detailId={},period={}",
snapshot.archiveType, snapshot.archiveDetailId, snapshot.followupPeriod, e);
return 0;
}
if (nodes.isEmpty()) {
plan.setPlanStatus(MchFollowupConstants.PLAN_STATUS_WAITING);
plan.setRemark("随访周期未配置发送节点");
planMapper.insertMchFollowupPlan(plan);
return 0;
}
plan.setPlanStatus(MchFollowupConstants.PLAN_STATUS_ACTIVE);
planMapper.insertMchFollowupPlan(plan);
List items = planAssembler.assemble(plan.getId(), snapshot.plannedFollowTime, nodes);
for (MchFollowupPlanItem item : items) {
itemMapper.insertMchFollowupPlanItem(item);
}
log.info("【妇幼随访】计划重建完成,archiveType={},archiveId={},detailId={},节点数={}",
snapshot.archiveType, snapshot.archiveId, snapshot.archiveDetailId, items.size());
return items.size();
}
@Override
public int rebuildAll(int limit) {
if (!isEnabled()) {
return 0;
}
int batch = limit <= 0 ? MchFollowupConstants.DEFAULT_BATCH_SIZE : limit;
List candidates =
planMapper.selectCandidates(SpecificArchiveConstants.LISHUI_MCH_ORG_ID, batch);
if (candidates == null || candidates.isEmpty()) {
return 0;
}
int generated = 0;
for (MchFollowupArchiveCandidate candidate : candidates) {
try {
generated += rebuildForArchive(candidate.getArchiveType(),
candidate.getArchiveId(), candidate.getArchiveDetailId());
} catch (Exception e) {
log.error("【妇幼随访】补建计划失败,archiveType={},archiveId={},detailId={}",
candidate.getArchiveType(), candidate.getArchiveId(),
candidate.getArchiveDetailId(), e);
}
}
return generated;
}
private ArchiveSnapshot loadSnapshot(String archiveType, Long archiveId, Long archiveDetailId) {
if (MchFollowupConstants.ARCHIVE_TYPE_MATERNITY.equals(archiveType)) {
return loadMaternitySnapshot(archiveId, archiveDetailId);
}
if (MchFollowupConstants.ARCHIVE_TYPE_CHILD.equals(archiveType)) {
return loadChildSnapshot(archiveId, archiveDetailId);
}
log.warn("【妇幼随访】未知档案类型:{}", archiveType);
return null;
}
private ArchiveSnapshot loadMaternitySnapshot(Long archiveId, Long archiveDetailId) {
PatMaternityArchiveDetail detail = null;
if (archiveDetailId != null) {
detail = maternityDetailMapper.selectPatMaternityArchiveDetailById(archiveDetailId);
}
if (detail == null && archiveId != null) {
PatMaternityArchiveDetail query = new PatMaternityArchiveDetail();
query.setArchiveid(archiveId);
List list =
maternityDetailMapper.selectPatMaternityArchiveDetailList(query);
if (list != null && !list.isEmpty()) {
detail = list.get(0);
}
}
if (detail == null) {
return null;
}
ArchiveSnapshot snapshot = new ArchiveSnapshot();
snapshot.archiveType = MchFollowupConstants.ARCHIVE_TYPE_MATERNITY;
snapshot.archiveId = detail.getArchiveid() == null ? archiveId : detail.getArchiveid();
snapshot.archiveDetailId = detail.getId();
snapshot.orgid = detail.getOrgid();
snapshot.campusid = detail.getCampusid();
snapshot.sourceType = detail.getSourceType();
snapshot.sourceBizNo = detail.getSourceBizNo();
snapshot.patid = detail.getPatid();
snapshot.patname = detail.getPatname();
snapshot.phone = detail.getPhone();
snapshot.idcardno = detail.getIdcardno();
snapshot.plannedFollowTime = detail.getPlannedFollowTime();
snapshot.followupPeriod = detail.getFollowupPeriod();
snapshot.followupCycleTimes = detail.getFollowupCycleTimes();
return snapshot;
}
private ArchiveSnapshot loadChildSnapshot(Long archiveId, Long archiveDetailId) {
PatChildArchiveDetail detail = null;
if (archiveDetailId != null) {
detail = childDetailMapper.selectPatChildArchiveDetailById(archiveDetailId);
}
if (detail == null && archiveId != null) {
PatChildArchiveDetail query = new PatChildArchiveDetail();
query.setArchiveid(archiveId);
List list =
childDetailMapper.selectPatChildArchiveDetailList(query);
if (list != null && !list.isEmpty()) {
detail = list.get(0);
}
}
if (detail == null) {
return null;
}
ArchiveSnapshot snapshot = new ArchiveSnapshot();
snapshot.archiveType = MchFollowupConstants.ARCHIVE_TYPE_CHILD;
snapshot.archiveId = detail.getArchiveid() == null ? archiveId : detail.getArchiveid();
snapshot.archiveDetailId = detail.getId();
snapshot.orgid = detail.getOrgid();
snapshot.campusid = detail.getCampusid();
snapshot.sourceType = detail.getSourceType();
snapshot.sourceBizNo = detail.getSourceBizNo();
snapshot.patid = detail.getPatid();
snapshot.patname = detail.getPatname();
snapshot.phone = firstNotBlank(detail.getParentPhone(), detail.getPhone());
snapshot.idcardno = detail.getIdcardno();
snapshot.plannedFollowTime = detail.getPlannedFollowTime();
snapshot.followupPeriod = detail.getFollowupPeriod();
return snapshot;
}
private boolean isEnabled() {
try {
String value = configService.selectConfigByKey(MchFollowupConstants.CONFIG_ENABLED);
return value == null || !"false".equalsIgnoreCase(value.trim());
} catch (Exception e) {
log.warn("【妇幼随访】读取启用开关失败,按启用处理:{}", e.getMessage());
return true;
}
}
private static boolean sameTime(Date left, Date right) {
if (left == null || right == null) {
return left == right;
}
return Math.abs(left.getTime() - right.getTime()) < 1000L;
}
private static String firstNotBlank(String first, String second) {
return StringUtils.isNotBlank(first) ? first : second;
}
private static String truncate(String text, int maxLength) {
if (text == null || text.length() <= maxLength) {
return text;
}
return text.substring(0, maxLength);
}
private static class ArchiveSnapshot {
private String archiveType;
private Long archiveId;
private Long archiveDetailId;
private String orgid;
private String campusid;
private String sourceType;
private String sourceBizNo;
private Long patid;
private String patname;
private String phone;
private String idcardno;
private Date plannedFollowTime;
private String followupPeriod;
private String followupCycleTimes;
}
}