package com.ruoyi.stock.service.impl;
|
|
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.StockInQualifiedRecordTypeEnum;
|
import com.ruoyi.common.exception.ServiceException;
|
import com.ruoyi.stock.dto.ProductBorrowReturnDto;
|
import com.ruoyi.stock.dto.StockInventoryDto;
|
import com.ruoyi.stock.mapper.ProductBorrowReturnMapper;
|
import com.ruoyi.stock.pojo.ProductBorrow;
|
import com.ruoyi.stock.pojo.ProductBorrowReturn;
|
import com.ruoyi.stock.service.ProductBorrowReturnService;
|
import com.ruoyi.stock.service.ProductBorrowService;
|
import com.ruoyi.stock.service.StockInventoryService;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.math.BigDecimal;
|
import java.time.LocalDateTime;
|
|
/**
|
* 产品归还记录 Service 实现类
|
*
|
* @author ruoyi
|
*/
|
@Slf4j
|
@Service
|
@RequiredArgsConstructor
|
public class ProductBorrowReturnServiceImpl extends ServiceImpl<ProductBorrowReturnMapper, ProductBorrowReturn> implements ProductBorrowReturnService {
|
|
private final ProductBorrowReturnMapper productBorrowReturnMapper;
|
private final ProductBorrowService productBorrowService;
|
private final StockInventoryService stockInventoryService;
|
|
@Override
|
public IPage<ProductBorrowReturnDto> listPage(Page<ProductBorrowReturnDto> page, ProductBorrowReturnDto dto) {
|
return productBorrowReturnMapper.listPage(page, dto);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Boolean add(ProductBorrowReturnDto dto) {
|
// 1. 查询领用记录
|
ProductBorrow borrow = productBorrowService.getById(dto.getBorrowId());
|
if (borrow == null) {
|
throw new ServiceException("领用记录不存在");
|
}
|
|
// 2. 检查领用记录状态(已通过的记录才能归还)
|
if (borrow.getApprovalStatus() != 1) {
|
throw new ServiceException("领用记录未完成出库,无法归还");
|
}
|
if (borrow.getStatus() == 2) {
|
throw new ServiceException("该领用记录已全部归还");
|
}
|
|
// 3. 计算剩余可归还数量
|
BigDecimal returnedQty = borrow.getReturnedQuantity() != null ? borrow.getReturnedQuantity() : BigDecimal.ZERO;
|
BigDecimal remainingQty = borrow.getBorrowQuantity().subtract(returnedQty);
|
|
// 4. 验证归还数量
|
if (dto.getReturnQuantity().compareTo(remainingQty) > 0) {
|
throw new ServiceException("归还数量不能大于剩余可归还数量:" + remainingQty);
|
}
|
|
// 5. 设置必要字段
|
dto.setProductModelId(borrow.getProductModelId());
|
dto.setBatchNo(borrow.getBatchNo());
|
dto.setReturnTime(LocalDateTime.now());
|
|
// 6. 保存归还记录
|
save(dto);
|
|
// 7. 更新领用记录的已归还数量和状态
|
BigDecimal newReturnedQty = returnedQty.add(dto.getReturnQuantity());
|
borrow.setReturnedQuantity(newReturnedQty);
|
|
// 判断是否全部归还
|
if (newReturnedQty.compareTo(borrow.getBorrowQuantity()) >= 0) {
|
borrow.setStatus(2); // 已全部归还
|
} else if (newReturnedQty.compareTo(BigDecimal.ZERO) > 0) {
|
borrow.setStatus(1); // 部分归还
|
}
|
productBorrowService.updateById(borrow);
|
|
// 8. 增加库存(入库),设置审批状态为已通过,自动审核
|
StockInventoryDto stockInDto = new StockInventoryDto();
|
stockInDto.setProductModelId(borrow.getProductModelId());
|
stockInDto.setBatchNo(borrow.getBatchNo());
|
stockInDto.setQualitity(dto.getReturnQuantity());
|
stockInDto.setRecordType(String.valueOf(StockInQualifiedRecordTypeEnum.PRODUCT_BORROW_RETURN.getCode()));
|
stockInDto.setRecordId(dto.getId());
|
stockInDto.setRemark("产品归还入库,领用单号:" + borrow.getBorrowNo());
|
// 设置审批状态为已通过,自动审核
|
stockInDto.setApprovalStatus(1);
|
stockInventoryService.addstockInventory(stockInDto);
|
|
log.info("产品归还成功,领用单号:{},归还数量:{}", borrow.getBorrowNo(), dto.getReturnQuantity());
|
return true;
|
}
|
|
@Override
|
public IPage<ProductBorrowReturnDto> listByBorrowId(Page<ProductBorrowReturnDto> page, Long borrowId) {
|
return productBorrowReturnMapper.listByBorrowId(page, borrowId);
|
}
|
}
|