package com.klp.service.impl; import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.klp.common.core.domain.PageQuery; import com.klp.common.core.page.TableDataInfo; import com.klp.common.exception.ServiceException; import com.klp.common.utils.StringUtils; import com.klp.domain.WmsProcessSpec; import com.klp.domain.WmsProcessPlan; import com.klp.domain.WmsProcessPlanParam; import com.klp.domain.WmsProcessSpecVersion; import com.klp.domain.bo.WmsProcessSpecVersionBo; import com.klp.domain.vo.WmsProcessSpecVersionVo; import com.klp.mapper.WmsProcessPlanMapper; import com.klp.mapper.WmsProcessPlanParamMapper; import com.klp.mapper.WmsProcessSpecMapper; import com.klp.mapper.WmsProcessSpecVersionMapper; import com.klp.service.IWmsProcessSpecVersionService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; /** * 规程版本Service实现 * * @author klp */ @RequiredArgsConstructor @Service public class WmsProcessSpecVersionServiceImpl implements IWmsProcessSpecVersionService { private final WmsProcessSpecVersionMapper baseMapper; private final WmsProcessSpecMapper wmsProcessSpecMapper; private final WmsProcessPlanMapper wmsProcessPlanMapper; private final WmsProcessPlanParamMapper wmsProcessPlanParamMapper; @Override public WmsProcessSpecVersionVo queryById(Long versionId) { return baseMapper.selectVoById(versionId); } @Override public TableDataInfo queryPageList(WmsProcessSpecVersionBo bo, PageQuery pageQuery) { LambdaQueryWrapper lqw = buildQueryWrapper(bo); Page result = baseMapper.selectVoPage(pageQuery.build(), lqw); return TableDataInfo.build(result); } @Override public List queryList(WmsProcessSpecVersionBo bo) { return baseMapper.selectVoList(buildQueryWrapper(bo)); } private LambdaQueryWrapper buildQueryWrapper(WmsProcessSpecVersionBo bo) { LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(bo.getSpecId() != null, WmsProcessSpecVersion::getSpecId, bo.getSpecId()); lqw.eq(StringUtils.isNotBlank(bo.getVersionCode()), WmsProcessSpecVersion::getVersionCode, bo.getVersionCode()); lqw.eq(bo.getIsActive() != null, WmsProcessSpecVersion::getIsActive, bo.getIsActive()); lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WmsProcessSpecVersion::getStatus, bo.getStatus()); lqw.orderByDesc(WmsProcessSpecVersion::getCreateTime); return lqw; } @Override @Transactional(rollbackFor = Exception.class) public Boolean insertByBo(WmsProcessSpecVersionBo bo) { WmsProcessSpec spec = wmsProcessSpecMapper.selectById(bo.getSpecId()); if (spec == null) { throw new ServiceException("规程不存在"); } WmsProcessSpecVersion add = BeanUtil.toBean(bo, WmsProcessSpecVersion.class); if (add.getIsActive() == null) { add.setIsActive(0); } if (StringUtils.isBlank(add.getStatus())) { add.setStatus("DRAFT"); } validEntityBeforeSave(add); boolean ok = baseMapper.insert(add) > 0; if (ok) { bo.setVersionId(add.getVersionId()); if (Integer.valueOf(1).equals(add.getIsActive())) { activateVersion(add.getVersionId()); } } return ok; } @Override @Transactional(rollbackFor = Exception.class) public Boolean updateByBo(WmsProcessSpecVersionBo bo) { WmsProcessSpecVersion exist = baseMapper.selectById(bo.getVersionId()); if (exist == null) { throw new ServiceException("版本不存在"); } WmsProcessSpecVersion update = BeanUtil.toBean(bo, WmsProcessSpecVersion.class); validEntityBeforeSave(update); boolean ok = baseMapper.updateById(update) > 0; if (ok && Integer.valueOf(1).equals(update.getIsActive())) { activateVersion(update.getVersionId()); } return ok; } private void validEntityBeforeSave(WmsProcessSpecVersion entity) { LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(WmsProcessSpecVersion::getSpecId, entity.getSpecId()); lqw.eq(WmsProcessSpecVersion::getVersionCode, entity.getVersionCode()); if (entity.getVersionId() != null) { lqw.ne(WmsProcessSpecVersion::getVersionId, entity.getVersionId()); } if (baseMapper.selectCount(lqw) > 0) { throw new ServiceException("同一规程下版本号已存在"); } } @Override @Transactional(rollbackFor = Exception.class) public Boolean activateVersion(Long versionId) { WmsProcessSpecVersion v = baseMapper.selectById(versionId); if (v == null) { throw new ServiceException("版本不存在"); } LambdaUpdateWrapper clear = Wrappers.lambdaUpdate(); clear.eq(WmsProcessSpecVersion::getSpecId, v.getSpecId()); clear.set(WmsProcessSpecVersion::getIsActive, 0); baseMapper.update(null, clear); WmsProcessSpecVersion one = new WmsProcessSpecVersion(); one.setVersionId(versionId); one.setIsActive(1); return baseMapper.updateById(one) > 0; } @Override public WmsProcessSpecVersionVo matchBestVersion(BigDecimal entryThick, BigDecimal exitThick, BigDecimal entryWidth, BigDecimal exitWidth, String grade, Long lineId) { LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(WmsProcessSpecVersion::getIsActive, 1); List versions = baseMapper.selectVoList(lqw); if (versions.isEmpty()) { return null; } // 按产线ID过滤:只匹配属于指定产线的规程版本 if (lineId != null) { List specIds = versions.stream() .map(WmsProcessSpecVersionVo::getSpecId).distinct().collect(Collectors.toList()); LambdaQueryWrapper slqw = Wrappers.lambdaQuery(); slqw.in(WmsProcessSpec::getSpecId, specIds); slqw.eq(WmsProcessSpec::getLineId, lineId); Set validSpecIds = wmsProcessSpecMapper.selectList(slqw) .stream().map(WmsProcessSpec::getSpecId).collect(Collectors.toSet()); versions = versions.stream() .filter(v -> validSpecIds.contains(v.getSpecId())).collect(Collectors.toList()); if (versions.isEmpty()) { return null; } } WmsProcessSpecVersionVo best = null; int bestScore = -1; for (WmsProcessSpecVersionVo v : versions) { int score = 0; if (entryThick != null && v.getMatchEntryThickMin() != null && v.getMatchEntryThickMax() != null && entryThick.compareTo(v.getMatchEntryThickMin()) >= 0 && entryThick.compareTo(v.getMatchEntryThickMax()) <= 0) { score++; } if (exitThick != null && v.getMatchExitThickMin() != null && v.getMatchExitThickMax() != null && exitThick.compareTo(v.getMatchExitThickMin()) >= 0 && exitThick.compareTo(v.getMatchExitThickMax()) <= 0) { score++; } if (entryWidth != null && v.getMatchEntryWidthMin() != null && v.getMatchEntryWidthMax() != null && entryWidth.compareTo(v.getMatchEntryWidthMin()) >= 0 && entryWidth.compareTo(v.getMatchEntryWidthMax()) <= 0) { score++; } if (exitWidth != null && v.getMatchExitWidthMin() != null && v.getMatchExitWidthMax() != null && exitWidth.compareTo(v.getMatchExitWidthMin()) >= 0 && exitWidth.compareTo(v.getMatchExitWidthMax()) <= 0) { score++; } if (StringUtils.isNotBlank(grade) && StringUtils.isNotBlank(v.getMatchSteelGrade()) && grade.toLowerCase().contains(v.getMatchSteelGrade().toLowerCase())) { score++; } if (score > bestScore) { bestScore = score; best = v; } } if (best == null || bestScore == 0) { return null; } WmsProcessSpec spec = wmsProcessSpecMapper.selectById(best.getSpecId()); if (spec != null) { best.setSpecCode(spec.getSpecCode()); best.setSpecName(spec.getSpecName()); } return best; } @Override public List queryActiveVersionsEnriched(Long lineId) { LambdaQueryWrapper lqw = Wrappers.lambdaQuery(); lqw.eq(WmsProcessSpecVersion::getIsActive, 1); List versions = baseMapper.selectVoList(lqw); if (versions.isEmpty()) { return versions; } if (lineId != null) { List specIds = versions.stream() .map(WmsProcessSpecVersionVo::getSpecId).distinct().collect(Collectors.toList()); LambdaQueryWrapper slqw = Wrappers.lambdaQuery(); slqw.in(WmsProcessSpec::getSpecId, specIds); slqw.eq(WmsProcessSpec::getLineId, lineId); Set validSpecIds = wmsProcessSpecMapper.selectList(slqw) .stream().map(WmsProcessSpec::getSpecId).collect(Collectors.toSet()); versions = versions.stream() .filter(v -> validSpecIds.contains(v.getSpecId())).collect(Collectors.toList()); if (versions.isEmpty()) { return versions; } } enrichVersionsWithSpec(versions); return versions; } @Override public List queryAllVersionsEnriched() { // 不过滤 isActive,获取全部版本(含历史版本),用于展示已绑定规程名称 List versions = baseMapper.selectVoList(Wrappers.lambdaQuery()); if (versions.isEmpty()) return versions; enrichVersionsWithSpec(versions); return versions; } /** 批量填充 specCode / specName / lineId(公共逻辑) */ private void enrichVersionsWithSpec(List versions) { List specIds = versions.stream() .map(WmsProcessSpecVersionVo::getSpecId).distinct().collect(Collectors.toList()); LambdaQueryWrapper sq = Wrappers.lambdaQuery(); sq.in(WmsProcessSpec::getSpecId, specIds); Map specMap = wmsProcessSpecMapper.selectList(sq).stream() .collect(Collectors.toMap(WmsProcessSpec::getSpecId, s -> s, (a, b) -> a)); for (WmsProcessSpecVersionVo v : versions) { WmsProcessSpec spec = specMap.get(v.getSpecId()); if (spec != null) { v.setSpecCode(spec.getSpecCode()); v.setSpecName(spec.getSpecName()); v.setLineId(spec.getLineId()); } } } @Override @Transactional(rollbackFor = Exception.class) public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { for (Long versionId : ids) { LambdaQueryWrapper pq = Wrappers.lambdaQuery(); pq.eq(WmsProcessPlan::getVersionId, versionId); List plans = wmsProcessPlanMapper.selectList(pq); for (WmsProcessPlan plan : plans) { LambdaQueryWrapper pr = Wrappers.lambdaQuery(); pr.eq(WmsProcessPlanParam::getPlanId, plan.getPlanId()); wmsProcessPlanParamMapper.delete(pr); } wmsProcessPlanMapper.delete(pq); } return baseMapper.deleteBatchIds(ids) > 0; } }