From 852904cbd94e2f91d540a810113bb0deb9bc160d Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 23 Jan 2024 11:16:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B4=BB=E5=8A=A8=E5=95=86?= =?UTF-8?q?=E5=93=81=E9=99=90=E8=B4=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/dao/store/StoreActivityDao.php | 44 +++++++++++++++++++ .../model/store/StoreActivityOrderProduct.php | 26 +++++++++++ .../order/StoreOrderCreateRepository.php | 5 +++ .../store/order/StoreOrderRepository.php | 2 + 4 files changed, 77 insertions(+) create mode 100644 app/common/model/store/StoreActivityOrderProduct.php diff --git a/app/common/dao/store/StoreActivityDao.php b/app/common/dao/store/StoreActivityDao.php index c18070ef..66f21c0a 100644 --- a/app/common/dao/store/StoreActivityDao.php +++ b/app/common/dao/store/StoreActivityDao.php @@ -17,9 +17,12 @@ use app\common\dao\BaseDao; use app\common\dao\store\product\CloudProductDao; use app\common\model\BaseModel; use app\common\model\store\GeoStreet; +use app\common\model\store\product\CloudProduct; use app\common\model\store\StoreActivity; +use app\common\model\store\StoreActivityOrderProduct; use app\common\repositories\store\product\SpuRepository; use app\common\repositories\system\RelevanceRepository; +use think\exception\ValidateException; use think\facade\Db; /** @@ -104,4 +107,45 @@ class StoreActivityDao extends BaseDao return ['count' => $count, 'list' => $list ?? []]; } + /** + * 是否可购买活动商品 + * @param $userId + * @param $productId + * @return bool + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function canBuy($userId, $productId) + { + $activityId = Db::name('cloud_product')->where('product_id', $productId)->value('activity_id'); + $find = Db::name('store_activity_order_product')->where('user_id', $userId)->where('product_id', $productId)->where('status', 1)->find(); + if ($find && $activityId == 2) { + return false; + } + return true; + } + + /** + * 保存活动订单商品 + * @param $activityId + * @param $order + * @return void + */ + public function saveOrderProduct($activityId, $order) + { + $orderProductIds = array_column($order->orderProduct->toArray(), 'product_id'); + $productIds = CloudProduct::whereIn('product_id', $orderProductIds)->where('activity_id', $activityId)->column('product_id'); + foreach ($productIds as $productId) { + $model = new StoreActivityOrderProduct(); + $model->user_id = $order['uid']; + $model->activity_id = $activityId; + $model->product_id = $productId; + $model->number = 1; + if (!$model->save()) { + throw new ValidateException('活动商品数据保存失败'); + } + } + } + } diff --git a/app/common/model/store/StoreActivityOrderProduct.php b/app/common/model/store/StoreActivityOrderProduct.php new file mode 100644 index 00000000..561648d8 --- /dev/null +++ b/app/common/model/store/StoreActivityOrderProduct.php @@ -0,0 +1,26 @@ +canBuy($cart['uid'], $cart['product_id']); + if (!$canBuy) { + throw new ValidateException('活动商品限购1个'); + } if ($cart['product_type'] == 0) { if ($cart['product']['once_min_count'] > 0 && $cart['product']['once_min_count'] > $cart['cart_num']) throw new ValidateException('[低于起购数:' . $cart['product']['once_min_count'] . ']' . mb_substr($cart['product']['store_name'], 0, 10) . '...'); diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index c9031ae7..76981766 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -15,6 +15,7 @@ use app\common\dao\store\consumption\CommissionDao; use app\common\dao\store\consumption\StoreConsumptionUserDao; use app\common\dao\store\order\StoreCartDao; use app\common\dao\store\order\StoreOrderDao; +use app\common\dao\store\StoreActivityDao; use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreOrder; use app\common\model\store\order\StoreOrderInterest; @@ -228,6 +229,7 @@ class StoreOrderRepository extends BaseRepository $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); $svipDiscount = 0; foreach ($groupOrder->orderList as $_k => $order) { + (new StoreActivityDao())->saveOrderProduct(2, $order); $order->paid = 1; $order->pay_time = $time; $svipDiscount = bcadd($order->svip_discount, $svipDiscount, 2);