|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|