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()); + } + }); + } + +}