Browse Source

更新脚本

shish 1 month ago
parent
commit
f6f8d3686c
1 changed files with 71 additions and 29 deletions
  1. 71 29
      console/controllers/CustomController.php

+ 71 - 29
console/controllers/CustomController.php

@@ -14,6 +14,7 @@ use common\components\dict;
 use common\components\noticeUtil;
 use common\components\noticeUtil;
 use common\components\util;
 use common\components\util;
 use bizHd\user\classes\UserClass;
 use bizHd\user\classes\UserClass;
+use bizHd\user\models\User;
 use Overtrue\ChineseCalendar\Calendar;
 use Overtrue\ChineseCalendar\Calendar;
 use Yii;
 use Yii;
 use yii\console\Controller;
 use yii\console\Controller;
@@ -108,42 +109,83 @@ class CustomController extends Controller
 
 
     }
     }
 
 
-    //每天凌晨3点35分执行,取出所有 lunar==1 的 且 birthdayTime 小于当前时间的客户,然后把他们的阴历转阳历,然后更新他们的 birthdayTime
+    /**
+     * 每天凌晨 3:35 执行:阴历生日跨年刷新
+     * 从 xhUser 分页取出 lunar=1 且 birthdayTime 已过的用户,农历转当年/次年阳历后回写用户并同步 xhCustom
+     * 命令:./yii custom/update-birthday
+     */
     public function actionUpdateBirthday()
     public function actionUpdateBirthday()
     {
     {
         try {
         try {
+            date_default_timezone_set('PRC');
+            ini_set('memory_limit', '512M');
+            set_time_limit(0);
+
             $calendar = new Calendar();
             $calendar = new Calendar();
-            $customList = CustomClass::getAllByCondition(['lunar' => 1, 'birthdayTime<' => time()], null, 'id, userId, birthday, birthdayTime, birthdayMonth, birthdayDate', null, true);
-            foreach ($customList as $custom) {
-                $birthdayMonth = $custom->birthdayMonth;
-                $birthdayDate = $custom->birthdayDate;
-                $year = date("Y");
-                // 农历转公历
-                $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
-                $month = $result['gregorian_month'];
-                $day = $result['gregorian_day'];
-                $birthday = $year . '-' . $month . '-' . $day;
-
-                // 如果今年的生日已经过去,则计算明年的生日
-                if (strtotime($birthday) < strtotime(date('Y-m-d'))) {
-                    $year++;
-                    $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
-                    $month = $result['gregorian_month'];
-                    $day = $result['gregorian_day'];
-                    $birthday = $year . '-' . $month . '-' . $day;
+            $now = time();
+            $todayStart = strtotime(date('Y-m-d'));
+            $updatedUserCount = 0;
+
+            // 生日主数据在 xhUser;按 id 分页避免一次加载全表
+            $query = User::find()
+                ->select('id, birthday, birthdayTime, birthdayMonth, birthdayDate, lunar')
+                ->where(['lunar' => 1])
+                ->andWhere(['<', 'birthdayTime', $now])
+                ->orderBy('id ASC');
+
+            foreach ($query->batch(200) as $userList) {
+                foreach ($userList as $user) {
+                    $userId = $user->id;
+                    $birthdayMonth = $user->birthdayMonth;
+                    $birthdayDate = $user->birthdayDate;
+                    $year = (int)date('Y');
+
+                    try {
+                        // 农历转公历,有多处使用,需要同步修改,关键词 change_my_birthday
+                        $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
+                        $month = $result['gregorian_month'] ?? 1;
+                        $day = $result['gregorian_day'] ?? 1;
+                        $birthday = $year . '-' . $month . '-' . $day;
+
+                        // 今年阳历生日已过,则换算明年农历对应阳历
+                        if (strtotime($birthday) < $todayStart) {
+                            $year++;
+                            $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
+                            $month = $result['gregorian_month'] ?? 1;
+                            $day = $result['gregorian_day'] ?? 1;
+                            $birthday = $year . '-' . $month . '-' . $day;
+                        }
+                    } catch (\InvalidArgumentException $e) {
+                        noticeUtil::push(
+                            '阴历转阳历脚本单条失败,userId:' . $userId . ' 月份:' . $birthdayMonth . ' 日期:' . $birthdayDate . ' ' . $e->getMessage(),
+                            '15280215347'
+                        );
+                        continue;
+                    }
+
+                    $birthdayTime = strtotime($birthday);
+                    if ($birthdayTime <= 0) {
+                        continue;
+                    }
+
+                    // 先更新用户表(生日源数据),再按 userId 同步门店客户
+                    UserClass::updateById($userId, [
+                        'birthday' => $birthday,
+                        'birthdayTime' => $birthdayTime,
+                    ]);
+                    CustomClass::updateByCondition(['userId' => $userId], [
+                        'birthday' => $birthday,
+                        'birthdayTime' => $birthdayTime,
+                    ]);
+                    $updatedUserCount++;
                 }
                 }
-                $custom->birthday = $birthday;
-                $custom->birthdayTime = strtotime($birthday);
-                $custom->save();
-
-                UserClass::updateById($custom->userId, [
-                    'birthday' => $birthday,
-                    'birthdayTime' => $custom->birthdayTime
-                ]);
             }
             }
-            Yii::info('阴历转阳历脚本成功执行');
+
+            Yii::info('阴历转阳历脚本成功执行,更新用户数:' . $updatedUserCount);
+            echo "阴历转阳历完成,更新用户数:{$updatedUserCount}\n";
         } catch (\Exception $e) {
         } catch (\Exception $e) {
             Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage());
             Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage());
+            echo '阴历转阳历脚本执行失败,原因:' . $e->getMessage() . "\n";
         }
         }
     }
     }
 
 
@@ -246,4 +288,4 @@ class CustomController extends Controller
         //重新写这个方法,用rabbitMQ来实现
         //重新写这个方法,用rabbitMQ来实现
     }
     }
 
 
-}
+}