Bladeren bron

紧缩和非紧缩方式

root 3 jaren geleden
bovenliggende
commit
bc1225e8c7
2 gewijzigde bestanden met toevoegingen van 184 en 38 verwijderingen
  1. 121 38
      common/helpers/bonus/BonusCalc.php
  2. 63 0
      common/models/CalcBonusST.php

+ 121 - 38
common/helpers/bonus/BonusCalc.php

@@ -26,6 +26,7 @@ use common\models\CalcBonusHB;
 use common\models\CalcBonusLS;
 use common\models\CalcBonusLX;
 use common\models\CalcBonusQY;
+use common\models\CalcBonusST;
 use common\models\CalcBonusStandard;
 use common\models\CalcBonusTG;
 use common\models\CalcBonusVIP;
@@ -262,7 +263,13 @@ class BonusCalc extends BaseObject {
 
             // 计算店服务奖 月奖
             if($this->_sysConfig['openStore']['VALUE']) {
-                $this->calcStoreBonus();
+                if ($this->_sysConfig['openStoreReduce']['VALUE']) {
+                    // 开启了紧缩方式,计算店补
+                    $this->calcStoreBonusReduce();
+                } else {
+                    // 未开启紧缩方式,计算店补
+                    $this->calcStoreBonus();
+                }
             }
             $t16 = microtime(true);
             echo('计算店服务奖金'.($this->_sysConfig['openStore']['VALUE']?'完成':'关闭').',耗时:' . round($t16 - $t13, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
@@ -427,6 +434,7 @@ class BonusCalc extends BaseObject {
         CalcBonusGL::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
         // 月结时要清空的数据
         if ($this->_isCalcMonth) {
+            CalcBonusST::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
             CalcBonusYC::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
             CalcBonusVIP::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
             CalcBonusStandard::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
@@ -927,25 +935,24 @@ class BonusCalc extends BaseObject {
         return true;
     }
 
-    public function calcStoreBonus(int $offset = 0) {
+    // 紧缩计算店补奖,不活跃,则给一个活跃的店铺
+    public function calcStoreBonusReduce(int $offset = 0) {
         if( !$this->_isCalcMonth ) {
             // 不是结算月,则不进行计算
             echo sprintf("时间:[%s]店服务奖金非月节点,不进行计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
             return false;
         }
-        echo sprintf("时间:[%s]店服务奖金计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
+        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);
+                $perfData = CalcCache::nowPeriodPerf($userId, $this->_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;
@@ -953,53 +960,129 @@ class BonusCalc extends BaseObject {
                 $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
                 $storeBonus = $this->bonusTotalLimit($storeBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
                 if( $storeBonus <= 0 ) continue;
+                // 判断此店铺是否是活跃用户,如果不活跃,则往上找一个活跃的店铺获得此奖
+                $isActive = $this->_isMonthPerfLimit($userId);
+                $bonusUserId = '';
+                // 如果用户不活跃,且上面没有活跃的,则不累计了,是不是
+                if (!$isActive) {
+                    // 如果此店铺不活跃,则找最近的一个店铺获得此奖
+                    $this->loopNetworkParentDo($userId, function ($parent) use (&$bonusUserId) {
+                        if ($this->_isMonthPerfLimit($parent['PARENT_UID'])) {
+                            // 判断是否是店铺
+                            $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
+                            if ($parentUser['IS_STUDIO'] == 1) {
+                                $bonusUserId = $parent['PARENT_UID'];
 
-                // 不扣除管理费和复消积分  //扣除相应的复消积分和管理费
-                // $deductData = $this->deduct($bonusUserId, $recBonus);
+                                return self::LOOP_FINISH;
+                            }
+                            
+                        }
+                        unset($parent, $parentBonus);
+                    });
+                }
+                if (!$isActive && !$bonusUserId) {
+                    continue;
+                }
+                $surplus = $storeBonus;
+                $manageTax = $reConsumePoints = '0.000';
+                if ($this->_sysConfig['openStoreDeduct']['VALUE']) {
+                    //扣除相应的复消积分和管理费
+                    $deductData = $this->deduct($userId, $storeBonus);
+                    // 把对碰后的奖金存入缓存中
+                    CalcCache::bonus($userId, $this->_periodNum, 'BONUS_STORE', $storeBonus, $deductData);
+                    $surplus = $deductData['surplus'];
+                    $manageTax = $deductData['manageTax'];
+                    $reConsumePoints = $deductData['reConsumePoints'];
+                } else {
+                    CalcCache::bonus($userId, $this->_periodNum, 'BONUS_STORE', $storeBonus);
+                }
+                //店补奖流水
+                $insertBonusData[] = [
+                    'ID' => SnowFake::instance()->generateId(),
+                    'USER_ID' => $bonusUserId ? $bonusUserId : $userId,
+                    'FROM_USER_ID' => $bonusUserId ? $userId : '', // 分享店铺的ID.只有紧缩且未活跃,才有此值
+                    'AMOUNT' => $surplus,
+                    'ORI_BONUS' => $storeBonus,
+                    'RECONSUME_POINTS' => $reConsumePoints,
+                    'MANAGE_TAX' => $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()
+                ];
 
-                CalcCache::bonus($userId, $periodNum, 'BONUS_STORE', $storeBonus);
+                unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
+            }
+            CalcBonusST::batchInsert($insertBonusData);
+            unset($allData, $insertBonusData);
+            return $this->calcStoreBonusReduce($offset + $this->_limit);
+        }
+    
+        unset($allData);
+    
+        return true;
+    }
 
-                //来源会员信息
-                $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
-                //推广奖流水
+    // 未开启紧缩方式计算店补奖,不活跃即不给奖金
+    public function calcStoreBonus(int $offset = 0) {
+        if( !$this->_isCalcMonth ) {
+            // 不是结算月,则不进行计算
+            echo sprintf("时间:[%s]店服务奖金非月节点,不进行计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
+            return false;
+        }
+        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) {
+            $insertBonusData = [];
+            foreach ($allData as $userId) {
+                // 从缓存中获取会员的业绩信息
+                $perfData = CalcCache::nowPeriodPerf($userId, $this->_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;
+                $surplus = $storeBonus;
+                $manageTax = $reConsumePoints = '0.000';
+                if ($this->_sysConfig['openStoreDeduct']['VALUE']) {
+                    //扣除相应的复消积分和管理费
+                    $deductData = $this->deduct($userId, $storeBonus);
+                    // 把对碰后的奖金存入缓存中
+                    CalcCache::bonus($userId, $this->_periodNum, 'BONUS_STORE', $storeBonus, $deductData);
+                    $surplus = $deductData['surplus'];
+                    $manageTax = $deductData['manageTax'];
+                    $reConsumePoints = $deductData['reConsumePoints'];
+                } else {
+                    CalcCache::bonus($userId, $this->_periodNum, 'BONUS_STORE', $storeBonus);
+                }
+
+                //店补奖流水
                 $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'],
+                    'AMOUNT' => $surplus,
+                    'ORI_BONUS' => $storeBonus,
+                    'RECONSUME_POINTS' => $reConsumePoints,
+                    'MANAGE_TAX' => $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'],
-                        ],
-                    ]),
+                    'CREATED_AT' => Date::nowTime()
                 ];
 
                 unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
             }
-            CalcBonusTG::batchInsert($insertBonusData);
+            CalcBonusST::batchInsert($insertBonusData);
             unset($allData, $insertBonusData);
-            return $this->calcBonusTG($offset + $this->_limit);
+            return $this->calcStoreBonus($offset + $this->_limit);
         }
     
         unset($allData);

+ 63 - 0
common/models/CalcBonusST.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace common\models;
+
+use Yii;
+
+/**
+ * This is the model class for table "{{%CALC_BONUS_ST}}".
+ *
+ * @property string $ID
+ * @property string $USER_ID 会员ID
+ * @property string $FROM_USER_ID 来源会员ID
+ * @property string $AMOUNT 金额
+ * @property int $PERIOD_NUM 结算期数
+ * @property int $CALC_MONTH 所在结算月
+ * @property int $CREATED_AT 创建时间
+ * @property $ORI_BONUS 原奖金
+ * @property $RECONSUME_POINTS 重复消费积分
+ * @property $MANAGE_TAX 管理费
+ */
+class CalcBonusST extends \common\components\ActiveRecord
+{
+    /**
+     * @inheritdoc
+     */
+    public static function tableName()
+    {
+        return '{{%CALC_BONUS_ST}}';
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public function rules()
+    {
+        return [
+            [['USER_ID', 'PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT'], 'required'],
+            [['AMOUNT', 'ORI_BONUS', 'RECONSUME_POINTS', 'MANAGE_TAX'], 'number'],
+            [['PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT'], 'integer'],
+            [['ID', 'USER_ID', 'FROM_USER_ID'], 'string', 'max' => 32],
+            [['ID'], 'unique'],
+        ];
+    }
+
+    /**
+     * @inheritdoc
+     */
+    public function attributeLabels()
+    {
+        return [
+            'ID' => 'ID',
+            'USER_ID' => '会员ID',
+            'FROM_USER_ID' => '来源会员ID',
+            'AMOUNT' => '金额',
+            'PERIOD_NUM' => '结算期数',
+            'CALC_MONTH' => '所在结算月',
+            'CREATED_AT' => '创建时间',
+            'ORI_BONUS' => '原奖金',
+            'RECONSUME_POINTS' => '重复消费积分',
+            'MANAGE_TAX' => '管理费',
+        ];
+    }
+}