From b0e5a4030dc7ed6af1853b04aa3e8ac49b17d0b4 Mon Sep 17 00:00:00 2001
From: xuhy <3313886187@qq.com>
Date: 星期一, 09 二月 2026 18:03:18 +0800
Subject: [PATCH] 定时任务修改

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java |  183 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 183 insertions(+), 0 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java
index 8bd772d..890a677 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/TaskUtil.java
@@ -2,11 +2,20 @@
 
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.core.domain.BaseModel;
 import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.http.HttpUtils;
+import com.ruoyi.system.mapper.TErpSupplierOutboundGoodsMapper;
+import com.ruoyi.system.mapper.TErpSupplierOutboundMapper;
+import com.ruoyi.system.mapper.TErpSupplierWarehousingBatchMapper;
+import com.ruoyi.system.mapper.TErpSupplierWarehousingMapper;
 import com.ruoyi.system.model.*;
 import com.ruoyi.system.service.*;
 import com.ruoyi.system.utils.util.AIUtil;
@@ -19,9 +28,11 @@
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
+import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -49,6 +60,16 @@
     private TSysOtherConfigService sysOtherConfigService;
     @Autowired
     private TSysCloudConfigService sysCloudConfigService;
+    @Autowired
+    private TErpProcurementGoodsService erpProcurementGoodsService;
+    @Resource
+    private TErpSupplierOutboundMapper erpSupplierOutboundMapper;
+    @Resource
+    private TErpSupplierOutboundGoodsMapper erpSupplierOutboundGoodsMapper;
+    @Resource
+    private TErpSupplierWarehousingMapper erpSupplierWarehousingMapper;
+    @Resource
+    private TErpSupplierWarehousingBatchMapper erpSupplierWarehousingBatchMapper;
 
 //    @Scheduled(fixedRate = 86460000)
 //    @Scheduled(cron = "10 0 0 * * ?")
@@ -209,6 +230,7 @@
                     for (TErpProcurement procurement : procurements) {
                         procurement.setLogisticsNo(map.get(procurement.getProcurementCode()));
                         procurement.setStatus(4);
+                        procurement.setSendTime(LocalDateTime.now());
                     }
                     erpProcurementService.updateBatchById(procurements);
                 }
@@ -216,12 +238,173 @@
                 JSONObject logisticsSyncAck = LogisticsSyncAck.logisticsSyncAck(sysCloudConfig.getSid(), sysCloudConfig.getAppKey(),
                         sysCloudConfig.getAppSecret(), sysCloudConfig.getUrl(), logistics_list);
                 log.info("物流同步成功");
+                if(!CollectionUtils.isEmpty(procurements)){
+                    List<String> supplierIds = procurements.stream().map(TErpProcurement::getSupplierId).collect(Collectors.toList());
+                    List<String> ids = procurements.stream().map(TErpProcurement::getId).collect(Collectors.toList());
+                    // 处理发货流程
+                    detailProcurementSend(supplierIds,ids);
+                }
             }
 
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
+    public void detailProcurementSend(List<String> clinicSupplierId, List<String> ids) {
+        // 查出这个采购的下级商品
+        List<TErpProcurementGoods> list = erpProcurementGoodsService.list(Wrappers.lambdaQuery(TErpProcurementGoods.class)
+                .in(TErpProcurementGoods::getProcurementId, ids));
+        List<TErpSupplierWarehousing> tErpSupplierWarehousings = erpSupplierWarehousingMapper.selectList(new LambdaQueryWrapper<TErpSupplierWarehousing>()
+                .in(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.setGoodsId(tErpSupplierWarehousing.getGoodsId());
+                        tErpSupplierOutbound.setSupplierId(tErpSupplierWarehousing.getSupplierId());
+                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
+                        tErpSupplierOutbound.setOutboundType(7);
+                        //TODO 确定这个是采购价还是销售价格
+                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
+
+                        TErpSupplierOutbound last = erpSupplierOutboundMapper.selectOne(new LambdaQueryWrapper<TErpSupplierOutbound>().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpSupplierOutbound::getOutboundNumber).last("limit 1"));
+                        if(last==null){
+                            tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow()+"00001");
+                        }else {
+                            String lastProcurementCode = last.getOutboundNumber();
+                            String datePart = "G" + DateUtils.dateTimeNow();
+
+                            // 直接获取后五位数字并+1,不考虑是否同一天
+                            try {
+                                String numberPart = lastProcurementCode.substring(Math.max(0, lastProcurementCode.length() - 5));
+                                int number = Integer.parseInt(numberPart);
+                                if (number < 99999) {
+                                    number++;
+                                } else {
+                                    number = 2; // 达到99999后重置为00002
+                                }
+                                tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number));
+                            } catch (NumberFormatException e) {
+                                // 如果解析失败,默认从00001开始
+                                tErpSupplierOutbound.setOutboundNumber(datePart + "00001");
+                            }
+                        }
+                        try {
+                            erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
+                        }catch (Exception e){
+                            throw new RuntimeException("网络繁忙,请重试");
+                        }
+
+                        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)));
+                        tErpSupplierOutboundGoods.setProcurementGoodsId(tErpProcurementGoods.getId());
+                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
+                        continue outer;
+                    }
+
+                    if(count<purchaseCount){
+                        purchaseCount = purchaseCount - count;
+                        // 添加出库记录
+                        TErpSupplierOutbound tErpSupplierOutbound= new TErpSupplierOutbound();
+                        tErpSupplierOutbound.setGoodsId(tErpSupplierWarehousing.getGoodsId());
+                        tErpSupplierOutbound.setSupplierId(tErpSupplierWarehousing.getSupplierId());
+                        tErpSupplierOutbound.setWarehouseId(tErpSupplierWarehousing.getWarehouseId());
+                        tErpSupplierOutbound.setOutboundType(7);
+                        //TODO 确定这个是采购价还是销售价格
+                        tErpSupplierOutbound.setTotalMoney(tErpProcurementGoods.getPurchasePrice().multiply(new BigDecimal(purchaseCount)));
+                        TErpSupplierOutbound last = erpSupplierOutboundMapper.selectOne(new LambdaQueryWrapper<TErpSupplierOutbound>().orderByDesc(BaseModel::getCreateTime).orderByDesc(TErpSupplierOutbound::getOutboundNumber).last("limit 1"));
+                        if(last==null){
+                            tErpSupplierOutbound.setOutboundNumber("G" + DateUtils.dateTimeNow()+"00001");
+                        }else {
+                            String lastProcurementCode = last.getOutboundNumber();
+                            String datePart = "G" + DateUtils.dateTimeNow();
+
+                            // 直接获取后五位数字并+1,不考虑是否同一天
+                            try {
+                                String numberPart = lastProcurementCode.substring(Math.max(0, lastProcurementCode.length() - 5));
+                                int number = Integer.parseInt(numberPart);
+                                if (number < 99999) {
+                                    number++;
+                                } else {
+                                    number = 2; // 达到99999后重置为00002
+                                }
+                                tErpSupplierOutbound.setOutboundNumber(datePart + String.format("%05d", number));
+                            } catch (NumberFormatException e) {
+                                // 如果解析失败,默认从00001开始
+                                tErpSupplierOutbound.setOutboundNumber(datePart + "00001");
+                            }
+                        }
+
+                        try {
+
+                            erpSupplierOutboundMapper.insert(tErpSupplierOutbound);
+                        }catch (Exception e){
+                            throw new RuntimeException("网络繁忙,请重试");
+                        }
+
+                        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)));
+                        tErpSupplierOutboundGoods.setProcurementGoodsId(tErpProcurementGoods.getId());
+                        erpSupplierOutboundGoodsMapper.insert(tErpSupplierOutboundGoods);
+                    }
+
+                }
+
+            }
+            if(purchaseCount>0){
+                throw new RuntimeException("商品供应商库存不足,请先入库");
+            }
+
+
+        }
+    }
 
     /**
      *  供应商周期核查

--
Gitblit v1.7.1