From 7d67779fea6e5be4a62762e3299c13531c5ae21d Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Sat, 24 Feb 2024 11:26:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=BA=97=E9=93=BA=E6=8A=B5?= =?UTF-8?q?=E6=89=A3=E5=88=B8=E5=8F=91=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/store/coupon/StoreCouponUserDao.php | 11 +---- .../model/store/coupon/StoreCouponDetail.php | 42 +++++++++++++++++ app/common/model/store/product/Product.php | 12 +++++ .../store/coupon/StoreCouponRepository.php | 16 ++++++- .../order/StoreOrderCreateRepository.php | 3 +- crmeb/jobs/PayGiveCouponJob.php | 4 ++ crmeb/listens/OrderDeliveryListen.php | 47 ++++++++++++++++--- 7 files changed, 115 insertions(+), 20 deletions(-) diff --git a/app/common/dao/store/coupon/StoreCouponUserDao.php b/app/common/dao/store/coupon/StoreCouponUserDao.php index da48fb36..23b23d48 100644 --- a/app/common/dao/store/coupon/StoreCouponUserDao.php +++ b/app/common/dao/store/coupon/StoreCouponUserDao.php @@ -203,16 +203,7 @@ class StoreCouponUserDao extends BaseDao if (!$storeCouponUser->save()) { throw new ValidateException('优惠券余额更新出错'); } - $detailModel = new StoreCouponDetail(); - $detailModel->uid = $order['uid']; - $detailModel->coupon_user_id = $id; - $detailModel->group_order_id = $order['group_order_id']; - $detailModel->order_id = $order['order_id'] ?? 0; - $detailModel->type = StoreCouponDetail::TYPE_EXPEND; - $detailModel->amount = $amount; - $detailModel->pay_price = $order['pay_price']; - $detailModel->create_time = time(); - $detailModel->save(); + StoreCouponDetail::expend($order, $id, $amount); } /** diff --git a/app/common/model/store/coupon/StoreCouponDetail.php b/app/common/model/store/coupon/StoreCouponDetail.php index b3221f7a..2d0c7503 100644 --- a/app/common/model/store/coupon/StoreCouponDetail.php +++ b/app/common/model/store/coupon/StoreCouponDetail.php @@ -35,4 +35,46 @@ class StoreCouponDetail extends BaseModel return $this->hasOne(StoreCoupon::class, 'coupon_id', 'coupon_id'); } + /** + * 使用优惠券 + * @param $order + * @param $id + * @param $amount + * @return void + */ + public static function expend($order, $id, $amount) + { + $detailModel = new self(); + $detailModel->uid = $order['uid']; + $detailModel->coupon_user_id = $id; + $detailModel->group_order_id = $order['group_order_id']; + $detailModel->order_id = $order['order_id'] ?? 0; + $detailModel->type = StoreCouponDetail::TYPE_EXPEND; + $detailModel->amount = $amount; + $detailModel->pay_price = $order['pay_price']; + $detailModel->create_time = time(); + $detailModel->save(); + } + + /** + * 获得优惠券 + * @param $order + * @param $id + * @param $amount + * @return void + */ + public static function income($order, $id, $amount) + { + $detailModel = new self(); + $detailModel->uid = $order['uid']; + $detailModel->coupon_user_id = $id; + $detailModel->group_order_id = $order['group_order_id']; + $detailModel->order_id = $order['order_id'] ?? 0; + $detailModel->type = StoreCouponDetail::TYPE_INCOME; + $detailModel->amount = $amount; + $detailModel->pay_price = $order['pay_price']; + $detailModel->create_time = time(); + $detailModel->save(); + } + } diff --git a/app/common/model/store/product/Product.php b/app/common/model/store/product/Product.php index 6cdbadca..eb467792 100644 --- a/app/common/model/store/product/Product.php +++ b/app/common/model/store/product/Product.php @@ -673,4 +673,16 @@ class Product extends BaseModel } + /** + * 判断是否为平台卡商品 + * @return bool + */ + public function isPlatformCard() + { + if (!empty($this->merCateId) && in_array(env('PLATFORM_CARD_CATE_ID'), array_column($this->merCateId->toArray(), 'mer_cate_id'))) { + return true; + } + return false; + } + } diff --git a/app/common/repositories/store/coupon/StoreCouponRepository.php b/app/common/repositories/store/coupon/StoreCouponRepository.php index a08532c3..b5c94170 100644 --- a/app/common/repositories/store/coupon/StoreCouponRepository.php +++ b/app/common/repositories/store/coupon/StoreCouponRepository.php @@ -78,6 +78,9 @@ class StoreCouponRepository extends BaseRepository * @var StoreCouponDao */ protected $dao; + public $status; + public $startTime; + public $endTime; /** * StoreCouponIssueRepository constructor. @@ -311,16 +314,18 @@ class StoreCouponRepository extends BaseRepository public function sendCoupon(StoreCoupon $coupon, $uid, $type) { event('user.coupon.send.before', compact('coupon', 'uid', 'type')); - Db::transaction(function () use ($uid, $type, $coupon) { - $this->preSendCoupon($coupon, $uid, $type); + $couponUser = Db::transaction(function () use ($uid, $type, $coupon) { + $couponUser = $this->preSendCoupon($coupon, $uid, $type); app()->make(StoreCouponIssueUserRepository::class)->issue($coupon['coupon_id'], $uid); if ($coupon->is_limited) { $coupon->remain_count--; $coupon->save(); } + return $couponUser; }); event('user.coupon.send', compact('coupon', 'uid', 'type')); event('user.coupon.send.' . $type, compact('coupon', 'uid', 'type')); + return $couponUser; } public function preSendCoupon(StoreCoupon $coupon, $uid, $type = 'send') @@ -355,6 +360,13 @@ class StoreCouponRepository extends BaseRepository $data['end_time'] = date('Y-m-d H:i:s', strtotime("+ {$coupon['coupon_time']}day")); } } + if (!empty($this->startTime)) { + $data['start_time'] = $this->startTime; + $data['end_time'] = $this->endTime; + } + if (!empty($this->isFail)) { + $data['is_fail'] = $this->isFail; + } return $data; } diff --git a/app/common/repositories/store/order/StoreOrderCreateRepository.php b/app/common/repositories/store/order/StoreOrderCreateRepository.php index e4fb2ba4..2f80eb99 100644 --- a/app/common/repositories/store/order/StoreOrderCreateRepository.php +++ b/app/common/repositories/store/order/StoreOrderCreateRepository.php @@ -144,8 +144,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository if ($presellType == 2) $down_price = bcadd($down_price, bcmul($cart['cart_num'], $cart['productPresellAttr']['down_price'], 2), 2); } - $merchantCate = $cart['product']->merCateId; - if ($cart['product']['type'] == 1 && !empty($merchantCate) && $merchantCate[0]['mer_cate_id'] == env('PLATFORM_CARD_CATE_ID') && $enabledPlatformCoupon) { + if ($cart['product']['type'] == 1 && !$cart['product']->isPlatformCard()) { $enabledPlatformCoupon = false; $enabledCoupon = false; } diff --git a/crmeb/jobs/PayGiveCouponJob.php b/crmeb/jobs/PayGiveCouponJob.php index 12eec6fb..c6d1cfd8 100644 --- a/crmeb/jobs/PayGiveCouponJob.php +++ b/crmeb/jobs/PayGiveCouponJob.php @@ -23,9 +23,13 @@ class PayGiveCouponJob implements JobInterface { public function fire($job, $data) { + /** @var StoreCouponRepository $storeCouponRepository */ $storeCouponRepository = app()->make(StoreCouponRepository::class); $coupons = $storeCouponRepository->getGiveCoupon($data['ids']); foreach ($coupons as $coupon) { + if ($coupon['type'] == StoreCouponRepository::TYPE_STORE_COUPON) { + continue; + } if ($coupon->is_limited && 0 == $coupon->remain_count) continue; try { diff --git a/crmeb/listens/OrderDeliveryListen.php b/crmeb/listens/OrderDeliveryListen.php index 11383105..5782ccd5 100644 --- a/crmeb/listens/OrderDeliveryListen.php +++ b/crmeb/listens/OrderDeliveryListen.php @@ -2,9 +2,11 @@ namespace crmeb\listens; +use app\common\model\store\coupon\StoreCouponDetail; use app\common\repositories\store\coupon\StoreCouponRepository; use app\common\repositories\store\coupon\StoreCouponUserRepository; use crmeb\interfaces\ListenerInterface; +use think\facade\Log; /** * Class OrderDeliveryListen @@ -15,20 +17,53 @@ class OrderDeliveryListen implements ListenerInterface { $order = $event['order']; foreach ($order->orderProduct as $orderProduct) { - $merchantCate = $orderProduct->product->merCateId; - if (empty($merchantCate) || $merchantCate[0]['mer_cate_id'] != env('PLATFORM_CARD_CATE_ID')) { + if (!$orderProduct->product->isPlatformCard()) { continue; } /** @var StoreCouponRepository $repo */ $repo = app()->make(StoreCouponRepository::class); - $coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY) - ->where('coupon_price', $orderProduct->product->price)->find(); + $coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY)->find(); if (empty($coupon)) { return; } - $coupon->coupon_price = bcmul($orderProduct->product_num, $orderProduct->product->price); - $repo->sendCoupon($coupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); + $coupon->coupon_price = $order->total_price; + $couponUser = $repo->sendCoupon($coupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); + StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $coupon->coupon_price); + + if (!empty($orderProduct->product->give_coupon_ids)) { + $giveCoupons = $repo->getGiveCoupon($orderProduct->product->give_coupon_ids); + foreach ($giveCoupons as $giveCoupon) { + if ($giveCoupon['type'] != StoreCouponRepository::TYPE_STORE_COUPON) { + continue; + } + try { + $rate = $this->getRate($order->total_price); + $giveCoupon->coupon_price = bcmul($order->total_price, $rate, 2); + $repo->startTime = date('Y-m-d H:i:s'); + $repo->endTime = '2025-07-01'; + $repo->status = -1; + $couponUser = $repo->sendCoupon($giveCoupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); + StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $giveCoupon->coupon_price); + } catch (\Exception $e) { + Log::info('自动发放买赠优惠券:' . $e->getMessage()); + } + } + } } } + public function getRate($orderAmount) + { + if ($orderAmount >= 1000 && $orderAmount < 5000) { + $rate = 0.05; + } elseif ($orderAmount >= 5000 && $orderAmount < 10000) { + $rate = 0.10; + } elseif ($orderAmount >= 10000 && $orderAmount < 50000) { + $rate = 0.15; + } elseif ($orderAmount >= 50000 && $orderAmount <= 100000) { + $rate = 0.20; + } + return $rate ?? 0; + } + }