This commit is contained in:
2026-06-06 17:08:51 +08:00
9 changed files with 799 additions and 1 deletions

View File

@@ -78,6 +78,7 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
// private final WmsDeliveryPlanMapper deliveryPlanMapper;
private final WmsProductMapper productMapper;
private final WmsRawMaterialMapper rawMaterialMapper;
private final IWmsMaterialWarningService materialWarningService;
private final WmsDeliveryWaybillDetailMapper deliveryWaybillDetailMapper;
private final WmsCoilWarehouseOperationLogMapper wmsCoilWarehouseOperationLogMapper;
private final IWmsCoilAbnormalService coilAbnormalService;
@@ -1117,7 +1118,7 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
BigDecimal volume = weight.multiply(new BigDecimal("1000")).divide(new BigDecimal("7.85"), 10, RoundingMode.HALF_UP);
// 计算理论厚度(需要实测长度)
if (bo.getTheoreticalThickness() == null && bo.getActualLength() != null && bo.getActualWidth().compareTo(BigDecimal.ZERO) > 0) {
if (bo.getTheoreticalThickness() == null && bo.getActualLength() != null) {
BigDecimal length = new BigDecimal(bo.getActualLength());
BigDecimal theoreticalThickness = volume.divide(length, 10, RoundingMode.HALF_UP).divide(width, 10, RoundingMode.HALF_UP).multiply(new BigDecimal("1000"));
bo.setTheoreticalThickness(theoreticalThickness);
@@ -1521,6 +1522,9 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
throw new RuntimeException("新增钢卷失败");
}
// 检查长度/厚度偏差并插入告警
materialWarningService.checkAndInsertWarnings(add);
// 设置返回用的ID并更新二维码内容中的coilId
bo.setCoilId(add.getCoilId());
updateQrcodeCoilId(qrcodeRecordId, add.getCoilId());
@@ -1732,6 +1736,10 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
updateWrapper.set(WmsMaterialCoil::getSaleId, (Long)null);
baseMapper.update(null, updateWrapper);
}
// 检查长度/厚度偏差并插入告警
materialWarningService.checkAndInsertWarnings(updateCoil);
// 如果实际库区id为-1或状态为1则清空钢卷上的实际库区绑定
if ((bo.getActualWarehouseId() != null && bo.getActualWarehouseId().equals(-1L))
|| (bo.getStatus() != null && bo.getStatus().equals(1))) {
@@ -1839,6 +1847,9 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
throw new RuntimeException("创建新钢卷失败");
}
// 检查长度/厚度偏差并插入告警
materialWarningService.checkAndInsertWarnings(newCoil);
// 如果实际库区id为-1则清空钢卷上的实际库区绑定
if (bo.getActualWarehouseId() != null && bo.getActualWarehouseId().equals(-1L)) {
clearActualWarehouseBinding(newCoil.getActualWarehouseId(), newCoil.getCoilId());
@@ -2096,6 +2107,10 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
// 把老记录的coilId赋值给新纪录的parentCoilId
newCoil.setParentCoilId(String.valueOf(oldCoil.getCoilId()));
baseMapper.insert(newCoil);
// 检查长度/厚度偏差并插入告警
materialWarningService.checkAndInsertWarnings(newCoil);
newCoils.add(newCoil);
// 更新二维码内容中的coilId
@@ -2234,6 +2249,10 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
newCoil.setParentCoilId(parentCoilIdsStr);
baseMapper.insert(newCoil);
// 检查长度/厚度偏差并插入告警
materialWarningService.checkAndInsertWarnings(newCoil);
newCoils.add(newCoil);
// 更新二维码内容中的coilId
@@ -5085,6 +5104,9 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
childCoil.setParentCoilId(String.valueOf(parentCoilId));
baseMapper.insert(childCoil);
// 检查长度/厚度偏差并插入告警
materialWarningService.checkAndInsertWarnings(childCoil);
// 插入子钢卷的异常信息
if (childCoilBo.getAbnormals() != null && !childCoilBo.getAbnormals().isEmpty()) {
for (WmsCoilAbnormalBo abnormalBo : childCoilBo.getAbnormals()) {

View File

@@ -0,0 +1,266 @@
package com.klp.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.klp.common.core.page.TableDataInfo;
import com.klp.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.klp.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.klp.domain.bo.WmsMaterialWarningBo;
import com.klp.domain.vo.WmsMaterialWarningVo;
import com.klp.domain.WmsMaterialCoil;
import com.klp.domain.WmsMaterialWarning;
import com.klp.mapper.WmsMaterialWarningMapper;
import com.klp.service.IWmsMaterialWarningService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
/**
* 钢卷通用维度告警(长度/厚度/宽度Service业务层处理
*
* @author klp
* @date 2026-06-06
*/
@Slf4j
@RequiredArgsConstructor
@Service
public class WmsMaterialWarningServiceImpl implements IWmsMaterialWarningService {
private final WmsMaterialWarningMapper baseMapper;
/**
* 查询钢卷通用维度告警(长度/厚度/宽度)
*/
@Override
public WmsMaterialWarningVo queryById(Long warningId){
return baseMapper.selectVoById(warningId);
}
/**
* 查询钢卷通用维度告警(长度/厚度/宽度)列表
*/
@Override
public TableDataInfo<WmsMaterialWarningVo> queryPageList(WmsMaterialWarningBo bo, PageQuery pageQuery) {
LambdaQueryWrapper<WmsMaterialWarning> lqw = buildQueryWrapper(bo);
Page<WmsMaterialWarningVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
return TableDataInfo.build(result);
}
/**
* 查询钢卷通用维度告警(长度/厚度/宽度)列表
*/
@Override
public List<WmsMaterialWarningVo> queryList(WmsMaterialWarningBo bo) {
LambdaQueryWrapper<WmsMaterialWarning> lqw = buildQueryWrapper(bo);
return baseMapper.selectVoList(lqw);
}
private LambdaQueryWrapper<WmsMaterialWarning> buildQueryWrapper(WmsMaterialWarningBo bo) {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<WmsMaterialWarning> lqw = Wrappers.lambdaQuery();
lqw.eq(bo.getCoilId() != null, WmsMaterialWarning::getCoilId, bo.getCoilId());
lqw.eq(StringUtils.isNotBlank(bo.getWarningType()), WmsMaterialWarning::getWarningType, bo.getWarningType());
lqw.eq(bo.getTheoreticalVal() != null, WmsMaterialWarning::getTheoreticalVal, bo.getTheoreticalVal());
lqw.eq(bo.getActualVal() != null, WmsMaterialWarning::getActualVal, bo.getActualVal());
lqw.eq(bo.getAllowDeviation() != null, WmsMaterialWarning::getAllowDeviation, bo.getAllowDeviation());
lqw.eq(bo.getDeviationValue() != null, WmsMaterialWarning::getDeviationValue, bo.getDeviationValue());
lqw.eq(bo.getDeviationRate() != null, WmsMaterialWarning::getDeviationRate, bo.getDeviationRate());
lqw.eq(StringUtils.isNotBlank(bo.getWarningLevel()), WmsMaterialWarning::getWarningLevel, bo.getWarningLevel());
lqw.eq(StringUtils.isNotBlank(bo.getWarningMsg()), WmsMaterialWarning::getWarningMsg, bo.getWarningMsg());
lqw.eq(bo.getWarningStatus() != null, WmsMaterialWarning::getWarningStatus, bo.getWarningStatus());
lqw.eq(StringUtils.isNotBlank(bo.getHandleBy()), WmsMaterialWarning::getHandleBy, bo.getHandleBy());
lqw.eq(bo.getHandleTime() != null, WmsMaterialWarning::getHandleTime, bo.getHandleTime());
lqw.eq(StringUtils.isNotBlank(bo.getHandleRemark()), WmsMaterialWarning::getHandleRemark, bo.getHandleRemark());
return lqw;
}
/**
* 新增钢卷通用维度告警(长度/厚度/宽度)
*/
@Override
public Boolean insertByBo(WmsMaterialWarningBo bo) {
WmsMaterialWarning add = BeanUtil.toBean(bo, WmsMaterialWarning.class);
validEntityBeforeSave(add);
boolean flag = baseMapper.insert(add) > 0;
if (flag) {
bo.setWarningId(add.getWarningId());
}
return flag;
}
/**
* 修改钢卷通用维度告警(长度/厚度/宽度)
*/
@Override
public Boolean updateByBo(WmsMaterialWarningBo bo) {
WmsMaterialWarning update = BeanUtil.toBean(bo, WmsMaterialWarning.class);
validEntityBeforeSave(update);
return baseMapper.updateById(update) > 0;
}
/**
* 保存前的数据校验
*/
private void validEntityBeforeSave(WmsMaterialWarning entity){
//TODO 做一些数据校验,如唯一约束
}
/**
* 批量删除钢卷通用维度告警(长度/厚度/宽度)
*/
@Override
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
if(isValid){
//TODO 做一些业务上的校验,判断是否需要校验
}
return baseMapper.deleteBatchIds(ids) > 0;
}
/**
* 检查钢卷长度/厚度偏差并批量插入告警记录
*
* 长度规则ABS((实测长度 - 理论长度) / 理论长度) > 0.1 → 告警
* 厚度规则:
* 1. 实测厚度 < 理论厚度 → 偏薄告警(ERROR)
* 2. (实测厚度 - 理论厚度) / 理论厚度 > 0.1 → 偏厚告警(WARNING)
*/
@Override
public void checkAndInsertWarnings(WmsMaterialCoil coil) {
try {
doCheckAndInsertWarnings(coil);
} catch (Exception e) {
log.error("钢卷告警检查异常, coilId={}, 不影响原流程", coil != null ? coil.getCoilId() : null, e);
}
}
private void doCheckAndInsertWarnings(WmsMaterialCoil coil) {
if (coil == null || coil.getCoilId() == null) {
return;
}
List<WmsMaterialWarning> warnings = new ArrayList<>();
// ========== 长度检查 ==========
checkLength(coil, warnings);
// ========== 厚度检查 ==========
checkThickness(coil, warnings);
// 批量插入
if (!warnings.isEmpty()) {
try {
baseMapper.insertBatch(warnings);
log.info("批量插入钢卷告警记录成功, coilId={}, 告警数={}", coil.getCoilId(), warnings.size());
} catch (Exception e) {
log.error("批量插入钢卷告警记录失败, coilId={}", coil.getCoilId(), e);
}
}
}
/**
* 长度偏差检查
* ABS((actualLength - theoreticalLength) / theoreticalLength) > 0.1 → WARNING
*/
private void checkLength(WmsMaterialCoil coil, List<WmsMaterialWarning> warnings) {
Long actualLength = coil.getActualLength();
BigDecimal theoreticalLength = coil.getTheoreticalLength();
if (actualLength == null || theoreticalLength == null
|| theoreticalLength.compareTo(BigDecimal.ZERO) == 0) {
return;
}
BigDecimal actual = new BigDecimal(actualLength);
BigDecimal diff = actual.subtract(theoreticalLength).abs();
BigDecimal rate = diff.divide(theoreticalLength, 10, RoundingMode.HALF_UP);
// 偏差率 > 0.1即超过10%
BigDecimal threshold = new BigDecimal("0.1");
if (rate.compareTo(threshold) > 0) {
WmsMaterialWarning warning = new WmsMaterialWarning();
warning.setCoilId(coil.getCoilId());
warning.setWarningType("LENGTH");
warning.setTheoreticalVal(theoreticalLength);
warning.setActualVal(actual);
warning.setAllowDeviation(threshold);
warning.setDeviationValue(actual.subtract(theoreticalLength));
warning.setDeviationRate(rate.multiply(new BigDecimal("100")).setScale(4, RoundingMode.HALF_UP));
warning.setWarningLevel("WARNING");
warning.setWarningMsg("钢卷长度偏差超过10%,理论长度=" + theoreticalLength + "m实测长度=" + actualLength + "mm");
warning.setWarningStatus(0);
warnings.add(warning);
}
}
/**
* 厚度偏差检查
* 1. 实测厚度 < 理论厚度 → ERROR不能比原来薄
* 2. (实测厚度 - 理论厚度) / 理论厚度 > 0.1 → WARNING不能厚太多
*/
private void checkThickness(WmsMaterialCoil coil, List<WmsMaterialWarning> warnings) {
String actualThicknessStr = coil.getActualThickness();
BigDecimal theoreticalThickness = coil.getTheoreticalThickness();
if (actualThicknessStr == null || theoreticalThickness == null
|| theoreticalThickness.compareTo(BigDecimal.ZERO) == 0) {
return;
}
BigDecimal actualThickness;
try {
actualThickness = new BigDecimal(actualThicknessStr.trim());
} catch (NumberFormatException e) {
log.warn("实测厚度无法解析为数值, coilId={}, actualThickness={}", coil.getCoilId(), actualThicknessStr);
return;
}
BigDecimal threshold = new BigDecimal("0.1");
// 规则1实测厚度 < 理论厚度 → 偏薄 ERROR
if (actualThickness.compareTo(theoreticalThickness) < 0) {
WmsMaterialWarning warning = new WmsMaterialWarning();
warning.setCoilId(coil.getCoilId());
warning.setWarningType("THICKNESS");
warning.setTheoreticalVal(theoreticalThickness);
warning.setActualVal(actualThickness);
warning.setAllowDeviation(BigDecimal.ZERO);
warning.setDeviationValue(actualThickness.subtract(theoreticalThickness));
BigDecimal rate = theoreticalThickness.subtract(actualThickness)
.divide(theoreticalThickness, 10, RoundingMode.HALF_UP)
.multiply(new BigDecimal("100")).setScale(4, RoundingMode.HALF_UP);
warning.setDeviationRate(rate);
warning.setWarningLevel("ERROR");
warning.setWarningMsg("钢卷实测厚度偏薄,理论厚度=" + theoreticalThickness + "mm实测厚度=" + actualThickness + "mm");
warning.setWarningStatus(0);
warnings.add(warning);
return; // 偏薄已经足够严重,不再重复判断偏厚
}
// 规则2(实测厚度 - 理论厚度) / 理论厚度 > 0.1 → 偏厚超10%
BigDecimal diff = actualThickness.subtract(theoreticalThickness);
BigDecimal rate = diff.divide(theoreticalThickness, 10, RoundingMode.HALF_UP);
if (rate.compareTo(threshold) > 0) {
WmsMaterialWarning warning = new WmsMaterialWarning();
warning.setCoilId(coil.getCoilId());
warning.setWarningType("THICKNESS");
warning.setTheoreticalVal(theoreticalThickness);
warning.setActualVal(actualThickness);
warning.setAllowDeviation(threshold);
warning.setDeviationValue(diff);
warning.setDeviationRate(rate.multiply(new BigDecimal("100")).setScale(4, RoundingMode.HALF_UP));
warning.setWarningLevel("WARNING");
warning.setWarningMsg("钢卷实测厚度偏厚超过10%,理论厚度=" + theoreticalThickness + "mm实测厚度=" + actualThickness + "mm");
warning.setWarningStatus(0);
warnings.add(warning);
}
}
}