274 lines
11 KiB
Java
274 lines
11 KiB
Java
package com.klp.service.impl;
|
||
|
||
import cn.hutool.core.bean.BeanUtil;
|
||
import com.klp.common.annotation.RepeatSubmit;
|
||
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 com.klp.domain.vo.WmsStockIoDetailVo;
|
||
import lombok.RequiredArgsConstructor;
|
||
import org.springframework.beans.BeanUtils;
|
||
import org.springframework.stereotype.Service;
|
||
import com.klp.domain.bo.WmsStockIoBo;
|
||
import com.klp.domain.vo.WmsStockIoVo;
|
||
import com.klp.domain.WmsStockIo;
|
||
import com.klp.mapper.WmsStockIoMapper;
|
||
import com.klp.service.IWmsStockIoService;
|
||
import com.klp.domain.WmsStockIoDetail;
|
||
import com.klp.domain.WmsStock;
|
||
import com.klp.mapper.WmsStockIoDetailMapper;
|
||
import com.klp.mapper.WmsStockMapper;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import java.math.BigDecimal;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Collection;
|
||
import com.klp.common.exception.ServiceException;
|
||
|
||
/**
|
||
* 出入库单主Service业务层处理
|
||
*
|
||
* @author Joshi
|
||
* @date 2025-07-18
|
||
*/
|
||
@RequiredArgsConstructor
|
||
@Service
|
||
public class WmsStockIoServiceImpl implements IWmsStockIoService {
|
||
|
||
private final WmsStockIoMapper baseMapper;
|
||
private final WmsStockIoDetailMapper stockIoDetailMapper;
|
||
private final WmsStockMapper stockMapper;
|
||
|
||
/**
|
||
* 查询出入库单主
|
||
*/
|
||
@Override
|
||
public WmsStockIoVo queryById(Long stockIoId){
|
||
return baseMapper.selectVoById(stockIoId);
|
||
}
|
||
|
||
/**
|
||
* 查询出入库单主列表
|
||
*/
|
||
@Override
|
||
public TableDataInfo<WmsStockIoVo> queryPageList(WmsStockIoBo bo, PageQuery pageQuery) {
|
||
LambdaQueryWrapper<WmsStockIo> lqw = buildQueryWrapper(bo);
|
||
Page<WmsStockIoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||
return TableDataInfo.build(result);
|
||
}
|
||
|
||
/**
|
||
* 查询出入库单主列表
|
||
*/
|
||
@Override
|
||
public List<WmsStockIoVo> queryList(WmsStockIoBo bo) {
|
||
LambdaQueryWrapper<WmsStockIo> lqw = buildQueryWrapper(bo);
|
||
return baseMapper.selectVoList(lqw);
|
||
}
|
||
|
||
private LambdaQueryWrapper<WmsStockIo> buildQueryWrapper(WmsStockIoBo bo) {
|
||
Map<String, Object> params = bo.getParams();
|
||
LambdaQueryWrapper<WmsStockIo> lqw = Wrappers.lambdaQuery();
|
||
lqw.eq(StringUtils.isNotBlank(bo.getStockIoCode()), WmsStockIo::getStockIoCode, bo.getStockIoCode());
|
||
lqw.eq(StringUtils.isNotBlank(bo.getIoType()), WmsStockIo::getIoType, bo.getIoType());
|
||
lqw.eq(StringUtils.isNotBlank(bo.getBizType()), WmsStockIo::getBizType, bo.getBizType());
|
||
lqw.eq(bo.getStatus() != null, WmsStockIo::getStatus, bo.getStatus());
|
||
return lqw;
|
||
}
|
||
|
||
/**
|
||
* 新增出入库单主
|
||
*/
|
||
@Override
|
||
public Boolean insertByBo(WmsStockIoBo bo) {
|
||
WmsStockIo add = BeanUtil.toBean(bo, WmsStockIo.class);
|
||
validEntityBeforeSave(add);
|
||
boolean flag = baseMapper.insert(add) > 0;
|
||
if (flag) {
|
||
bo.setStockIoId(add.getStockIoId());
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
/**
|
||
* 修改出入库单主
|
||
*/
|
||
@Override
|
||
public Boolean updateByBo(WmsStockIoBo bo) {
|
||
WmsStockIo update = BeanUtil.toBean(bo, WmsStockIo.class);
|
||
validEntityBeforeSave(update);
|
||
return baseMapper.updateById(update) > 0;
|
||
}
|
||
|
||
/**
|
||
* 保存前的数据校验
|
||
*/
|
||
private void validEntityBeforeSave(WmsStockIo entity){
|
||
//TODO 做一些数据校验,如唯一约束
|
||
}
|
||
|
||
/**
|
||
* 批量删除出入库单主
|
||
*/
|
||
@Override
|
||
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
||
if(isValid){
|
||
//TODO 做一些业务上的校验,判断是否需要校验
|
||
}
|
||
return baseMapper.deleteBatchIds(ids) > 0;
|
||
}
|
||
|
||
/**
|
||
* 审核出入库/移库单,变更库存,含库存校验
|
||
*/
|
||
@Override
|
||
@Transactional(rollbackFor = Exception.class)
|
||
public Boolean auditStockIo(Long stockIoId) {
|
||
WmsStockIo stockIo = baseMapper.selectById(stockIoId);
|
||
if (stockIo == null || stockIo.getStatus() == null || stockIo.getStatus() != 1) {
|
||
// 只允许审核“已提交”状态的单据
|
||
throw new RuntimeException("单据不存在或状态不允许审核");
|
||
}
|
||
List<WmsStockIoDetail> details = stockIoDetailMapper.selectList(
|
||
Wrappers.<WmsStockIoDetail>lambdaQuery().eq(WmsStockIoDetail::getStockIoId, stockIoId)
|
||
);
|
||
if (details == null || details.isEmpty()) {
|
||
throw new RuntimeException("单据明细不能为空");
|
||
}
|
||
for (WmsStockIoDetail detail : details) {
|
||
String ioType = stockIo.getIoType();
|
||
if ("in".equals(ioType)) {
|
||
// 入库:目标库位库存增加
|
||
changeStock(detail.getWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), true, detail.getUnit());
|
||
} else if ("out".equals(ioType)) {
|
||
// 出库:目标库位库存减少
|
||
changeStock(detail.getWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), false, detail.getUnit());
|
||
} else if ("transfer".equals(ioType)) {
|
||
// 移库:fromWarehouseId减少,warehouseId增加
|
||
if (detail.getFromWarehouseId() == null) {
|
||
throw new RuntimeException("移库明细缺少源库位ID");
|
||
}
|
||
// 先减少源库位
|
||
changeStock(detail.getFromWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), false, detail.getUnit());
|
||
// 再增加目标库位
|
||
changeStock(detail.getWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), true, detail.getUnit());
|
||
} else {
|
||
throw new RuntimeException("未知的出入库类型");
|
||
}
|
||
}
|
||
// 更新单据状态为已审核(2)
|
||
stockIo.setStatus(2);
|
||
baseMapper.updateById(stockIo);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 撤销出入库/移库单,库存回滚
|
||
*/
|
||
@Override
|
||
@RepeatSubmit()
|
||
@Transactional(rollbackFor = Exception.class)
|
||
public Boolean cancelStockIo(Long stockIoId) {
|
||
WmsStockIo stockIo = baseMapper.selectById(stockIoId);
|
||
if (stockIo == null || stockIo.getStatus() == null || stockIo.getStatus() != 2) {
|
||
throw new ServiceException("只能撤销已审核的单据");
|
||
}
|
||
List<WmsStockIoDetail> details = stockIoDetailMapper.selectList(
|
||
Wrappers.<WmsStockIoDetail>lambdaQuery().eq(WmsStockIoDetail::getStockIoId, stockIoId)
|
||
);
|
||
if (details == null || details.isEmpty()) {
|
||
throw new ServiceException("单据明细不能为空");
|
||
}
|
||
for (WmsStockIoDetail detail : details) {
|
||
String ioType = stockIo.getIoType();
|
||
if ("in".equals(ioType)) {
|
||
// 入库撤销:目标库位库存减少
|
||
changeStock(detail.getWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), false, detail.getUnit());
|
||
} else if ("out".equals(ioType)) {
|
||
// 出库撤销:目标库位库存增加
|
||
changeStock(detail.getWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), true, detail.getUnit());
|
||
} else if ("transfer".equals(ioType)) {
|
||
if (detail.getFromWarehouseId() == null) {
|
||
throw new ServiceException("移库明细缺少源库位ID");
|
||
}
|
||
// 源库位库存增加
|
||
changeStock(detail.getFromWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), true, detail.getUnit());
|
||
// 目标库位库存减少
|
||
changeStock(detail.getWarehouseId(), detail.getItemType(), detail.getItemId(), detail.getBatchNo(), detail.getQuantity(), false, detail.getUnit());
|
||
} else {
|
||
throw new ServiceException("未知的出入库类型");
|
||
}
|
||
}
|
||
// 更新单据状态为已撤销(3)
|
||
stockIo.setStatus(3);
|
||
baseMapper.updateById(stockIo);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 根据ioType和stockIoId联查明细
|
||
*/
|
||
@Override
|
||
public List<WmsStockIoDetailVo> detailByTypeAndId(String ioType, Long stockIoId) {
|
||
WmsStockIo stockIo = baseMapper.selectById(stockIoId);
|
||
if (stockIo == null) {
|
||
throw new ServiceException("单据不存在");
|
||
}
|
||
if (!ioType.equals(stockIo.getIoType())) {
|
||
throw new ServiceException("单据类型不匹配");
|
||
}
|
||
List<WmsStockIoDetail> details = stockIoDetailMapper.selectList(
|
||
Wrappers.<WmsStockIoDetail>lambdaQuery().eq(WmsStockIoDetail::getStockIoId, stockIoId)
|
||
);
|
||
if (details == null || details.isEmpty()) {
|
||
return java.util.Collections.emptyList();
|
||
}
|
||
// 转VO
|
||
List<WmsStockIoDetailVo> voList = new ArrayList<>();
|
||
for (WmsStockIoDetail detail : details) {
|
||
WmsStockIoDetailVo vo = new WmsStockIoDetailVo();
|
||
BeanUtils.copyProperties(detail, vo);
|
||
voList.add(vo);
|
||
}
|
||
return voList;
|
||
}
|
||
|
||
/**
|
||
* 库存增减,isAdd=true为增加,false为减少,减少时校验库存是否足够
|
||
*/
|
||
private void changeStock(Long warehouseId, String itemType, Long itemId, String batchNo, BigDecimal quantity, boolean isAdd, String unit) {
|
||
WmsStock stock = stockMapper.selectOne(Wrappers.<WmsStock>lambdaQuery()
|
||
.eq(WmsStock::getWarehouseId, warehouseId)
|
||
.eq(WmsStock::getItemType, itemType)
|
||
.eq(WmsStock::getItemId, itemId)
|
||
.eq(WmsStock::getBatchNo, batchNo)
|
||
.last("limit 1"));
|
||
if (stock == null) {
|
||
if (!isAdd) {
|
||
throw new RuntimeException("库存不足,无法出库/移库");
|
||
}
|
||
// 新增库存记录
|
||
stock = new WmsStock();
|
||
stock.setWarehouseId(warehouseId);
|
||
stock.setItemType(itemType);
|
||
stock.setItemId(itemId);
|
||
stock.setBatchNo(batchNo);
|
||
stock.setQuantity(quantity);
|
||
stock.setUnit(unit);
|
||
stockMapper.insert(stock);
|
||
} else {
|
||
BigDecimal newQty = isAdd ? stock.getQuantity().add(quantity) : stock.getQuantity().subtract(quantity);
|
||
if (newQty.compareTo(BigDecimal.ZERO) < 0) {
|
||
throw new RuntimeException("库存不足,无法出库/移库");
|
||
}
|
||
stock.setQuantity(newQty);
|
||
stockMapper.updateById(stock);
|
||
}
|
||
}
|
||
}
|