gongchunyi
10 小时以前 6c95c2f6a3602fe6f92898dd322c20bbe955e69d
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -9,6 +9,10 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.service.AccountIncomeService;
import com.ruoyi.approve.service.IApproveProcessService;
import com.ruoyi.approve.vo.ApproveProcessVO;
import com.ruoyi.approve.pojo.ApproveProcess;
import com.ruoyi.common.enums.ApproveTypeEnum;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.mapper.ProductMapper;
import com.ruoyi.basic.mapper.ProductModelMapper;
@@ -103,6 +107,9 @@
    private static final String LOCK_PREFIX = "contract_no_lock:";
    private static final long LOCK_WAIT_TIMEOUT = 10; // 锁等待超时时间(秒)
    private static final long LOCK_EXPIRE_TIME = 30;  // 锁自动过期时间(秒)
    private static final int INBOUND_BIZ_TYPE_WEB = 1;
    private static final int INBOUND_BIZ_TYPE_SCAN_QUALIFIED = 2;
    private static final int INBOUND_BIZ_TYPE_SCAN_UNQUALIFIED = 3;
    private final AccountIncomeService accountIncomeService;
    private final SalesLedgerMapper salesLedgerMapper;
    private final CustomerMapper customerMapper;
@@ -144,6 +151,8 @@
    private final StockInRecordService stockInRecordService;
    private final StockOutRecordService stockOutRecordService;
    private final StockUtils stockUtils;
    @Autowired
    private IApproveProcessService approveProcessService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
@@ -1857,6 +1866,9 @@
        if (ledger.getStockStatus() == null) {
            throw new ServiceException("入库失败,销售订单入库状态异常");
        }
        if (ledger.getStockStatus() == 3) {
            throw new ServiceException("入库失败,该销售订单正在入库审批中,请勿重复提交");
        }
        if (ledger.getStockStatus() == 2) {
            throw new ServiceException("入库失败,该销售订单已入库,请勿重复入库");
        }
@@ -1869,6 +1881,42 @@
        if (salesLedgerProducts == null || salesLedgerProducts.isEmpty()) {
            throw new ServiceException("入库失败,未查询到该销售订单的销售产品");
        }
        String approveUserIds = resolveApproveUserIds(dto.getApproveUserIds(), ledger.getId(), INBOUND_BIZ_TYPE_WEB);
        if (StringUtils.isEmpty(approveUserIds)) {
            throw new ServiceException("入库失败,请选择审批人");
        }
        String productIds = products.stream().map(String::valueOf).collect(Collectors.joining(","));
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ApproveProcessVO approveProcessVO = new ApproveProcessVO();
        approveProcessVO.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
        approveProcessVO.setApproveReason("入库审批:" + ledger.getSalesContractNo());
        approveProcessVO.setApproveRemark("salesStock:" + ledger.getId() + ":" + productIds);
        approveProcessVO.setApproveUserIds(approveUserIds);
        approveProcessVO.setApproveUser(loginUser.getUserId());
        approveProcessVO.setApproveTime(LocalDate.now().toString());
        try {
            approveProcessService.addApprove(approveProcessVO);
        } catch (Exception e) {
            throw new ServiceException("入库审批发起失败:" + e.getMessage());
        }
        // 审批中
        ledger.setStockStatus(3);
        baseMapper.updateById(ledger);
    }
    @Override
    public void executeSalesStockApproved(Long salesLedgerId, List<Long> products) {
        SalesProductStockDto dto = new SalesProductStockDto();
        dto.setSalesLedgerId(salesLedgerId);
        dto.setSalesLedgerProducts(products);
        SalesLedger ledger = baseMapper.selectById(dto.getSalesLedgerId());
        if (ledger == null) {
            throw new ServiceException("入库失败,销售订单不存在");
        }
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery().in(SalesLedgerProduct::getId, products));
        for (SalesLedgerProduct product : salesLedgerProducts) {
            if (!Objects.equals(product.getSalesLedgerId(), ledger.getId())) {
                throw new ServiceException("入库失败,存在不属于当前销售订单的产品");
@@ -1978,11 +2026,71 @@
            }
            inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add);
        }
        List<SalesLedgerProduct> selectedProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery()
                .in(SalesLedgerProduct::getId, inboundQtyByLineId.keySet()));
        if (CollectionUtils.isEmpty(selectedProducts)) {
            throw new ServiceException("入库失败,未查询到入库产品");
        }
        if (selectedProducts.size() != inboundQtyByLineId.size()) {
            throw new ServiceException("入库失败,部分产品不存在");
        }
        for (SalesLedgerProduct selectedProduct : selectedProducts) {
            if (!Objects.equals(selectedProduct.getSalesLedgerId(), salesLedger.getId())) {
                throw new ServiceException("入库失败,销售产品与订单不匹配");
            }
            if (!Objects.equals(selectedProduct.getType(), SaleEnum.SALE.getCode())) {
                throw new ServiceException("入库失败,仅支持销售订单产品行");
            }
            if (selectedProduct.getProductModelId() == null) {
                throw new ServiceException("入库失败,产品规格未维护,无法入库");
            }
        }
        String approveUserIds = resolveApproveUserIds(dto.getApproveUserIds(), salesLedger.getId(), INBOUND_BIZ_TYPE_SCAN_QUALIFIED);
        if (StringUtils.isEmpty(approveUserIds)) {
            throw new ServiceException("入库失败,请选择审批人");
        }
        String lines = inboundQtyByLineId.entrySet().stream()
                .map(e -> e.getKey() + "@" + e.getValue().stripTrailingZeros().toPlainString())
                .collect(Collectors.joining(","));
        String reason = "销售扫码合格入库审批:" + salesLedger.getSalesContractNo();
        String remark = "scanQualified:" + salesLedger.getId() + ":" + lines;
        ApproveProcess exist = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
                .eq(ApproveProcess::getApproveType, ApproveTypeEnum.STOCK_IN.getCode())
                .eq(ApproveProcess::getApproveRemark, remark)
                .eq(ApproveProcess::getApproveDelete, 0)
                .orderByDesc(ApproveProcess::getCreateTime)
                .last("limit 1"));
        if (exist != null && !Objects.equals(exist.getApproveStatus(), 3)) {
            throw new ServiceException("入库失败,该申请已提交审批");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ApproveProcessVO approveProcessVO = new ApproveProcessVO();
        approveProcessVO.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
        approveProcessVO.setApproveReason(reason);
        approveProcessVO.setApproveRemark(remark);
        approveProcessVO.setApproveUserIds(approveUserIds);
        approveProcessVO.setApproveUser(loginUser.getUserId());
        approveProcessVO.setApproveTime(LocalDate.now().toString());
        try {
            approveProcessService.addApprove(approveProcessVO);
        } catch (Exception e) {
            throw new ServiceException("入库审批发起失败:" + e.getMessage());
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executeSalesScanInboundApproved(Long salesLedgerId, Map<Long, BigDecimal> inboundQtyByLineId) {
        SalesLedger salesLedger = baseMapper.selectById(salesLedgerId);
        if (salesLedger == null) {
            throw new ServiceException("入库失败,销售订单不存在");
        }
        Long ledgerId = salesLedger.getId();
        for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) {
            Long salesLedgerProductId = entry.getKey();
            BigDecimal inboundThisLine = entry.getValue();
            if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
            if (inboundThisLine == null || inboundThisLine.compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId);
@@ -2065,11 +2173,71 @@
            }
            inboundQtyByLineId.merge(inbound.getId(), inboundQty, BigDecimal::add);
        }
        List<SalesLedgerProduct> selectedProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery()
                .in(SalesLedgerProduct::getId, inboundQtyByLineId.keySet()));
        if (CollectionUtils.isEmpty(selectedProducts)) {
            throw new ServiceException("不合格入库失败,未查询到入库产品");
        }
        if (selectedProducts.size() != inboundQtyByLineId.size()) {
            throw new ServiceException("不合格入库失败,部分产品不存在");
        }
        for (SalesLedgerProduct selectedProduct : selectedProducts) {
            if (!Objects.equals(selectedProduct.getSalesLedgerId(), salesLedger.getId())) {
                throw new ServiceException("不合格入库失败,销售产品与订单不匹配");
            }
            if (!Objects.equals(selectedProduct.getType(), SaleEnum.SALE.getCode())) {
                throw new ServiceException("不合格入库失败,仅支持销售订单产品行");
            }
            if (selectedProduct.getProductModelId() == null) {
                throw new ServiceException("不合格入库失败,产品规格未维护,无法入库");
            }
        }
        String approveUserIds = resolveApproveUserIds(dto.getApproveUserIds(), salesLedger.getId(), INBOUND_BIZ_TYPE_SCAN_UNQUALIFIED);
        if (StringUtils.isEmpty(approveUserIds)) {
            throw new ServiceException("不合格入库失败,请选择审批人");
        }
        String lines = inboundQtyByLineId.entrySet().stream()
                .map(e -> e.getKey() + "@" + e.getValue().stripTrailingZeros().toPlainString())
                .collect(Collectors.joining(","));
        String reason = "销售扫码不合格入库审批:" + salesLedger.getSalesContractNo();
        String remark = "scanUnqualified:" + salesLedger.getId() + ":" + lines;
        ApproveProcess exist = approveProcessService.getOne(new LambdaQueryWrapper<ApproveProcess>()
                .eq(ApproveProcess::getApproveType, ApproveTypeEnum.STOCK_IN.getCode())
                .eq(ApproveProcess::getApproveRemark, remark)
                .eq(ApproveProcess::getApproveDelete, 0)
                .orderByDesc(ApproveProcess::getCreateTime)
                .last("limit 1"));
        if (exist != null && !Objects.equals(exist.getApproveStatus(), 3)) {
            throw new ServiceException("不合格入库失败,该申请已提交审批");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ApproveProcessVO approveProcessVO = new ApproveProcessVO();
        approveProcessVO.setApproveType(ApproveTypeEnum.STOCK_IN.getCode());
        approveProcessVO.setApproveDeptId(loginUser.getCurrentDeptId());
        approveProcessVO.setApproveReason(reason);
        approveProcessVO.setApproveRemark(remark);
        approveProcessVO.setApproveUserIds(approveUserIds);
        approveProcessVO.setApproveUser(loginUser.getUserId());
        approveProcessVO.setApproveTime(LocalDate.now().toString());
        try {
            approveProcessService.addApprove(approveProcessVO);
        } catch (Exception e) {
            throw new ServiceException("不合格入库审批发起失败:" + e.getMessage());
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executeSalesScanInboundUnqualifiedApproved(Long salesLedgerId, Map<Long, BigDecimal> inboundQtyByLineId) {
        SalesLedger salesLedger = baseMapper.selectById(salesLedgerId);
        if (salesLedger == null) {
            throw new ServiceException("不合格入库失败,销售订单不存在");
        }
        Long ledgerId = salesLedger.getId();
        for (Map.Entry<Long, BigDecimal> entry : inboundQtyByLineId.entrySet()) {
            Long salesLedgerProductId = entry.getKey();
            BigDecimal inboundThisLine = entry.getValue();
            if (inboundThisLine.compareTo(BigDecimal.ZERO) == 0) {
            if (inboundThisLine == null || inboundThisLine.compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            SalesLedgerProduct dbProduct = salesLedgerProductMapper.selectById(salesLedgerProductId);
@@ -2087,7 +2255,6 @@
            }
            stockUtils.addUnStock(ledgerId, dbProduct.getId(), dbProduct.getProductModelId(), inboundThisLine,
                    StockInUnQualifiedRecordTypeEnum.SALES_SCAN_UNSTOCK_IN.getCode(), dbProduct.getId());
            BigDecimal oldUnStocked = dbProduct.getUnqualifiedStockedQuantity() == null ? BigDecimal.ZERO : dbProduct.getUnqualifiedStockedQuantity();
            dbProduct.setUnqualifiedStockedQuantity(oldUnStocked.add(inboundThisLine));
            dbProduct.fillRemainingQuantity();
@@ -2095,6 +2262,31 @@
        }
    }
    private String resolveApproveUserIds(String approveUserIds, Long salesLedgerId, Integer bizType) {
        if (StringUtils.isNotEmpty(approveUserIds)) {
            return approveUserIds;
        }
        LambdaQueryWrapper<ApproveProcess> wrapper = new LambdaQueryWrapper<ApproveProcess>()
                .eq(ApproveProcess::getApproveType, ApproveTypeEnum.STOCK_IN.getCode())
                .eq(ApproveProcess::getApproveDelete, 0)
                .orderByDesc(ApproveProcess::getCreateTime)
                .last("limit 1");
        if (Objects.equals(bizType, INBOUND_BIZ_TYPE_WEB)) {
            wrapper.likeRight(ApproveProcess::getApproveReason, "入库审批:")
                    .likeRight(ApproveProcess::getApproveRemark, "salesStock:" + salesLedgerId + ":");
        } else if (Objects.equals(bizType, INBOUND_BIZ_TYPE_SCAN_QUALIFIED)) {
            wrapper.likeRight(ApproveProcess::getApproveReason, "销售扫码合格入库审批:")
                    .likeRight(ApproveProcess::getApproveRemark, "scanQualified:" + salesLedgerId + ":");
        } else if (Objects.equals(bizType, INBOUND_BIZ_TYPE_SCAN_UNQUALIFIED)) {
            wrapper.likeRight(ApproveProcess::getApproveReason, "销售扫码不合格入库审批:")
                    .likeRight(ApproveProcess::getApproveRemark, "scanUnqualified:" + salesLedgerId + ":");
        } else {
            return null;
        }
        ApproveProcess latest = approveProcessService.getOne(wrapper);
        return latest == null ? null : latest.getApproveUserIds();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void scanOutbound(SalesScanInboundDto dto) {