liujie
2025-10-14 a5a5dddc7c8d7ec5c2c6b5219a88081172809327
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TErpProcurementServiceImpl.java
@@ -87,6 +87,18 @@
    @Autowired
    private TErpProcurementGoodsService erpProcurementGoodsService;
    @Resource
    private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper;
    @Resource
    private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper;
    @Resource
    private TErpSupplierOutboundMapper erpSupplierOutboundMapper;
    @Resource
    private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper;
    @Override
    public PageInfo<TErpProcurementVo> pageList(TErpProcurementQuery query, SysUser user) {
@@ -379,6 +391,9 @@
            BigDecimal add = BigDecimal.ZERO;
            BigDecimal supplierCommissionPrice = BigDecimal.ZERO;
            for (AddProcurementDto dto : value) {
                TErpProcurementGoods tErpProcurementGoods = new TErpProcurementGoods();
                tErpProcurementGoods.setProcurementId(tErpProcurement.getId());
@@ -398,6 +413,7 @@
                tErpProcurementGoods.setSupplierId(supplierClinicId);
                tErpProcurementGoods.setRecvMerchantNo(supplier.getRecvMerchantNo());
                tErpProcurementGoods.setSupplierMoney(dto.getSalesAmount().subtract(dto.getPlatformCommissionPrice()));
                supplierCommissionPrice = supplierCommissionPrice.add(tErpProcurementGoods.getSupplierMoney().multiply(BigDecimal.valueOf(dto.getPurchaseCount())));
                if (tErpProcurementGoods.getSupplierMoney().doubleValue() < 0) {
                    throw new RuntimeException("平台抽成不能大于售卖价格");
                }
@@ -410,6 +426,7 @@
            if (tErpProcurement.getMoney().doubleValue() < 0) {
                throw new RuntimeException("金额设置错误,请联系平台管理员");
            }
            tErpProcurement.setSupplierMoney(supplierCommissionPrice);
            this.save(tErpProcurement);
            tErpProcurementGoods1.forEach(e -> e.setProcurementId(tErpProcurement.getId()));
            erpProcurementGoodsService.saveBatch(tErpProcurementGoods1);
@@ -449,4 +466,105 @@
        return detailProcurementVO;
    }
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void detailProcurementSend(String clinicSupplierId, SysUser user, String id) {
        // 查出这个采购的下级商品
        List<TErpProcurementGoods> list = erpProcurementGoodsService.list(new QueryWrapper<TErpProcurementGoods>().eq("procurement_id", id));
        List<TErpSupplierWarehousing> tErpSupplierWarehousings = erpSupplierWarehousingMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousing>()
                .eq(TErpSupplierWarehousing::getSupplierId, clinicSupplierId).in(TErpSupplierWarehousing::getGoodsId, list.stream().map(TErpProcurementGoods::getGoodsId).collect(Collectors.toList()))
                .orderByAsc(BaseModel::getCreateTime));
        if(tErpSupplierWarehousings.isEmpty()){
            throw new RuntimeException("商品供应商未入库,请先入库");
        }
        List<TErpSupplierWarehousingBatch> tErpSupplierWarehousingBatches = erpSupplierWarehousingBatchMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousingBatch>().in(TErpSupplierWarehousingBatch::getWarehousingId, tErpSupplierWarehousings.stream().map(TErpSupplierWarehousing::getId).collect(Collectors.toList())));
        // 获取所有的出库记录
        List<TErpSupplierOutboundGoods> tErpSupplierOutboundGoodsLists = erpSupplierOutboundGoodsMapper.selectList(new LambdaQueryWrapper<TErpSupplierOutboundGoods>().in(TErpSupplierOutboundGoods::getWarehousingBatchId, tErpSupplierWarehousingBatches.stream().map(TErpSupplierWarehousingBatch::getId).collect(Collectors.toList())));
        outer:for (TErpProcurementGoods tErpProcurementGoods : list) {
            // 分别入库
            String goodsId = tErpProcurementGoods.getGoodsId();
            // 查询这个供应商的 该商品是否入过库 未入库提示  出库判断库存 先找先入库的
            List<TErpSupplierWarehousing> collect = tErpSupplierWarehousings.stream().filter(e -> e.getGoodsId().equals(goodsId)).collect(Collectors.toList());
            if(collect.isEmpty()){
                throw new RuntimeException("商品供应商未入库,请先入库");
            }
            // 需要出库的数量
            Integer purchaseCount = tErpProcurementGoods.getPurchaseCount();
            if(purchaseCount==0){
                continue;
            }
            for (TErpSupplierWarehousing tErpSupplierWarehousing : collect) {
                List<TErpSupplierWarehousingBatch> warehousingBatches = tErpSupplierWarehousingBatches.stream().filter(e -> e.getWarehousingId().equals(tErpSupplierWarehousing.getId())).collect(Collectors.toList());
                for (TErpSupplierWarehousingBatch warehousingBatch : warehousingBatches) {
                    // 已出库数量
                    int sum = tErpSupplierOutboundGoodsLists.stream().filter(e -> e.getWarehousingBatchId().equals(warehousingBatch.getId())).mapToInt(TErpSupplierOutboundGoods::getOutboundCount).sum();
                    // 该批次剩余的数量
                    int count = warehousingBatch.getWarehousingNumber() - sum;
                    // 该批次没有数量 找下个批次
                    if(count==0 ){
                        continue;
                    }
                    // 如果大于这个数量 则使用这个批次
                    if(count>=purchaseCount){
                        // 添加出库记录
                        TErpSupplierOutbound tErpSupplierOutbound= new TErpSupplierOutbound();
                        tErpSupplierOutbound.setSupplierId(clinicSupplierId);
                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                        tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                        tErpSupplierOutboundGoods.setWarehousingBatchId(warehousingBatch.getId());
                        tErpSupplierOutboundGoods.setOutboundCount(purchaseCount);
                        tErpSupplierOutboundGoods.setTotalPrice(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
                        continue outer;
                    }
                    if(count<purchaseCount){
                        purchaseCount = purchaseCount - count;
                        // 添加出库记录
                        TErpSupplierOutbound tErpSupplierOutbound= new TErpSupplierOutbound();
                        tErpSupplierOutbound.setSupplierId(clinicSupplierId);
                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
                        tErpSupplierOutbound.setOutboundType(7);
                        //TODO 确定这个是采购价还是销售价格
                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow());
                        erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
                        TErpSupplierOutboundGoods tErpSupplierOutboundGoods = new TErpSupplierOutboundGoods();
                        tErpSupplierOutboundGoods.setOutboundId(tErpSupplierOutbound.getId());
                        tErpSupplierOutboundGoods.setWarehousingId(tErpSupplierWarehousing.getId());
                        tErpSupplierOutboundGoods.setWarehousingBatchId(warehousingBatch.getId());
                        tErpSupplierOutboundGoods.setOutboundCount(purchaseCount);
                        tErpSupplierOutboundGoods.setTotalPrice(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
                    }
                }
            }
            if(purchaseCount>0){
                throw new RuntimeException("商品供应商库存不足,请先入库");
            }
        }
    }
}