Przeglądaj źródła

Merge branch 'master' into dev

shizhongqi 3 miesięcy temu
rodzic
commit
ed42666a77
1 zmienionych plików z 70 dodań i 65 usunięć
  1. 70 65
      console/controllers/CustomController.php

+ 70 - 65
console/controllers/CustomController.php

@@ -149,82 +149,87 @@ class CustomController extends Controller
     // 把所有客户的累计消费和成长值、积分都刷新一下
     public function actionRefreshCustomData()
     {
+        try {
+            ini_set('memory_limit', '2045M');
+            set_time_limit(0);
+
+            $redisKey = 'refresh_custom_data_processed_shops';
+
+            // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据,每次取出500个进行处理
+            $query = \bizHd\shop\models\Shop::find()
+                ->select('id, mainId')
+                ->where(['ptStyle' => 1])
+                ->orderBy('id DESC')
+                ->asArray();
+
+            // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分
+            foreach ($query->batch(500) as $allShops) {
+                if (!empty($allShops)) {
+                    foreach ($allShops as $shop) {
+                        $shopId = $shop['id'];
+                        $mainId = $shop['mainId'];
+
+                        // 判断是否已经执行过,执行过则跳过
+                        $hasProcessed = Yii::$app->redis->executeCommand('SISMEMBER', [$redisKey, $shopId]);
+                        if ($hasProcessed) {
+                            continue;
+                        }
 
-        ini_set('memory_limit', '2045M');
-        set_time_limit(0);
-
-        $redisKey = 'refresh_custom_data_processed_shops';
-
-        // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据,每次取出500个进行处理
-        $query = \bizHd\shop\models\Shop::find()
-            ->select('id, mainId')
-            ->where(['ptStyle' => 1])
-            ->orderBy('id DESC')
-            ->asArray();
-
-        // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分
-        foreach ($query->batch(500) as $allShops) {
-            if (!empty($allShops)) {
-                foreach ($allShops as $shop) {
-                    $shopId = $shop['id'];
-                    $mainId = $shop['mainId'];
-
-                    // 判断是否已经执行过,执行过则跳过
-                    $hasProcessed = Yii::$app->redis->executeCommand('SISMEMBER', [$redisKey, $shopId]);
-                    if ($hasProcessed) {
-                        continue;
-                    }
-
-                    $customs = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, balance, buyAmount, growth, integral, member, memberName', null, true);
-                    $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
-                    if (!empty($customs) && !empty($levelDatas)) {
-                        foreach ($customs as $c) {
-                            $buyAmount = 0;
-                            $integral = 0;
-                            // 获取所有零售订单,计算消费金额
-                            $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $c->id], null, 'id, forward, actPrice, payWay');
-                            if (!empty($orderList)) {
-                                foreach ($orderList as $order) {
-                                    if ($order['forward'] == 0) { //0 常规订单
-                                        if ($order['payWay'] == dict::getDict('payWay', 'balancePay')) {
-                                            // 余额支付不计算积分与成长值
+                        $customs = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, balance, buyAmount, growth, integral, member, memberName', null, true);
+                        $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
+                        if (!empty($customs) && !empty($levelDatas)) {
+                            foreach ($customs as $c) {
+                                $buyAmount = 0;
+                                $integral = 0;
+                                // 获取所有零售订单,计算消费金额
+                                $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $c->id], null, 'id, forward, actPrice, payWay');
+                                if (!empty($orderList)) {
+                                    foreach ($orderList as $order) {
+                                        if ($order['forward'] == 0) { //0 常规订单
+                                            if ($order['payWay'] == dict::getDict('payWay', 'balancePay')) {
+                                                // 余额支付不计算积分与成长值
+                                            } else {
+                                                $integral = bcadd($integral, $order['actPrice'], 2);
+                                            }
+                                            $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
+                                        } elseif ($order['forward'] == 1) { //1 售后付款单(红冲)
+                                            $integral = bcsub($integral, $order['actPrice'], 2);
+                                            $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
                                         } else {
-                                            $integral = bcadd($integral, $order['actPrice'], 2);
+                                            Yii::info('订单类型不存在,订单ID:' . $order['id']);
+                                            continue;
                                         }
-                                        $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
-                                    } elseif ($order['forward'] == 1) { //1 售后付款单(红冲)
-                                        $integral = bcsub($integral, $order['actPrice'], 2);
-                                        $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
-                                    } else {
-                                        Yii::info('订单类型不存在,订单ID:' . $order['id']);
-                                        continue;
                                     }
                                 }
+                                $c->buyAmount = $buyAmount;
+                                $c->growth = $integral + $c->balance;
+                                $c->integral = $integral  + $c->balance;
+
+                                // 根据 growth 设置等级
+                                $memberData = CustomClass::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
+                                $c->member = $memberData['level'];
+                                $c->memberName = $memberData['name'];
+                                $c->listend = false; //关闭对 xhCustom 的 buyAmount 变动监听
+                                $c->save();
+
+                                // 更新客户所对应花店的消费金额
+                                HdClass::updateByCondition(['shopId' => $shopId, 'userId' => $c->userId], ['expendAmount' => $buyAmount]);
                             }
-                            $c->buyAmount = $buyAmount;
-                            $c->growth = $integral + $c->balance;
-                            $c->integral = $integral  + $c->balance;
-
-                            // 根据 growth 设置等级
-                            $memberData = CustomClass::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
-                            $c->member = $memberData['level'];
-                            $c->memberName = $memberData['name'];
-                            $c->listend = false; //关闭对 xhCustom 的 buyAmount 变动监听
-                            $c->save();
-
-                            // 更新客户所对应花店的消费金额
-                            HdClass::updateByCondition(['shopId' => $shopId, 'userId' => $c->userId], ['expendAmount' => $buyAmount]);
                         }
-                    }
 
-                    // 记录该 shopId 已经执行完成
-                    Yii::$app->redis->executeCommand('SADD', [$redisKey, $shopId]);
+                        // 记录该 shopId 已经执行完成
+                        Yii::$app->redis->executeCommand('SADD', [$redisKey, $shopId]);
+                    }
                 }
             }
-        }
 
-        // 脚本执行完之后,清除缓存
-        Yii::$app->redis->executeCommand('DEL', [$redisKey]);
+            // 脚本执行完之后,清除缓存
+            Yii::$app->redis->executeCommand('DEL', [$redisKey]);
+
+            Yii::info('把所有客户的累计消费和成长值、积分都刷新一下脚本成功执行');
+        } catch (\Exception $e) {
+            Yii::error('把所有客户的累计消费和成长值、积分都刷新一下脚本执行失败,原因:' . $e->getMessage());
+        }
     }
 
     //批量创建客户