package jnpf.dmsService.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import jnpf.dmsEntity.DmsDanganCundangShenqingDanganheEntity; import jnpf.dmsEntity.DmsDanganXiaohuiShenqingEntity; import jnpf.dmsEntity.DmsDanganXiaohuiShenqingZixiangEntity; import jnpf.dmsMapper.DmsDanganCundangShenqingDanganheMapper; import jnpf.dmsMapper.DmsDanganXiaohuiShenqingMapper; import jnpf.dmsMapper.DmsDanganXiaohuiShenqingZixiangMapper; import jnpf.dmsService.DmsDanganXiaohuiShenqingTongguoService; import jnpf.exception.DataException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @Service @RequiredArgsConstructor public class DmsDanganXiaohuiShenqingTongguoServiceImpl implements DmsDanganXiaohuiShenqingTongguoService { private final DmsDanganXiaohuiShenqingMapper shenqingMapper; private final DmsDanganXiaohuiShenqingZixiangMapper zixiangMapper; private final DmsDanganCundangShenqingDanganheMapper danganheMapper; @Override @Transactional(rollbackFor = Exception.class) public void tongguo(String flowId, String taskId) throws DataException { String resolvedFlowId = requireValue(flowId, "流程ID(flow_id)"); String resolvedTaskId = requireValue(taskId, "流程任务ID(task_id)"); DmsDanganXiaohuiShenqingEntity shenqing = findApplication(resolvedFlowId, resolvedTaskId); Set danganheIds = findDanganheIds(shenqing.getId()); List danganheList = findSourceDanganhe(danganheIds); ensureAssignable(danganheList, shenqing.getId()); LambdaUpdateWrapper update = new LambdaUpdateWrapper<>(); update.in(DmsDanganCundangShenqingDanganheEntity::getId, danganheIds) .isNull(DmsDanganCundangShenqingDanganheEntity::getDeleteMark) .and(wrapper -> wrapper.isNull(DmsDanganCundangShenqingDanganheEntity::getXiaohuiId) .or() .eq(DmsDanganCundangShenqingDanganheEntity::getXiaohuiId, shenqing.getId())) .set(DmsDanganCundangShenqingDanganheEntity::getXiaohuiId, shenqing.getId()); if (danganheMapper.update(null, update) != danganheIds.size()) { throw new DataException("档案盒销毁关联发生并发变更,请重新处理"); } } private DmsDanganXiaohuiShenqingEntity findApplication(String flowId, String taskId) throws DataException { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(DmsDanganXiaohuiShenqingEntity::getFlowId, flowId) .eq(DmsDanganXiaohuiShenqingEntity::getFlowTaskId, taskId) .isNull(DmsDanganXiaohuiShenqingEntity::getDeleteMark); List applications = shenqingMapper.selectList(query); if (applications.isEmpty()) { throw new DataException("未找到档案销毁申请:flow_id=" + flowId + ", task_id=" + taskId); } if (applications.size() > 1) { throw new DataException("flow_id与task_id关联了多条档案销毁申请,无法确认通过记录"); } return applications.get(0); } private Set findDanganheIds(String shenqingId) throws DataException { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(DmsDanganXiaohuiShenqingZixiangEntity::getForeignId, shenqingId) .isNull(DmsDanganXiaohuiShenqingZixiangEntity::getDeleteMark); List details = zixiangMapper.selectList(query); if (details.isEmpty()) { throw new DataException("档案销毁申请未关联档案盒明细,无法通过"); } Set ids = new LinkedHashSet<>(); for (DmsDanganXiaohuiShenqingZixiangEntity detail : details) { String danganheId = trimToEmpty(detail.getDanganheId()); if (danganheId.isEmpty()) { throw new DataException("档案销毁申请明细缺少档案盒ID(danganhe_id)"); } if (!ids.add(danganheId)) { throw new DataException("档案销毁申请明细包含重复档案盒ID(danganhe_id)"); } } return ids; } private List findSourceDanganhe(Set danganheIds) throws DataException { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.in(DmsDanganCundangShenqingDanganheEntity::getId, danganheIds) .isNull(DmsDanganCundangShenqingDanganheEntity::getDeleteMark); List danganheList = danganheMapper.selectList(query); if (danganheList.size() != danganheIds.size()) { throw new DataException("存在未找到或已删除的档案盒,无法通过销毁申请"); } return danganheList; } private void ensureAssignable(List danganheList, String shenqingId) throws DataException { for (DmsDanganCundangShenqingDanganheEntity danganhe : danganheList) { String currentXiaohuiId = trimToEmpty(danganhe.getXiaohuiId()); if (!currentXiaohuiId.isEmpty() && !shenqingId.equals(currentXiaohuiId)) { throw new DataException("档案盒已关联其他销毁申请,无法重复通过"); } } } private String requireValue(String value, String fieldName) throws DataException { String resolved = trimToEmpty(value); if (resolved.isEmpty()) { throw new DataException(fieldName + "不能为空"); } return resolved; } private String trimToEmpty(String value) { return value == null ? "" : value.trim(); } }