shizhongqi 1 сар өмнө
parent
commit
059c3747cf

+ 3 - 34
app-mall/controllers/UserController.php

@@ -15,7 +15,6 @@ use biz\shop\classes\ShopClass;
 use common\components\dict;
 use common\components\imgUtil;
 use common\components\jwt;
-use common\components\noticeUtil;
 use common\components\stringUtil;
 use Yii;
 use common\components\util;
@@ -110,6 +109,9 @@ class UserController extends BaseController
                 $custom->birthday = $birthday;
                 $custom->birthdayTime = $birthdayTime;
                 $custom->save();
+
+                //变更客户生日的同时,同步清空含对应客户的门店的缓存
+                BirthdayGiftClass::clearWorkbenchSummaryCache($custom->shopId);
             }
         }
 
@@ -139,39 +141,6 @@ class UserController extends BaseController
     {
         $user = $this->user;
         if (!empty($user)) {
-            $birthday = $user->birthday ?? '';
-            //如果当年的生日时间没有更新,则需要给更新
-            $year = date("Y", strtotime($birthday));
-            $thisYear = date("Y");
-            if ($year != $thisYear) {
-                $lunar = $user->lunar ?? 0;
-                $userId = $this->userId;
-                $birthdayMonth = $user->birthdayMonth ?? 0;
-                $birthdayDate = $user->birthdayDate ?? 0;
-                if ($lunar == 1 && $birthdayMonth > 0 && $birthdayDate > 0) {
-                    try {
-                        //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
-                        $calendar = new Calendar();
-                        $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
-                        $month = $result['gregorian_month'] ?? 1;
-                        $date = $result['gregorian_day'] ?? 1;
-                        $birthday = date("Y") . '-' . $month . '-' . $date;
-                        $user->birthday = $birthday;
-                        $user->save();
-                        CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
-                    } catch (\InvalidArgumentException $e) {
-                        $msg = $e->getMessage();
-                        noticeUtil::push("更新生日出错了呢,userId:" . $userId . ' ' . $msg, '15280215347');
-                    }
-                } else {
-                    $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
-                    $user->birthday = $birthday;
-                    $user->save();
-                    CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
-                }
-            }
-
-            // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
             $user = $user->toArray();
             $shortAvatar = $user['avatar'] ?? '';
             $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";

+ 71 - 18
biz-hd/birthday/classes/BirthdayGiftClass.php

@@ -241,23 +241,32 @@ class BirthdayGiftClass extends BaseClass
      */
     public static function getEligibleCustoms($shopId, $mainId)
     {
+        $errMsgs = '';
         $levelMap = self::getLevelBenefitMap($mainId, $shopId);
         $list = CustomClass::getAllByCondition([
             'shopId' => $shopId,
             'member>' => 0,
             'delStatus' => 0,
         ], 'id ASC', '*', null, true);
-        $result = [];
+        $customs = [];
         if (!empty($list)) {
             foreach ($list as $custom) {
                 if (self::hasBirthdayBenefit($custom, $levelMap)) {
-                    $result[] = $custom;
+                    $customs[] = $custom;
                 } else {
-                    noticeUtil::push('客户' . $custom->name . '(' . $custom->member . ')没有生日权益');
+                    $errMsgs .= '客户' . $custom->name . '(等级' . $custom->member . ')没有生日权益' . ', ';
                 }
             }
         }
-        return [$result, $levelMap];
+
+        if ($errMsgs != '') {
+            $msg = '门店id='.$shopId.':'.$errMsgs;
+            //noticeUtil::push($msg);
+            Yii::info($msg);
+            return [false, false];
+        }
+
+        return [$customs, $levelMap];
     }
 
     /**
@@ -268,7 +277,7 @@ class BirthdayGiftClass extends BaseClass
      */
     public static function getWorkbenchSummary($shopId, $mainId)
     {
-        $cacheKey = self::getWorkbenchSummaryCacheKey($shopId, $mainId);
+        $cacheKey = self::getWorkbenchSummaryCacheKey($shopId);
         try {
             $cached = Yii::$app->redis->get($cacheKey);
             if ($cached !== false && $cached !== null) {
@@ -286,6 +295,11 @@ class BirthdayGiftClass extends BaseClass
         $week = 0;
 
         list($customs,) = self::getEligibleCustoms($shopId, $mainId);
+        if ($customs === false) { //门店的会员等级设置异常,直接返回
+            Yii::error('门店'.$shopId.'--会员等级设置异常');
+            return [];
+        }
+
         foreach ($customs as $custom) {
             if (self::getBirthdayMonthDay($custom) === null) {
                 continue;
@@ -319,17 +333,16 @@ class BirthdayGiftClass extends BaseClass
         return $data;
     }
 
-    private static function getWorkbenchSummaryCacheKey($shopId, $mainId)
+    private static function getWorkbenchSummaryCacheKey($shopId)
     {
-        return 'hd:birthdayGift:workbenchSummary:'
-            . intval($mainId) . ':' . intval($shopId) . ':' . date('Ymd');
+        return 'hd_birthdayGift_workbenchSummary:' . '_' . intval($shopId) . ':' . date('Ymd');
     }
 
-    private static function clearWorkbenchSummaryCache($shopId, $mainId)
+    public static function clearWorkbenchSummaryCache($shopId)
     {
         try {
             Yii::$app->redis->executeCommand('DEL', [
-                self::getWorkbenchSummaryCacheKey($shopId, $mainId),
+                self::getWorkbenchSummaryCacheKey($shopId),
             ]);
         } catch (\Throwable $throwable) {
             Yii::warning('清理生日工作台缓存失败:' . $throwable->getMessage(), __METHOD__);
@@ -431,7 +444,7 @@ class BirthdayGiftClass extends BaseClass
             $levelMap = self::getLevelBenefitMap($shop->mainId, $shop->id);
             $gift = self::createOrUpdateYearRecord($shop, $custom, $levelMap);
             $transaction->commit();
-            self::clearWorkbenchSummaryCache($shop->id, $shop->mainId);
+            self::clearWorkbenchSummaryCache($shop->id);
             return $gift;
         } catch (\Exception $exception) {
             $transaction->rollBack();
@@ -450,8 +463,9 @@ class BirthdayGiftClass extends BaseClass
         $shopId = intval($shop->id);
         $year = intval(date('Y')); //今年
         list($customs, $levelMap) = self::getEligibleCustoms($shopId, $shop->mainId);
-        if (empty($customs)) {
-            return true;
+        if ($customs === false) {
+            Yii::error('会员权益配置异常');
+            return false;
         }
         $existingRows = self::getAllByCondition(['shopId' => $shopId, 'year' => $year], null, 'customId');
         $existingIds = [];
@@ -511,7 +525,10 @@ class BirthdayGiftClass extends BaseClass
      */
     public static function getBoardList($shop, $params)
     {
-        self::ensureYearRecords($shop);
+        $re = self::ensureYearRecords($shop);
+        if ($re === false) {
+            return [];
+        }
         $period = $params['period'] ?? 'week';
         $statusFilter = isset($params['status']) ? intval($params['status']) : -1;
         $searchText = trim($params['searchText'] ?? $params['keyword'] ?? '');
@@ -519,6 +536,10 @@ class BirthdayGiftClass extends BaseClass
         $pageSize = max(1, min(50, intval($params['pageSize'] ?? 20)));
 
         list($customs,) = self::getEligibleCustoms($shop->id, $shop->mainId);
+        if ($customs === false) {
+            Yii::error('会员权益配置异常');
+            return [];
+        }
         $customMap = [];
         foreach ($customs as $c) {
             $customMap[$c->id] = $c;
@@ -583,9 +604,17 @@ class BirthdayGiftClass extends BaseClass
 
     public static function getStatusCounts($shop, $params)
     {
-        self::ensureYearRecords($shop);
+        $re = self::ensureYearRecords($shop);
+        if ($re === false) {
+            return [];
+        }
         $period = $params['period'] ?? 'week';
         list($customs,) = self::getEligibleCustoms($shop->id, $shop->mainId);
+        if ($customs === false) {
+            Yii::error('会员权益配置异常');
+            return [];
+        }
+
         $customMap = [];
         foreach ($customs as $c) {
             $customMap[$c->id] = $c;
@@ -634,8 +663,15 @@ class BirthdayGiftClass extends BaseClass
 
     public static function getGiftStats($shop, $period)
     {
-        self::ensureYearRecords($shop);
+        $re = self::ensureYearRecords($shop);
+        if ($re === false) {
+            return [];
+        }
         list($customs,) = self::getEligibleCustoms($shop->id, $shop->mainId);
+        if ($customs === false) {
+            Yii::error('会员权益配置异常');
+            return [];
+        }
         $customMap = [];
         foreach ($customs as $c) {
             $customMap[$c->id] = $c;
@@ -786,8 +822,22 @@ class BirthdayGiftClass extends BaseClass
 
     public static function batchNotifyTomorrow($shop)
     {
-        self::ensureYearRecords($shop);
+        $re = self::ensureYearRecords($shop);
+        if ($re === false) {
+            return [
+                'success' => 0,
+                'fail' => 1,
+                'messages' => ['会员等级设置异常'],
+            ];
+        }
         list($customs, $levelMap) = self::getEligibleCustoms($shop->id, $shop->mainId);
+        if ($customs === false) {
+            return [
+                'success' => 0,
+                'fail' => 1,
+                'messages' => ['会员等级设置异常'],
+            ];
+        }
         $success = 0;
         $fail = 0;
         $messages = [];
@@ -908,7 +958,10 @@ class BirthdayGiftClass extends BaseClass
 
     public static function notifyOneTomorrow($shop, $giftId)
     {
-        self::ensureYearRecords($shop);
+        $re = self::ensureYearRecords($shop);
+        if ($re === false) {
+            util::fail('会员等级设置异常');
+        }
         $gift = self::getById($giftId, true);
         if (empty($gift) || intval($gift->shopId) != intval($shop->id)) {
             util::fail('记录不存在');

+ 1 - 81
console/controllers/UserController.php

@@ -22,84 +22,4 @@ use yii\console\Controller;
 class UserController extends Controller
 {
 
-    /**
-     * 每天凌晨 3:35 执行:阴历生日跨年刷新
-     * 从 xhUser 分页取出 lunar=1 且 birthdayTime 已过的用户,农历转当年/次年阳历后回写用户并同步 xhCustom
-     * 命令:./yii custom/update-birthday
-     */
-    public function actionUpdateBirthday()
-    {
-        try {
-            date_default_timezone_set('PRC');
-            ini_set('memory_limit', '512M');
-            set_time_limit(0);
-
-            $calendar = new Calendar();
-            $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++;
-                }
-            }
-
-            Yii::info('阴历转阳历脚本成功执行,更新用户数:' . $updatedUserCount);
-            echo "阴历转阳历完成,更新用户数:{$updatedUserCount}\n";
-        } catch (\Exception $e) {
-            Yii::error('阴历转阳历脚本执行失败,原因:' . $e->getMessage());
-            echo '阴历转阳历脚本执行失败,原因:' . $e->getMessage() . "\n";
-        }
-    }
-
-}
+}

+ 2 - 2
vendor/composer/installed.php

@@ -5,7 +5,7 @@
         'type' => 'project',
         'install_path' => __DIR__ . '/../../',
         'aliases' => array(),
-        'reference' => '6c1ee9f69967eaa7464da99e1e1e3762e664c938',
+        'reference' => '31cb99b7b0503fc12f9fa2b1df7ef1181a7c75f3',
         'name' => 'yiisoft/yii2-app-advanced',
         'dev' => true,
     ),
@@ -1770,7 +1770,7 @@
             'type' => 'project',
             'install_path' => __DIR__ . '/../../',
             'aliases' => array(),
-            'reference' => '6c1ee9f69967eaa7464da99e1e1e3762e664c938',
+            'reference' => '31cb99b7b0503fc12f9fa2b1df7ef1181a7c75f3',
             'dev_requirement' => false,
         ),
         'yiisoft/yii2-bootstrap' => array(