From d787726c86b8796b26ca8ae2a1d9de47e55ce393 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Wed, 28 Feb 2024 12:09:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=95=86=E5=93=81=E6=AF=9B?= =?UTF-8?q?=E5=88=A9=E7=8E=87=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/store/product/ProductAttrDao.php | 3 ++ app/event.php | 1 + crmeb/listens/SetProductProfitRateListen.php | 36 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 crmeb/listens/SetProductProfitRateListen.php diff --git a/app/common/dao/store/product/ProductAttrDao.php b/app/common/dao/store/product/ProductAttrDao.php index 54c05cb7..5cfc9cd8 100755 --- a/app/common/dao/store/product/ProductAttrDao.php +++ b/app/common/dao/store/product/ProductAttrDao.php @@ -41,6 +41,9 @@ class ProductAttrDao extends BaseDao */ public function insert(array $data) { + foreach ($data as &$item) { + $item['profit_rate'] = bcdiv(bcsub($item['price'], $item['procure_price'], 2), $item['price'], 2) * 100; + } return ($this->getModel()::getDB())->insertAll($data); } diff --git a/app/event.php b/app/event.php index 9c80c6cf..c5645222 100755 --- a/app/event.php +++ b/app/event.php @@ -57,6 +57,7 @@ return [ \crmeb\listens\SendSvipCouponListen::class, \crmeb\listens\AutoCheckCreditBuyListen::class, \crmeb\listens\ActivateConsumptionListen::class, + \crmeb\listens\SetProductProfitRateListen::class, ] : [], 'pay_success_user_recharge' => [\crmeb\listens\pay\UserRechargeSuccessListen::class], 'pay_success_user_order' => [\crmeb\listens\pay\UserOrderSuccessListen::class], diff --git a/crmeb/listens/SetProductProfitRateListen.php b/crmeb/listens/SetProductProfitRateListen.php new file mode 100644 index 00000000..b84c0a86 --- /dev/null +++ b/crmeb/listens/SetProductProfitRateListen.php @@ -0,0 +1,36 @@ +tick(1000 * 60, function () { + Log::info('定时任务:更新商品利润率'); + try { + $storeProducts = ProductAttrValue::where('profit_rate', 0) + ->where('procure_price', '>', 0) + ->whereRaw('price>procure_price') + ->limit(100) + ->select(); + foreach ($storeProducts as $storeProduct) { + $storeProduct->profit_rate = bcdiv(bcsub($storeProduct->price, $storeProduct->procure_price, 2), $storeProduct->price, 2) * 100; + $storeProduct->save(); + } + } catch (\Throwable $e) { + Log::info('定时任务:更新商品利润率,error => ' . $e->getMessage()); + } + }); + } + +}