调整店铺抵扣券发放

This commit is contained in:
luofei 2024-02-24 11:26:41 +08:00
parent 63c6cb8072
commit 7d67779fea
7 changed files with 115 additions and 20 deletions

View File

@ -203,16 +203,7 @@ class StoreCouponUserDao extends BaseDao
if (!$storeCouponUser->save()) { if (!$storeCouponUser->save()) {
throw new ValidateException('优惠券余额更新出错'); throw new ValidateException('优惠券余额更新出错');
} }
$detailModel = new StoreCouponDetail(); StoreCouponDetail::expend($order, $id, $amount);
$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();
} }
/** /**

View File

@ -35,4 +35,46 @@ class StoreCouponDetail extends BaseModel
return $this->hasOne(StoreCoupon::class, 'coupon_id', 'coupon_id'); 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();
}
} }

View File

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

View File

@ -78,6 +78,9 @@ class StoreCouponRepository extends BaseRepository
* @var StoreCouponDao * @var StoreCouponDao
*/ */
protected $dao; protected $dao;
public $status;
public $startTime;
public $endTime;
/** /**
* StoreCouponIssueRepository constructor. * StoreCouponIssueRepository constructor.
@ -311,16 +314,18 @@ class StoreCouponRepository extends BaseRepository
public function sendCoupon(StoreCoupon $coupon, $uid, $type) public function sendCoupon(StoreCoupon $coupon, $uid, $type)
{ {
event('user.coupon.send.before', compact('coupon', 'uid', 'type')); event('user.coupon.send.before', compact('coupon', 'uid', 'type'));
Db::transaction(function () use ($uid, $type, $coupon) { $couponUser = Db::transaction(function () use ($uid, $type, $coupon) {
$this->preSendCoupon($coupon, $uid, $type); $couponUser = $this->preSendCoupon($coupon, $uid, $type);
app()->make(StoreCouponIssueUserRepository::class)->issue($coupon['coupon_id'], $uid); app()->make(StoreCouponIssueUserRepository::class)->issue($coupon['coupon_id'], $uid);
if ($coupon->is_limited) { if ($coupon->is_limited) {
$coupon->remain_count--; $coupon->remain_count--;
$coupon->save(); $coupon->save();
} }
return $couponUser;
}); });
event('user.coupon.send', compact('coupon', 'uid', 'type')); event('user.coupon.send', compact('coupon', 'uid', 'type'));
event('user.coupon.send.' . $type, compact('coupon', 'uid', 'type')); event('user.coupon.send.' . $type, compact('coupon', 'uid', 'type'));
return $couponUser;
} }
public function preSendCoupon(StoreCoupon $coupon, $uid, $type = 'send') 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")); $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; return $data;
} }

View File

@ -144,8 +144,7 @@ class StoreOrderCreateRepository extends StoreOrderRepository
if ($presellType == 2) if ($presellType == 2)
$down_price = bcadd($down_price, bcmul($cart['cart_num'], $cart['productPresellAttr']['down_price'], 2), 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 && !$cart['product']->isPlatformCard()) {
if ($cart['product']['type'] == 1 && !empty($merchantCate) && $merchantCate[0]['mer_cate_id'] == env('PLATFORM_CARD_CATE_ID') && $enabledPlatformCoupon) {
$enabledPlatformCoupon = false; $enabledPlatformCoupon = false;
$enabledCoupon = false; $enabledCoupon = false;
} }

View File

@ -23,9 +23,13 @@ class PayGiveCouponJob implements JobInterface
{ {
public function fire($job, $data) public function fire($job, $data)
{ {
/** @var StoreCouponRepository $storeCouponRepository */
$storeCouponRepository = app()->make(StoreCouponRepository::class); $storeCouponRepository = app()->make(StoreCouponRepository::class);
$coupons = $storeCouponRepository->getGiveCoupon($data['ids']); $coupons = $storeCouponRepository->getGiveCoupon($data['ids']);
foreach ($coupons as $coupon) { foreach ($coupons as $coupon) {
if ($coupon['type'] == StoreCouponRepository::TYPE_STORE_COUPON) {
continue;
}
if ($coupon->is_limited && 0 == $coupon->remain_count) if ($coupon->is_limited && 0 == $coupon->remain_count)
continue; continue;
try { try {

View File

@ -2,9 +2,11 @@
namespace crmeb\listens; namespace crmeb\listens;
use app\common\model\store\coupon\StoreCouponDetail;
use app\common\repositories\store\coupon\StoreCouponRepository; use app\common\repositories\store\coupon\StoreCouponRepository;
use app\common\repositories\store\coupon\StoreCouponUserRepository; use app\common\repositories\store\coupon\StoreCouponUserRepository;
use crmeb\interfaces\ListenerInterface; use crmeb\interfaces\ListenerInterface;
use think\facade\Log;
/** /**
* Class OrderDeliveryListen * Class OrderDeliveryListen
@ -15,20 +17,53 @@ class OrderDeliveryListen implements ListenerInterface
{ {
$order = $event['order']; $order = $event['order'];
foreach ($order->orderProduct as $orderProduct) { foreach ($order->orderProduct as $orderProduct) {
$merchantCate = $orderProduct->product->merCateId; if (!$orderProduct->product->isPlatformCard()) {
if (empty($merchantCate) || $merchantCate[0]['mer_cate_id'] != env('PLATFORM_CARD_CATE_ID')) {
continue; continue;
} }
/** @var StoreCouponRepository $repo */ /** @var StoreCouponRepository $repo */
$repo = app()->make(StoreCouponRepository::class); $repo = app()->make(StoreCouponRepository::class);
$coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY) $coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY)->find();
->where('coupon_price', $orderProduct->product->price)->find();
if (empty($coupon)) { if (empty($coupon)) {
return; return;
} }
$coupon->coupon_price = bcmul($orderProduct->product_num, $orderProduct->product->price); $coupon->coupon_price = $order->total_price;
$repo->sendCoupon($coupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); $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;
}
} }