236 lines
10 KiB
Java
236 lines
10 KiB
Java
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 org.springframework.stereotype.Service;
|
||
import com.klp.domain.bo.WmsRawMaterialBo;
|
||
import com.klp.domain.vo.WmsRawMaterialVo;
|
||
import com.klp.domain.WmsRawMaterial;
|
||
import com.klp.mapper.WmsRawMaterialMapper;
|
||
import com.klp.service.IWmsRawMaterialService;
|
||
import com.klp.service.IWmsStockService;
|
||
import com.klp.service.IWmsPurchasePlanDetailService;
|
||
import com.klp.service.IWmsOrderDetailService;
|
||
import com.klp.service.IWmsProductBomService;
|
||
import com.klp.service.IWmsOrderService;
|
||
import com.klp.domain.bo.WmsPurchasePlanDetailBo;
|
||
import com.klp.domain.bo.WmsOrderDetailBo;
|
||
import com.klp.domain.bo.WmsProductBomBo;
|
||
import com.klp.domain.vo.WmsPurchasePlanDetailVo;
|
||
import com.klp.domain.vo.WmsProductBomVo;
|
||
import com.klp.domain.vo.WmsOrderDetailVo;
|
||
import com.klp.domain.vo.WmsOrderVo;
|
||
import java.math.BigDecimal;
|
||
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Collection;
|
||
|
||
/**
|
||
* 原材料Service业务层处理
|
||
*
|
||
* @author Joshi
|
||
* @date 2025-07-18
|
||
*/
|
||
@RequiredArgsConstructor
|
||
@Service
|
||
public class WmsRawMaterialServiceImpl implements IWmsRawMaterialService {
|
||
|
||
private final WmsRawMaterialMapper baseMapper;
|
||
private final IWmsStockService stockService;
|
||
private final IWmsPurchasePlanDetailService purchasePlanDetailService;
|
||
private final IWmsOrderDetailService orderDetailService;
|
||
private final IWmsProductBomService productBomService;
|
||
private final IWmsOrderService orderService;
|
||
|
||
/**
|
||
* 查询原材料
|
||
*/
|
||
@Override
|
||
public WmsRawMaterialVo queryById(Long rawMaterialId){
|
||
return baseMapper.selectVoById(rawMaterialId);
|
||
}
|
||
|
||
/**
|
||
* 查询原材料列表
|
||
*/
|
||
@Override
|
||
public TableDataInfo<WmsRawMaterialVo> queryPageList(WmsRawMaterialBo bo, PageQuery pageQuery) {
|
||
LambdaQueryWrapper<WmsRawMaterial> lqw = buildQueryWrapper(bo);
|
||
Page<WmsRawMaterialVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||
return TableDataInfo.build(result);
|
||
}
|
||
|
||
/**
|
||
* 查询原材料列表
|
||
*/
|
||
@Override
|
||
public List<WmsRawMaterialVo> queryList(WmsRawMaterialBo bo) {
|
||
LambdaQueryWrapper<WmsRawMaterial> lqw = buildQueryWrapper(bo);
|
||
return baseMapper.selectVoList(lqw);
|
||
}
|
||
|
||
/**
|
||
* 查询原材料列表(含需求、库存、在途信息)
|
||
*/
|
||
@Override
|
||
public TableDataInfo<WmsRawMaterialVo> queryPageListWithDemand(WmsRawMaterialBo bo, PageQuery pageQuery) {
|
||
LambdaQueryWrapper<WmsRawMaterial> lqw = buildQueryWrapper(bo);
|
||
Page<WmsRawMaterialVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||
// 填充需求、库存、在途信息
|
||
fillDemandInfo(result.getRecords());
|
||
return TableDataInfo.build(result);
|
||
}
|
||
|
||
/**
|
||
* 填充原材料需求、库存、在途信息
|
||
*/
|
||
private void fillDemandInfo(List<WmsRawMaterialVo> rawMaterialList) {
|
||
if (rawMaterialList == null || rawMaterialList.isEmpty()) {
|
||
return;
|
||
}
|
||
// 为每个原材料填充信息
|
||
for (WmsRawMaterialVo vo : rawMaterialList) {
|
||
Long rawMaterialId = vo.getRawMaterialId();
|
||
// 查询库存量
|
||
BigDecimal inventory = stockService.getStockByItemId(rawMaterialId);
|
||
vo.setInventory(inventory != null ? inventory : BigDecimal.ZERO);
|
||
// 查询在途量(采购计划明细)
|
||
BigDecimal onTheWay = getOnTheWayQuantity(rawMaterialId);
|
||
vo.setOnTheWay(onTheWay);
|
||
// 查询需求量(订单+BOM)
|
||
BigDecimal demand = getDemandQuantity(rawMaterialId);
|
||
vo.setDemand(demand);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取在途量
|
||
*/
|
||
private BigDecimal getOnTheWayQuantity(Long rawMaterialId) {
|
||
WmsPurchasePlanDetailBo bo = new WmsPurchasePlanDetailBo();
|
||
bo.setRawMaterialId(rawMaterialId);
|
||
List<WmsPurchasePlanDetailVo> list = purchasePlanDetailService.queryList(bo);
|
||
return list.stream()
|
||
.filter(item -> item.getStatus() != null && item.getStatus() == 1) // 在途状态
|
||
.map(WmsPurchasePlanDetailVo::getQuantity)
|
||
.filter(qty -> qty != null)
|
||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||
}
|
||
|
||
/**
|
||
* 获取需求量
|
||
*/
|
||
private BigDecimal getDemandQuantity(Long rawMaterialId) {
|
||
// 先查询包含该原材料的BOM
|
||
WmsProductBomBo bomBo = new WmsProductBomBo();
|
||
bomBo.setRawMaterialId(rawMaterialId);
|
||
List<WmsProductBomVo> bomList = productBomService.queryList(bomBo);
|
||
if (bomList.isEmpty()) {
|
||
return BigDecimal.ZERO;
|
||
}
|
||
// 查询这些产品的订单明细
|
||
BigDecimal totalDemand = BigDecimal.ZERO;
|
||
for (WmsProductBomVo bom : bomList) {
|
||
WmsOrderDetailBo orderDetailBo = new WmsOrderDetailBo();
|
||
orderDetailBo.setProductId(bom.getProductId());
|
||
List<WmsOrderDetailVo> orderDetails = orderDetailService.queryList(orderDetailBo);
|
||
// 逐个查询订单状态,过滤出有效订单的明细
|
||
BigDecimal productDemand = BigDecimal.ZERO;
|
||
for (WmsOrderDetailVo detail : orderDetails) {
|
||
// 查询订单主表状态
|
||
WmsOrderVo order = orderService.queryById(detail.getOrderId());
|
||
if (order != null && order.getOrderStatus() != null && order.getOrderStatus() < 2) {
|
||
// 新建、生产中状态,累加数量
|
||
if (detail.getQuantity() != null) {
|
||
productDemand = productDemand.add(detail.getQuantity());
|
||
}
|
||
}
|
||
}
|
||
// 需求量 = 产品需求量 × BOM用量
|
||
if (bom.getQuantity() != null) {
|
||
totalDemand = totalDemand.add(productDemand.multiply(bom.getQuantity()));
|
||
}
|
||
}
|
||
|
||
return totalDemand;
|
||
}
|
||
|
||
private LambdaQueryWrapper<WmsRawMaterial> buildQueryWrapper(WmsRawMaterialBo bo) {
|
||
Map<String, Object> params = bo.getParams();
|
||
LambdaQueryWrapper<WmsRawMaterial> lqw = Wrappers.lambdaQuery();
|
||
lqw.eq(StringUtils.isNotBlank(bo.getRawMaterialCode()), WmsRawMaterial::getRawMaterialCode, bo.getRawMaterialCode());
|
||
lqw.like(StringUtils.isNotBlank(bo.getRawMaterialName()), WmsRawMaterial::getRawMaterialName, bo.getRawMaterialName());
|
||
lqw.eq(StringUtils.isNotBlank(bo.getSteelGrade()), WmsRawMaterial::getSteelGrade, bo.getSteelGrade());
|
||
lqw.eq(StringUtils.isNotBlank(bo.getTargetColdGrade()), WmsRawMaterial::getTargetColdGrade, bo.getTargetColdGrade());
|
||
lqw.eq(bo.getBaseMaterialId() != null, WmsRawMaterial::getBaseMaterialId, bo.getBaseMaterialId());
|
||
lqw.eq(bo.getSurfaceTreatmentId() != null, WmsRawMaterial::getSurfaceTreatmentId, bo.getSurfaceTreatmentId());
|
||
lqw.eq(bo.getThickness() != null, WmsRawMaterial::getThickness, bo.getThickness());
|
||
lqw.eq(bo.getThicknessDeviation() != null, WmsRawMaterial::getThicknessDeviation, bo.getThicknessDeviation());
|
||
lqw.eq(bo.getWidth() != null, WmsRawMaterial::getWidth, bo.getWidth());
|
||
lqw.eq(bo.getTargetColdWidth() != null, WmsRawMaterial::getTargetColdWidth, bo.getTargetColdWidth());
|
||
lqw.eq(bo.getTargetColdThickness() != null, WmsRawMaterial::getTargetColdThickness, bo.getTargetColdThickness());
|
||
lqw.eq(bo.getCrown() != null, WmsRawMaterial::getCrown, bo.getCrown());
|
||
lqw.eq(bo.getCoilWeight() != null, WmsRawMaterial::getCoilWeight, bo.getCoilWeight());
|
||
lqw.eq(StringUtils.isNotBlank(bo.getSurfaceQuality()), WmsRawMaterial::getSurfaceQuality, bo.getSurfaceQuality());
|
||
lqw.eq(bo.getHardnessHv5() != null, WmsRawMaterial::getHardnessHv5, bo.getHardnessHv5());
|
||
lqw.eq(bo.getHardnessDiff() != null, WmsRawMaterial::getHardnessDiff, bo.getHardnessDiff());
|
||
lqw.eq(bo.getCompositionMn() != null, WmsRawMaterial::getCompositionMn, bo.getCompositionMn());
|
||
lqw.eq(bo.getCompositionP() != null, WmsRawMaterial::getCompositionP, bo.getCompositionP());
|
||
lqw.eq(StringUtils.isNotBlank(bo.getGrainSize()), WmsRawMaterial::getGrainSize, bo.getGrainSize());
|
||
lqw.eq(bo.getHeadTailCutFlag() != null, WmsRawMaterial::getHeadTailCutFlag, bo.getHeadTailCutFlag());
|
||
lqw.eq(StringUtils.isNotBlank(bo.getInspectionResult()), WmsRawMaterial::getInspectionResult, bo.getInspectionResult());
|
||
lqw.eq(bo.getIsEnabled() != null, WmsRawMaterial::getIsEnabled, bo.getIsEnabled());
|
||
lqw.eq(bo.getBomId() != null, WmsRawMaterial::getBomId, bo.getBomId());
|
||
return lqw;
|
||
}
|
||
|
||
/**
|
||
* 新增原材料
|
||
*/
|
||
@Override
|
||
public WmsRawMaterialBo insertByBo(WmsRawMaterialBo bo) {
|
||
WmsRawMaterial add = BeanUtil.toBean(bo, WmsRawMaterial.class);
|
||
validEntityBeforeSave(add);
|
||
boolean flag = baseMapper.insert(add) > 0;
|
||
if (flag) {
|
||
bo.setRawMaterialId(add.getRawMaterialId());
|
||
}
|
||
return bo;
|
||
}
|
||
|
||
/**
|
||
* 修改原材料
|
||
*/
|
||
@Override
|
||
public Boolean updateByBo(WmsRawMaterialBo bo) {
|
||
WmsRawMaterial update = BeanUtil.toBean(bo, WmsRawMaterial.class);
|
||
validEntityBeforeSave(update);
|
||
return baseMapper.updateById(update) > 0;
|
||
}
|
||
|
||
/**
|
||
* 保存前的数据校验
|
||
*/
|
||
private void validEntityBeforeSave(WmsRawMaterial entity){
|
||
//TODO 做一些数据校验,如唯一约束
|
||
}
|
||
|
||
/**
|
||
* 批量删除原材料
|
||
*/
|
||
@Override
|
||
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
||
if(isValid){
|
||
//TODO 做一些业务上的校验,判断是否需要校验
|
||
}
|
||
return baseMapper.deleteBatchIds(ids) > 0;
|
||
}
|
||
}
|