|
|
@@ -30,6 +30,7 @@ use common\models\PerfActiveUser;
|
|
|
use common\models\PerfMonthPrepare;
|
|
|
use common\models\PerfOrderPrepare;
|
|
|
use common\models\PerfPeriodPrepare;
|
|
|
+use common\models\PeriodPrepare;
|
|
|
use yii\base\Exception;
|
|
|
use yii\base\StaticInstanceTrait;
|
|
|
|
|
|
@@ -102,11 +103,19 @@ class PreparePerfCalc {
|
|
|
*/
|
|
|
public function calcStep($periodNum = null, $handleUserId = null) {
|
|
|
try {
|
|
|
+ // 疑问,是否要将最后结算月的业绩同步过来
|
|
|
+ // 先计算是否是结算月,并且是否到了周日0点
|
|
|
+ $checkPrepare = $this->checkCanCalcPref();
|
|
|
+ if (!$checkPrepare) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
$this->_errors = [];
|
|
|
$this->_handleUserId = $handleUserId;
|
|
|
+ $this->_updatePercent(5);
|
|
|
$t1 = microtime(true);
|
|
|
// 初始化结算任务
|
|
|
$this->initCalcTask();
|
|
|
+ $this->_updatePercent(10);
|
|
|
// 设置结算状态
|
|
|
$this->setCalcStatus('start');
|
|
|
// 清空所有本期结算用到的缓存
|
|
|
@@ -115,6 +124,7 @@ class PreparePerfCalc {
|
|
|
PrepareCalcCache::clearNetCache();
|
|
|
// 清空相关表数据
|
|
|
$this->clearTableData();
|
|
|
+ $this->_updatePercent(15);
|
|
|
$t2 = microtime(true);
|
|
|
echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
|
|
|
// 计算月奖,才需要向缓存中加入按推荐深度的所有用户
|
|
|
@@ -157,20 +167,19 @@ class PreparePerfCalc {
|
|
|
|
|
|
$t9 = microtime(true);
|
|
|
echo('本月业绩入库完成,耗时:' . round($t9 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
|
|
|
- $this->_updatePercent(95);
|
|
|
|
|
|
-
|
|
|
- // 通过插入到perforder中的业绩订单数据,生成此业绩期活跃用户数据
|
|
|
- // $this->loopWriteActiveUser();
|
|
|
$this->_updatePercent(100);
|
|
|
$t10 = microtime(true);
|
|
|
-
|
|
|
+
|
|
|
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());
|
|
|
return false;
|
|
|
}
|
|
|
+
|
|
|
+ $this->endCalcTask();
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -181,20 +190,6 @@ class PreparePerfCalc {
|
|
|
public function endCalcTask() {
|
|
|
// 更新结算状态
|
|
|
$this->setCalcStatus('end');
|
|
|
- //如果自动结算
|
|
|
- if(boolval($this->_sysConfig['autoCalcPeriod']['VALUE'])){
|
|
|
- $period = Period::instance();
|
|
|
- if($period->isLastSent($this->_periodNum)) {
|
|
|
- $bonusCalc = BonusCalc::instance();
|
|
|
- $asyncResult = $bonusCalc->calcStep($this->_periodNum);
|
|
|
- if ($asyncResult) {
|
|
|
- $bonusCalc->endCalcTask();
|
|
|
- } else {
|
|
|
- $bonusCalc->errorCalcTask();
|
|
|
- }
|
|
|
- return $asyncResult;
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -214,14 +209,49 @@ class PreparePerfCalc {
|
|
|
*/
|
|
|
public function setCalcStatus($type) {
|
|
|
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]);
|
|
|
+ PeriodPrepare::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]);
|
|
|
+ PeriodPrepare::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]);
|
|
|
+ PeriodPrepare::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FAIL, 'PERFED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public function checkCanCalcPref() {
|
|
|
+ // 先判断时间,现在是周几,如果不是周日,则直接结束
|
|
|
+ $w = date('w',time());
|
|
|
+ if ($w != '0') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 判断是否是周日0点
|
|
|
+ $h = date('H',time());
|
|
|
+ if ($h !=0 ) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 判断此结算周期是否是月结算节点
|
|
|
+ $periodObj = Period::instance();
|
|
|
+ $periodDataArr = $periodObj->setPeriodNum(null);
|
|
|
+ if ($periodDataArr['IS_MONTH'] != 1) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 判断此结算周期是否已经进行业绩计算,如果已进行或者进行过,则不能继续执行
|
|
|
+ $prepare = PeriodPrepare::find()
|
|
|
+ ->select('IS_MONTH, IS_PERFING,IS_PERFED')
|
|
|
+ ->where(
|
|
|
+ 'PERIOD_NUM=:PERIOD_NUM',
|
|
|
+ [
|
|
|
+ 'PERIOD_NUM'=>$periodDataArr['PERIOD_NUM']
|
|
|
+ ])
|
|
|
+ ->asArray()
|
|
|
+ ->one();
|
|
|
+ // 如果正在结算或者已计算完,则不进行计算,因为预计计算是定时任务每5秒扫一次,所以不能再触发预计算业绩
|
|
|
+ if ($prepare['IS_PERFING'] == 1 || $prepare['IS_PERFED'] == 1) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 初始化结算任务
|
|
|
* @throws \yii\db\Exception
|
|
|
@@ -757,11 +787,11 @@ class PreparePerfCalc {
|
|
|
if (!$this->_isCalcMonth) {
|
|
|
return true;
|
|
|
}
|
|
|
- // 因为预业绩计算,是从另外一个表记录的,所以需要再获取prepare表数据一起sum上
|
|
|
- // 干脆正常的时候,也同步维护此per表中.当不是结算月的时候才维护进来,是结算月的话,就不维护进来了.
|
|
|
+ // 正常计算的时候,维护业绩到prepare表中,预计计算月业绩的时候,只需要插入定时任务的结算那期的业绩即可.
|
|
|
+ // 这里直接获取预计计算业绩单数据
|
|
|
echo sprintf("时间:[%s]月业绩,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
|
|
|
// 从缓存列表里面从底层往上倒序获取会员
|
|
|
- $allData = PerfPeriod::findUseDbCalc()
|
|
|
+ $allData = PerfPeriodPrepare::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,
|
|
|
@@ -882,8 +912,8 @@ class PreparePerfCalc {
|
|
|
$insertDataMonthPerf[] = $this->nowMonthPerfData($userId);
|
|
|
unset($userId);
|
|
|
}
|
|
|
-
|
|
|
- PerfMonth::batchInsert($insertDataMonthPerf);
|
|
|
+ // 月预计计算业绩插入数据
|
|
|
+ PerfMonthPrepare::batchInsert($insertDataMonthPerf);
|
|
|
unset($insertDataMonthPerf, $allData);
|
|
|
return $this->loopWriteMonthPerf($offset + $this->_limit);
|
|
|
}
|
|
|
@@ -1078,4 +1108,12 @@ class PreparePerfCalc {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新百分比并发送
|
|
|
+ * @param $percent
|
|
|
+ */
|
|
|
+ private function _updatePercent($percent) {
|
|
|
+ // 把数据写入数据库中
|
|
|
+ PeriodPrepare::updateAll(['PERF_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
|
|
|
+ }
|
|
|
}
|