| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- <?php
- /**
- */
- namespace common\helpers\bonus;
- use common\helpers\Cache;
- use common\helpers\Date;
- use common\helpers\snowflake\SnowFake;
- use common\models\PerfOrder;
- use common\models\PerfPeriod;
- use common\models\Period;
- use common\models\PerfMonth;
- use common\models\ServeProcess;
- use common\models\UserNetwork;
- use yii\base\Exception;
- use yii\base\StaticInstanceTrait;
- class CalcServePerfCalc {
- use StaticInstanceTrait;
- private $_limit = 3000;
- private $_companyMonthPerf = 0;
- private $_cfTotalPercent = 0;
- private $_lxTotalPercent = 0;
- private $_sysConfig = [];
- private $_decLevelConfig = [];
- private $_empLevelConfig = [];
- private $_decRoleConfig = [];
- private $_periodNum = 0;
- private $_isCalcMonth = 0;
- private $_calcYear;
- private $_calcMonth;
- private $_calcYearMonth;
- private $_lastCalcYear;
- private $_lastCalcMonth;
- private $_lastCalcYearMonth;
- private $_lastPeriodNum;
- private $_periodObj;
- const LOOP_FINISH = 1;
- const LOOP_CONTINUE = 2;
- const ORDER_PAY_TYPE_CASH = 'cash';
- /**
- * 获取期数
- * @return int
- */
- public function getPeriodNum() {
- return $this->_periodNum;
- }
- // 校验是否可进行计算
- public function isCalcIng($periodNum) {
- $periodObj = Period::instance();
- $periodDataArr = $periodObj->setPeriodNum($periodNum);
- if (empty($periodDataArr)) {
- return false;
- }
- if ($periodDataArr['IS_PREPARE'] != 1) {
- return false;
- }
- $this->_periodNum = $periodDataArr['PERIOD_NUM'];
- $this->_periodObj = $periodObj;
- return true;
- }
- /**
- * 累计业绩数据
- * @param $periodNum
- * @return bool
- */
- public function calcStep($periodNum = null) {
- try {
- $requestTime = date('Y-m-d H:i:s', time());
- // if (empty($periodNum)) {
- // echo('触发时间:【'.$requestTime.'】'.'定时器执行累计业绩计算 ,内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- // } else {
- // echo('触发时间:【'.$requestTime.'】'.'手动触发累计业绩计算 ,内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- // }
- //一、查询此业绩状态,是否能进行计算
- $calcIng = $this->isCalcIng($periodNum);
- ServeProcess::recordRequest($requestTime, $this->_periodNum, '', true);
- if ($calcIng !== true) {
- //echo('触发时间:【'.date('Y-m-d H:i:s', time()).'】'.'业绩期表中,此期状态不正确');
- //ServeProcess::recordRequest($requestTime, $this->_periodNum, '业绩期表中,此期状态不正确');
- return false;
- } else {
- // 将IS_PREPARE改成2,计算中
- Period::updateCalcProcess(2, $this->_periodNum);
- ServeProcess::recordRequest($requestTime, $this->_periodNum, '调整IS_PREPARE值为2,计算开始');
- }
-
- // 记录初始化数据,用户总数,业绩单业绩总pv值.
- ServeProcess::recordDataInfo(date('Y-m-d H:i:s',time()), $this->_periodNum);
- $t1 = microtime(true);
- //二、初始化
- $this->initCalcTask();
- echo '业绩期为:'.$this->_periodNum;
-
- Period::updatePercent(10, $this->_periodNum);
- ServeProcess::recordProcess($t1, time(), $this->_periodNum, '初始化---初始化配置');
- $initT2 = microtime(true);
- //三、 设置结算状态
- $this->setCalcStatus('start', $this->_periodNum);
- ServeProcess::recordProcess($initT2, time(), $this->_periodNum, '初始化---设置结算状态');
- $initT3 = microtime(true);
- //四、 清空所有本期结算用到的缓存
- CalcCache::clearAll($this->_periodNum);
- ServeProcess::recordProcess($initT3, time(), $this->_periodNum, '初始化---清空业绩缓存');
- $initT4 = microtime(true);
- //五、 清空会员推荐和接点关系缓存
- CalcCache::clearNetCache();
- ServeProcess::recordProcess($initT4, time(), $this->_periodNum, '初始化---清空会员推荐和接点关系缓存');
- $initT5 = microtime(true);
- //六、 清空相关表数据
- $this->clearTableData();
- $this->_updatePercent(15);
- ServeProcess::recordProcess($initT5, time(), $this->_periodNum, '初始化---清空相关表数据');
- $t2 = microtime(true);
- echo(date('Y-m-d H:i:s',time()).'初始化、清空缓存及相关数据表完成,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- ServeProcess::recordProcess($t1, $t2, $this->_periodNum, '初始化---初始化、清空缓存及相关数据表完成');
-
- //七、 添加缓存中用户数据
- CalcCache::addUsers($this->_periodNum);
- $t3 = microtime(true);
- echo(date('Y-m-d H:i:s',time()).'计算业绩向缓存中加入用户完成,耗时:' . round($t3 - $t2, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- ServeProcess::recordProcess($t2, $t3, $this->_periodNum, '计算业绩向缓存中加入用户完成');
- $this->_updatePercent(20);
- // 八、循环累计用户各项业绩数据
- $this->loopGrandPerf();
- $t4 = microtime(true);
- echo(date('Y-m-d H:i:s',time()).'累计用户业绩完成' . round($t4 - $t3, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- ServeProcess::recordProcess($t3, $t4, $this->_periodNum, '累计用户业绩完成');
- $this->_updatePercent(60);
-
- // 九、本期业绩入库
- $this->loopWriteNowPerf();
- $t6 = microtime(true);
- echo(date('Y-m-d H:i:s',time()).'本期业绩入库完成,耗时:' . round($t6 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- ServeProcess::recordProcess($t4, $t6, $this->_periodNum, '本期业绩入库完成');
- $this->_updatePercent(70);
- //十、计算月业绩表中的数据
- $this->loopCalcMonthPerfTableData();
- $t7 = microtime(true);
- echo(date('Y-m-d H:i:s',time()).'计算月业绩表中的数据完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- if($this->_isCalcMonth) {
- ServeProcess::recordProcess($t6, $t7, $this->_periodNum, '计算月业绩表中的数据完成');
- }
- $this->_updatePercent(80);
- //十一、本月业绩入库
- $this->loopWriteMonthPerf();
- $t8 = microtime(true);
- echo(date('Y-m-d H:i:s',time()).'本月业绩入库完成,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- if($this->_isCalcMonth) {
- ServeProcess::recordProcess($t7, $t8, $this->_periodNum, '本月业绩入库完成');
- }
- $this->_updatePercent(100);
- $t9 = microtime(true);
- ServeProcess::recordProcess($t6, $t7, $this->_periodNum, '计算业绩业绩结算全部完成');
- echo(date('Y-m-d H:i:s',time()).'计算业绩业绩结算全部完成,共耗时:' . round($t9 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
- } catch (\Exception $e) {
- $this->errorCalcTask();
- var_dump($e->getMessage());
- return false;
- }
- $this->endCalcTask();
- return true;
- }
- /**
- * 结算完成
- * @return bool
- */
- public function endCalcTask() {
- // 更新结算状态
- $this->setCalcStatus('end');
- }
- /**
- * 结算错误
- */
- public function errorCalcTask() {
- // 清空所有本期结算用到的缓存
- CalcCache::clearAll($this->_periodNum);
- // 更新结算状态
- $this->setCalcStatus('fail');
- }
- /**
- * 设置生成业绩单状态
- * @param $type
- * start|end|fail
- */
- public function setCalcStatus($type, $periodNum = null) {
- if ($type == 'start') {
- Period::updateAll(['IS_PERFING' => 1, 'IS_PERFED' => Period::PERF_NONE, 'PERF_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
- } elseif ($type == 'end') {
- Period::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FINISH, 'PERFED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
- } elseif ($type == 'fail') {
- Period::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FAIL, 'PERFED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
- }
- }
- /**
- * 初始化结算任务
- * @throws \yii\db\Exception
- */
- public function initCalcTask() {
- try {
- $periodObj = Period::instance();
- $this->_sysConfig = Cache::getSystemConfig();
- $this->_decLevelConfig = Cache::getDecLevelConfig();
- $this->_empLevelConfig = Cache::getEmpLevelConfig();
- $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
- $this->_isCalcMonth = $periodObj->isCalcMonth($this->_periodNum);
- $this->_calcYear = $periodObj->getYear($this->_periodNum);
- $this->_calcMonth = $periodObj->getMonth($this->_periodNum);
- $this->_calcYearMonth = $periodObj->getYearMonth($this->_periodNum);
- } catch(Exception $e) {
- var_dump($e->getMessage());
- }
- }
- /**
- * 清空相关表数据
- */
- public function clearTableData() {
- PerfPeriod::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);// 周业绩
- if ($this->_isCalcMonth) {
- PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");// 月业绩表
- }
- }
- /**
- * 累计用户业绩
- * @param int $offset
- * @return bool
- * @throws \yii\db\Exception
- */
- public function loopGrandPerf($offset = 0) {
- $allData = PerfOrder::findUseDbCalc()
- ->select('ORDER_AMOUNT,PERF_TYPE,USER_ID,PV,PERIOD_NUM,DEC_USER_ID,PAY_TYPE')
- ->where(
- "PERIOD_NUM=:PERIOD_NUM",
- [':PERIOD_NUM' => $this->_periodNum]
- )
- ->orderBy('CREATED_AT DESC,ID DESC')
- ->offset($offset)
- ->limit($this->_limit)
- ->asArray()
- ->all();
- if (!empty($allData)) {
- foreach ($allData as $data) {
- // 循环累计报单业绩
- try{
- echo "开始累计用户业绩,用户ID为:".$data['USER_ID'].PHP_EOL;
- if ($data['PERF_TYPE'] == PerfOrder::ZC_TYPE) {
- // 给自己增加PCS(个人消费)
- CalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
- 'PV_PCS' => $data['PV'],
- 'PV_PCS_ZC' => $data['PV'],
- ]);
- // 把该会员加入到能拿到业绩的会员缓存中
- CalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
- //加入到报单会员中
- $toInfo = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
- CalcCache::addHasBDUsers($data['USER_ID'], $this->_periodNum, [
- 'TO_USER_ID' => $data['USER_ID'],
- 'USER_ID' => $toInfo['DEC_ID'],
- 'DEC_ID' => $toInfo['DEC_ID'],
- //考虑可能会移网的情况
- 'REC_USER_ID' => $toInfo['REC_UID'] ?? '',
- 'CON_USER_ID' => $toInfo['CON_UID'] ?? '',
- 'DEC_AMOUNT' => $data['ORDER_AMOUNT'],
- 'DEC_PV' => $data['PV'],
- ]);
- // 给上追加业绩
- $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data) {
- // 给上级会员追加本期业绩到缓存中
- CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
- 'PV_' . $parent['LOCATION'] . 'L' => $data['PV'],
- 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $data['PV'],
- 'PV_' . $parent['LOCATION'] . 'L_' . $data['PERF_TYPE'] => $data['PV'],
- ]);
- // 把该会员加入到能拿到业绩的会员缓存中
- CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
- unset($parent);
- });
- //给推荐关系累计增加业绩
- $this->loopRelationParentDo($data['USER_ID'], function ($parent) use (&$data) {
- // 给上级会员追加本期业绩到缓存中
- CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
- 'PV_PSS' => $data['PV'],
- ]);
- // 把该会员加入到能拿到业绩的会员缓存中
- CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
- unset($parent);
- });
- }
- } catch(Exception $e) {
- var_dump(__LINE__,__FILE__,$e->getMessage(), $data);
- }
-
- // 循环累计复消业绩
- try{
- if ($data['PERF_TYPE'] == PerfOrder::FX_TYPE) {
- if( $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_CASH ) {
- $orderCashAmount = $data['ORDER_AMOUNT'];
- $payPv = $data['PV'] * $this->_sysConfig['cashReconsumeBonusPercent']['VALUE'] / 100;
- $cacheDataKey = 'PV_PCS_FX_CASH';
- } else {
- $orderCashAmount = 0;
- $payPv = $data['PV'];
- $cacheDataKey = 'PV_PCS_FX_POINT';
- }
- if( $payPv <= 0 ) continue;
- // 给自己增加PCS(个人消费)
- CalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
- 'FX_AMOUNT_CASH' => $orderCashAmount,
- 'PV_PCS' => $payPv,
- 'PV_PCS_FX' => $payPv,
- $cacheDataKey => $payPv,
- ]);
- // 把该会员加入到能拿到业绩的会员缓存中
- CalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
- // 给上追加业绩
- $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data, $payPv) {
- // 给上级会员追加本期业绩到缓存中
- CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
- 'PV_' . $parent['LOCATION'] . 'L' => $payPv,
- 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $payPv,
- 'PV_' . $parent['LOCATION'] . 'L_FX' => $payPv,
- ]);
- // 把该会员加入到能拿到业绩的会员缓存中
- CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
- });
- //给推荐关系累计增加业绩
- $this->loopRelationParentDo($data['USER_ID'], function ($parent) use ($data, $payPv) {
- // 给上级会员追加本期业绩到缓存中
- CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
- 'PV_PSS' => $payPv,
- ]);
- // 把该会员加入到能拿到业绩的会员缓存中
- CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
- });
- }
- }catch(Exception $e) {
- var_dump(__LINE__,__FILE__,$e->getMessage(), $data);
- }
- }
- return $this->loopGrandPerf($offset + $this->_limit);
- }
- unset($allData);
- return true;
- }
- /**
- * 计算月业绩表相关的数据并写入数据库
- * @param int $offset
- * @return bool
- * @throws Exception
- * @throws \yii\db\Exception
- */
- public function loopCalcMonthPerfTableData(int $offset = 0) {
- if (!$this->_isCalcMonth) {
- return true;
- }
- echo sprintf("时间:[%s]月业绩,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
- $allData = PerfPeriod::findUseDbCalc()
- ->select('USER_ID, SUM(FX_AMOUNT_CASH) AS FX_AMOUNT_CASH_SUM,SUM(PV_PCS) AS PV_PCS_SUM,SUM(PV_PCS_FX) AS PV_PCS_FX_SUM,
- SUM(PV_PSS) AS PV_PSS_SUM,SUM(PV_1L) AS PV_1L_SUM,SUM(PV_2L) AS PV_2L_SUM,SUM(PV_3L) AS PV_3L_SUM,
- SUM(PV_4L) AS PV_4L_SUM,SUM(PV_5L) AS PV_5L_SUM,SUM(PV_1L_ZC) AS PV_1L_ZC_SUM,SUM(PV_2L_ZC) AS PV_2L_ZC_SUM,
- SUM(PV_3L_ZC) AS PV_3L_ZC_SUM,SUM(PV_4L_ZC) AS PV_4L_ZC_SUM,SUM(PV_5L_ZC) AS PV_5L_ZC_SUM')
- ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
- ->groupBy('USER_ID')
- ->orderBy('USER_ID DESC')
- ->offset($offset)
- ->limit($this->_limit)
- ->asArray()
- ->all();
- if ($allData) {
- // 月度业绩表
- foreach ($allData as $everyData) {
- $userId = $everyData['USER_ID'];
- //往期业绩
- $userLastPerf = CalcCache::userPerf($userId, $this->_periodNum);
- //本期业绩
- $periodPerf = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
- $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
- $nowMonthPerf = [
- 'USER_ID' => $userId,
- 'FX_AMOUNT_CASH' => $everyData['FX_AMOUNT_CASH_SUM'],
- 'PV_PCS' => $everyData['PV_PCS_SUM'],
- 'PV_PCS_FX' => $everyData['PV_PCS_FX_SUM'],
- 'PV_PSS' => $everyData['PV_PSS_SUM'],
- 'PV_1L' => $everyData['PV_1L_SUM'],
- 'PV_2L' => $everyData['PV_2L_SUM'],
- 'PV_3L' => $everyData['PV_3L_SUM'],
- 'PV_4L' => $everyData['PV_4L_SUM'],
- 'PV_5L' => $everyData['PV_5L_SUM'],
- //总数据,历史+本期。不能用上月加本月,因为上月可能没业绩,上上个月有业绩。
- 'PV_1L_TOTAL' => $periodPerf['PV_1L'] + $userLastPerf['PV_1L'],
- 'PV_2L_TOTAL' => $periodPerf['PV_2L'] + $userLastPerf['PV_2L'],
- 'PV_3L_TOTAL' => $periodPerf['PV_3L'] + $userLastPerf['PV_3L'],
- 'PV_4L_TOTAL' => $periodPerf['PV_4L'] + $userLastPerf['PV_4L'],
- 'PV_5L_TOTAL' => $periodPerf['PV_5L'] + $userLastPerf['PV_5L'],
- 'PV_PSS_TOTAL' => $periodPerf['PV_PSS'] + $userLastPerf['PV_PSS_TOTAL'],
- ];
- // 把会员的月业绩写入缓存中,以便下面的奖金计算从缓冲中获取数据效率高
- CalcCache::addHasMonthPerfUsers($userId, $this->_periodNum);
- CalcCache::nowMonthPerf($userId, $this->_periodNum, $nowMonthPerf);
- unset($userId, $everyData, $nowMonthPerf, $lastMonthData, $userBaseInfo, $isVip);
- }
- unset($allData);
- $this->loopCalcMonthPerfTableData($offset + $this->_limit);
- }
- unset($allData);
- return true;
- }
- /**
- * 循环有业绩会员,并入库
- * @param int $offset
- * @return bool
- * @throws \yii\db\Exception
- */
- public function loopWriteNowPerf($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){
- $insertDataPeriodPerf = [];
- foreach($allData as $userId){
- $insertDataPeriodPerf[] = $this->nowPeriodPerfData($userId);
- unset($userId);
- }
- PerfPeriod::batchInsert($insertDataPeriodPerf);
- unset($insertDataPeriodPerf, $allData);
- return $this->loopWriteNowPerf($offset + $this->_limit);
- }
- unset($allData);
- return true;
- }
- /**
- * 循环有月业绩会员,并入库
- * @param int $offset
- * @return bool
- * @throws \yii\db\Exception
- */
- public function loopWriteMonthPerf($offset = 0) {
- if(!$this->_isCalcMonth){
- return true;
- }
- echo sprintf("时间:[%s]缓存本月业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
- // 从缓存列表里面从底层往上倒序获取会员
- $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
- if($allData){
- $insertDataMonthPerf = [];
- foreach($allData as $userId){
- $insertDataMonthPerf[] = $this->nowMonthPerfData($userId);
- unset($userId);
- }
- PerfMonth::batchInsert($insertDataMonthPerf);
- unset($insertDataMonthPerf, $allData);
- return $this->loopWriteMonthPerf($offset + $this->_limit);
- }
- unset($allData);
- return true;
- }
- /**
- * 本期业绩数据
- * @param $userId
- * @return array
- */
- public function nowPeriodPerfData($userId){
- $data = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
- $result = [
- 'ID' => SnowFake::instance()->generateId(),
- 'USER_ID' => $userId,
- 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
- 'PV_PCS' => $data['PV_PCS'],
- 'PV_PSS' => $data['PV_PSS'],
- 'PV_PCS_ZC' => $data['PV_PCS_ZC'],
- 'PV_PCS_FX' => $data['PV_PCS_FX'],
- 'PV_PCS_FX_CASH' => $data['PV_PCS_FX_CASH'],
- 'PV_PCS_FX_POINT' => $data['PV_PCS_FX_POINT'],
- 'PV_1L' => $data['PV_1L'],
- 'PV_1L_TOUCH' => $data['PV_1L_TOUCH'],
- 'PV_1L_ZC' => $data['PV_1L_ZC'],
- 'PV_1L_FX' => $data['PV_1L_FX'],
- 'PV_2L' => $data['PV_2L'],
- 'PV_2L_TOUCH' => $data['PV_2L_TOUCH'],
- 'PV_2L_ZC' => $data['PV_2L_ZC'],
- 'PV_2L_FX' => $data['PV_2L_FX'],
- 'PV_3L' => $data['PV_3L'],
- 'PV_3L_TOUCH' => $data['PV_3L_TOUCH'],
- 'PV_3L_ZC' => $data['PV_3L_ZC'],
- 'PV_3L_FX' => $data['PV_3L_FX'],
- 'PV_4L' => $data['PV_4L'],
- 'PV_4L_TOUCH' => $data['PV_4L_TOUCH'],
- 'PV_4L_ZC' => $data['PV_4L_ZC'],
- 'PV_4L_FX' => $data['PV_4L_FX'],
- 'PV_5L' => $data['PV_5L'],
- 'PV_5L_TOUCH' => $data['PV_5L_TOUCH'],
- 'PV_5L_ZC' => $data['PV_5L_ZC'],
- 'PV_5L_FX' => $data['PV_5L_FX'],
- 'SURPLUS_1L' => $data['SURPLUS_1L'],
- 'SURPLUS_2L' => $data['SURPLUS_2L'],
- 'SURPLUS_3L' => $data['SURPLUS_3L'],
- 'SURPLUS_4L' => $data['SURPLUS_4L'],
- 'SURPLUS_5L' => $data['SURPLUS_5L'],
- 'PERIOD_NUM' => $this->_periodNum,
- 'CALC_MONTH' => $this->_calcYearMonth,
- 'CREATED_AT' => Date::nowTime(),
- ];
- unset($data);
- return $result;
- }
- /**
- * 本月业绩
- * @param $userId
- * @return array
- */
- public function nowMonthPerfData($userId){
- $data = CalcCache::nowMonthPerf($userId, $this->_periodNum);
- $result = [
- 'ID' => SnowFake::instance()->generateId(),
- 'USER_ID' => $userId,
- 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
- 'PV_PCS' => $data['PV_PCS'],
- 'PV_PCS_FX' => $data['PV_PCS_FX'],
- 'PV_PSS' => $data['PV_PSS'],
- 'PV_1L' => $data['PV_1L'],
- 'PV_2L' => $data['PV_2L'],
- 'PV_3L' => $data['PV_3L'],
- 'PV_4L' => $data['PV_4L'],
- 'PV_5L' => $data['PV_5L'],
- 'PV_1L_TOTAL' => $data['PV_1L_TOTAL'],
- 'PV_2L_TOTAL' => $data['PV_2L_TOTAL'],
- 'PV_3L_TOTAL' => $data['PV_3L_TOTAL'],
- 'PV_4L_TOTAL' => $data['PV_4L_TOTAL'],
- 'PV_5L_TOTAL' => $data['PV_5L_TOTAL'],
- 'PV_PSS_TOTAL' => $data['PV_PSS_TOTAL'],
- 'CALC_MONTH' => $this->_calcYearMonth,
- 'CREATED_AT' => Date::nowTime(),
- ];
- unset($data);
- return $result;
- }
- /**
- * 循环父级并执行回调函数
- * @param $userId
- * @param callable $callbackFunc
- * @param int $offset
- * @return bool
- */
- public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
- $allParents = Cache::getAllNetworkParents($userId, true);
- $allData = array_slice($allParents, $offset, $this->_limit);
- unset($allParents);
- if ($allData) {
- foreach ($allData as $data) {
- $funcResult = $callbackFunc($data);
- if ($funcResult === self::LOOP_FINISH) {
- return true;
- } elseif ($funcResult === self::LOOP_CONTINUE) {
- continue;
- }
- unset($data, $funcResult);
- }
- unset($allData);
- return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
- }
- return true;
- }
- /**
- * 循环推荐网络的父级
- * @param $userId
- * @param callable $callbackFunc
- * @param int $offset
- * @return bool
- */
- public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
- $allParents = Cache::getAllRelationParents($userId,true);
- $allData = array_slice($allParents, $offset, $this->_limit);
- unset($allParents);
- if ($allData) {
- foreach ($allData as $data) {
- $funcResult = $callbackFunc($data);
- if ($funcResult === self::LOOP_FINISH) {
- return true;
- } elseif ($funcResult === self::LOOP_CONTINUE) {
- continue;
- }
- unset($data, $funcResult);
- }
- unset($allData);
- return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
- }
- return true;
- }
- /**
- * 更新百分比并发送
- * @param $percent
- */
- private function _updatePercent($percent) {
- // 把数据写入数据库中
- Period::updateAll(['PERF_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
- }
- }
|