zxiansheng 4 лет назад
Родитель
Сommit
7446a001e2

+ 33 - 4
common/helpers/bonus/BonusCalc.php

@@ -41,6 +41,7 @@ use common\models\ResendQY;
 use common\models\DeclarationLevel;
 use common\models\DecOrder;
 use common\models\EmployLevel;
+use common\models\PerfActiveUser;
 use common\models\ScoreMonth;
 use common\models\User;
 use common\models\UserInfo;
@@ -2979,11 +2980,39 @@ class BonusCalc extends BaseObject {
         $bonusReal = $bonus['BONUS_BD'] + $bonus['BONUS_TG'] + $bonus['BONUS_XF'] + $bonus['BONUS_YJ'] + $bonus['BONUS_QY'] + $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'] + $bonus['BONUS_VIP'] + $standardBonus;
         $realBonusGx = 0;
         $realBonusGl = 0;
+        // if( $this->_isCalcMonth ) {
+        //     //查看是否复消300
+        //     $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
+        //     $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
+        //     if ( $fxPvStatus ) {//加上本期和往期的共享和管理奖
+        //         $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();
+        //         $bonusGxSum = $monthSumData['BONUS_GX_SUM'] ?? 0;
+        //         $bonusGlSum = $monthSumData['BONUS_GL_SUM'] ?? 0;
+        //         $realBonusGx += $bonusGxSum + $bonus['BONUS_GX'];
+        //         $realBonusGl += $bonusGlSum + $bonus['BONUS_GL'];
+        //         $bonusReal += $realBonusGx + $realBonusGl;
+        //         unset($monthSumData, $bonusGxSum, $bonusGlSum);
+        //     }
+        // }
+
         if( $this->_isCalcMonth ) {
-            //查看是否复消300
-            $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
-            $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
-            if ( $fxPvStatus ) {//加上本期和往期的共享和管理奖
+            // 将查看是否复消300的逻辑,改成月结时判断当前月有一周活跃则本月活跃
+            // 1. 获取此业绩期的月份
+            $periodInfo = Period::getInfoByPeriodNum($this->_periodNum);
+            $calcMonth = $periodInfo['CALC_YEAR'].'-'.$periodInfo['CALC_MONTH']; // 2025-7
+            // 获取此月的第一天和最后一天时间戳
+            $fday = date('Y-m-01', strtotime($calcMonth));
+            $lday = date('Y-m-d', strtotime("$fday +1 month -1 day"));
+            $ftime = strtotime($fday);
+            $ltime = strtotime($lday.' 23:59:59');
+            // 2. 根据业绩期的月份,判断用户是否是活跃状态.
+            // "select `ID`  from `AR_PERF_ACTIVE_USER` where (`SRATR_AT` <= {$ftime}  AND `END_AT` >= {$ftime})"
+            $fxPvStatus = PerfActiveUser::findUseDbCalc()
+            ->select('USER_ID')
+            ->andFilterWhere(['>=', 'SRATR_AT', $ftime])
+            ->andFilterWhere(['<=', 'END_AT', $ftime])
+            ->find();
+            if ( !empty($fxPvStatus) ) {//加上本期和往期的共享和管理奖
                 $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();
                 $bonusGxSum = $monthSumData['BONUS_GX_SUM'] ?? 0;
                 $bonusGlSum = $monthSumData['BONUS_GL_SUM'] ?? 0;

+ 24 - 0
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\PerfActiveUser;
 use common\models\PerfMonth;
 use common\models\User;
 use Yii;
@@ -156,6 +157,12 @@ class BonusSend extends BaseObject {
             $t8 = microtime(true);
             echo('更新会员累计月业绩完成,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
 
+            // 更新活跃用户状态,更新为已处理
+            $this->updateActiveUser();
+            $this->_updatePercent(95);
+            $t9 = microtime(true);
+            echo('更新会员累计月业绩完成,耗时:' . round($t9 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
+            
             // 开启子进程去完成下面的循环发放和循环改聘级和循环更新累计业绩
             /*$process = new Process('sendBonus', 3);
             $process->run(function($workId, $pmid){
@@ -397,6 +404,23 @@ class BonusSend extends BaseObject {
         return true;
     }
 
+    // 更活跃会员,将is_send改成1
+    public function updateActiveUser() {
+        try {
+            $ret = PerfActiveUser::updateAll(
+                ['IS_SENT' => 1], 
+                'PERIOD_NUM=:PERIOD_NUM AND IS_SENT=:IS_SENT', 
+                ['IS_SENT'=>0, 'PERIOD_NUM'=>$this->_periodNum]
+            );
+
+            return $ret;
+        } catch(Exception $e) {
+
+            $this->addError('updateActiveUser', '更新活跃会员业绩期处理状态失败,原因:' . $e->getMessage());
+            return false;
+        }
+    }
+
     /**
      * 更新会员的累计业绩
      * @param int $offset

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

@@ -31,6 +31,7 @@ use common\models\PerfStandard;
 use common\models\Period;
 use common\models\DecOrder;
 use common\models\EmployLevel;
+use common\models\PerfActiveUser;
 use common\models\UserRelation;
 use yii\base\Exception;
 use yii\base\StaticInstanceTrait;
@@ -171,18 +172,22 @@ class PerfCalc {
             $this->loopWriteMonthPerf();
             $t7 = microtime(true);
             echo('本月业绩入库完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
-            $this->_updatePercent(95);
+            $this->_updatePercent(90);
 
             // //达标业绩
             // $this->loopCalcPerfByStandardFXOrder();
             // //达标业绩入库
             // $this->loopWriteStandardPerf();
-            $t8 = microtime(true);
+            $t9 = microtime(true);
             //echo('本月业绩入库完成,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
-            $this->_updatePercent(100);
+            $this->_updatePercent(95);
 
+            $t10 = microtime(true);
+            // 通过插入到perforder中的业绩订单数据,生成此业绩期活跃用户数据
+            $this->loopWriteActiveUser();
+            $this->_updatePercent(100);
 
-            echo('结算全部完成,共耗时:' . round($t8 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
+            echo('结算全部完成,共耗时:' . round($t10 - $t9, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
         } catch (\Exception $e) {
             $this->errorCalcTask();
             $this->addError('calc', $e->getMessage());
@@ -282,6 +287,8 @@ class PerfCalc {
         PerfPeriod::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
         // 业绩单
         PerfOrder::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
+        // 删除活跃用户
+        PerfActiveUser::pageDeleteAll('PERIOD_NUM='.$this->_periodNum.'  AND IS_SENT=0 ');
         // 月结时要清空的数据
         if ($this->_isCalcMonth) {
             // 月业绩表
@@ -291,6 +298,106 @@ class PerfCalc {
         }
     }
 
+    /**
+     * 循环判断活跃会员,并入库
+     * @param int $offset
+     * @return bool
+     * @throws \yii\db\Exception
+     */
+    public function loopWriteActiveUser($offset = 0) {
+        echo sprintf("时间:[%s]本期活跃用户PerfOrder表中,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
+        // 从PerfOrder表中获取此周期的用户数据
+        $data = PerfPeriod::findUseDbCalc()
+        ->select('USER_ID')
+        ->where('PERIOD_NUM=:PERIOD_NUM',[
+            ':PERIOD_NUM' => $this->_periodNum
+        ])->offset($offset)->limit($this->_limit)->asArray()->all();
+        if ($data) {
+            foreach ($data as $upv) {
+                $userId = $upv['USER_ID'];
+                // 判断用户每周是否是活跃用户
+                $allData = PerfOrder::findUseDbCalc()
+                ->select('USER_ID,PV,DEC_AMOUNT,PERIOD_NUM,CALC_MONTH,P_CALC_MONTH, DEC_SN,ORDER_CREATED_AT')
+                ->where('PERIOD_NUM=:PERIOD_NUM',[
+                    ':PERIOD_NUM' => $this->_periodNum
+                ])
+                ->where('USER_ID=:USER_ID',[
+                    ':USER_ID' => $userId
+                ])
+                ->where('IS_SENT=:IS_SENT',[
+                    ':IS_SENT' => 0
+                ])
+                ->orderBy('ORDER_CREATED_AT DESC')
+                ->asArray()
+                ->all();
+                if($allData) {
+                    $this->weekMonthOrder($allData);
+
+                    unset($insertDataPeriodPerf, $allData);
+                }
+
+                unset($allData);
+            }
+            
+            unset($data);
+            return $this->loopWriteActiveUser($offset + $this->_limit);
+        }
+
+        return true;
+    }
+
+    // 活跃判断
+    public function weekMonthOrder($data) {
+        // 获取每个订单是周几,把他们分别放到各自的周里面.
+        // 因为是自然月,所以需要知道是此月的几号,是此月的第几周.然后往前和往后推出时间戳范围内pv的累计之和是多少
+        $one = 24*60*60; // 一天的时间戳
+        $time_arr = [];
+        foreach ($data as $orders) {
+            $day = intval(date('d', $orders['ORDER_CREATED_AT'])); // 当月第多少号
+            $weekDay = (date('w', $orders['ORDER_CREATED_AT']) == 0) ? 7 : date('w', $orders['ORDER_CREATED_AT']);// 周几 1-7
+            $format = date('Y-m-d', $orders['ORDER_CREATED_AT']);
+            $orderDayStart = strtotime($format.' 0:0:0');
+            $orderDayEnd = strtotime($format.' 23:59:59');
+            $caluc = 7-$weekDay;
+            $calucWeekStart = $orderDayStart - $caluc*$one;
+            $calucWeekEnd = $orderDayEnd + $caluc*$one;
+            if (!isset($time_arr[$calucWeekStart])) {
+                $time_arr[$calucWeekStart] = array_merge($orders, [
+                    'temp_start' => $calucWeekStart,
+                    'temp_end' => $calucWeekEnd,
+                    'calculate_total_pv' => $orders['PV']
+                ]);
+            } else {
+                $time_arr[$calucWeekStart]['calculate_total_pv'] = $time_arr[$calucWeekStart]['calculate_total_pv'] + $orders['PV'];
+            }
+        }
+        unset($data);
+        $insert = [];
+        // 循环判断,当周pv累计是否达到400
+        foreach ($time_arr as $k => $v) {
+            if ($v['calculate_total_pv'] >= $this->_sysConfig['activeUserPvCondition']['VALUE']) {
+                // 如果个人业累计绩达400PV,取得连续四周活跃
+                // 取出周末再增加三周的时间戳
+                $insert[] = [
+                    'ID' => SnowFake::instance()->generateId(),
+                    'USER_ID' => $v['USER_ID'],
+                    'PERIOD_NUM' => $v['PERIOD_NUM'],
+                    'CALC_MONTH' => $v['CALC_MONTH'],
+                    'P_CALC_MONTH' => $v['P_CALC_MONTH'],
+                    'SRATR_AT' => $v['temp_start'],
+                    'END_AT' => $v['temp_end']+(3*7*24*60*60),
+                    'CREATED_AT' => time(),
+                    'PV' => $v['calculate_total_pv'],
+                ];
+            }
+        }
+        PerfActiveUser::batchInsert($insert);
+        
+        unset($insert);
+
+        return true;
+    }
+
     /**
      * 周结,向上级算业绩,并计入业绩单
      * @param int $offset
@@ -392,6 +499,7 @@ class PerfCalc {
                     'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                     'CREATED_AT' => Date::nowTime(),
                     'CLOSED_AT' => 0,
+                    'ORDER_CREATED_AT' => $data['CREATED_AT'],
                 ];
                 unset($data, $decInfo, $sn, $toInfo);
             }
@@ -411,7 +519,7 @@ class PerfCalc {
      */
     public function loopCalcPeriodPerfByOrderDec($offset = 0) {
         // 循环获取全部报单
-        $allData = OrderDec::findUseDbCalc()->select('ID,SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PV,PAY_AMOUNT,PAY_PV,PERIOD_NUM,PAY_TYPE')->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE", [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>'ZC'])->orderBy('ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
+        $allData = OrderDec::findUseDbCalc()->select('CREATED_AT,ID,SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PV,PAY_AMOUNT,PAY_PV,PERIOD_NUM,PAY_TYPE')->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE", [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>'ZC'])->orderBy('ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
         if ($allData) {
             $insertPerfOrderData = [];
             foreach ($allData as $data) {
@@ -493,6 +601,7 @@ class PerfCalc {
                     'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                     'CREATED_AT' => Date::nowTime(),
                     'CLOSED_AT' => 0,
+                    'ORDER_CREATED_AT' => $data['CREATED_AT']
                 ];
                 unset($data, $decInfo, $sn, $toInfo);
             }
@@ -615,6 +724,7 @@ class PerfCalc {
                     'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                     'CREATED_AT' => Date::nowTime(),
                     'CLOSED_AT' => 0,
+                    'ORDER_CREATED_AT' => $data['CREATED_AT']
                 ];
                 unset($data, $baseInfo, $sn, $orderCashAmount, $payPv, $cacheDataKey);
             }
@@ -730,6 +840,7 @@ class PerfCalc {
                     'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
                     'CREATED_AT' => Date::nowTime(),
                     'CLOSED_AT' => 0,
+                    'ORDER_CREATED_AT' => $data['CREATED_AT']
                 ];
                 unset($data, $baseInfo, $sn, $orderCashAmount, $payPv, $cacheDataKey);
             }

+ 77 - 0
common/models/PerfActiveUser.php

@@ -0,0 +1,77 @@
+<?php
+
+namespace common\models;
+
+use common\helpers\snowflake\SnowFake;
+use common\helpers\Tool;
+use Yii;
+use yii\base\Exception;
+
+/**
+ * This is the model class for table "{{%PERF_ACTIVE_USER}}".
+ *
+ * 
+ * 
+ * @property string $ID
+ * @property string $USER_ID 用户id
+ * @property string $IS_ACTIVE 是否活跃 1活跃
+ * @property string $IS_SENT 是否已挂网,已官网不能删除 1已挂网
+ * @property int $PERIOD_NUM 业绩期,是第几期
+ * @property string $CALC_MONTH 结算年月202110
+ * @property string $P_CALC_MONTH 结算年月日2021-12-10
+ * @property string $WHICH_WEEK 当月第几周
+ * @property int $WHICH_MONTH 当年第几月
+ * @property string $WHICH_YEAR 结算周期年份
+ * @property int $CREATED_AT 创建时间
+ */
+class PerfActiveUser extends \common\components\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return '{{%PERF_ACTIVE_USER}}';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['IS_ACTIVE', 'USER_ID', 'PERIOD_NUM', 'CALC_MONTH', 'P_CALC_MONTH', 'CREATED_AT', 'SRATR_AT', 'END_AT'], 'required'],
+            [['PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT'], 'integer'],
+            [['ID', 'USER_ID'], 'string', 'max' => 32],
+            [['P_CALC_MONTH'], 'string', 'max' => 7],
+            [['ID'], 'unique'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'ID' => 'ID',
+            'USER_ID' => '用户ID',
+            'IS_ACTIVE' => '是否活跃',
+            'IS_SENT' => '是否已挂网',
+            'PERIOD_NUM' => '业绩期',
+            'CALC_MONTH' => '结算年月',
+            'P_CALC_MONTH' => '结算年月日',
+            'SRATR_AT' => '活跃开始时间',
+            'END_AT' => '活跃结束时间',
+            'CREATED_AT' => '创建时间'
+        ];
+    }
+
+    /**
+     * 生成sn
+     * @return string
+     */
+    public static function generateSN() {
+        return  SnowFake::instance()->generateId();
+    }
+}

+ 0 - 1
common/models/PerfPeriod.php

@@ -224,5 +224,4 @@ class PerfPeriod extends \common\components\ActiveRecord
 
         return $result;
     }
-
 }

+ 6 - 0
common/models/Period.php

@@ -146,6 +146,12 @@ class Period extends \common\components\ActiveRecord
         return $this->periodArr;
     }
 
+    // 通过期数,获取此期数据
+    public static function getInfoByPeriodNum($periodNum) {
+        
+        return static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
+    }
+
     /**
      * 是否存在所传期数
      * @param int $periodNum