package com.ruoyi.sales.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
|
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.procurementrecord.utils.StockUtils;
|
import com.ruoyi.sales.mapper.SalesLedgerMapper;
|
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
|
import com.ruoyi.sales.mapper.WeighingRecordMapper;
|
import com.ruoyi.sales.pojo.SalesLedger;
|
import com.ruoyi.sales.pojo.SalesLedgerProduct;
|
import com.ruoyi.sales.pojo.WeighingRecord;
|
import com.ruoyi.sales.service.IWeighingRecordService;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.util.List;
|
|
@Service
|
@Slf4j
|
@RequiredArgsConstructor
|
public class WeighingRecordServiceImpl extends ServiceImpl<WeighingRecordMapper, WeighingRecord> implements IWeighingRecordService {
|
|
private final WeighingRecordMapper weighingRecordMapper;
|
private final SalesLedgerProductMapper salesLedgerProductMapper;
|
private final SalesLedgerMapper salesLedgerMapper;
|
private final StockUtils stockUtils;
|
|
@Override
|
public IPage<WeighingRecord> listPage(Page<WeighingRecord> page, WeighingRecord weighingRecord) {
|
LambdaQueryWrapper<WeighingRecord> queryWrapper = new LambdaQueryWrapper<>();
|
// 流水号(模糊查询)
|
queryWrapper.like(StringUtils.isNotBlank(weighingRecord.getSerialNo()),
|
WeighingRecord::getSerialNo, weighingRecord.getSerialNo());
|
|
// 车号(模糊查询)
|
queryWrapper.like(StringUtils.isNotBlank(weighingRecord.getCarNo()),
|
WeighingRecord::getCarNo, weighingRecord.getCarNo());
|
|
// 货名(模糊查询)
|
queryWrapper.like(StringUtils.isNotBlank(weighingRecord.getGoodsName()),
|
WeighingRecord::getGoodsName, weighingRecord.getGoodsName());
|
|
// 收货单位(模糊查询)
|
queryWrapper.like(StringUtils.isNotBlank(weighingRecord.getReceiveUnit()),
|
WeighingRecord::getReceiveUnit, weighingRecord.getReceiveUnit());
|
|
// 发货单位(精确查询,可选)
|
queryWrapper.eq(StringUtils.isNotBlank(weighingRecord.getShipUnit()),
|
WeighingRecord::getShipUnit, weighingRecord.getShipUnit());
|
return weighingRecordMapper.selectPage(page, queryWrapper);
|
}
|
|
/**
|
* 根据客户、产品名称、规格型号匹配销售台账产品并出库
|
* @param receiveUnit 收货单位(客户名称)
|
* @param goodsName 货名(产品名称)
|
* @param specification 规格
|
* @param quantity 数量(净重)
|
* @param recordId 磅单记录ID(作为关联记录ID)
|
* @return 匹配到的销售台账产品ID,未匹配返回null
|
*/
|
public Long matchAndOutStock(String receiveUnit, String goodsName, String specification, BigDecimal quantity, Long recordId) {
|
// 1. 查询所有销售台账产品(只查询未发货的)
|
LambdaQueryWrapper<SalesLedgerProduct> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.eq(SalesLedgerProduct::getType, 1); // 销售类型
|
List<SalesLedgerProduct> productList = salesLedgerProductMapper.selectList(queryWrapper);
|
if (productList == null || productList.isEmpty()) {
|
log.warn("未找到未发货的销售台账产品数据");
|
return null;
|
}
|
|
// 2. 遍历匹配:客户名称 + 产品大类 + 规格型号
|
for (SalesLedgerProduct product : productList) {
|
// 获取对应的销售台账主表信息
|
SalesLedger salesLedger = salesLedgerMapper.selectById(product.getSalesLedgerId());
|
if (salesLedger == null) {
|
continue;
|
}
|
|
// 匹配条件:收货单位=客户名称,货名=产品大类,规格=规格型号
|
boolean customerMatch = receiveUnit != null && receiveUnit.equals(salesLedger.getCustomerName());
|
boolean goodsMatch = goodsName != null && goodsName.equals(product.getProductCategory());
|
boolean specMatch = specification != null && specification.equals(product.getSpecificationModel());
|
|
if (customerMatch && goodsMatch && specMatch) {
|
// 3. 匹配成功,执行出库
|
try {
|
WeighingRecord weighingRecord = new WeighingRecord();
|
weighingRecord.setRecordId(product.getId());
|
weighingRecord.setId(recordId);
|
this.updateById(weighingRecord);
|
stockUtils.substractStock(
|
product.getProductModelId(),
|
quantity,
|
StockOutQualifiedRecordTypeEnum.WEIGHBRIDGE_OUT.getCode(), // 磅单出库类型
|
recordId // 磅单ID作为关联记录ID
|
);
|
log.info("磅单出库成功:客户={}, 产品={}, 规格={}, 数量={}, 磅单ID={}",
|
receiveUnit, goodsName, specification, quantity, recordId);
|
return product.getId();
|
} catch (Exception e) {
|
log.error("磅单出库失败:{}", e.getMessage());
|
throw new RuntimeException("出库失败:" + e.getMessage());
|
}
|
}
|
}
|
|
log.warn("未匹配到销售台账产品:客户={}, 产品={}, 规格={}", receiveUnit, goodsName, specification);
|
return null;
|
}
|
}
|