From 9d27d32f370e06d2c690753cc380c7521fa0606a Mon Sep 17 00:00:00 2001 From: liu <1873441552@qq.com> Date: Sat, 29 Jun 2024 17:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A3=80=E9=AA=8C=E4=B8=8B=E5=8D=95=E5=BE=97?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/order/OrderController.php | 15 +++++++ app/api/logic/order/OrderLogic.php | 47 ++++++++++++++++++++ app/api/validate/OrderValidate.php | 7 +++ 3 files changed, 69 insertions(+) diff --git a/app/api/controller/order/OrderController.php b/app/api/controller/order/OrderController.php index 6e6e9e17b..1e2835ed7 100644 --- a/app/api/controller/order/OrderController.php +++ b/app/api/controller/order/OrderController.php @@ -99,6 +99,21 @@ class OrderController extends BaseApiController return $this->data($res); } + public function checkInventory() + { + $params = (new OrderValidate())->post()->goCheck('cart'); + $res = OrderLogic::checkLeft($params, $this->userId); + if (!$res) { + $msg = OrderLogic::getError(); + if ($msg == '购物车为空') { + return $this->data([]); + } + return $this->fail(OrderLogic::getError()); + } + return $this->data($res); + } + + /** * 创建订单 */ diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php index fa680a536..0b2475af9 100644 --- a/app/api/logic/order/OrderLogic.php +++ b/app/api/logic/order/OrderLogic.php @@ -216,6 +216,7 @@ class OrderLogic extends BaseLogic $_order['pay_type'] = $orderInfo['order']['pay_type']; $_order['verify_code'] = $verify_code; $_order['reservation_time'] = null; + $_order['reservation'] = $params['reservation']??0;//是否需要预约 if (isset($params['reservation_time']) && $params['reservation_time']) { $_order['reservation_time'] = $params['reservation_time']; $_order['reservation'] = YesNoEnum::YES; @@ -264,6 +265,52 @@ class OrderLogic extends BaseLogic } } + /** + * 检验丢失 + * @param $params + * @return array|false + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function checkLeft($params,$uid) + { + $where = ['is_pay' => 0]; + $cart_select = Cart::whereIn('id', $params['cart_id']) + ->where($where)->field('id,product_id,cart_num,store_id')->select()->toArray(); + if (empty($cart_select)) { + self::setError('购物车为空'); + return false; + } + $newArr = []; + //检查购物车对比店铺得商品数量差异 + foreach ($cart_select as $v) { + $store = StoreBranchProduct::where([ + 'store_id'=>$v['store_id'], + 'product_id'=>$v['product_id'], + ])->field('id,store_name,stock')->withTrashed()->find(); + if($store['stock'] < $v['cart_num']){ + //缺失 + $newArr[] = [ + 'uid'=>$uid, + 'store_id'=>$v['store_id'], + 'product_id'=>$v['product_id'], + 'missing_quantity'=> $v['cart_num'] - $store['stock'] + ]; + } + } + + if($newArr){ + return [ + 'detail'=>$newArr, + 'reservation'=>1 + ]; + } + return [ + 'detail'=>[], + 'reservation'=>0 + ]; + } /** * @notes 获取订单号 diff --git a/app/api/validate/OrderValidate.php b/app/api/validate/OrderValidate.php index f223800f6..7d518c202 100644 --- a/app/api/validate/OrderValidate.php +++ b/app/api/validate/OrderValidate.php @@ -23,6 +23,7 @@ class OrderValidate extends BaseValidate 'id' => 'require|number', 'old_cart_id' => 'require|array', 'refund_type' => 'require|number', + 'cart_id' => 'require', ]; @@ -37,8 +38,14 @@ class OrderValidate extends BaseValidate 'id' => '订单id', 'old_cart_id' => '购物车id', 'refund_type' => '退款申请类型', + 'cart_id' => '购物车id', ]; + public function sceneCart() + { + return $this->only(['cart_id']); + } + /** * @notes 添加场景