root 3 лет назад
Родитель
Сommit
0c2109ebd2

+ 84 - 5
common/helpers/bonus/BonusCalc.php

@@ -253,13 +253,17 @@ class BonusCalc extends BaseObject {
 //            $t14 = microtime(true);
 //            echo('计算复消管理奖'.($this->_sysConfig['fxOpenGL']['VALUE']?'完成':'关闭').',耗时:' . round($t14 - $t13, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
 //            $this->_updatePercent(70);
+          
 
-            if($this->_sysConfig['openYC']['VALUE']) {
-                $this->calcBonusYCStepOne();
-                $this->calcBonusYCStepTwo();
-            }
+            // if($this->_sysConfig['openYC']['VALUE']) {
+            //     $this->calcBonusYCStepOne();
+            //     $this->calcBonusYCStepTwo();
+            // }
+
+            // 计算店服务奖
+            $this->calcStoreBonus();
             $t16 = microtime(true);
-            echo('计算荣衔奖'.($this->_sysConfig['openYC']['VALUE']?'完成':'关闭').',耗时:' . round($t16 - $t13, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
+            echo('计算店服务奖金'.($this->_sysConfig['openYC']['VALUE']?'完成':'关闭').',耗时:' . round($t16 - $t13, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
             $this->_updatePercent(55);
 
             if($this->_sysConfig['openVIP']['VALUE']) {
@@ -922,6 +926,81 @@ class BonusCalc extends BaseObject {
         return true;
     }
 
+    public function calcStoreBonus(int $offset = 0) {
+        echo sprintf("时间:[%s]店服务奖金计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
+        $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
+        if ($allData) {
+            $periodNum = $this->_periodNum;
+            $insertBonusData = [];
+            foreach ($allData as $userId) {
+                // 从缓存中获取会员的业绩信息
+                $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
+                if( !$perfData ) continue;
+                // 获取小组+自己的业绩
+                $perfPv = $perfData['STORE_PV_GRAND'] ?? 0;
+                if( $perfPv <= 0 ) continue;
+
+                // 业绩乘以比例为奖金
+                $storeBonus = Tool::formatPrice($perfPv * $this->_sysConfig['storePercent']['VALUE'] / 100);
+                if ($storeBonus <= 0) continue;
+                // 获取会员的报单级别
+                $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
+                $storeBonus = $this->bonusTotalLimit($storeBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
+                if( $storeBonus <= 0 ) continue;
+
+                // 不扣除管理费和复消积分  //扣除相应的复消积分和管理费
+                // $deductData = $this->deduct($bonusUserId, $recBonus);
+
+                CalcCache::bonus($userId, $periodNum, 'BONUS_STORE', $storeBonus);
+
+                //来源会员信息
+                $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
+                //推广奖流水
+                $insertBonusData[] = [
+                    'ID' => SnowFake::instance()->generateId(),
+                    'USER_ID' => $userId,
+                    'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
+                    'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
+                    'LAST_STATUS' => $userBaseInfo['STATUS'],
+                    'FROM_USER_ID' => $userId,
+                    'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'],
+                    'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'],
+                    'LAST_FROM_STATUS' => $fromUserInfo['STATUS'],
+                    'AMOUNT' => $deductData['surplus'],
+                    'ORI_BONUS' => $recBonus,
+                    'RECONSUME_POINTS' => $deductData['reConsumePoints'],
+                    'MANAGE_TAX' => $deductData['manageTax'],
+                    '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,
+                        'recPercentConfig' => $this->_sysConfig['recPercent']['VALUE'],
+                        'recNum' => $userBaseInfo['REC_NUM'],
+                        'decAmount' => $userBaseInfo['ZC_AMOUNT'],
+                        'decLevel' => $userBaseInfo['DEC_LV'],
+                        'bonusTotalLimit' => [
+                            $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
+                            $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
+                            $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
+                        ],
+                    ]),
+                ];
+
+                unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
+            }
+            CalcBonusTG::batchInsert($insertBonusData);
+            unset($allData, $insertBonusData);
+            return $this->calcBonusTG($offset + $this->_limit);
+        }
+    
+        unset($allData);
+    
+        return true;
+    }
+
     /**
      * 报单业绩奖
      * @param int $offset

+ 15 - 3
common/helpers/bonus/CalcCache.php

@@ -754,16 +754,27 @@ class CalcCache {
         $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
         $data = Yii::$app->redis->hget($key, $userId);
         if (!$data) {
-            $userInfo = UserInfo::findUseDbCalc()->select('USER_ID,USER_NAME,ZC_AMOUNT,ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,STORE_TYPE,UPDATED_AT,SYSTEM_ID')->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
+            $userInfo = UserInfo::findUseDbCalc()
+            ->select('USER_ID,USER_NAME,ZC_AMOUNT,ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,STORE_TYPE,
+            UPDATED_AT,SYSTEM_ID')
+            ->where('USER_ID=:USER_ID', [':USER_ID' => $userId])
+            ->asArray()
+            ->one();
             if( !$userInfo ) {
                 throw new \Exception('用户信息表数据不存在,userId:'.$userId);
             }
             // 在获取商城库的会员的级别信息和是否报单中心的会员
-            $userShopInfo = User::find()->select('REAL_NAME,LAST_DEC_LV,DEC_LV,EMP_LV,IS_DEC,DEC_ID,DEC_ROLE_ID,STATUS,PERIOD_AT,MOBILE,CREATED_AT,SUB_COM_ID,PROVINCE,CITY,COUNTY,DEC_PROVINCE,DEC_CITY,DEC_COUNTY,IS_DIRECT_SELLER,SUB_COM_LEADER')->where('ID=:ID', [':ID' => $userId])->asArray()->one();
+            $userShopInfo = User::find()
+            ->select('IS_STUDIO,REAL_NAME,LAST_DEC_LV,DEC_LV,EMP_LV,IS_DEC,DEC_ID,DEC_ROLE_ID,STATUS,PERIOD_AT,MOBILE,CREATED_AT,SUB_COM_ID,
+            PROVINCE,CITY,COUNTY,DEC_PROVINCE,DEC_CITY,DEC_COUNTY,IS_DIRECT_SELLER,SUB_COM_LEADER')
+            ->where('ID=:ID', [':ID' => $userId])
+            ->asArray()
+            ->one();
             if ( !$userShopInfo ) {
                 throw new \Exception('用户表数据不存在,userId:'.$userId);
             }
             $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
+            $userInfo['IS_STUDIO'] = $userShopInfo['IS_STUDIO'];
 //            $userInfo['DEC_LV'] = $userShopInfo['DEC_LV'];
             //先用last_dec_lv
             $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
@@ -1066,6 +1077,7 @@ class CalcCache {
             $value = Json::decode($cacheValue);
         } else {
             $value = [
+                'STORE_PV_GRAND' => 0, // 用户的店铺业绩
                 'FX_AMOUNT_CASH' => 0,
                 'PV_PCS' => 0,
                 'PV_PSS' => 0,//本期的团队业绩
@@ -1206,7 +1218,7 @@ class CalcCache {
                 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
                 'CF_PERCENT' => 0,
                 'LX_PERCENT' => 0,
-                'FX_STATUS' => PerfMonth::NEXT_MONTH_FX_FALSE,
+                'FX_STATUS' => PerfMonth::NEXT_MONTH_FX_FALSE
             ];
         }
         if ($perf !== null) {

+ 104 - 5
common/helpers/bonus/PerfCalc.php

@@ -32,6 +32,7 @@ use common\models\Period;
 use common\models\DecOrder;
 use common\models\EmployLevel;
 use common\models\PerfActiveUser;
+use common\models\StorePerfLog;
 use common\models\UserRelation;
 use yii\base\Exception;
 use yii\base\StaticInstanceTrait;
@@ -174,12 +175,18 @@ class PerfCalc {
             echo('本月业绩入库完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
             $this->_updatePercent(90);
 
-            // //达标业绩
-            $this->loopCalcPerfByStandardFXOrder();
-            // //达标业绩入库
-            $this->loopWriteStandardPerf();
+            // 店服务奖/月节点
+            $this->storePerf();
+            $t8 = microtime(true);
+            echo('店服务业绩计算,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
+            $this->_updatePercent(95);
+            
+            // // //达标业绩
+            // $this->loopCalcPerfByStandardFXOrder();
+            // // //达标业绩入库
+            // $this->loopWriteStandardPerf();
             $t9 = microtime(true);
-            echo('本月业绩入库完成,耗时:' . round($t9 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
+            echo('本月业绩入库完成,耗时:' . round($t9 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
             $this->_updatePercent(95);
 
             
@@ -294,6 +301,8 @@ class PerfCalc {
         if ($this->_isCalcMonth) {
             // 月业绩表
             PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
+            // 店服务业绩log表
+            StorePerfLog::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
             //达标业绩表
             PerfStandard::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
         }
@@ -1165,6 +1174,96 @@ class PerfCalc {
         return true;
     }
 
+    /**
+     * 店服务奖,循环结算月所有数据,并计算小组业绩
+     * @param int $offset
+     * @return bool
+     * @throws \yii\db\Exception
+     */
+    public function storePerf($offset = 0) {
+        if(!$this->_isCalcMonth){
+            return true;
+        }
+        echo sprintf("时间:[%s]店服务奖业绩计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
+        // 从缓存列表里面从底层往上倒序获取会员      IS_HAS_USER 0 代表是此结算周期新增的业绩 
+        $allData = PerfOrder::findUseDbCalc()
+        ->select('ID,SN,DEC_SN,DEC_TYPE,USER_ID,PV,PERIOD_NUM,CALC_MONTH')
+        ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
+        ->orderBy('USER_ID DESC')
+        ->offset($offset)
+        ->limit($this->_limit)
+        ->asArray()
+        ->all();
+        if ($allData) {
+            foreach ($allData as $everyData) {
+                // 记录pv分配,为了好找小组业绩,需要将即便是没有订单的用户也保存进去.
+                // 这样,需要判断是否有用户,有的话,需要更新此用户的PV,把0改成正常的PV,不在重新插入了.
+                $userId = $everyData['USER_ID'];
+                $pv = $everyData['PV'];
+                $perfOrderId = $everyData['ID'];
+                $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
+                // 判断此用户是否是店铺,如果是店铺则累计此业绩到自己身上
+                if ($userBaseInfo['IS_STUDIO'] == 1) {
+                    CalcCache::addHasPerfUsers($userId, $this->_periodNum);
+                    CalcCache::nowPeriodPerf($userId, $this->_periodNum, [
+                        'STORE_PV_GRAND' => $pv,
+                    ]);
+                    
+                    StorePerfLog::addOrUpdate(
+                        $this->_periodNum, 
+                        $this->_calcYearMonth, 
+                        $userId, 
+                        [
+                            'PERF_ORDER_ID' => $perfOrderId,
+                            'USER_ID' => $userId,
+                            'IS_STUDIO' => 1,
+                            'PV' => $pv,
+                            'PERIOD_NUM' => $this->_periodNum,
+                            'CALC_MONTH' => $this->_calcYearMonth,
+                            'CREATE_AT' => time(),
+                        ]
+                    );
+                } else {
+                // 如果此用户不是店铺,则往上找用户上级,是否是店铺,并累计上去
+                    // 给上追加业绩
+                    $this->loopNetworkParentDo($userId, function ($parent) use (&$everyData) {
+                        $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
+                        $isStudio = $parentUser['IS_STUDIO'] == 1 ? true : false;
+                        StorePerfLog::addOrUpdate(
+                            $this->_periodNum, 
+                            $this->_calcYearMonth, 
+                            $parent['PARENT_UID'], 
+                            [
+                                'PERF_ORDER_ID' => $everyData['ID'],
+                                'USER_ID' => $parent['PARENT_UID'],
+                                'IS_STUDIO' => $parentUser['IS_STUDIO'],
+                                'PV' => $everyData['PV'],
+                                'PERIOD_NUM' => $this->_periodNum,
+                                'CALC_MONTH' => $this->_calcYearMonth,
+                                'CREATE_AT' => time(),
+                            ]
+                        );
+                        if ($isStudio) {
+                            CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
+                                'STORE_PV_GRAND' => $everyData['PV']
+                            ]);
+                            CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
+                            
+                            return self::LOOP_FINISH;
+                        }
+                        
+                        unset($parent);
+                    });
+                }
+            }
+            unset($allData);
+            return $this->storePerf($offset + $this->_limit);
+        }
+
+        unset($allData);
+        return true;
+    }
+
 
     /**
      * 循环达标业绩会员,并入库

+ 78 - 0
common/models/StorePerfLog.php

@@ -0,0 +1,78 @@
+<?php
+
+namespace common\models;
+
+/**
+ * This is the model class for table "{{%STORE_PERF_LOG}}".
+ *
+ * @property string $ID
+ * @property string $PERF_ORDER_ID PERF_ORDER表的ID字段值
+ * @property string $USER_ID USER表用户ID
+ * @property int $IS_STUDIO 是否是工作室 0 不是 1是
+ * @property string $PV 业绩单PV
+ * @property string $PERIOD_NUM 业绩期
+ * @property int $CALC_MONTH 结算月份
+ * @property int $CREATE_AT 创建时间
+ * @property string $GRAND_TOTAL_PV 工作室用户累计获得的小组PV
+ */
+class StorePerfLog extends \common\components\ActiveRecord
+{
+    /**
+     * @inheritdoc
+     */
+    public static function tableName()
+    {
+        return '{{%STORE_PERF_LOG}}';
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public function rules()
+    {
+        return [
+            [['USER_ID', 'IS_STUDIO', 'CREATED_AT'], 'required'],
+            [['ID'], 'unique'],
+        ];
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public function attributeLabels()
+    {
+        return [
+            'ID' => 'ID',
+            'PERF_ORDER_ID' => 'PERF_ORDER表的ID字段值',
+            'USER_ID' => 'USER表用户ID',
+            'IS_STUDIO' => '是否是工作室 0 不是 1是',
+            'PV' => '业绩单PV',
+            'PERIOD_NUM' => '业绩期',
+            'CALC_MONTH' => '结算月份',
+            'CREATE_AT' => '创建时间',
+            'GRAND_TOTAL_PV' => '工作室用户累计获得的小组PV'
+        ];
+    }
+
+    // 更新或者添加业绩数据
+    public static function addOrUpdate($periodNum, $calcMonth, $userId, $data) {
+        // 判断是否有此数据,如果有则进行更新
+        $info = StorePerfLog::findUseDbCalc()
+        ->select('ID')
+        ->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM AND CALC_MONTH=:CALC_MONTH',
+         [':USER_ID'=>$userId,':PERIOD_NUM' => $periodNum, ':CALC_MONTH' => $calcMonth]
+        )
+        ->asArray()
+        ->one();
+        if ($data['IS_STUDIO'] == 1) {
+            $data['GRAND_TOTAL_PV'] = $data['PV'] + $info['GRAND_TOTAL_PV'];
+        }
+        if (empty($info)) {
+            $ret = StorePerfLog::insertOne($data);
+        } else {
+            $ret = StorePerfLog::updateAll($data, 'ID=:ID', [':ID' => $info['ID']]);
+        }
+
+        return $ret;
+    }
+}