1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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);
    }
}