package jnpf.limsService.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import jnpf.audit.sdk.annotation.AuditLog; import jnpf.base.ActionResult; import jnpf.base.BillRuleApi; import jnpf.base.UserInfo; import jnpf.exception.DataException; import jnpf.limsEntity.*; import jnpf.limsMapper.*; import jnpf.limsService.LimsWendingxingKaocaJihuaZhouqiService; import jnpf.limsService.LimsQingyandanService; import jnpf.limsService.ZhiliangBiaozhunGuanliService; import jnpf.util.RandomUtil; import jnpf.util.StringUtil; import jnpf.util.UserProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.time.LocalDate; import java.time.ZoneId; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @Service public class LimsWendingxingKaocaJihuaZhouqiServiceImpl implements LimsWendingxingKaocaJihuaZhouqiService { private static final String JIANYAN_LIUSHUIHAO_RULE = "jianyan_liushuihao"; @Autowired private LimsWendingxingKaocaJihuaBiangengMapper biangengMapper; @Autowired private LimsWendingxingKaocaJihuaBiangengZhouqiMapper biangengZhouqiMapper; @Autowired private LimsWendingxingKaocaJihuaZhouqiMapper jihuaZhouqiMapper; @Autowired private LimsQingyandanService qingyandanService; @Autowired private ZhiliangBiaozhunGuanliService zhiliangBiaozhunGuanliService; @Autowired private LimsWendingxingKaocaJihuaMapper jihuaMapper; @Autowired private YangpinGuanliMapper yangpinGuanliMapper; @Autowired private BillRuleApi billRuleApi; @Override @Transactional(rollbackFor = Exception.class) @AuditLog(action = "EXECUTE", label = "稳定性考察计划检测执行", bizType = "WENDINGXING", bizCodeExpr = "#jihuaZhouqiId", bizCodeRequired = false, targetTable = "lims_wendingxing_kaoca_jihua_zhouqi", targetIdExpr = "#jihuaZhouqiId", diff = true, entityClass = LimsWendingxingKaocaJihuaZhouqiEntity.class) public String executeJiance(String jihuaZhouqiId, String zhixingBizSign) { UserInfo userInfo = UserProvider.getUser(); if (userInfo == null || !StringUtils.hasText(userInfo.getUserId())) { throw new DataException("当前未登录"); } Date now = new Date(); LambdaUpdateWrapper update = new LambdaUpdateWrapper<>(); update.eq(LimsWendingxingKaocaJihuaZhouqiEntity::getId, jihuaZhouqiId) .and(wrapper -> wrapper.isNull(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark) .or().eq(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, 0)) .and(wrapper -> wrapper.isNull(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouYizhixing) .or().ne(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouYizhixing, "yes")) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouYizhixing, "yes") .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhixingBizSign, zhixingBizSign) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhixingShijian, now); int updated = jihuaZhouqiMapper.update(null, update); if (updated == 0) { LambdaQueryWrapper existing = new LambdaQueryWrapper<>(); existing.eq(LimsWendingxingKaocaJihuaZhouqiEntity::getId, jihuaZhouqiId) .and(wrapper -> wrapper .isNull(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark) .or().eq(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, 0)); if (jihuaZhouqiMapper.selectCount(existing) > 0) { throw new DataException("稳定性考察计划周期已执行,请勿重复操作"); } throw new DataException("稳定性考察计划周期不存在"); } LimsWendingxingJianceZhixingVo jihuaZhouqi = jihuaZhouqiMapper.selectJianceZhixing(jihuaZhouqiId); if (jihuaZhouqi == null) { throw new DataException("稳定性样品还未取样"); } QueryWrapper query = new QueryWrapper<>(); query.eq("f_id", jihuaZhouqi.getKaocaJihuaId()); query.and(w -> w.isNull("f_delete_mark").or().ne("f_delete_mark", 1)); LimsWendingxingKaocaJihuaEntity jihuaEntity = jihuaMapper.selectOne(query); if (jihuaEntity == null) { throw new DataException("稳定性计划不存在"); } ZhiliangBiaozhunGuanliEntity zhiliangBiaozhun = zhiliangBiaozhunGuanliService.getById(jihuaZhouqi.getZhiliangBiaozhunId()); if (zhiliangBiaozhun == null) { throw new DataException("稳定性样品关联的质量标准不存在"); } QueryWrapper yangpinQuery = new QueryWrapper<>(); yangpinQuery.eq("f_id", zhiliangBiaozhun.getYangpinId()); yangpinQuery.and(w -> w.isNull("f_delete_mark").or().ne("f_delete_mark", 1)); YangpinGuanliEntity yangpinEntity = yangpinGuanliMapper.selectOne(yangpinQuery); if (yangpinEntity == null) { throw new DataException("样品不存在"); } LimsQingyandanEntity qingyandan = buildQingyandan(jihuaZhouqi, userInfo, zhiliangBiaozhun, jihuaEntity, yangpinEntity); if (!qingyandanService.save(qingyandan)) { throw new DataException("稳定性请验单生成失败"); } qingyandanService.wendingxingSubmit(qingyandan); return qingyandan.getId(); } private LimsQingyandanEntity buildQingyandan(LimsWendingxingJianceZhixingVo jihuaZhouqi, UserInfo userInfo, ZhiliangBiaozhunGuanliEntity zhiliangBiaozhun, LimsWendingxingKaocaJihuaEntity jihuaEntity, YangpinGuanliEntity yangpinEntity) { LimsQingyandanEntity qingyandan = new LimsQingyandanEntity(); qingyandan.setId(RandomUtil.uuId()); qingyandan.setJianyanLiushuihao(nextJianyanLiushuihao()); qingyandan.setBizType(jihuaZhouqi.getLeixin()); qingyandan.setChanpinMingcheng(jihuaZhouqi.getYangpinMingcheng()); qingyandan.setChanpinId(jihuaZhouqi.getYangpinId()); qingyandan.setChanpinBianma(jihuaZhouqi.getYangpinBianhao()); qingyandan.setGuige(jihuaZhouqi.getGuige()); qingyandan.setChucunTiaojian(yangpinEntity.getCucunTiaojian()); qingyandan.setPihao(jihuaZhouqi.getPihao()); qingyandan.setJianceXiangmu(jihuaZhouqi.getJianchaXiangmuIds()); qingyandan.setQuyangren(jihuaZhouqi.getQuyangren()); qingyandan.setQuyangRiqi(jihuaZhouqi.getQuyangRiqi()); qingyandan.setQingyanRen(userInfo.getUserId()); qingyandan.setQingyanRiqi(Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant())); qingyandan.setJianyanLiang(jihuaZhouqi.getShiquShuliang()); qingyandan.setJianyanLiangShiji(jihuaZhouqi.getShiquShuliang()); qingyandan.setJianceLiuyangLiang("0"); qingyandan.setJianceLiuyangLiangShiji("0"); qingyandan.setLiuyangLiang("0"); qingyandan.setLiuyangLiangShiji("0"); qingyandan.setQuyangDanwei(jihuaZhouqi.getYangpinDanwei()); qingyandan.setLiuyangDanwei(jihuaZhouqi.getYangpinDanwei()); qingyandan.setZhouqi(jihuaZhouqi.getZhouqi().toPlainString()); qingyandan.setZhouqiDanwei(jihuaZhouqi.getZhouqiDanwei()); qingyandan.setQingyanFenlei(QingyanFenlei.WENDINGXING.getCode()); qingyandan.setWendingxingJiluId(jihuaZhouqi.getKaocaJihuaId()); qingyandan.setQuyangStatus(BizStatus.FETCHED.getCode()); qingyandan.setBizStatus(BizStatus.CONFIRMED.getCode()); qingyandan.setJieyangStatus(BizStatus.PENDING_RECEIVE.getCode()); qingyandan.setJianceFangshi(JianceFangshi.QUANJIAN.getCode()); qingyandan.setJianyanBiaozhun(jihuaZhouqi.getZhiliangBiaozhunId()); qingyandan.setShifouShixiaoxingYaoqiu(zhiliangBiaozhun.getShifouShixiaoxingYaoqiu()); qingyandan.setCreatorUserId(userInfo.getUserId()); qingyandan.setCreatorTime(new Date()); qingyandan.setWendingxingZhouqiId(jihuaZhouqi.getId()); qingyandan.setWendingxingBianhao(jihuaEntity.getBianhao()); qingyandan.setChangqiFangzhiTiaojian(jihuaZhouqi.getChangqiFangzhiTiaojian()); return qingyandan; } private String nextJianyanLiushuihao() { ActionResult result = billRuleApi.getBillNumber(JIANYAN_LIUSHUIHAO_RULE); String liushuihao = result == null ? null : result.getData(); if (StringUtil.isEmpty(liushuihao)) { throw new DataException("检验流水号取号失败:单据规则[" + JIANYAN_LIUSHUIHAO_RULE + "]不可用,请确认规则已配置且系统服务在线"); } return liushuihao; } @Override @Transactional(rollbackFor = Exception.class) @AuditLog(action = "PLAN_CHANGE_SYNC", label = "同步稳定性考察计划变更周期", bizType = "WENDINGXING", bizCodeExpr = "#result > 0 ? #flowId : ''", targetTable = "lims_wendingxing_kaoca_jihua_zhouqi", targetIdExpr = "T(jnpf.limsService.support.LimsAuditTargetIds).firstWendingxingChangedPeriod(#flowId, #flowTaskId)", diff = true, entityClass = LimsWendingxingKaocaJihuaZhouqiEntity.class) public int syncByFlow(String flowId, String flowTaskId) { LambdaQueryWrapper biangengQuery = new LambdaQueryWrapper<>(); biangengQuery.eq(LimsWendingxingKaocaJihuaBiangengEntity::getFlowId, flowId) .eq(LimsWendingxingKaocaJihuaBiangengEntity::getFlowTaskId, flowTaskId); LimsWendingxingKaocaJihuaBiangengEntity biangeng = biangengMapper.selectOne(biangengQuery); if (biangeng == null) { throw new DataException("未找到对应的稳定性考察计划变更数据"); } String biangengId = biangeng.getId(); LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(LimsWendingxingKaocaJihuaBiangengZhouqiEntity::getBiangengId, biangengId) .isNull(LimsWendingxingKaocaJihuaBiangengZhouqiEntity::getDeleteMark); List biangengZhouqiList = biangengZhouqiMapper.selectList(query); if (biangengZhouqiList.isEmpty()) { throw new DataException("未找到对应的稳定性考察计划变更周期数据"); } UserInfo userInfo = UserProvider.getUser(); if (userInfo == null || !StringUtils.hasText(userInfo.getUserId())) { throw new DataException("当前未登录"); } Date now = new Date(); Set kaocaJihuaIds = new LinkedHashSet<>(); for (LimsWendingxingKaocaJihuaBiangengZhouqiEntity row : biangengZhouqiList) { if (!StringUtils.hasText(row.getKaocaJihuaId())) { throw new DataException("变更周期未关联稳定性考察计划:" + row.getId()); } kaocaJihuaIds.add(row.getKaocaJihuaId()); } softDeleteJihuaZhouqi(kaocaJihuaIds, userInfo.getUserId(), now); List insertList = new ArrayList<>(); for (LimsWendingxingKaocaJihuaBiangengZhouqiEntity row : biangengZhouqiList) { if (StringUtils.hasText(row.getYuanjihuaZhouqiId())) { restoreAndUpdateJihuaZhouqi(row); } else { insertList.add(row); } } insertJihuaZhouqi(insertList); return biangengZhouqiList.size(); } private void softDeleteJihuaZhouqi(Set kaocaJihuaIds, String userId, Date now) { LambdaUpdateWrapper update = new LambdaUpdateWrapper<>(); update.in(LimsWendingxingKaocaJihuaZhouqiEntity::getKaocaJihuaId, kaocaJihuaIds) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, 1) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteTime, now) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteUserId, userId); jihuaZhouqiMapper.update(null, update); } private void restoreAndUpdateJihuaZhouqi(LimsWendingxingKaocaJihuaBiangengZhouqiEntity source) { LambdaUpdateWrapper update = new LambdaUpdateWrapper<>(); update.eq(LimsWendingxingKaocaJihuaZhouqiEntity::getId, source.getYuanjihuaZhouqiId()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhouqi, source.getZhouqi()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getZhouqiDanwei, source.getZhouqiDanwei()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getPihao, source.getPihao()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJiahuaAnpaiRiqi, source.getJiahuaAnpaiRiqi()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJianyanLeixingId, source.getJianyanLeixingId()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJianchaXiangmuIds, source.getJianchaXiangmuIds()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getJianchaXiangmuMingcheng, source.getJianchaXiangmuMingcheng()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getYangpinDanwei, source.getYangpinDanwei()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getShiquShuliang, source.getShiquShuliang()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getKaocaJihuaId, source.getKaocaJihuaId()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getShifouJixuZhixing, source.getShifouJixuZhixing()) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteMark, null) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteTime, null) .set(LimsWendingxingKaocaJihuaZhouqiEntity::getDeleteUserId, null); if (jihuaZhouqiMapper.update(null, update) != 1) { throw new DataException("原计划周期数据不存在:" + source.getYuanjihuaZhouqiId()); } } private void insertJihuaZhouqi(List sourceList) { if (sourceList.isEmpty()) { return; } List targetList = new ArrayList<>(sourceList.size()); for (LimsWendingxingKaocaJihuaBiangengZhouqiEntity source : sourceList) { LimsWendingxingKaocaJihuaZhouqiEntity target = new LimsWendingxingKaocaJihuaZhouqiEntity(); target.setId(RandomUtil.uuId()); target.setKaocaJihuaId(source.getKaocaJihuaId()); target.setZhouqi(source.getZhouqi()); target.setZhouqiDanwei(source.getZhouqiDanwei()); target.setPihao(source.getPihao()); target.setJiahuaAnpaiRiqi(source.getJiahuaAnpaiRiqi()); target.setJianyanLeixingId(source.getJianyanLeixingId()); target.setJianchaXiangmuIds(source.getJianchaXiangmuIds()); target.setJianchaXiangmuMingcheng(source.getJianchaXiangmuMingcheng()); target.setYangpinDanwei(source.getYangpinDanwei()); target.setShiquShuliang(source.getShiquShuliang()); target.setShifouJixuZhixing(source.getShifouJixuZhixing()); targetList.add(target); } jihuaZhouqiMapper.insert(targetList); } }