Browse Source

预计算,定时执行相关

theo 3 years ago
parent
commit
3ce43cac79
2 changed files with 49 additions and 11 deletions
  1. 14 11
      common/helpers/bonus/Calc/CalcConsole.php
  2. 35 0
      common/models/forms/PeriodForm.php

+ 14 - 11
common/helpers/bonus/Calc/CalcConsole.php

@@ -100,12 +100,17 @@ if (empty($period)){
     }
 
     public static function autoPrePerf(){
-        $nowTs = 1669778882;//time();
-        $currentPeriod = Period::find()->where('START_TIME< :NOW_TIME',['NOW_TIME'=>$nowTs])->where('END_TIME> :NOW_TIME',['NOW_TIME'=>$nowTs])->asArray()->one();
+        $nowTs = time();
+        $currentPeriod = Period::find()->where('START_TIME< :NOW_TIME',['NOW_TIME'=>$nowTs])->where('END_TIME>= :NOW_TIME',['NOW_TIME'=>$nowTs-6])->asArray()->one();
         $periodNum = $currentPeriod['PERIOD_NUM'];
 
-        if($nowTs+86400>$currentPeriod['END_TIME'] && !Period::isProcessing($periodNum)){
-            print_r("该预结算".PHP_EOL);
+        if($nowTs+86400>$currentPeriod['END_TIME'] ){
+            $nowMin = date("i", $nowTs);
+            $nowSec = date("s", $nowTs);
+            if(($nowMin!=0 || $nowSec >= 5 ) || Period::isProcessing($periodNum)){ // 当不是整点小时,当计算中,不计算
+                return;
+            }
+            print_r("该预结算".$nowMin.PHP_EOL.$nowSec);
             //设置计算进行中标识
             Period::updatePeriodIsProcessing($periodNum, Period::IS_PROCESSING);
             //设置自动计算标识
@@ -115,19 +120,17 @@ if (empty($period)){
             $formModel = new PeriodForm();
             $formModel->scenario = 'autoPerf';
 
-            if($formModel->load(['PERIOD_NUM'=>$periodNum], '') && $formModel->autoPerf()) {
-                echo('自动计算已开始,请等待');
+            if($formModel->load(['periodNum'=>$periodNum], '') && $formModel->autoPrePerf()) {
+                echo('自动计算已开始,请等待'.PHP_EOL);
+                CalcRecord::record($periodNum, '第' . $periodNum . '期,定时计算开始');
             } else {
-                echo('自动计算未开始');
-//                return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
+                echo('自动计算未开始'.PHP_EOL);
+                CalcRecord::record($periodNum, '第' . $periodNum  . '期,定时计算未开始');
             }
             return;
         }else{
             return;
         }
-//        if ($nowTs + 432000 <= $currentPeriodEnd || $currentPeriodEnd < $nowTs) {
-//            return static::notice(['user' => [],'team'=>[]]);
-//        }
     }
 
     public static function listenAutoPerfPeriod()

+ 35 - 0
common/models/forms/PeriodForm.php

@@ -7,6 +7,7 @@ use common\helpers\bonus\PerfCalc;
 use common\helpers\Cache;
 use common\helpers\Date;
 use common\components\Model;
+use common\components\SwooleAsyncTimer;
 use common\helpers\Form;
 use common\models\Period;
 use common\models\UserWallet;
@@ -14,6 +15,8 @@ use common\models\User;
 use common\models\UserStatusAudit;
 use common\models\Withdraw;
 use yii\base\Exception;
+use Yii;
+use yii\helpers\Json;
 
 /**
  * Login form
@@ -425,6 +428,38 @@ class PeriodForm extends Model
         return $model;
     }
 
+    /**
+     * 页面请求异步处理 自动计算
+     * @return string | null
+     */
+    public function autoPrePerf()
+    {
+        $settings      = \Yii::$app->params['swooleAsyncTimer'];
+        $bonusSettings = \Yii::$app->params['swooleBonusConfig'];
+        $settings      = array_merge($settings, $bonusSettings);
+
+        $params['handleUserId'] = null;
+        $params['handleUserName'] = null;
+        $params['periodNum'] = $this->periodNum;
+        $taskKey = Cache::setAsyncParams($params);
+        echo($taskKey.PHP_EOL);
+        $data = [
+            'data' => [
+                [
+                    'a' => 'calc/auto-perf',
+                    'p' => [$taskKey],
+                ]
+            ],
+        ];
+        try {
+            (new SwooleAsyncTimer())->async(Json::encode($data), $settings);
+        }catch (\Exception $e){
+            echo($e);
+            return null;
+        }
+        return true;
+    }
+
     /**
      * 页面请求异步处理 生成业绩单
      * @return string | null