<?php


namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Validate;
use think\Request;
use think\facade\Cache;

class Exec extends Base
{
    public function list()
    {
        $post = $this->post;
        $token = isset($post['token']) ? trim($post['token']) : "";
        if ($token == "") {
            return error_show(101, 'token不能为空');
        }
        $where = ['type' => 0];
        $page = isset($post['page']) ? intval($post['page']) : 1;
        $size = isset($post['size']) ? intval($post['size']) : 10;
        $count = Db::name("exec")->where($where)->count();
        $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
        $page = $page >= $total ? intval($total) : $page;
        $list = Db::name("exec")->where($where)->page($page, $size)->select();
        return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
    }

    public function nowlist()
    {

        $param = $this->request->only(['token', 'page' => 1, 'size' => 15, 'type' => 1], 'post', 'trim');

        $val = Validate::rule([
            'token' => 'require',
            'page' => 'require|number',
            'size' => 'require|number',
            'type' => 'require|number',
        ]);

        if (!$val->check($param)) return error_show(1005, $val->getError());

        $count = Db::name("exec")
            ->where('type', $param['type'])
            ->count('id');

        $list = Db::name("exec")
            ->where('type', $param['type'])
            ->page($param['page'], $param['size'])
            ->order('id')
            ->select()
            ->toArray();

        return app_show(0, "获取成功", ["list" => $list, "count" => $count]);
    }

    public function save()
    {
        $post = $this->post;
        $token = isset($post['token']) ? trim($post['token']) : "";
        if ($token == "") {
            return error_show(101, 'token不能为空');
        }
        $userinfo = GetUserInfo($token);
        if (!isset($userinfo['code']) || $userinfo['code'] != 0) {
            return error_show(101, '未能获取用户信息');
        }
        $start = isset($post['start']) && $post['start'] != "" ? $post['start'] . " 00:00:00" : '';
        $end = isset($post['end']) && $post['end'] != "" ? $post['end'] . " 23:59:59" : '';
        $id = isset($post['id']) && $post['id'] !== '' ? intval($post['id']) : "";
        if ($id == "") {
            return error_show(1004, "参数id不能为空");
        }
        $info = Db::name("exec")->where(["id" => $id])->find();
        if (empty($info)) {
            return error_show(1004, "未找到下载数据");
        }
     if($info['type']==1){
	        $num = Db::name("exec")->where(["type" =>1,"status"=>1])->where("id","<>",$id)->count();
	        if($num>0){
		        return error_show(1004, "已有脚本在运行");
	        }
        }
        if ($info['start'] == $start && $info['end'] == $end) {
            return error_show(1004, "数据已提交");
        }
        if ($start != "") {
            $info['start'] = $start;
        }
        if ($end != "") {
            $info['end'] = $end;
        }
        if (is_file(root_path() . 'public' . $info['down_url'])) {
            @unlink(root_path() . "public" . $info['down_url']);
        }
        $info['status'] = 1;
        $info['apply_id'] = $userinfo['data']['id'];
        $info['apply_name'] = $userinfo['data']['nickname'];
        $info['down_url'] = '';
        $info['remark'] = '';
        $info['updatetime'] = date("Y-m-d H:i:s");
        $info['addtime'] = date("Y-m-d H:i:s");
        $info['expiretime'] = date("Y-m-d H:i:s", time() + 7 * 24 * 3600);
        $up = Db::name("exec")->save($info);
        $redis = Cache::store("redis")->handler()->lPush("cxreport", json_encode($info));
        return $up ? app_show(0, "编辑成功") : error_show(1004, "编辑失败");
    }

    public function down()
    {
        $post = $this->post;
        $token = isset($post['token']) ? trim($post['token']) : "";
        if ($token == "") {
            return error_show(101, 'token不能为空');
        }
        $id = isset($post['id']) && $post['id'] !== '' ? intval($post['id']) : "";
        if ($id == "") {
            return error_show(1004, "参数id不能为空");
        }
        $info = Db::name("exec")->where(["id" => $id])->find();
        if (empty($info)) {
            return error_show(1004, "未找到下载数据");
        }
                if($info['type']==1){
	        $num = Db::name("exec")->where(["type" =>1,"status"=>1])->count();
	        if($num>0){
		        return error_show(1004, "已有脚本在运行");
	        }
        }
//        $effetc = VerifyTokens($token);
//        if(!empty($effetc) && $effetc['code']!=0){
//            return error_show($effetc['code'],$effetc['message']);
//        }
        $userinfo = GetUserInfo($token);
        if (!isset($userinfo['code']) || $userinfo['code'] != 0) {
            return error_show(101, '未能获取用户信息');
        }
        $info['status'] = 1;
        $info['down_url'] = '';
        $info['remark'] = '';
        $info['apply_id'] = $userinfo['data']['id'];
        $info['apply_name'] = $userinfo['data']['nickname'];
        $info['expiretime'] = date("Y-m-d H:i:s", time() + 7 * 24 * 3600);
        $info['updatetime'] = date("Y-m-d H:i:s");
        $info['addtime'] = date("Y-m-d H:i:s");
        $up = Db::name("exec")->save($info);
        $redis = Cache::store("redis")->handler()->lPush("cxreport", json_encode($info));
        return $up ? app_show(0, "编辑成功") : error_show(1004, "编辑失败");
    }
}