Browse Source

fix:增加待验收订单状态

super-yimizi 1 month ago
parent
commit
1ef64b1107

+ 10 - 4
application/api/controller/inspection/Index.php

@@ -31,6 +31,7 @@ class Index extends Base
       * @ApiMethod   (POST)
       * @ApiMethod   (POST)
       * @ApiRoute    (/api/inspection/index/submitTask)
       * @ApiRoute    (/api/inspection/index/submitTask)
       * @ApiParams   (name="order_id", type="integer", required=true, description="订单ID")
       * @ApiParams   (name="order_id", type="integer", required=true, description="订单ID")
+      * @ApiParams   (name="order_goods_id", type="integer", required=true, description="订单商品ID")
       * @ApiParams   (name="inspector_id", type="integer", required=true, description="验货员ID")
       * @ApiParams   (name="inspector_id", type="integer", required=true, description="验货员ID")
       * @ApiParams   (name="remark", type="string", required=false, description="备注信息")
       * @ApiParams   (name="remark", type="string", required=false, description="备注信息")
       * @ApiParams   (name="images", type="string", required=false, description="任务图片,多张用逗号分隔")
       * @ApiParams   (name="images", type="string", required=false, description="任务图片,多张用逗号分隔")
@@ -64,6 +65,10 @@ class Index extends Base
              $this->error('订单ID不能为空');
              $this->error('订单ID不能为空');
          }
          }
          
          
+         if (empty($params['order_goods_id'])) {
+             $this->error('订单商品ID不能为空');
+         }
+         
          $params['inspector_id'] = $this->auth->id;
          $params['inspector_id'] = $this->auth->id;
          if (empty($params['results']) || !is_array($params['results'])) {
          if (empty($params['results']) || !is_array($params['results'])) {
              $this->error('验货结果不能为空');
              $this->error('验货结果不能为空');
@@ -72,6 +77,7 @@ class Index extends Base
          // 组装任务数据
          // 组装任务数据
          $taskData = [
          $taskData = [
              'order_id' => $params['order_id'],
              'order_id' => $params['order_id'],
+             'order_goods_id' => $params['order_goods_id'],
              'inspector_id' => $params['inspector_id'],
              'inspector_id' => $params['inspector_id'],
              'remark' => $params['remark'] ?? '',
              'remark' => $params['remark'] ?? '',
              'images' => $params['images'] ?? '',
              'images' => $params['images'] ?? '',
@@ -105,12 +111,12 @@ class Index extends Base
          }
          }
          
          
          // 调用服务提交验货任务
          // 调用服务提交验货任务
-         $result = InspectService::submitInspectionTask($taskData, $resultData);
+         list($code, $msg, $data) = InspectService::submitInspectionTask($taskData, $resultData);
          
          
-         if ($result['code'] == 1) {
-             $this->success($result['msg'], $result['data']);
+         if ($code == 1) {
+             $this->success($msg, $data);
          } else {
          } else {
-             $this->error($result['msg']);
+             $this->error($msg);
          }
          }
      }
      }
 
 

+ 67 - 55
application/api/controller/inspection/Order.php

@@ -88,62 +88,74 @@ class Order extends Base
       public function send()
       public function send()
       {
       {
           // 验证请求参数
           // 验证请求参数
-        $userId = $this->auth->id;
-        $validate = new \app\api\validate\inspection\Order();
-        $params = [
-            'order_id' => $this->request->post('order_id'),
-            'express_name' => $this->request->post('express_name'),
-            'express_no' => $this->request->post('express_no'),
-            'express_image' => $this->request->post('express_image/a', [])
-        ];
-        if (!$validate->scene('send')->check($params)) {
-            $this->error($validate->getError());
-        }        
-        $orderId = $params['order_id'] ?? 0;
-        if(empty($orderId)){
-            $this->error('订单ID不能为空');
-        }
-        $order = OrderService::getByOrderId($orderId);
-        if (empty($order)) {
-            $this->error('订单不存在');
-        }
-        if ($order->user_id != $this->auth->id) {
-            $this->error('该订单不属于当前用户');
-        }
-        $expressname = $params['express_name'];
-        $expressno = $params['express_no'];
-        $expressImage = $params['express_image'];
-        $order = OrderService::getDetail($orderId, $this->auth->id);
-        if (empty($order)) {
-            $this->error('未找到订单');
-        }
-        /** @var \app\common\model\Order $order */
-        //发货 / 修改快递信息
-        if ($order->order_status == OrderEnum::STATUS_INSPECTION_PASS) {
-            $oldStatus = $order->order_status;
-            $order->express_name = $expressname;
-            $order->express_no = $expressno;
-            $order->order_status = OrderEnum::STATUS_SHIP;
-            $order->shipping_time = time();
-            $order->express_image = json_encode($expressImage);
-            $order->save();
-            // 记录管理员操作
-            $orderActionService = new OrderActionService();
-            $orderActionService->addInspectionAction(
-                $order->order_sn,
-                OrderActionEnum::ACTION_SHIP,
-                $userId,
-                '验货通过,发货,快递公司:' . $expressname . ',快递单号:' . $expressno,
-                $userId ?? 0,   
-                [
-                    'express_name' => $expressname,
-                    'express_no' => $expressno,
-                ]
-            );             
-            $this->success('发货成功');
-          }else{
-            $this->error('订单不允许确认收货');
+          $userId = $this->auth->id;
+          $validate = new \app\api\validate\inspection\Order();
+          $params = [
+              'order_id' => $this->request->post('order_id'),
+              'order_goods_id' => $this->request->post('order_goods_id'),
+              'express_name' => $this->request->post('express_name'),
+              'express_no' => $this->request->post('express_no'),
+              'express_image' => $this->request->post('express_image/a', [])
+          ];
+          if (!$validate->scene('send')->check($params)) {
+              $this->error($validate->getError());
+          }        
+          
+          $orderId = intval($params['order_id'] ?? 0);
+          $orderGoodsId = intval($params['order_goods_id'] ?? 0);
+          
+          if (empty($orderId)) {
+              $this->error('订单ID不能为空');
+          }
+          
+          if (empty($orderGoodsId)) {
+              $this->error('订单商品ID不能为空');
+          }
+          
+          // 验证订单商品是否可以发货
+          $orderData = OrderService::validateOrderGoodsForDelivery($orderId, $orderGoodsId);
+          if (!$orderData) {
+              $this->error('订单或订单商品不存在,或不允许发货');
+          }
+          
+          $order = $orderData['order'];
+          $orderGoods = $orderData['order_goods'];
+          
+          // 验证权限(根据业务需求,这里可能需要验证供应商权限而不是用户权限)
+          if ($order->user_id != $this->auth->id) {
+              $this->error('该订单不属于当前用户');
           }
           }
+          
+          $expressName = trim($params['express_name'] ?? '');
+          $expressNo = trim($params['express_no'] ?? '');
+          $expressImage = $params['express_image'] ?? [];
+          
+          // 1. 更新订单商品发货状态
+          $result = OrderService::updateOrderGoodsDeliveryStatus($orderGoodsId, $expressName, $expressNo, $expressImage);
+          if (!$result) {
+              $this->error('更新订单商品发货状态失败');
+          }
+          
+          // 2. 检查并更新订单状态
+          OrderService::updateOrderStatusByDeliveryResult($orderId, $userId);
+          
+          // 3. 记录操作日志
+          $orderActionService = new OrderActionService();
+          $orderActionService->addInspectionAction(
+              $order->order_sn,
+              OrderActionEnum::ACTION_SHIP,
+              $userId,
+              '订单商品发货,商品:' . $orderGoods->goods_title . ',快递公司:' . $expressName . ',快递单号:' . $expressNo,
+              $userId,   
+              [
+                  'order_goods_id' => $orderGoodsId,
+                  'goods_title' => $orderGoods->goods_title,
+                  'express_name' => $expressName,
+                  'express_no' => $expressNo,
+              ]
+          );             
+          
+          $this->success('发货成功');
       }
       }
 
 
       // 统计订单 
       // 统计订单 

+ 5 - 1
application/api/validate/inspection/Order.php

@@ -52,6 +52,7 @@ class Order extends Validate
         'days'            => 'integer|between:1,365',
         'days'            => 'integer|between:1,365',
         
         
         // 发货相关
         // 发货相关
+        'order_goods_id'  => 'require|integer|gt:0',
         'express_name'    => 'require|max:50',
         'express_name'    => 'require|max:50',
         'express_no'      => 'require|alphaNum|max:50', 
         'express_no'      => 'require|alphaNum|max:50', 
         'express_image'   => 'require|array|max:9|checkExpressImages',
         'express_image'   => 'require|array|max:9|checkExpressImages',
@@ -117,6 +118,9 @@ class Order extends Validate
         'days.between'              => '天数必须在1-365之间',
         'days.between'              => '天数必须在1-365之间',
         
         
         // 发货相关提示
         // 发货相关提示
+        'order_goods_id.require'    => '订单商品ID不能为空',
+        'order_goods_id.integer'    => '订单商品ID必须是整数',
+        'order_goods_id.gt'         => '订单商品ID必须大于0',
         'express_name.require'      => '快递公司不能为空',
         'express_name.require'      => '快递公司不能为空',
         'express_name.max'          => '快递公司名称不能超过50个字符',
         'express_name.max'          => '快递公司名称不能超过50个字符',
         'express_no.require'        => '快递单号不能为空',
         'express_no.require'        => '快递单号不能为空',
@@ -149,7 +153,7 @@ class Order extends Validate
         'trend'       => ['type', 'days'],
         'trend'       => ['type', 'days'],
         
         
         // 发货相关场景
         // 发货相关场景
-        'send'        => ['order_id', 'express_name', 'express_no', 'express_image'],
+        'send'        => ['order_id', 'order_goods_id', 'express_name', 'express_no', 'express_image'],
     ];
     ];
 
 
 
 

+ 6 - 0
application/common/Enum/OrderEnum.php

@@ -156,6 +156,7 @@ class OrderEnum
     const SHOW_TYPE_WAIT_DELIVERY = 2;//待发货订单
     const SHOW_TYPE_WAIT_DELIVERY = 2;//待发货订单
     const SHOW_TYPE_WAIT_RECEIPT = 3;//待收货订单
     const SHOW_TYPE_WAIT_RECEIPT = 3;//待收货订单
     const SHOW_TYPE_WAIT_COMMENT = 4;//待评价订单
     const SHOW_TYPE_WAIT_COMMENT = 4;//待评价订单
+    const SHOW_TYPE_WAIT_INSPECT =  5;//待验货订单
 
 
 
 
     const SHOW_TYPE_STATUS_MAP = [
     const SHOW_TYPE_STATUS_MAP = [
@@ -181,6 +182,11 @@ class OrderEnum
         self::SHOW_TYPE_WAIT_COMMENT => [
         self::SHOW_TYPE_WAIT_COMMENT => [
             self::STATUS_CONFIRM
             self::STATUS_CONFIRM
         ],
         ],
+        self::SHOW_TYPE_WAIT_INSPECT => [
+            self::STATUS_INSPECTION,
+            self::STATUS_INSPECTION_PASS,
+            self::STATUS_INSPECTION_FAIL,
+        ],
     ];
     ];
 
 
     const SHOW_TYPE_WAIT_INSPECTION = 1;//待验收订单
     const SHOW_TYPE_WAIT_INSPECTION = 1;//待验收订单

+ 48 - 18
application/common/Service/InspectService.php

@@ -5,6 +5,8 @@ namespace app\common\Service;
 
 
 use think\Db;
 use think\Db;
 use app\common\enum\OrderEnum;
 use app\common\enum\OrderEnum;
+use app\common\Service\OrderService;
+use app\common\exception\BusinessException;
 
 
 class InspectService
 class InspectService
 {
 {
@@ -167,11 +169,28 @@ class InspectService
         Db::startTrans();
         Db::startTrans();
         
         
         try {
         try {
+            // 0. 验证订单和订单商品是否存在
+            $orderId = $taskData['order_id'] ?? 0;
+            $orderGoodsId = $taskData['order_goods_id'] ?? 0;
+            
+            $orderData = OrderService::validateOrderAndOrderGoods($orderId, $orderGoodsId);
+            if (!$orderData) {
+                throw new BusinessException('订单或订单商品不存在');
+            }
+            
+            // 0.1 验证是否重复提交验货任务(如果不是更新任务的话)
+            if (empty($taskData['task_id'])) {
+                $existingTask = self::checkDuplicateInspectionTask($orderId, $orderGoodsId);
+                if ($existingTask) {
+                    throw new BusinessException('该订单商品已存在验货任务,不能重复提交');
+                }
+            }
+            
             // 1. 创建或更新验货任务
             // 1. 创建或更新验货任务
             $taskId = self::saveInspectionTask($taskData);
             $taskId = self::saveInspectionTask($taskData);
             
             
             if (!$taskId) {
             if (!$taskId) {
-                throw new \Exception('验货任务创建失败');
+                throw new BusinessException('验货任务创建失败');
             }
             }
             
             
             // 2. 保存验货结果
             // 2. 保存验货结果
@@ -186,37 +205,32 @@ class InspectService
                     }
                     }
                 }
                 }
             }
             }
+            
             //  验货结果 是 通过 还是 不通过
             //  验货结果 是 通过 还是 不通过
             $isPass = 1;
             $isPass = 1;
             foreach ($resultData as $result) {
             foreach ($resultData as $result) {
                 if ($result['is_qualified'] == 0) {
                 if ($result['is_qualified'] == 0) {
                     $isPass = 0;
                     $isPass = 0;
+                    break;
                 }
                 }
             }            
             }            
+            
             // 4. 更新任务状态为已完成
             // 4. 更新任务状态为已完成
             self::updateTaskStatus($taskId, 2, $isPass); // 2:已完成
             self::updateTaskStatus($taskId, 2, $isPass); // 2:已完成
 
 
-            if ($isPass) {
-               $orderStatus = OrderEnum::STATUS_INSPECTION_PASS;
-            } else {
-               $orderStatus = OrderEnum::STATUS_INSPECTION_FAIL;
-            }
-            // 5. 更新订单状态
-            OrderService::updateOrderStatus($taskData['order_id'], $taskData['inspector_id'], $orderStatus); //修改订单状态            
+            // 5. 更新订单商品的验收状态
+            $inspectStatus = $isPass ? 1 : 2; // 1:验收通过 2:验收不通过
+            OrderService::updateOrderGoodsInspectStatus($taskData['order_goods_id'], $inspectStatus);
+            
+            // 6. 检查订单是否所有商品都验收完成,决定是否更新订单状态
+            OrderService::updateOrderStatusByInspectResult($taskData['order_id'], $taskData['inspector_id'] ?? 0);
+            
             Db::commit();            
             Db::commit();            
-            return [
-                'code' => 1,
-                'msg' => '验货任务提交成功',
-                'data' => ['task_id' => $taskId]
-            ];
+            return [1, '验货任务提交成功', ['task_id' => $taskId]];
             
             
         } catch (\Exception $e) {
         } catch (\Exception $e) {
             Db::rollback();
             Db::rollback();
-            return [
-                'code' => 0,
-                'msg' => '验货任务提交失败:' . $e->getMessage(),
-                'data' => []
-            ];
+            return [0, '验货任务提交失败:' . $e->getMessage(), []];
         }
         }
     }
     }
     
     
@@ -229,6 +243,7 @@ class InspectService
     {
     {
         $data = [
         $data = [
             'order_id' => $taskData['order_id'] ?? 0,
             'order_id' => $taskData['order_id'] ?? 0,
+            'order_goods_id' => $taskData['order_goods_id'] ?? 0,
             'inspector_id' => $taskData['inspector_id'] ?? 0,
             'inspector_id' => $taskData['inspector_id'] ?? 0,
             'remark' => $taskData['remark'] ?? '',
             'remark' => $taskData['remark'] ?? '',
             'images' => $taskData['images'] ?? '',
             'images' => $taskData['images'] ?? '',
@@ -401,4 +416,19 @@ class InspectService
         
         
         return $query->select();
         return $query->select();
     }
     }
+    
+    /**
+     * 检查是否重复提交验货任务
+     * @param int $orderId 订单ID
+     * @param int $orderGoodsId 订单商品ID
+     * @return array|null 返回已存在的任务信息,不存在返回null
+     */
+    private static function checkDuplicateInspectionTask($orderId, $orderGoodsId)
+    {
+        return Db::table('inspection_task')
+            ->where('order_id', $orderId)
+            ->where('order_goods_id', $orderGoodsId)
+            ->where('deletetime', 'exp', 'IS NULL')
+            ->find();
+    }
 }
 }

+ 203 - 0
application/common/Service/OrderService.php

@@ -724,4 +724,207 @@ class OrderService
         ];
         ];
     }
     }
 
 
+    /**
+     * 验证订单和订单商品是否存在
+     * @param int $orderId 订单ID
+     * @param int $orderGoodsId 订单商品ID
+     * @return array|false 返回订单和订单商品信息,不存在返回false
+     */
+    public static function validateOrderAndOrderGoods($orderId, $orderGoodsId)
+    {
+        // 查询订单
+        $order = Order::where('id', $orderId)->find();
+        if (!$order) {
+            return false;
+        }
+        
+        // 查询订单商品
+        $orderGoods = OrderGoods::where('id', $orderGoodsId)
+            ->where('order_id', $orderId)
+            ->find();
+        if (!$orderGoods) {
+            return false;
+        }
+        
+        return [
+            'order' => $order,
+            'order_goods' => $orderGoods
+        ];
+    }
+
+    /**
+     * 更新订单商品的验收状态
+     * @param int $orderGoodsId 订单商品ID
+     * @param int $inspectStatus 验收状态 0:待验收 1:验收通过 2:验收不通过
+     * @return bool
+     */
+    public static function updateOrderGoodsInspectStatus($orderGoodsId, $inspectStatus)
+    {
+        return OrderGoods::where('id', $orderGoodsId)->update([
+            'inspect_status' => $inspectStatus,
+            'inspect_time' => time()
+        ]);
+    }
+
+    /**
+     * 检查订单中所有商品的验收状态
+     * @param int $orderId 订单ID
+     * @return array 返回验收状态统计
+     */
+    public static function checkOrderInspectStatus($orderId)
+    {
+        $orderGoods = OrderGoods::where('order_id', $orderId)->select();
+        
+        $statusCount = [
+            'total' => count($orderGoods), // 总商品数
+            'pending' => 0,    // 待验收
+            'passed' => 0,     // 验收通过
+            'failed' => 0      // 验收不通过
+        ];
+        
+        foreach ($orderGoods as $goods) {
+            switch ($goods['inspect_status']) {
+                case 0:
+                    $statusCount['pending']++;
+                    break;
+                case 1:
+                    $statusCount['passed']++;
+                    break;
+                case 2:
+                    $statusCount['failed']++;
+                    break;
+            }
+        }
+        
+        return $statusCount;
+    }
+
+    /**
+     * 根据验收状态决定是否更新订单状态
+     * @param int $orderId 订单ID
+     * @param int $userId 用户ID
+     * @return bool
+     */
+    public static function updateOrderStatusByInspectResult($orderId, $userId = 0)
+    {
+        $inspectStatus = self::checkOrderInspectStatus($orderId);
+        
+        // 如果所有商品都验收完成(没有待验收的商品)
+        if ($inspectStatus['pending'] == 0) {
+            // 如果所有商品都验收通过
+            if ($inspectStatus['failed'] == 0 && $inspectStatus['passed'] > 0) {
+                // 更新订单状态为验收通过
+                return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_INSPECTION_PASS);
+            }
+            // 如果所有商品都验收不通过
+            elseif ($inspectStatus['failed'] > 0 && $inspectStatus['passed'] == 0) {
+                // 更新订单状态为验收失败
+                return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_INSPECTION_FAIL);
+            }
+            // 如果部分商品验收通过,部分不通过
+            elseif ($inspectStatus['failed'] > 0 && $inspectStatus['passed'] > 0) {
+                // 混合状态,由业务人员手动处理,不自动更新订单状态
+                return true;
+            }
+        }
+        
+        return true;
+    }
+
+    /**
+     * 更新订单商品的发货状态
+     * @param int $orderGoodsId 订单商品ID
+     * @param string $expressName 快递公司
+     * @param string $expressNo 快递单号
+     * @param array $expressImage 快递图片
+     * @return bool
+     */
+    public static function updateOrderGoodsDeliveryStatus($orderGoodsId, $expressName, $expressNo, $expressImage = [])
+    {
+        $updateData = [
+            'express_name' => $expressName,
+            'express_no' => $expressNo,
+            'express_image' => is_array($expressImage) ? json_encode($expressImage) : $expressImage,
+            'delivery_status' => 1, // 已发货
+            'updatetime' => time()
+        ];
+        
+        return OrderGoods::where('id', $orderGoodsId)->update($updateData);
+    }
+
+    /**
+     * 检查订单中所有商品的发货状态
+     * @param int $orderId 订单ID
+     * @return array 返回发货状态统计
+     */
+    public static function checkOrderDeliveryStatus($orderId)
+    {
+        $orderGoods = OrderGoods::where('order_id', $orderId)->select();
+        
+        $statusCount = [
+            'total' => count($orderGoods), // 总商品数
+            'pending' => 0,    // 待发货
+            'delivered' => 0,  // 已发货
+        ];
+        
+        foreach ($orderGoods as $goods) {
+            if ($goods['delivery_status'] == 1) {
+                $statusCount['delivered']++;
+            } else {
+                $statusCount['pending']++;
+            }
+        }
+        
+        return $statusCount;
+    }
+
+    /**
+     * 根据发货状态决定是否更新订单状态
+     * @param int $orderId 订单ID
+     * @param int $userId 用户ID
+     * @return bool
+     */
+    public static function updateOrderStatusByDeliveryResult($orderId, $userId = 0)
+    {
+        $deliveryStatus = self::checkOrderDeliveryStatus($orderId);
+        
+        // 如果所有商品都已发货(没有待发货的商品)
+        if ($deliveryStatus['pending'] == 0 && $deliveryStatus['delivered'] > 0) {
+            // 更新订单状态为已发货
+            return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_SHIP);
+        }
+        
+        return true;
+    }
+
+    /**
+     * 验证订单商品是否可以发货
+     * @param int $orderId 订单ID
+     * @param int $orderGoodsId 订单商品ID
+     * @return array|false 返回订单和订单商品信息,验证失败返回false
+     */
+    public static function validateOrderGoodsForDelivery($orderId, $orderGoodsId)
+    {
+        // 验证订单和订单商品是否存在
+        $orderData = self::validateOrderAndOrderGoods($orderId, $orderGoodsId);
+        if (!$orderData) {
+            return false;
+        }
+        
+        $order = $orderData['order'];
+        $orderGoods = $orderData['order_goods'];
+        
+        // 检查订单状态是否允许发货(验收通过状态)
+        if ($order['order_status'] != OrderEnum::STATUS_INSPECTION_PASS) {
+            return false;
+        }
+        
+        // 检查该订单商品是否已经发货
+        if ($orderGoods['delivery_status'] == 1) {
+            return false;
+        }
+        
+        return $orderData;
+    }
+
 } 
 } 

+ 203 - 0
application/common/service/OrderService.php

@@ -724,4 +724,207 @@ class OrderService
         ];
         ];
     }
     }
 
 
+    /**
+     * 验证订单和订单商品是否存在
+     * @param int $orderId 订单ID
+     * @param int $orderGoodsId 订单商品ID
+     * @return array|false 返回订单和订单商品信息,不存在返回false
+     */
+    public static function validateOrderAndOrderGoods($orderId, $orderGoodsId)
+    {
+        // 查询订单
+        $order = Order::where('id', $orderId)->find();
+        if (!$order) {
+            return false;
+        }
+        
+        // 查询订单商品
+        $orderGoods = OrderGoods::where('id', $orderGoodsId)
+            ->where('order_id', $orderId)
+            ->find();
+        if (!$orderGoods) {
+            return false;
+        }
+        
+        return [
+            'order' => $order,
+            'order_goods' => $orderGoods
+        ];
+    }
+
+    /**
+     * 更新订单商品的验收状态
+     * @param int $orderGoodsId 订单商品ID
+     * @param int $inspectStatus 验收状态 0:待验收 1:验收通过 2:验收不通过
+     * @return bool
+     */
+    public static function updateOrderGoodsInspectStatus($orderGoodsId, $inspectStatus)
+    {
+        return OrderGoods::where('id', $orderGoodsId)->update([
+            'inspect_status' => $inspectStatus,
+            'inspect_time' => time()
+        ]);
+    }
+
+    /**
+     * 检查订单中所有商品的验收状态
+     * @param int $orderId 订单ID
+     * @return array 返回验收状态统计
+     */
+    public static function checkOrderInspectStatus($orderId)
+    {
+        $orderGoods = OrderGoods::where('order_id', $orderId)->select();
+        
+        $statusCount = [
+            'total' => count($orderGoods), // 总商品数
+            'pending' => 0,    // 待验收
+            'passed' => 0,     // 验收通过
+            'failed' => 0      // 验收不通过
+        ];
+        
+        foreach ($orderGoods as $goods) {
+            switch ($goods['inspect_status']) {
+                case 0:
+                    $statusCount['pending']++;
+                    break;
+                case 1:
+                    $statusCount['passed']++;
+                    break;
+                case 2:
+                    $statusCount['failed']++;
+                    break;
+            }
+        }
+        
+        return $statusCount;
+    }
+
+    /**
+     * 根据验收状态决定是否更新订单状态
+     * @param int $orderId 订单ID
+     * @param int $userId 用户ID
+     * @return bool
+     */
+    public static function updateOrderStatusByInspectResult($orderId, $userId = 0)
+    {
+        $inspectStatus = self::checkOrderInspectStatus($orderId);
+        
+        // 如果所有商品都验收完成(没有待验收的商品)
+        if ($inspectStatus['pending'] == 0) {
+            // 如果所有商品都验收通过
+            if ($inspectStatus['failed'] == 0 && $inspectStatus['passed'] > 0) {
+                // 更新订单状态为验收通过
+                return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_INSPECTION_PASS);
+            }
+            // 如果所有商品都验收不通过
+            elseif ($inspectStatus['failed'] > 0 && $inspectStatus['passed'] == 0) {
+                // 更新订单状态为验收失败
+                return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_INSPECTION_FAIL);
+            }
+            // 如果部分商品验收通过,部分不通过
+            elseif ($inspectStatus['failed'] > 0 && $inspectStatus['passed'] > 0) {
+                // 混合状态,由业务人员手动处理,不自动更新订单状态
+                return true;
+            }
+        }
+        
+        return true;
+    }
+
+    /**
+     * 更新订单商品的发货状态
+     * @param int $orderGoodsId 订单商品ID
+     * @param string $expressName 快递公司
+     * @param string $expressNo 快递单号
+     * @param array $expressImage 快递图片
+     * @return bool
+     */
+    public static function updateOrderGoodsDeliveryStatus($orderGoodsId, $expressName, $expressNo, $expressImage = [])
+    {
+        $updateData = [
+            'express_name' => $expressName,
+            'express_no' => $expressNo,
+            'express_image' => is_array($expressImage) ? json_encode($expressImage) : $expressImage,
+            'delivery_status' => 1, // 已发货
+            'updatetime' => time()
+        ];
+        
+        return OrderGoods::where('id', $orderGoodsId)->update($updateData);
+    }
+
+    /**
+     * 检查订单中所有商品的发货状态
+     * @param int $orderId 订单ID
+     * @return array 返回发货状态统计
+     */
+    public static function checkOrderDeliveryStatus($orderId)
+    {
+        $orderGoods = OrderGoods::where('order_id', $orderId)->select();
+        
+        $statusCount = [
+            'total' => count($orderGoods), // 总商品数
+            'pending' => 0,    // 待发货
+            'delivered' => 0,  // 已发货
+        ];
+        
+        foreach ($orderGoods as $goods) {
+            if ($goods['delivery_status'] == 1) {
+                $statusCount['delivered']++;
+            } else {
+                $statusCount['pending']++;
+            }
+        }
+        
+        return $statusCount;
+    }
+
+    /**
+     * 根据发货状态决定是否更新订单状态
+     * @param int $orderId 订单ID
+     * @param int $userId 用户ID
+     * @return bool
+     */
+    public static function updateOrderStatusByDeliveryResult($orderId, $userId = 0)
+    {
+        $deliveryStatus = self::checkOrderDeliveryStatus($orderId);
+        
+        // 如果所有商品都已发货(没有待发货的商品)
+        if ($deliveryStatus['pending'] == 0 && $deliveryStatus['delivered'] > 0) {
+            // 更新订单状态为已发货
+            return self::updateOrderStatus($orderId, $userId, OrderEnum::STATUS_SHIP);
+        }
+        
+        return true;
+    }
+
+    /**
+     * 验证订单商品是否可以发货
+     * @param int $orderId 订单ID
+     * @param int $orderGoodsId 订单商品ID
+     * @return array|false 返回订单和订单商品信息,验证失败返回false
+     */
+    public static function validateOrderGoodsForDelivery($orderId, $orderGoodsId)
+    {
+        // 验证订单和订单商品是否存在
+        $orderData = self::validateOrderAndOrderGoods($orderId, $orderGoodsId);
+        if (!$orderData) {
+            return false;
+        }
+        
+        $order = $orderData['order'];
+        $orderGoods = $orderData['order_goods'];
+        
+        // 检查订单状态是否允许发货(验收通过状态)
+        if ($order['order_status'] != OrderEnum::STATUS_INSPECTION_PASS) {
+            return false;
+        }
+        
+        // 检查该订单商品是否已经发货
+        if ($orderGoods['delivery_status'] == 1) {
+            return false;
+        }
+        
+        return $orderData;
+    }
+
 } 
 }