添加定时发放商户补贴券

This commit is contained in:
luofei 2024-03-15 18:00:50 +08:00
parent b2ea8b4a5e
commit 3b4ffa9eeb
3 changed files with 86 additions and 13 deletions

View File

@ -58,6 +58,7 @@ return [
\crmeb\listens\SyncQueueStatusListen::class, \crmeb\listens\SyncQueueStatusListen::class,
\crmeb\listens\ActivateCouponListen::class, \crmeb\listens\ActivateCouponListen::class,
\crmeb\listens\SetProductProfitRateListen::class, \crmeb\listens\SetProductProfitRateListen::class,
\crmeb\listens\SendSubsidyCouponListen::class,
] : [], ] : [],
'pay_success_user_recharge' => [\crmeb\listens\pay\UserRechargeSuccessListen::class], 'pay_success_user_recharge' => [\crmeb\listens\pay\UserRechargeSuccessListen::class],
'pay_success_user_order' => [\crmeb\listens\pay\UserOrderSuccessListen::class], 'pay_success_user_order' => [\crmeb\listens\pay\UserOrderSuccessListen::class],

View File

@ -18,24 +18,39 @@ use app\common\repositories\user\UserBillRepository;
use crmeb\interfaces\ListenerInterface; use crmeb\interfaces\ListenerInterface;
use crmeb\services\TimerService; use crmeb\services\TimerService;
use think\facade\Db; use think\facade\Db;
use think\facade\Log;
class AutoUnlockMerchantMoneyListen extends TimerService implements ListenerInterface class AutoUnlockMerchantMoneyListen extends TimerService implements ListenerInterface
{ {
public function handle($event): void public function handle($event): void
{ {
$this->tick(1000 * 60 * 20, function () { $this->tick(1000 * 60 * 60, function () {
if (time() >= strtotime('today 18:00:00') && time() <= strtotime('today 20:00:00')) {
request()->clearCache(); request()->clearCache();
/** @var UserBillRepository $userBill */
$userBill = app()->make(UserBillRepository::class); $userBill = app()->make(UserBillRepository::class);
$timer = ((int)systemConfig('mer_lock_time')); // $timer = ((int)systemConfig('mer_lock_time'));
$time = date('Y-m-d H:i:s', $timer ? strtotime("- $timer day") : time()); // $time = date('Y-m-d H:i:s', $timer ? strtotime("- $timer day") : time());
$time = date('Y-m-d 00:00:00');
$bills = $userBill->getTimeoutMerchantMoneyBill($time); $bills = $userBill->getTimeoutMerchantMoneyBill($time);
$merchant = app()->make(MerchantRepository::class); $merchant = app()->make(MerchantRepository::class);
$count = 0;
foreach ($bills as $bill) { foreach ($bills as $bill) {
Db::transaction(function () use ($bill, $merchant) { Db::startTrans();
try {
$merchant->addMoney($bill->mer_id, $bill->number); $merchant->addMoney($bill->mer_id, $bill->number);
$bill->status = 1; $bill->status = 1;
$bill->save(); $bill->save();
}); Db::commit();
$count++;
} catch (\Throwable $e) {
Db::rollback();
Log::error('商户冻结金额解冻出错:' . $e->getMessage());
}
}
if ($count > 0) {
Log::info('商户冻结金额解冻成功:' . $count);
}
} }
}); });
} }

View File

@ -0,0 +1,57 @@
<?php
namespace crmeb\listens;
use app\common\model\store\coupon\StoreCoupon;
use app\common\model\store\coupon\StoreCouponDetail;
use app\common\model\store\coupon\StoreCouponUser;
use app\common\model\system\merchant\Merchant;
use app\common\repositories\store\coupon\StoreCouponRepository;
use app\common\repositories\store\coupon\StoreCouponUserRepository;
use crmeb\interfaces\ListenerInterface;
use crmeb\services\TimerService;
use think\facade\Log;
class SendSubsidyCouponListen extends TimerService implements ListenerInterface
{
public function handle($event): void
{
//TODO 上线后改成30分钟
$this->tick(1000 * 60 * 1, function () {
Log::info('定时任务:发放商户采购补贴券');
try {
$coupon = StoreCoupon::where('type', StoreCouponRepository::TYPE_SALE_SUBSIDY)->find();
$count = 0;
if ($coupon) {
foreach ($coupon['config'] as $item) {
$purchaseAmount = $item['amount'] * 0.5;
$merchants = Merchant::whereIn('type_id', $item['type_id'])
->where('sale_amount', '>=', $item['amount'])
->where('purchase_amount', '>=', $purchaseAmount)
->select();
foreach ($merchants as $merchant) {
//商户已获得的补贴金额
$gotSubsidy = StoreCouponUser::where('coupon_id', $coupon['coupon_id'])
->where('uid', $merchant->uid)
->value('origin_price');
if ($gotSubsidy >= $item['subsidy']) {
continue;
}
//补贴金额为当前补贴减去已获得的补贴
$amount = bcsub($item['subsidy'], $gotSubsidy, 2);
/** @var StoreCouponRepository $repo */
$repo = app()->make(StoreCouponRepository::class);
$coupon->coupon_price = $amount;
$couponUser = $repo->sendCoupon($coupon, $merchant->uid, StoreCouponUserRepository::SEND_TYPE_SEND);
StoreCouponDetail::income([], $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title']);
$count++;
}
}
}
Log::info('定时任务:发放商户采购补贴券,成功数量:' . $count);
} catch (\Throwable $e) {
Log::info('定时任务发放商户采购补贴券error => ' . $e->getMessage());
}
});
}
}