2026-06-06 15:52:29 +08:00
|
|
|
|
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;
|
2026-06-08 17:45:10 +08:00
|
|
|
|
import com.klp.common.core.service.ConfigService;
|
2026-06-06 15:52:29 +08:00
|
|
|
|
import com.klp.common.utils.StringUtils;
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
2026-06-06 16:24:35 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2026-06-06 15:52:29 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.klp.domain.bo.WmsMaterialWarningBo;
|
|
|
|
|
|
import com.klp.domain.vo.WmsMaterialWarningVo;
|
2026-06-06 16:24:35 +08:00
|
|
|
|
import com.klp.domain.WmsMaterialCoil;
|
2026-06-06 15:52:29 +08:00
|
|
|
|
import com.klp.domain.WmsMaterialWarning;
|
2026-06-08 17:45:10 +08:00
|
|
|
|
import com.klp.domain.bo.WmsMaterialCoilBo;
|
2026-06-06 15:52:29 +08:00
|
|
|
|
import com.klp.mapper.WmsMaterialWarningMapper;
|
|
|
|
|
|
import com.klp.service.IWmsMaterialWarningService;
|
|
|
|
|
|
|
2026-06-06 16:24:35 +08:00
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
|
|
import java.util.ArrayList;
|
2026-06-06 15:52:29 +08:00
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 钢卷通用维度告警(长度/厚度/宽度)Service业务层处理
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author klp
|
|
|
|
|
|
* @date 2026-06-06
|
|
|
|
|
|
*/
|
2026-06-06 16:24:35 +08:00
|
|
|
|
@Slf4j
|
2026-06-06 15:52:29 +08:00
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class WmsMaterialWarningServiceImpl implements IWmsMaterialWarningService {
|
|
|
|
|
|
|
|
|
|
|
|
private final WmsMaterialWarningMapper baseMapper;
|
2026-06-08 17:45:10 +08:00
|
|
|
|
private final ConfigService configService;
|
2026-06-06 15:52:29 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 查询钢卷通用维度告警(长度/厚度/宽度)
|
|
|
|
|
|
*/
|
|
|
|
|
|
@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;
|
|
|
|
|
|
}
|
2026-06-06 16:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 检查钢卷长度/厚度偏差并批量插入告警记录
|
|
|
|
|
|
*
|
2026-06-08 17:45:10 +08:00
|
|
|
|
* 长度规则:ABS((实测长度 - 理论长度) / 理论长度) > 阈值 → WARNING
|
|
|
|
|
|
* 阈值从 sys_config(material.warning.length) 读取,默认 0.1
|
|
|
|
|
|
* 厚度规则:理论厚度 - 实测厚度 > 阈值 → ERROR
|
|
|
|
|
|
* 阈值从 sys_config(material.warning.thickness) 读取(绝对值差 mm),默认 -0.01
|
2026-06-06 16:24:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
@Override
|
2026-06-08 17:45:10 +08:00
|
|
|
|
public void checkAndInsertWarnings(WmsMaterialCoil coil, WmsMaterialCoilBo bo) {
|
2026-06-06 16:24:35 +08:00
|
|
|
|
try {
|
2026-06-08 17:45:10 +08:00
|
|
|
|
doCheckAndInsertWarnings(coil, bo);
|
2026-06-06 16:24:35 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.error("钢卷告警检查异常, coilId={}, 不影响原流程", coil != null ? coil.getCoilId() : null, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 17:45:10 +08:00
|
|
|
|
private void doCheckAndInsertWarnings(WmsMaterialCoil coil, WmsMaterialCoilBo bo) {
|
2026-06-06 16:24:35 +08:00
|
|
|
|
if (coil == null || coil.getCoilId() == null) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<WmsMaterialWarning> warnings = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
// ========== 长度检查 ==========
|
|
|
|
|
|
checkLength(coil, warnings);
|
|
|
|
|
|
|
|
|
|
|
|
// ========== 厚度检查 ==========
|
2026-06-08 17:45:10 +08:00
|
|
|
|
checkThickness(coil, bo, warnings);
|
2026-06-06 16:24:35 +08:00
|
|
|
|
|
|
|
|
|
|
// 批量插入
|
|
|
|
|
|
if (!warnings.isEmpty()) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
baseMapper.insertBatch(warnings);
|
|
|
|
|
|
log.info("批量插入钢卷告警记录成功, coilId={}, 告警数={}", coil.getCoilId(), warnings.size());
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.error("批量插入钢卷告警记录失败, coilId={}", coil.getCoilId(), e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 长度偏差检查
|
2026-06-08 17:45:10 +08:00
|
|
|
|
* ABS((actualLength - theoreticalLength) / theoreticalLength) > 阈值 → WARNING
|
|
|
|
|
|
* 阈值从 sys_config(material.warning.length) 读取,默认 0.1
|
2026-06-06 16:24:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 17:45:10 +08:00
|
|
|
|
// 读取配置阈值,默认 0.1(10%)
|
|
|
|
|
|
BigDecimal threshold;
|
|
|
|
|
|
try {
|
|
|
|
|
|
String val = configService.getConfigValue("material.warning.length");
|
|
|
|
|
|
threshold = StringUtils.isNotBlank(val) ? new BigDecimal(val.trim()) : new BigDecimal("0.1");
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.warn("读取长度告警阈值失败,使用默认值 0.1", e);
|
|
|
|
|
|
threshold = new BigDecimal("0.1");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-06 16:24:35 +08:00
|
|
|
|
BigDecimal actual = new BigDecimal(actualLength);
|
|
|
|
|
|
BigDecimal diff = actual.subtract(theoreticalLength).abs();
|
|
|
|
|
|
BigDecimal rate = diff.divide(theoreticalLength, 10, RoundingMode.HALF_UP);
|
|
|
|
|
|
|
|
|
|
|
|
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");
|
2026-06-08 17:45:10 +08:00
|
|
|
|
warning.setWarningMsg("钢卷长度偏差超过阈值,理论长度=" + theoreticalLength + "m,实测长度=" + actualLength + "mm");
|
2026-06-06 16:24:35 +08:00
|
|
|
|
warning.setWarningStatus(0);
|
|
|
|
|
|
warnings.add(warning);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 厚度偏差检查
|
2026-06-08 17:45:10 +08:00
|
|
|
|
* 规则:理论厚度 - 规格厚度 > 阈值 → WARNING
|
|
|
|
|
|
* 规格厚度从 BO 中取得(calculateTheoretical 已解析)
|
|
|
|
|
|
* 阈值从 sys_config(material.warning.thickness) 读取,默认 -0.01
|
2026-06-06 16:24:35 +08:00
|
|
|
|
*/
|
2026-06-08 17:45:10 +08:00
|
|
|
|
private void checkThickness(WmsMaterialCoil coil, WmsMaterialCoilBo bo, List<WmsMaterialWarning> warnings) {
|
2026-06-06 16:24:35 +08:00
|
|
|
|
BigDecimal theoreticalThickness = coil.getTheoreticalThickness();
|
2026-06-08 17:45:10 +08:00
|
|
|
|
BigDecimal specThickness = bo != null ? bo.getSpecThickness() : null;
|
|
|
|
|
|
if (theoreticalThickness == null || specThickness == null
|
|
|
|
|
|
|| specThickness.compareTo(BigDecimal.ZERO) == 0) {
|
2026-06-06 16:24:35 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 17:45:10 +08:00
|
|
|
|
// 读取配置阈值(单位 mm),默认 -0.01
|
|
|
|
|
|
BigDecimal threshold;
|
2026-06-06 16:24:35 +08:00
|
|
|
|
try {
|
2026-06-08 17:45:10 +08:00
|
|
|
|
String val = configService.getConfigValue("material.warning.thickness");
|
|
|
|
|
|
threshold = StringUtils.isNotBlank(val) ? new BigDecimal(val.trim()) : new BigDecimal("-0.01");
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
log.warn("读取厚度告警阈值失败,使用默认值 -0.01", e);
|
|
|
|
|
|
threshold = new BigDecimal("-0.01");
|
2026-06-06 16:24:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 17:45:10 +08:00
|
|
|
|
// 理论厚度 - 规格厚度 > 阈值 → 触发
|
|
|
|
|
|
BigDecimal diff = theoreticalThickness.subtract(specThickness);
|
|
|
|
|
|
if (diff.compareTo(threshold) > 0) {
|
2026-06-06 16:24:35 +08:00
|
|
|
|
WmsMaterialWarning warning = new WmsMaterialWarning();
|
|
|
|
|
|
warning.setCoilId(coil.getCoilId());
|
|
|
|
|
|
warning.setWarningType("THICKNESS");
|
|
|
|
|
|
warning.setTheoreticalVal(theoreticalThickness);
|
2026-06-08 17:45:10 +08:00
|
|
|
|
warning.setActualVal(specThickness);
|
|
|
|
|
|
warning.setAllowDeviation(threshold);
|
|
|
|
|
|
warning.setDeviationValue(specThickness.subtract(theoreticalThickness));
|
|
|
|
|
|
BigDecimal rate = diff.divide(specThickness, 10, RoundingMode.HALF_UP)
|
2026-06-06 16:24:35 +08:00
|
|
|
|
.multiply(new BigDecimal("100")).setScale(4, RoundingMode.HALF_UP);
|
|
|
|
|
|
warning.setDeviationRate(rate);
|
|
|
|
|
|
warning.setWarningLevel("WARNING");
|
2026-06-08 17:45:10 +08:00
|
|
|
|
warning.setWarningMsg("钢卷厚度偏差超出允许范围,理论厚度=" + theoreticalThickness + "mm,规格厚度=" + specThickness + "mm");
|
2026-06-06 16:24:35 +08:00
|
|
|
|
warning.setWarningStatus(0);
|
|
|
|
|
|
warnings.add(warning);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-06-06 15:52:29 +08:00
|
|
|
|
}
|