调整店铺抵扣券发放
This commit is contained in:
parent
63c6cb8072
commit
7d67779fea
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user