UserController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\custom\models\Custom;
  5. use bizGhs\order\classes\OrderClass;
  6. use bizHd\custom\classes\CustomClass;
  7. use bizHd\custom\classes\HdClass;
  8. use bizHd\member\classes\MemberLevelClass;
  9. use bizHd\purchase\classes\PurchaseClass;
  10. use bizHd\shop\models\Shop;
  11. use common\components\dict;
  12. use common\components\noticeUtil;
  13. use common\components\util;
  14. use bizHd\user\classes\UserClass;
  15. use bizHd\user\models\User;
  16. use Overtrue\ChineseCalendar\Calendar;
  17. use Yii;
  18. use yii\console\Controller;
  19. class UserController extends Controller
  20. {
  21. /**
  22. * 每天凌晨 3:35 执行:阴历生日跨年刷新
  23. * 从 xhUser 分页取出 lunar=1 且 birthdayTime 已过的用户,农历转当年/次年阳历后回写用户并同步 xhCustom
  24. * 命令:./yii custom/update-birthday
  25. */
  26. public function actionUpdateBirthday()
  27. {
  28. try {
  29. date_default_timezone_set('PRC');
  30. ini_set('memory_limit', '512M');
  31. set_time_limit(0);
  32. $calendar = new Calendar();
  33. $now = time();
  34. $todayStart = strtotime(date('Y-m-d'));
  35. $updatedUserCount = 0;
  36. // 生日主数据在 xhUser;按 id 分页避免一次加载全表
  37. $query = User::find()
  38. ->select('id, birthday, birthdayTime, birthdayMonth, birthdayDate, lunar')
  39. ->where(['lunar' => 1])
  40. ->andWhere(['<', 'birthdayTime', $now])
  41. ->orderBy('id ASC');
  42. foreach ($query->batch(200) as $userList) {
  43. foreach ($userList as $user) {
  44. $userId = $user->id;
  45. $birthdayMonth = $user->birthdayMonth;
  46. $birthdayDate = $user->birthdayDate;
  47. $year = (int)date('Y');
  48. try {
  49. // 农历转公历,有多处使用,需要同步修改,关键词 change_my_birthday
  50. $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
  51. $month = $result['gregorian_month'] ?? 1;
  52. $day = $result['gregorian_day'] ?? 1;
  53. $birthday = $year . '-' . $month . '-' . $day;
  54. // 今年阳历生日已过,则换算明年农历对应阳历
  55. if (strtotime($birthday) < $todayStart) {
  56. $year++;
  57. $result = $calendar->lunar($year, $birthdayMonth, $birthdayDate);
  58. $month = $result['gregorian_month'] ?? 1;
  59. $day = $result['gregorian_day'] ?? 1;
  60. $birthday = $year . '-' . $month . '-' . $day;
  61. }
  62. } catch (\InvalidArgumentException $e) {
  63. noticeUtil::push(
  64. '阴历转阳历脚本单条失败,userId:' . $userId . ' 月份:' . $birthdayMonth . ' 日期:' . $birthdayDate . ' ' . $e->getMessage(),
  65. '15280215347'
  66. );
  67. continue;
  68. }
  69. $birthdayTime = strtotime($birthday);
  70. if ($birthdayTime <= 0) {
  71. continue;
  72. }
  73. // 先更新用户表(生日源数据),再按 userId 同步门店客户
  74. UserClass::updateById($userId, [
  75. 'birthday' => $birthday,
  76. 'birthdayTime' => $birthdayTime,
  77. ]);
  78. CustomClass::updateByCondition(['userId' => $userId], [
  79. 'birthday' => $birthday,
  80. 'birthdayTime' => $birthdayTime,
  81. ]);
  82. $updatedUserCount++;
  83. }
  84. }
  85. Yii::info('阴历转阳历脚本成功执行,更新用户数:' . $updatedUserCount);
  86. echo "阴历转阳历完成,更新用户数:{$updatedUserCount}\n";
  87. } catch (\Exception $e) {
  88. Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage());
  89. echo '阴历转阳历脚本执行失败,原因:' . $e->getMessage() . "\n";
  90. }
  91. }
  92. }