<?php


namespace console\controllers;


use common\models\LwtCompanyUserRegister;
use common\models\LwtPhoneMessageList;
use common\util\LwtApiDataSave;
use common\util\PhoneMessage;
use yii\console\Controller;

class LwtController extends Controller
{
    public function actionAutoRunRegisterData()
    {
        set_time_limit(0);
        $nextDayTime = strtotime(date('Y-m-d', strtotime(' +1 day')));
        $range = $nextDayTime - time();
        if ($range <= 300) {
            echo "每日最后5分钟不跑数据";
            return;
        }
        $continue = true;
        /** @var  LwtCompanyUserRegister $LwtCompanyUserRegister */
        $LwtCompanyUserRegister = LwtCompanyUserRegister::findOne(["state" => 1]);
        if ($LwtCompanyUserRegister) {
            if (time() - $LwtCompanyUserRegister->refresh_time >= 1200) {
                if ($LwtCompanyUserRegister->is_send == 0) {
                    //查询超过了20分钟，发短信告知
                    PhoneMessage::saveMsg($LwtCompanyUserRegister->company_name,"查询超过20分钟");
                    //
                    $LwtCompanyUserRegister->is_send = 1;
                    $LwtCompanyUserRegister->msg = "查询超过20分钟";
                    $LwtCompanyUserRegister->save();
                }
            } else {
                $continue = false;
            }
        }
        if ($continue) {
            $exist = false;
            $LwtCompanyUserRegister = LwtCompanyUserRegister::findOne(["state" => 0]);
            if ($LwtCompanyUserRegister) {
                $LwtCompanyUserRegister->state = 1;
                $LwtCompanyUserRegister->refresh_time = time();
                $LwtCompanyUserRegister->save();
                $exist = true;
            } else {
                $time = time() - 3600 * 4;
                $LwtCompanyUserRegister = LwtCompanyUserRegister::find()->where(["<", "refresh_time", $time])->andWhere(["state" => 4])->one();
                if ($LwtCompanyUserRegister) {
                    $exist = true;
                } else {
                    $time = time() - 86400;
                    $LwtCompanyUserRegister = LwtCompanyUserRegister::find()->where(["<", "update_time", $time])->andWhere(["state" => 2])->one();
                    if ($LwtCompanyUserRegister) {
                        $exist = true;
                    }
                }
            }
            if ($exist) {
                $LwtCompanyUserRegister->state = 1;
                $LwtCompanyUserRegister->refresh_time = time();
                $LwtCompanyUserRegister->is_send = 0;
                $LwtCompanyUserRegister->msg = "";
                $LwtCompanyUserRegister->save();
                try {
                    LwtApiDataSave::getAllData($LwtCompanyUserRegister->credit_code, $LwtCompanyUserRegister->company_name);
                    if($LwtCompanyUserRegister->is_first == 0){
                        //第一次查询成功，发送短信告知
                        PhoneMessage::saveMsg($LwtCompanyUserRegister->company_name,"数据获取成功");
                        //更改状态
                        $LwtCompanyUserRegister->is_first = 1;
                    }
                    $LwtCompanyUserRegister->state = 2;
                    $LwtCompanyUserRegister->update_time = time();
                    $LwtCompanyUserRegister->save();
                } catch (\Exception $e) {
                    if ($e->getMessage() == "Initializing") {
                        $LwtCompanyUserRegister->state = 4;
                    } else {
                        $LwtCompanyUserRegister->state = 3;
                        //查询超过了20分钟，发短信告知
                        PhoneMessage::saveMsg($LwtCompanyUserRegister->company_name,"数据获取失败");
                        //
                        $LwtCompanyUserRegister->is_send = 1;
                    }
                    $LwtCompanyUserRegister->update_time = time();
                    $LwtCompanyUserRegister->msg = $e->getMessage();
                    $LwtCompanyUserRegister->save();
                }
            }
        }
    }

    public function actionGetData()
    {
        $msg = [
            "creditCode" => "91310000329555773R",
            "companyName" => "上海蔚来汽车有限公司",
        ];
        LwtApiDataSave::getAllData($msg["creditCode"], $msg["companyName"]);
    }

    protected function LwtApiDataSave()
    {
        $msg = [
            "creditCode" => "91310000329555773R",
            "companyName" => "上海蔚来汽车有限公司",
        ];
        return LwtApiDataSave::AddScanSaveReturnDataAndSave($msg["creditCode"], $msg["companyName"]);
    }

    protected function GetTopDomainReturnDataAndSave()
    {
        $msg = [
            "scanId" => "15855142931144704",
            "assetName" => "",
        ];
        return LwtApiDataSave::GetTopDomainReturnDataAndSave($msg["scanId"], $msg["assetName"]);
    }

    protected function GetSecondDomainReturnDataAndSave()
    {
        $msg = [
            "scanId" => "15855142931144704",
            "topName" => "niostg.com",
            "assetName" => "",
        ];
        return LwtApiDataSave::GetSecondDomainReturnDataAndSave($msg["scanId"], $msg["topName"], $msg["assetName"]);
    }

    protected function GetAssetIpReturnDataAndSave()
    {
        $msg = [
            "scanId" => "15855142931144704",
            "assetName" => "",
            "position" => "",
        ];
        return LwtApiDataSave::GetAssetIpReturnDataAndSave($msg["scanId"], $msg["assetName"], $msg["position"]);
    }
}