From 3b4ffa9eebd2d56f386133aa55ee115657fa6551 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Fri, 15 Mar 2024 18:00:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6=E5=8F=91?= =?UTF-8?q?=E6=94=BE=E5=95=86=E6=88=B7=E8=A1=A5=E8=B4=B4=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/event.php | 1 + .../listens/AutoUnlockMerchantMoneyListen.php | 41 ++++++++----- crmeb/listens/SendSubsidyCouponListen.php | 57 +++++++++++++++++++ 3 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 crmeb/listens/SendSubsidyCouponListen.php diff --git a/app/event.php b/app/event.php index 2374caa0..84bdd715 100644 --- a/app/event.php +++ b/app/event.php @@ -58,6 +58,7 @@ return [ \crmeb\listens\SyncQueueStatusListen::class, \crmeb\listens\ActivateCouponListen::class, \crmeb\listens\SetProductProfitRateListen::class, + \crmeb\listens\SendSubsidyCouponListen::class, ] : [], 'pay_success_user_recharge' => [\crmeb\listens\pay\UserRechargeSuccessListen::class], 'pay_success_user_order' => [\crmeb\listens\pay\UserOrderSuccessListen::class], diff --git a/crmeb/listens/AutoUnlockMerchantMoneyListen.php b/crmeb/listens/AutoUnlockMerchantMoneyListen.php index 612b2080..68ce5969 100644 --- a/crmeb/listens/AutoUnlockMerchantMoneyListen.php +++ b/crmeb/listens/AutoUnlockMerchantMoneyListen.php @@ -18,24 +18,39 @@ use app\common\repositories\user\UserBillRepository; use crmeb\interfaces\ListenerInterface; use crmeb\services\TimerService; use think\facade\Db; +use think\facade\Log; class AutoUnlockMerchantMoneyListen extends TimerService implements ListenerInterface { public function handle($event): void { - $this->tick(1000 * 60 * 20, function () { - request()->clearCache(); - $userBill = app()->make(UserBillRepository::class); - $timer = ((int)systemConfig('mer_lock_time')); - $time = date('Y-m-d H:i:s', $timer ? strtotime("- $timer day") : time()); - $bills = $userBill->getTimeoutMerchantMoneyBill($time); - $merchant = app()->make(MerchantRepository::class); - foreach ($bills as $bill) { - Db::transaction(function () use ($bill, $merchant) { - $merchant->addMoney($bill->mer_id, $bill->number); - $bill->status = 1; - $bill->save(); - }); + $this->tick(1000 * 60 * 60, function () { + if (time() >= strtotime('today 18:00:00') && time() <= strtotime('today 20:00:00')) { + request()->clearCache(); + /** @var UserBillRepository $userBill */ + $userBill = app()->make(UserBillRepository::class); +// $timer = ((int)systemConfig('mer_lock_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); + $merchant = app()->make(MerchantRepository::class); + $count = 0; + foreach ($bills as $bill) { + Db::startTrans(); + try { + $merchant->addMoney($bill->mer_id, $bill->number); + $bill->status = 1; + $bill->save(); + Db::commit(); + $count++; + } catch (\Throwable $e) { + Db::rollback(); + Log::error('商户冻结金额解冻出错:' . $e->getMessage()); + } + } + if ($count > 0) { + Log::info('商户冻结金额解冻成功:' . $count); + } } }); } diff --git a/crmeb/listens/SendSubsidyCouponListen.php b/crmeb/listens/SendSubsidyCouponListen.php new file mode 100644 index 00000000..e55d896b --- /dev/null +++ b/crmeb/listens/SendSubsidyCouponListen.php @@ -0,0 +1,57 @@ +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()); + } + }); + } +}