package com.smartor.service.mch;
|
|
import com.ruoyi.common.exception.base.BaseException;
|
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.MchFollowupTaskQuery;
|
import com.smartor.domain.MchFollowupTaskVO;
|
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;
|
|
/**
|
* 妇幼随访计划服务实现。
|
*
|
* <p>只处理丽水市妇幼保健院。计划生成不发送消息,发送由
|
* {@link IMchFollowupSendService} 独立完成。</p>
|
*/
|
@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;
|
}
|
// 档案必须经过人工保存/确认后才允许生成计划;接诊完成自动建档只是草稿。
|
if (!isFollowupActive(snapshot.followupStatus)) {
|
log.debug("【妇幼随访】档案未确认随访,跳过计划重建,archiveType={},archiveId={},detailId={},followupStatus={}",
|
snapshot.archiveType, snapshot.archiveId, snapshot.archiveDetailId, snapshot.followupStatus);
|
return 0;
|
}
|
|
List<MchFollowupPlan> actives = planMapper.selectActiveByArchive(
|
snapshot.archiveType, snapshot.archiveId, snapshot.archiveDetailId);
|
if (actives == null) {
|
actives = Collections.emptyList();
|
}
|
for (MchFollowupPlan active : actives) {
|
// 只有“有效”计划才做幂等判断;待补充计划即使日期/周期字符串相同,
|
// 也可能是因为之前配置非法而挂起,人工确认后必须重新解析并生成节点。
|
if (MchFollowupConstants.PLAN_STATUS_ACTIVE.equals(active.getPlanStatus())
|
&& 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<MchFollowupNode> 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<MchFollowupPlanItem> 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<MchFollowupArchiveCandidate> 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;
|
}
|
|
@Override
|
public int updateItemFollowupResult(MchFollowupPlanItem item) {
|
if (item == null || item.getId() == null) {
|
throw new BaseException("随访节点ID不能为空");
|
}
|
String followupStatus = item.getFollowupStatus();
|
if (StringUtils.isEmpty(followupStatus)) {
|
throw new BaseException("随访状态不能为空");
|
}
|
if (!isValidItemFollowupStatus(followupStatus)) {
|
throw new BaseException("不支持的随访状态:" + followupStatus);
|
}
|
MchFollowupPlanItem existing = itemMapper.selectMchFollowupPlanItemById(item.getId());
|
if (existing == null) {
|
throw new BaseException("随访节点不存在:" + item.getId());
|
}
|
item.setUpdateTime(DateUtils.getNowDate());
|
return itemMapper.updateFollowupResult(item);
|
}
|
|
@Override
|
public List<MchFollowupPlanItem> listItems(Long planId) {
|
if (planId == null) {
|
throw new BaseException("计划ID不能为空");
|
}
|
return itemMapper.selectByPlanId(planId);
|
}
|
|
@Override
|
public List<MchFollowupTaskVO> listTasks(MchFollowupTaskQuery query) {
|
if (query == null || StringUtils.isEmpty(query.getOrgid())) {
|
throw new BaseException("机构ID不能为空");
|
}
|
return itemMapper.selectTaskList(query);
|
}
|
|
private boolean isValidItemFollowupStatus(String status) {
|
return MchFollowupConstants.ITEM_FOLLOWUP_STATUS_PENDING.equals(status)
|
|| MchFollowupConstants.ITEM_FOLLOWUP_STATUS_DONE.equals(status)
|
|| MchFollowupConstants.ITEM_FOLLOWUP_STATUS_LOST.equals(status)
|
|| MchFollowupConstants.ITEM_FOLLOWUP_STATUS_REFUSED.equals(status)
|
|| MchFollowupConstants.ITEM_FOLLOWUP_STATUS_BAD_NUMBER.equals(status);
|
}
|
|
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<PatMaternityArchiveDetail> 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();
|
snapshot.followupStatus = detail.getFollowupStatus();
|
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<PatChildArchiveDetail> 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();
|
snapshot.followupStatus = detail.getFollowupStatus();
|
return snapshot;
|
}
|
|
/**
|
* 历史数据没有状态时按已确认处理,保证已有档案的计划不受影响。
|
*/
|
private boolean isFollowupActive(String followupStatus) {
|
return StringUtils.isEmpty(followupStatus)
|
|| MchFollowupConstants.FOLLOWUP_STATUS_ACTIVE.equals(followupStatus);
|
}
|
|
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;
|
private String followupStatus;
|
}
|
}
|