zxiansheng 4 лет назад
Родитель
Сommit
40e43cb5a3
2 измененных файлов с 127 добавлено и 16 удалено
  1. 67 7
      common/helpers/bonus/BonusSend.php
  2. 60 9
      common/models/EmployLevel.php

+ 67 - 7
common/helpers/bonus/BonusSend.php

@@ -36,6 +36,7 @@ use yii\base\StaticInstanceTrait;
 use common\helpers\Cache;
 use common\helpers\Date;
 use common\models\CalcBonus;
+use common\models\CalcBonusBS;
 use common\models\PerfActiveUser;
 use common\models\PerfMonth;
 use common\models\User;
@@ -379,14 +380,28 @@ class BonusSend extends BaseObject {
     }
 
     /**
-     * 更新会员聘级
+     * 蓝星奖金(即新的管理奖),更新会员聘级
      * @param int $offset
      * @return bool
      * @throws \yii\db\Exception
      */
     public function updateEmpLevel(int $offset = 0) {
         if ($this->_isCalcMonth) {
-            $allData = PerfMonth::findUseDbCalc()->yearMonth($this->_calcYearMonth)->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->all();
+            $empLv = EmployLevel::getIdConvertLevelSortCache();
+            $allData = CalcBonusBS::findUseDbCalc()
+            ->yearMonth($this->_calcYearMonth)
+            ->where(
+                'CALC_MONTH=:CALC_MONTH AND PERIOD_NUM=:PERIOD_NUM', 
+                [
+                    ':CALC_MONTH' => $this->_calcYearMonth,
+                    ':PERIOD_NUM' =>$this->_periodNum
+                ]
+            )
+            ->orderBy('CREATED_AT DESC')
+            ->groupBy('USER_ID')
+            ->offset($offset)
+            ->limit($this->_limit)
+            ->all();
             //@todo 用户级别不变则不更新
             $defaultEmpLv = EmployLevel::getDefaultLevelId();
             if ($allData) {
@@ -394,11 +409,20 @@ class BonusSend extends BaseObject {
                 try {
                     foreach ($allData as $data) {
                         //@todo 用户级别不变则不更新
-                        if( $data['LAST_EMP_LV'] === $defaultEmpLv ) continue;
-
-                        User::updateAll(['EMP_LV' => $data['LAST_EMP_LV']], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
-                        User::deleteBaseInfoFromRedis($data['USER_ID']);
-                        unset($data);
+                        if( $data['LEVEL_ID'] === $defaultEmpLv ) continue;
+                        $user = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
+                        $userEmpLv = $user['EMP_LV']; // 用户的历史最高聘级
+                        $nowBsEmpLv = $data['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
+                        $userEmpLvSort = $empLv[$userEmpLv]; // 历史最高聘级的 级别值
+                        $nowBsEmpLvSort = $empLv[$nowBsEmpLv]; // 当前蓝星计算的聘级 级别值
+                        if ($nowBsEmpLvSort > $userEmpLvSort) {
+                            // 如果当前期的级别值大于历史最高级别,则更新用户表里的最高聘级
+                            User::updateAll(['EMP_LV' => $data['LEVEL_ID']], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
+                            User::deleteBaseInfoFromRedis($data['USER_ID']);
+                            unset($data);
+                        } else {
+                            continue;
+                        }
                     }
                     $transaction->commit();
                 } catch (Exception $e) {
@@ -414,6 +438,42 @@ class BonusSend extends BaseObject {
         return true;
     }
 
+    // /**
+    //  * 更新会员聘级
+    //  * @param int $offset
+    //  * @return bool
+    //  * @throws \yii\db\Exception
+    //  */
+    // public function updateEmpLevel(int $offset = 0) {
+    //     if ($this->_isCalcMonth) {
+    //         $allData = PerfMonth::findUseDbCalc()->yearMonth($this->_calcYearMonth)->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->all();
+    //         //@todo 用户级别不变则不更新
+    //         $defaultEmpLv = EmployLevel::getDefaultLevelId();
+    //         if ($allData) {
+    //             $transaction = Yii::$app->db->beginTransaction();
+    //             try {
+    //                 foreach ($allData as $data) {
+    //                     //@todo 用户级别不变则不更新
+    //                     if( $data['LAST_EMP_LV'] === $defaultEmpLv ) continue;
+
+    //                     User::updateAll(['EMP_LV' => $data['LAST_EMP_LV']], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
+    //                     User::deleteBaseInfoFromRedis($data['USER_ID']);
+    //                     unset($data);
+    //                 }
+    //                 $transaction->commit();
+    //             } catch (Exception $e) {
+    //                 $transaction->rollBack();
+    //                 $this->addError('updateEmpLevel', '更新聘级失败,原因:' . $e->getMessage());
+    //                 return false;
+    //             }
+    //             unset($transaction, $allData, $defaultEmpLv);
+    //             return $this->updateEmpLevel($offset + $this->_limit);
+    //         }
+    //         unset($allData);
+    //     }
+    //     return true;
+    // }
+
     // 更活跃会员,将is_send改成1
     public function updateActiveUser() {
         try {

+ 60 - 9
common/models/EmployLevel.php

@@ -40,16 +40,29 @@ class EmployLevel extends \common\components\ActiveRecord
     const JX_ZR_LEVEL_ID = 'E121497617216708615';
     const SHX_ZJ_LEVEL_ID = '67BE82019BE524CAE055736AECE8644D';
 
+    // CONST EMP_LEVEL_SORT = [
+    //     'NO_LEVEL' => 0,//无聘级
+    //     'JX_ZR_LEVEL' => 1,//见习主任
+    //     'ZR_LEVEL' => 2,//主任
+    //     'GJ_ZR_LEVEL' => 3,//高级主任
+    //     'JL_LEVEL' => 4,//经理
+    //     'GJ_JL_LEVEL' => 5,//高级经理
+    //     'ZJ_LEVEL' => 6,//总监
+    //     'GJ_ZJ_LEVEL' => 7,//高级总监
+    //     'SHX_ZJ_LEVEL' => 8,//首席总监
+    // ];
+
     CONST EMP_LEVEL_SORT = [
         'NO_LEVEL' => 0,//无聘级
-        'JX_ZR_LEVEL' => 1,//见习主任
-        'ZR_LEVEL' => 2,//主任
-        'GJ_ZR_LEVEL' => 3,//高级主任
-        'JL_LEVEL' => 4,//经理
-        'GJ_JL_LEVEL' => 5,//高级经理
-        'ZJ_LEVEL' => 6,//总监
-        'GJ_ZJ_LEVEL' => 7,//高级总监
-        'SHX_ZJ_LEVEL' => 8,//首席总监
+        'BS_LEVEL' => 1, // 蓝星
+        'JX_ZR_LEVEL' => 2,//见习主任
+        'ZR_LEVEL' => 3,//主任
+        'GJ_ZR_LEVEL' => 4,//高级主任
+        'JL_LEVEL' => 5,//经理
+        'GJ_JL_LEVEL' => 6,//高级经理
+        'ZJ_LEVEL' => 7,//总监
+        'GJ_ZJ_LEVEL' => 8,//高级总监
+        'SHX_ZJ_LEVEL' => 9,//首席总监
     ];
 
     /**
@@ -140,6 +153,34 @@ class EmployLevel extends \common\components\ActiveRecord
         return $allData;
     }
 
+    // 获取级别配置一维度数组,id为键,级别等级为值
+    public static function getIdConvertLevelSort(){
+        $ret = [];
+        $allData = static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('ID')->asArray()->all();
+        foreach($allData as $data) {
+            $ret[$data['ID']] = $data['LEVEL_SORT'];
+        }
+        return $ret;
+    }
+
+    /**
+     * 从缓存获取信息
+     * @return array|mixed|\yii\db\ActiveRecord[]
+     */
+    public static function getIdConvertLevelSortCache(){
+        $key = Cache::EMP_LEVEL_CONFIG_KEY.':idsort';
+        $data = Yii::$app->cache->get($key);
+        if(!$data){
+            // 获取信息
+            $data = self::getIdConvertLevelSort();
+            Yii::$app->cache->set($key, $data);
+        }
+
+        return $data;
+    }
+
+    
+
     /**
      * 从缓存获取信息
      * @return array|mixed|\yii\db\ActiveRecord[]
@@ -207,13 +248,23 @@ class EmployLevel extends \common\components\ActiveRecord
         return $empLevels[$id]['SORT']??0;
     }
 
+    // /**
+    //  * 是否合格高级经理及以上级别
+    //  * @param $id
+    //  * @return bool
+    //  */
+    // public static function chkSenior($id) {
+    //     if (self::getSortById($id) >= 2) return true;
+    //     return false;
+    // }
+
     /**
      * 是否合格高级经理及以上级别
      * @param $id
      * @return bool
      */
     public static function chkSenior($id) {
-        if (self::getSortById($id) >= 2) return true;
+        if (self::getSortById($id) >= 3) return true;
         return false;
     }