<?php


namespace common\libs\taskQueue;


use common\helpers\Cache;
use common\helpers\Date;
use common\helpers\DingTalk;
use common\helpers\LoggerTool;
use common\models\forms\HistoryBonusForm;
use common\models\TaskQueue;
use SebastianBergmann\CodeCoverage\Report\PHP;

class TaskFunc
{
    /**
     * 初始化自动备份余额任务
     */
    public static function initAutoBakBalance(){
        // 查看数据库中是否存在未开始的备份奖金表的任务，如果没有就添加一个新任务
        if(!TaskQueue::find()->where('TYPE=:TYPE AND CONTENT=:CONTENT AND STARTED_AT>:STARTED_AT', [':TYPE'=>Queue::TYPE_FUNC, ':CONTENT'=>TaskFunc::class.'::autoBakBonusTable', ':STARTED_AT'=>Date::nowTime()])->asArray()->exists()){
            // 获取站点配置中的备份时间
            $config = Cache::getSystemConfig();
            $backupWeekDate = (int)$config['backupWeekDate']['VALUE'];
            $backupTime = (int)$config['backupTime']['VALUE'];
            Queue::instance()->addTask(Queue::TYPE_FUNC, TaskFunc::class.'::autoBakBonusTable', [], Queue::LOOP_TYPE_WEEK, $backupWeekDate.','.$backupTime.':00', 0);
        }
    }

    /**
     * 自动备份奖金表
     * @throws \yii\base\InvalidConfigException
     *
     */
    public static function autoBakBonusTable(){
        $form = new HistoryBonusForm();
        $form->taskAutoBackup();
    }

    /**
     * 测试队列任务
     */
    public static function testTaskQueue(){
        print_r('执行到测试任务'.PHP_EOL);
    }

    /**
     * 初始化自动发送钉钉提醒任务
     */
//    public static function initAutoSendDingTalk() {
//        // 查看数据库中是否存在未开始的任务，如果没有就添加一个新任务
//        if(!TaskQueue::find()->where('TYPE=:TYPE AND CONTENT=:CONTENT AND STARTED_AT>:STARTED_AT', [':TYPE'=>Queue::TYPE_FUNC, ':CONTENT'=>TaskFunc::class.'::autoSendDingTalkTable', ':STARTED_AT'=>Date::nowTime()])->asArray()->exists()){
//            // 获取站点配置中的备份时间
//            $config = Cache::getSystemConfig();
//            $sendDingTalkTime = $config['sendDingTalkTime']['VALUE'];
//            Queue::instance()->addTask(Queue::TYPE_FUNC, TaskFunc::class.'::autoSendDingTalkTable', [], Queue::LOOP_TYPE_DAY, $sendDingTalkTime, 0);
//        }
//    }

    /**
     * 自动送钉钉提醒
     */
//    public static function autoSendDingTalkTable() {
//        $ip = 'https://fapi.ekhkad.com';
////        $ip = 'http://16.163.228.151:8013';
//        $curl = curl_init();
//        curl_setopt($curl, CURLOPT_URL, $ip . '/v1/site/send-notice');
//        curl_setopt($curl, CURLOPT_TIMEOUT, 5000);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//        $res = curl_exec($curl);
//        if ($res) {
//            curl_close($curl);
//            LoggerTool::info($res);
//        } else {
//            $error = curl_errno($curl);
//            curl_close($curl);
//            LoggerTool::error($error);
//        }
//    }
}