root 3 лет назад
Родитель
Сommit
f82ec0b1d3
3 измененных файлов с 176 добавлено и 314 удалено
  1. 10 25
      common/helpers/bonus/CalcCache.php
  2. 136 289
      common/helpers/bonus/CalcServeBonusCalc.php
  3. 30 0
      common/models/Period.php

+ 10 - 25
common/helpers/bonus/CalcCache.php

@@ -580,30 +580,7 @@ class CalcCache {
             if ( !$userShopInfo ) {
                 throw new \Exception('用户表数据不存在,userId:'.$userId);
             }
-            // 先判断此期是否是月节点
-            $nowPeriod = Period::find()
-                ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
-                ->asArray()
-                ->one();
-            if ($nowPeriod['IS_MONTH'] == 1) {
-                $bsMonthPeriod = $nowPeriod['PERIOD_NUM'];
-            } else {
-                $forwardMonthPeriod = Period::find()
-                ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
-                ->orderBy('PERIOD_NUM DESC')
-                ->asArray()
-                ->one();
-                $bsMonthPeriod = $forwardMonthPeriod['PERIOD_NUM'];
-            }
-            $blueBsInfo = CalcBonusBS::findOneAsArray(
-                'PERIOD_NUM=:PERIOD_NUM  AND USER_ID=:USER_ID', 
-                [':PERIOD_NUM'=>$bsMonthPeriod, ':USER_ID'=>$userId]
-            );
-            if (!empty($blueBsInfo)) {
-                $bsEmpLv = $blueBsInfo['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
-            } else {
-                $bsEmpLv = EmployLevel::NO_LEVEL_ID;
-            }
+            $bsEmpLv = Period::userLastEmpLv($userId, $periodNum);
             // 获取用户的最新级别数据,根据蓝星最近一期月结数据进行查询
             $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
             $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
@@ -626,8 +603,16 @@ class CalcCache {
             $data = Json::encode($userInfo);
             Yii::$app->redis->hset($key, $userId, $data);
             unset($userInfo, $key, $userId, $periodNum);
+        } else {
+            if ($data) {
+                $ret = Json::decode($data, true);
+                $bsEmpLv = Period::userLastEmpLv($userId, $periodNum);
+                $ret['LAST_EMP_LV'] = $bsEmpLv; // 最新的聘级
+                return $ret;
+            } else {
+                return [];
+            }
         }
-        return $data ? Json::decode($data, true) : [];
     }
 
     public static function setUserInfo($userId, $periodNum, $userInfo) {

+ 136 - 289
common/helpers/bonus/CalcServeBonusCalc.php

@@ -127,20 +127,21 @@ class CalcServeBonusCalc extends BaseObject {
             echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
             $this->_updatePercent(10);
             $t3 = microtime(true);
-            // @todo 蓝星奖放到最前面
+            // 蓝星奖放到最前面 奖金计算开始
             if($this->_sysConfig['openGL']['VALUE']) {
                 echo('计算蓝星奖开始,' . date('Y-m-d H:i:s', $t3) . PHP_EOL);
                 // 调用存储过程,计算蓝星管理奖金
                 $this->calcBsProcedure();
-                // 将有【蓝星业绩奖金】的用户加入到有奖金缓存用户中
-                $this->calcBonusBsYJ();
-                // 将有【蓝星管理奖金】的用户加入到有奖金缓存用户中
+                // 将有蓝星管理奖金的用户加入到有奖金缓存用户中
                 $this->calcBonusBsGL();
+                // 将有【蓝星业绩奖金】的用户加入到有奖金缓存用户中
+                // $this->calcBonusBsYJ();
+                // // 将有【蓝星管理奖金】的用户加入到有奖金缓存用户中
+                // $this->calcBonusBsGL();
             }
             $t4 = microtime(true);
             echo('计算蓝星奖'.($this->_sysConfig['openGL']['VALUE']?'完成':'关闭').',耗时:' . round($t4 - $t3, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
 
-            // 奖金部分
             if($this->_sysConfig['openFW']['VALUE']) {
                 $this->calcBonusBDStepOne();
                 $this->calcBonusBDStepTwo();
@@ -187,8 +188,6 @@ class CalcServeBonusCalc extends BaseObject {
             $t24 = microtime(true);
             echo('计算季度奖' . ($this->_sysConfig['openQuarter']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t24 - $t23, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL);
 
-            //把奖金会员写入缓存
-            $this->loopMonthBonusUserFromDbToCache();
             $t30 = microtime(true);
             echo('奖金会员写入缓存完成,耗时:' . round($t30 - $t22, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
             $this->_updatePercent(70);
@@ -200,12 +199,10 @@ class CalcServeBonusCalc extends BaseObject {
             $t31 = microtime(true);
             echo('奖金写库操作完成,耗时:' . round($t31 - $t30, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
 
-            //把本期奖金会员入库 - 把缓存中的月奖用户信息存到数据库.存储过程的入库不在这里进行,这里代码取的缓存,存储过程没有缓存,在上面进行入库
-            $this->loopMonthBonusUserToDb();
             $t32 = microtime(true);
             echo('奖金会员入库完成,耗时:' . round($t32 - $t31, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
-            $this->_updatePercent(80);
-
+            $this->_updatePercent(100);
+            Period::updateCalcProcess(3, $this->_periodNum);
             $t35 = microtime(true);
             echo('结算全部完成,共耗时:' . round($t35 - $t32, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL);
         } catch (\Exception $e) {
@@ -239,15 +236,17 @@ class CalcServeBonusCalc extends BaseObject {
      * @throws \yii\db\Exception
      */
     public function initCalcTask() {
+        $periodObj = Period::instance();
+        $periodDataArr = $periodObj->setPeriodNum($this->_periodNum);
+        if (empty($this->_periodNum)) {
+            $this->_periodNum = $periodDataArr['PERIOD_NUM'];
+        }
         $this->_sysConfig = Cache::getSystemConfig();
         $this->_decLevelConfig = Cache::getDecLevelConfig();
         $this->_empLevelConfig = Cache::getEmpLevelConfig();
         $this->_starCrownLevelConfig = Cache::getStarCrownLevelConfig();
         $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
         $periodNum = $this->_periodNum;
-        // 获取本年月和上年月
-        $periodObj = Period::instance();
-        $periodDataArr = $periodObj->setPeriodNum($periodNum);
         $this->_periodId = $periodDataArr['ID'];
         $this->_isCalcMonth = $periodObj->isCalcMonth($periodNum);
         $this->_calcYear = $periodObj->getYear($periodNum);
@@ -343,7 +342,6 @@ class CalcServeBonusCalc extends BaseObject {
                     'PERIOD_NUM' => $this->_periodNum,
                     'CALC_YEAR' => $this->_calcYear,
                     'CALC_MONTH' => $this->_calcYearMonth,
-                    'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                     'CREATED_AT' => Date::nowTime(),
                     'LOGS' => json_encode([
                         'perfPv' => $perfPv,
@@ -388,7 +386,6 @@ class CalcServeBonusCalc extends BaseObject {
                 $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
                 if( !$perfData ) continue;
 
-                //
                 $decRoleBonusFrom = explode(',', $this->_sysConfig['decRoleBonusFrom']['VALUE']);
                 $validPvPcs = 0;
                 foreach ($decRoleBonusFrom as $orderType) {
@@ -483,7 +480,6 @@ class CalcServeBonusCalc extends BaseObject {
                     'PERIOD_NUM' => $this->_periodNum,
                     'CALC_YEAR' => $this->_calcYear,
                     'CALC_MONTH' => $this->_calcYearMonth,
-                    'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                     'CREATED_AT' => Date::nowTime(),
                     'LOGS' => json_encode([
                         'decRoleId' => $decRoleId,
@@ -491,7 +487,6 @@ class CalcServeBonusCalc extends BaseObject {
                 ];
 
                 unset($userId, $fwBonusData, $userBaseInfo, $decRoleId, $fwBonus);
-
             }
 
             CalcBonusBD::batchInsert($insertBonusData);
@@ -590,7 +585,6 @@ class CalcServeBonusCalc extends BaseObject {
                     'PERIOD_NUM' => $this->_periodNum,
                     'CALC_YEAR' => $this->_calcYear,
                     'CALC_MONTH' => $this->_calcYearMonth,
-                    'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                     'CREATED_AT' => Date::nowTime(),
                     'LOGS' => json_encode([
                         'perfArr' => $perfArr,
@@ -813,7 +807,6 @@ class CalcServeBonusCalc extends BaseObject {
                 'PERIOD_NUM' => $this->_periodNum,
                 'CALC_YEAR' => $this->_calcYear,
                 'CALC_MONTH' => $this->_calcYearMonth,
-                'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                 'CREATED_AT' => Date::nowTime(),
                 'PERF' => $monthTotalPV,
                 'TRANSFER_RATE' => $mate,
@@ -937,7 +930,6 @@ class CalcServeBonusCalc extends BaseObject {
                 'PERIOD_NUM' => $this->_periodNum,
                 'CALC_YEAR' => $this->_calcYear,
                 'CALC_MONTH' => $this->_calcYearMonth,
-                'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                 'CREATED_AT' => Date::nowTime(),
                 'PERF' => $monthTotalPV,
                 'TRANSFER_RATE' => $mate,
@@ -1028,7 +1020,7 @@ class CalcServeBonusCalc extends BaseObject {
     }
 
     /**
-     * 蓝星管理奖金
+     * 蓝星管理奖金未拆分
      * @param int $offset
      * @return bool
      * @throws \yii\db\Exception
@@ -1047,32 +1039,24 @@ class CalcServeBonusCalc extends BaseObject {
             ->asArray()
             ->all();
         if ($allData) {
-            // 达标条件:会员级别:钻卡
-            $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
-            $minDecLevel = $config['mntDec'] ?? [];
-
             foreach ($allData as $user) {
-                //扣除相应的复消积分和管理费
-                $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_MNT']);
-                $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
-                $manageTax = $deductData['manageTax']; // 管理费
-                $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
-
-                // 管理奖钻卡发放
-//                if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
-                    // 把对碰后的奖金存入缓存中
-                    CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_MNT', $user['ORI_BONUS_MNT'], $deductData);
-                    // 加入月奖的会员
-                    CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
-//                }
-
-                // 更新蓝星奖金存储过程的实发金额数据
-                CalcBonusBS::updateAll([
-                    'RECONSUME_POINTS' => $point,
-                    'AMOUNT_MNT' => $realBonusBs,
-                    'MANAGE_TAX_MNT' => $manageTax],
-                'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', 
-                [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
+                // 添加到有奖励信息的数据中
+                CalcCache::addHasBonusUsers($user['USER_ID'], $this->_periodNum);
+                $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
+                if ($fxStatus) {
+                    // //扣除相应的复消积分和管理费
+                    // $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS']);
+                    // // 把对碰后的奖金存入缓存中
+                    // CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS'], $deductData);
+                    CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
+                    // $realBonusBs = $user['ORI_BONUS']; // 扣除管理费和复消积分后的实发蓝星奖金
+                    // $manageTax = $deductData['manageTax']; // 管理费
+                    // $point = $deductData['reConsumePoints'];// 复消积分
+                    // // 更新蓝星奖金存储过程的实发金额数据
+                    // CalcBonusBS::updateAll(['AMOUNT' => $realBonusBs, 'MANAGE_TAX' => $manageTax, 'RECONSUME_POINTS' => $point],
+                    // 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
+                    // [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
+                }  
             }
             return $this->calcBonusBsGL($offset + $this->_limit);
         }
@@ -1080,58 +1064,111 @@ class CalcServeBonusCalc extends BaseObject {
         return true;
     }
 
-    /**
-     * 蓝星业绩奖金
-     * @param int $offset
-     * @return bool
-     * @throws \yii\db\Exception
-     */
-    public function calcBonusBsYJ(int $offset = 0) {
-        if( !$this->_isCalcMonth ) {
-            // 不是结算月,则不进行计算
-            return false;
-        }
-        // 从缓存获取分页有收入的会员信息
-        $allData = CalcBonusBS::findUseDbCalc()
-            ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
-            ->groupBy('USER_ID')
-            ->offset($offset)
-            ->limit($this->_limit)
-            ->asArray()
-            ->all();
-        if ($allData) {
-            // 达标条件:会员级别:金卡、钻卡
-            $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
-            $minDecLevel = $config['abbrDec'] ?? [];
-
-            foreach ($allData as $user) {
-                //扣除相应的复消积分和管理费
-                $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']);
-                $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
-                $manageTax = $deductData['manageTax']; // 管理费
-                $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
-
-                // 业绩奖金卡、钻卡发放
-//                if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
-                    // 把对碰后的奖金存入缓存中
-                    CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_ABBR', $user['ORI_BONUS_ABBR'], $deductData);
-                    // 加入月奖的会员
-                    CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
-//                }
-
-                // 更新蓝星业绩奖金存储过程的实发金额数据
-                CalcBonusBS::updateAll([
-                    'AMOUNT_ABBR' => $realBonusBs,
-                    'MANAGE_TAX_ABBR' => $manageTax,
-                    'RECONSUME_POINTS' => $point],
-            'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
-                    [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
-            }
-            return $this->calcBonusBsYJ($offset + $this->_limit);
-        }
-        unset($allData);
-        return true;
-    }
+//     /**
+//      * 蓝星管理奖金
+//      * @param int $offset
+//      * @return bool
+//      * @throws \yii\db\Exception
+//      */
+//     public function calcBonusBsGL(int $offset = 0) {
+//         if( !$this->_isCalcMonth ) {
+//             // 不是结算月,则不进行计算
+//             return false;
+//         }
+//         // 从缓存获取分页有收入的会员信息
+//         $allData = CalcBonusBS::findUseDbCalc()
+//             ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
+//             ->groupBy('USER_ID')
+//             ->offset($offset)
+//             ->limit($this->_limit)
+//             ->asArray()
+//             ->all();
+//         if ($allData) {
+//             // 达标条件:会员级别:钻卡
+//             $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
+//             $minDecLevel = $config['mntDec'] ?? [];
+
+//             foreach ($allData as $user) {
+//                 //扣除相应的复消积分和管理费
+//                 $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_MNT']);
+//                 $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
+//                 $manageTax = $deductData['manageTax']; // 管理费
+//                 $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
+
+//                 // 管理奖钻卡发放
+// //                if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
+//                     // 把对碰后的奖金存入缓存中
+//                     CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_MNT', $user['ORI_BONUS_MNT'], $deductData);
+//                     // 加入月奖的会员
+//                     CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
+// //                }
+
+//                 // 更新蓝星奖金存储过程的实发金额数据
+//                 CalcBonusBS::updateAll([
+//                     'RECONSUME_POINTS' => $point,
+//                     'AMOUNT_MNT' => $realBonusBs,
+//                     'MANAGE_TAX_MNT' => $manageTax],
+//                 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', 
+//                 [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
+//             }
+//             return $this->calcBonusBsGL($offset + $this->_limit);
+//         }
+//         unset($allData);
+//         return true;
+//     }
+
+//     /**
+//      * 蓝星业绩奖金
+//      * @param int $offset
+//      * @return bool
+//      * @throws \yii\db\Exception
+//      */
+//     public function calcBonusBsYJ(int $offset = 0) {
+//         if( !$this->_isCalcMonth ) {
+//             // 不是结算月,则不进行计算
+//             return false;
+//         }
+//         // 从缓存获取分页有收入的会员信息
+//         $allData = CalcBonusBS::findUseDbCalc()
+//             ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
+//             ->groupBy('USER_ID')
+//             ->offset($offset)
+//             ->limit($this->_limit)
+//             ->asArray()
+//             ->all();
+//         if ($allData) {
+//             // 达标条件:会员级别:金卡、钻卡
+//             $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
+//             $minDecLevel = $config['abbrDec'] ?? [];
+
+//             foreach ($allData as $user) {
+//                 //扣除相应的复消积分和管理费
+//                 $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']);
+//                 $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
+//                 $manageTax = $deductData['manageTax']; // 管理费
+//                 $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
+
+//                 // 业绩奖金卡、钻卡发放
+// //                if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
+//                     // 把对碰后的奖金存入缓存中
+//                     CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_ABBR', $user['ORI_BONUS_ABBR'], $deductData);
+//                     // 加入月奖的会员
+//                     CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
+// //                }
+
+//                 // 更新蓝星业绩奖金存储过程的实发金额数据
+//                 CalcBonusBS::updateAll([
+//                     'AMOUNT_ABBR' => $realBonusBs,
+//                     'MANAGE_TAX_ABBR' => $manageTax,
+//                     'RECONSUME_POINTS' => $point],
+//             'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
+//                     [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
+//             }
+//             return $this->calcBonusBsYJ($offset + $this->_limit);
+//         }
+//         unset($allData);
+//         return true;
+//     }
 
     /**
      * 对碰
@@ -1310,66 +1347,6 @@ class CalcServeBonusCalc extends BaseObject {
         \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
     }
 
-    /**
-     * @todo 将这个表去掉
-     * 把往期有月奖金的会员加到本期有奖会员缓存列表中
-     * @param int $offset
-     * @return bool
-     */
-    public function loopMonthBonusUserFromDbToCache($offset = 0) {
-        if (!$this->_isCalcMonth) {
-            return true;
-        }
-
-        $allData = CalcMonthBonusUser::findUseDbCalc()->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->asArray()->all();
-        if ( $allData ) {
-            foreach ($allData as $everyData) {
-                CalcCache::addHasBonusUsers($everyData['USER_ID'], $this->_periodNum);
-
-                unset($everyData);
-            }
-
-            unset($allData);
-            $this->loopMonthBonusUserFromDbToCache($offset + $this->_limit);
-        }
-
-        unset($allData);
-        return true;
-    }
-
-    /**
-     * 把缓存中的月奖用户信息存到数据库
-     * @param int $offset
-     * @return bool
-     * @throws \yii\db\Exception
-     */
-    public function loopMonthBonusUserToDb($offset = 0) {
-        echo sprintf("时间:[%s]月奖会员入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
-        $allData = CalcCache::getHasMonthBonusUsers($this->_periodNum, $offset, $this->_limit);
-        if($allData) {
-            $insertDataBonusUser = [];
-            foreach($allData as $userId){
-                $insertDataBonusUser[] = [
-                    'ID' => SnowFake::instance()->generateId(),
-                    'USER_ID' => $userId,
-                    'CALC_MONTH' => $this->_calcYearMonth,
-                    'PERIOD_NUM' => $this->_periodNum,
-                    'CREATED_AT' => Date::nowTime()
-                ];
-
-                unset($userId);
-            }
-
-            if( $insertDataBonusUser ) CalcMonthBonusUser::batchInsert($insertDataBonusUser);
-
-            unset($insertDataBonus, $allData);
-            return $this->loopMonthBonusUserToDb($offset + $this->_limit);
-        }
-
-        unset($allData);
-        return true;
-    }
-
     /**
      * 循环奖服务奖金会员,并入库
      * @param int $offset
@@ -1398,78 +1375,6 @@ class CalcServeBonusCalc extends BaseObject {
         return true;
     }
 
-    // 奖金入库完成,将各个奖金计算流水会员聘级,更新成蓝星奖当时计算的聘级
-    public function loopCalcBlueEmpLv($offset = 0) {
-        if( !$this->_isCalcMonth ) {
-            // 不是结算月,则不需要进行聘级调整
-            return false;
-        }
-        // 从缓存获取分页有收入的会员信息
-        $allData = CalcBonusBS::findUseDbCalc()
-            ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
-            ->groupBy('USER_ID')
-            ->offset($offset)
-            ->limit($this->_limit)
-            ->asArray()
-            ->all();
-        if ($allData) {
-            foreach ($allData as $data) {
-                $nowBsEmpLv = $data['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
-                // 期结算结果
-                CalcBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', 
-                    [
-                        ':USER_ID' => $data['USER_ID'],
-                        ':PERIOD_NUM' => $this->_periodNum
-                    ]
-                );
-                // 奖金流水
-                FlowBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', 
-                    [
-                        ':USER_ID' => $data['USER_ID'],
-                        ':PERIOD_NUM' => $this->_periodNum
-                    ]
-                );
-                // 推广奖流水
-                CalcBonusTG::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
-                    [
-                        ':USER_ID' => $data['USER_ID'],
-                        ':PERIOD_NUM' => $this->_periodNum
-                    ]
-                );
-                // 团队奖流水
-                CalcBonusQY::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', 
-                    [
-                        ':USER_ID' => $data['USER_ID'],
-                        ':PERIOD_NUM' => $this->_periodNum
-                    ]
-                );
-//                // 服务奖流水
-//                CalcBonusBD::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
-//                    [
-//                        ':USER_ID' => $data['USER_ID'],
-//                        ':PERIOD_NUM' => $this->_periodNum
-//                    ]
-//                );
-                CalcBonusTG::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
-                    [
-                        ':FROM_USER_ID' => $data['USER_ID'],
-                        ':PERIOD_NUM' => $this->_periodNum
-                    ]
-                );
-//                CalcBonusBD::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
-//                    [
-//                        ':FROM_USER_ID' => $data['USER_ID'],
-//                        ':PERIOD_NUM' => $this->_periodNum
-//                    ]
-//                );
-            }
-            unset($allData);
-            return $this->loopCalcBlueEmpLv($offset + $this->_limit);
-        }
-        unset($allData);
-        return true;
-    }
-
     /**
      * 奖金
      * @param $userId
@@ -1540,20 +1445,8 @@ class CalcServeBonusCalc extends BaseObject {
 
                 $blueStartManageTax += $blueStartManageTaxMnt + $blueStartManageTaxAbbr; // 管理费
 
-                $monthSumData = CalcBonus::findUseSlaves()
-                ->select('SUM(BONUS_GX) AS BONUS_GX_SUM, SUM(BONUS_GL) AS BONUS_GL_SUM')
-                ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
-                    [
-                        'USER_ID' => $userId,
-                        'CALC_MONTH' => $this->_calcYearMonth
-                    ]
-                )
-                ->asArray()
-                ->one();
                 $realBonusBs = $blueStartAmount; // 蓝星奖直接取数据库中算好的值PRODUCT_POINT
                 $exchangePoints = isset($userBS['PRODUCT_POINT']) && !empty($userBS['PRODUCT_POINT']) ? $userBS['PRODUCT_POINT'] : 0; // 兑换积分
-
-                unset($monthSumData, $bonusGxSum, $bonusGlSum);
             }
         }
 
@@ -1570,15 +1463,6 @@ class CalcServeBonusCalc extends BaseObject {
             'LAST_EMP_LV' => $baseInfo['LAST_EMP_LV'],
             'LAST_CROWN_LV' => $starCrownLv ?? StarCrownLevel::getDefaultLevelId(),
             'LAST_STATUS' => $baseInfo['STATUS'],
-            'LAST_MOBILE' => $baseInfo['MOBILE'],
-            'LAST_PERIOD_AT' => $baseInfo['PERIOD_NUM'],
-            'LAST_CREATED_AT' => $baseInfo['CREATED_AT'],
-            'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
-            'LAST_PROVINCE' => $baseInfo['PROVINCE'],
-            'LAST_CITY' => $baseInfo['CITY'],
-            'LAST_COUNTY' => $baseInfo['COUNTY'],
-            'LAST_SYSTEM_ID' => $baseInfo['SYSTEM_ID'] ? $baseInfo['SYSTEM_ID'] : '',
-            'LAST_IS_DIRECT_SELLER' => $baseInfo['IS_DIRECT_SELLER'],
             'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
             'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
             'LAST_CON_USER_NAME' => $baseInfo['CON_USER_NAME'],
@@ -1589,13 +1473,7 @@ class CalcServeBonusCalc extends BaseObject {
             'LAST_LOCATION' => 1,
             'BONUS_BD' => $bonus['BONUS_BD'],
             'BONUS_TG' => $bonus['BONUS_TG'],
-            'BONUS_XF' => $bonus['BONUS_XF'],
-            'BONUS_YJ' => $bonus['BONUS_YJ'],
-            'BONUS_GX' => $bonus['BONUS_GX'],
-            'BONUS_GL' => $bonus['BONUS_GL'],
             'BONUS_QY' => $bonus['BONUS_QY'],
-            'BONUS_YC' => $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'],
-            'BONUS_VIP' => $bonus['BONUS_VIP'],
             'RECONSUME_POINTS' => $bonus['RECONSUME_POINTS'],
             'MANAGE_TAX' => $blueStartManageTax,    // 管理费
             'BONUS_INCOME'=>$bonus['INCOME_TOTAL'],
@@ -1603,14 +1481,6 @@ class CalcServeBonusCalc extends BaseObject {
             'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
             'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
             'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
-            'ORI_BONUS_XF' => $bonus['ORI_BONUS_XF'],
-            'ORI_BONUS_YJ' => $bonus['ORI_BONUS_YJ'],
-            'ORI_BONUS_YJ_BD' => $bonus['ORI_BONUS_YJ_BD'],
-            'ORI_BONUS_YJ_FX' => $bonus['ORI_BONUS_YJ_FX'],
-            'ORI_BONUS_GX' => $bonus['ORI_BONUS_GX'],
-            'REAL_BONUS_GX' => $realBonusGx,
-            'ORI_BONUS_GL' => $bonus['ORI_BONUS_GL'],
-            'REAL_BONUS_GL' => $realBonusGl,
 
             'BONUS_BS' => $realBonusBs, // 新的管理奖金,即蓝星管理奖
             'ORI_BONUS_BS' => $blueStartOriBonus, // 蓝星管理奖金原奖金,即包含管理费
@@ -1626,13 +1496,7 @@ class CalcServeBonusCalc extends BaseObject {
             'REAL_BONUS_BS_ABBR' => $realBonusBsAbbr, // 实发蓝星业绩奖金
             'MANAGE_TAX_ABBR' => $blueStartManageTaxAbbr, // 实发蓝星业绩奖——管理费
 
-            'ORI_BONUS_GL_BD' => $bonus['ORI_BONUS_GL_BD'],
-            'ORI_BONUS_GL_FX' => $bonus['ORI_BONUS_GL_FX'],
             'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
-            'ORI_BONUS_QY_BD' => $bonus['ORI_BONUS_QY_BD'],
-            'ORI_BONUS_QY_FX' => $bonus['ORI_BONUS_QY_FX'],
-            'ORI_BONUS_YC' => $bonus['ORI_BONUS_YC'] + $bonus['ORI_BONUS_YC_EXTRA'],
-            'ORI_BONUS_VIP' => $bonus['ORI_BONUS_VIP'],
             'ORI_CAPPED_BONUS_QY' => $bonus['ORI_CAPPED_BONUS_QY'], // 团队奖封顶前的奖金
             'BONUS_QUARTER' => $bonus['BONUS_QUARTER'],
             'ORI_BONUS_QUARTER' => $bonus['ORI_BONUS_QUARTER'],
@@ -1642,19 +1506,6 @@ class CalcServeBonusCalc extends BaseObject {
             'BONUS_GARAGE' => $garageBonus, // 车奖
 
             //以下没有用
-            'BONUS_FW' => 0,
-            'BONUS_FX' => $bonus['BONUS_FX'],
-            'BONUS_LS' => $bonus['BONUS_LS'],
-            'BONUS_BT' => $bonus['BONUS_BT'],
-            'BONUS_BT_PROD' => $bonus['BONUS_BT_PROD'],
-            'BONUS_BT_TOOL' => $bonus['BONUS_BT_TOOL'],
-            'DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
-            'BONUS_FL' => $bonus['BONUS_FL'],
-            'BONUS_CF' => $bonus['BONUS_CF'],
-            'BONUS_LX' => $bonus['BONUS_LX'],
-            'SHOULD_QY' => $bonus['BONUS_QY'],
-            'SHOULD_DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
-
             'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
             'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
             'SURPLUS_1L' => $perfData['SURPLUS_1L'],
@@ -1671,14 +1522,10 @@ class CalcServeBonusCalc extends BaseObject {
             'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
             'SURPLUS_5L' => $perfData['SURPLUS_5L'],
             'PV_PCS' => $perfData['PV_PCS'],
-            'PV_LS_TOUCH' => 0.00,
-            'SURPLUS_LS' => 0.00,
-            'QY_LS' => 0.00,
             'PV_TOUCH' => Tool::formatPrice($perfData['PV_1L_TOUCH'] + $perfData['PV_2L_TOUCH'] + $perfData['PV_3L_TOUCH'] + $perfData['PV_4L_TOUCH'] + $perfData['PV_5L_TOUCH'] + $perfData['PV_LS_TOUCH']),
             'PERIOD_NUM' => $this->_periodNum,
             'CALC_YEAR' => $this->_calcYear,
             'CALC_MONTH' => $this->_calcYearMonth,
-            'CALCULATED_AT' => Date::nowTime(),
             'CREATED_AT' => Date::nowTime(),
         ];
         $resend = [];

+ 30 - 0
common/models/Period.php

@@ -390,4 +390,34 @@ class Period extends \common\components\ActiveRecord
 
         return $result;
     }
+
+    // 通过业绩期和用户id,获取蓝星的最新级别信息
+    public static function userLastEmpLv($userId, $periodNum) {
+        // 先判断此期是否是月节点
+        $nowPeriod = Period::find()
+        ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
+        ->asArray()
+        ->one();
+        if ($nowPeriod['IS_MONTH'] == 1) {
+            $bsMonthPeriod = $nowPeriod['PERIOD_NUM'];
+        } else {
+            $forwardMonthPeriod = Period::find()
+            ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
+            ->orderBy('PERIOD_NUM DESC')
+            ->asArray()
+            ->one();
+            $bsMonthPeriod = $forwardMonthPeriod['PERIOD_NUM'];
+        }
+        $blueBsInfo = CalcBonusBS::findOneAsArray(
+            'PERIOD_NUM=:PERIOD_NUM  AND USER_ID=:USER_ID', 
+            [':PERIOD_NUM'=>$bsMonthPeriod, ':USER_ID'=>$userId]
+        );
+        if (!empty($blueBsInfo)) {
+            $bsEmpLv = $blueBsInfo['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
+        } else {
+            $bsEmpLv = EmployLevel::NO_LEVEL_ID;
+        }
+
+        return $bsEmpLv;
+    }
 }