Преглед изворни кода

Merge branch 'feature/2175-team-perf' into dev

theo пре 3 година
родитељ
комит
f073c0b198

+ 1 - 1
backendApi/modules/v1/controllers/CalcController.php

@@ -320,4 +320,4 @@ class CalcController extends BaseController
     }
 
 
-}
+}

+ 2 - 1
common/components/SwooleAsyncTimer.php

@@ -50,9 +50,10 @@ class SwooleAsyncTimer extends SwooleAsyncTimerComponent implements SocketInterf
         // 自动封期
 //        AutoClosePeriod::instance()->autoClose();
         // 自动执行任务队列中的任务
-        Queue::instance()->consumeTask();
+//        Queue::instance()->consumeTask();
         // 实时监听计算系统修改的period表字段
 //        CalcConsole::listenCalcPeriod();
+        CalcConsole::autoPrePerf();
         CalcConsole::listenAutoPerfPeriod();
         return true;
     }

+ 37 - 0
common/helpers/bonus/Calc/CalcConsole.php

@@ -3,8 +3,11 @@
 namespace common\helpers\bonus\Calc;
 
 
+use common\helpers\Form;
 use common\models\CalcRecord;
+use common\models\forms\PeriodForm;
 use common\models\Period;
+use Yii;
 
 class CalcConsole extends BaseBusiness
 {
@@ -96,6 +99,40 @@ if (empty($period)){
         return true;
     }
 
+    public static function autoPrePerf(){
+        $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'] ){
+            $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);
+            //设置自动计算标识
+            Period::updatePeriodIsAutoExec($periodNum, Period::MANUAL_EXEC_CALC);
+            //记录开始计算的时间
+            Period::updateAll(['START_EXEC_TIME' => time()], ['PERIOD_NUM' => $periodNum]);
+            $formModel = new PeriodForm();
+            $formModel->scenario = 'autoPerf';
+
+            if($formModel->load(['periodNum'=>$periodNum], '') && $formModel->autoPrePerf()) {
+                echo('自动计算已开始,请等待'.PHP_EOL);
+                CalcRecord::record($periodNum, '第' . $periodNum . '期,定时计算开始');
+            } else {
+                echo('自动计算未开始'.PHP_EOL);
+                CalcRecord::record($periodNum, '第' . $periodNum  . '期,定时计算未开始');
+            }
+            return;
+        }else{
+            return;
+        }
+    }
+
     public static function listenAutoPerfPeriod()
     {
         $db        = self::CALC_DB_NAME;

+ 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