<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
use think\facade\Db;
use app\admin\model\ActionLog;

//异常原因
class Result extends BaseController
{
    public $post = "";

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->post = $this->request->post();
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        if($token==""){
            return error_show(101,'token不能为空');
        }
        $effetc = VerifyTokens($token);
        if(!empty($effetc) && $effetc['code']!=0){
            return error_show($effetc['code'],$effetc['message']);
        }
    }

    public function list()
    {
        $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
        $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
        $where = ['is_del' => 0];
        $type = isset($this->post['type']) && $this->post['type'] !== "" ? intval($this->post['type']) : "";
        if ($type !== "") {
            $where['type'] = $type;
        }
        $count = Db::name("result_info")->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('result_info')->where($where)->page($page, $size)->order("addtime desc")->select();
        return app_show(0, "获取成功", ['list' => $list, 'count' => $count]);
    }

    /*新建*/
    public function create()
    {
        $result = isset($this->post['result']) && $this->post['result'] !== "" ? trim($this->post['result']) : "";
        if ($result == "") {
            return error_show(1002, "异常原因不能为空");
        }
        $desc = isset($this->post['result_desc']) && $this->post['result_desc'] !== "" ? trim($this->post['result_desc']) : "";
        if ($desc == "") {
            return error_show(1002, "异常描述不能为空");
        }
        $type = isset($this->post['type']) && $this->post['type'] !== "" ? intval($this->post['type']) : "";
        //$result_code = isset($this->post['result_code']) && $this->post['result_code'] !=="" ? intval($this->post['result_code']):"";
        $count = Db::name('result_info')->count();
        $str = sprintf("%04d", $count);
        $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "0";
        $data = [
            "result" => $result,
            "result_desc" => $desc,
            "result_code" => $str,
            "status" => $status,
            "type" => $type,
            "is_del" => 0,
            "updatetime" => date("Y-m-d H:i:s"),
            "addtime" => date("Y-m-d H:i:s")
        ];
        $cr = Db::name('result_info')->insert($data);
        $stn = ["order_code"=>$result,"status"=>$status,"action_remark"=>'',"action_type"=>"create"];
        ActionLog::logAdd($this->post['token'],$stn,"resign_info",$status,$stn);
        return $cr ? error_show(0, "添加成功") : error_show(1002, "添加失败");
    }

    /*查询*/
    public function selec()
    {
        $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
        if ($id == "") {
            return error_show(1002, "异常原因不存在");
        }
        $su = Db::name('result_info')->where(['id' => $id, 'is_del' => 0])->find();
        return app_show(0, "获取成功", $su);
    }

    /*编辑*/
    public function edit()
    {
        $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
        $sid = Db::name('result_info')->where(['id' => $id, 'is_del' => 0])->find();
        if ($sid == "") {
            return error_show(1002, "异常信息不存在");
        }
        $result = isset($this->post['result']) && $this->post['result'] !== "" ? trim($this->post['result']) : "";
        if ($result == "") {
            return error_show(1002, "异常内容不能为空");
        }
        $result_desc = isset($this->post['result_desc']) && $this->post['result_desc'] !== "" ? trim($this->post['result_desc']) : "";
        if ($result_desc == "") {
            return error_show(1002, "异常描述不能为空");
        }
        $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "0";
        $li = [
            "id" => $id,
            "result" => $result,
            "result_desc" => $result_desc,
            "is_del" => 0,
            "status" => $status,
            "updatetime" => date("Y-m-d H:i:s"),
        ];
        $stn = Db::name('result_info')->where(['is_del' => 0, 'id' => $id])->save($li);
        $st = ["order_code"=>"RKD","status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
        ActionLog::logAdd($this->post['token'],$st,"resign_info",$status,$st);
        return $stn ? error_show(0, "编辑成功") : error_show(1002, "编辑失败");
    }

    public function del()
    {
        $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
        $kid = Db::name('result_info')->where(['is_del' => 0, 'id' => $id])->find();
        if ($kid == false) {
            return error_show(1002, "异常信息不存在");
        }
        $back = Db::name('result_info')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date('Y-m-d H:i:s')]);
        if ($back) {
            $st = ["order_code"=>"RKD","status"=>0,"action_remark"=>'',"action_type"=>"delete"];
            ActionLog::logAdd($this->post['token'],$st,"resign_info",0,$st);
            return error_show(0, '删除成功');
        } else {
            return error_show(1002, '删除失败');
        }
    }

    public function statu()
    {
        $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
        if ($id == "") {
            return error_show(1004, "参数id 不能为空");
        }
        $pd = Db::name('result_info')->where(['id' => $id])->find();
        if (empty($pd)) {
            return error_show(1002, "未找到原因编码");
        }
        $de =  $pd['status'];
        $dp = $pd['status'] == 0 ? "1" : "0";
        $pd['status'] = $dp;
        $pd ['updatetime'] = date('Y-m-d H:i:s');
        $tn = Db::name('result_info')->save($pd);
        if ($tn) {
            $st = ["order_code"=>"RKD","status"=>$de,"action_remark"=>'',"action_type"=>"edit"];
            ActionLog::logAdd($this->post['token'],$st,"resign_info",$pd['status'],$st);
            return error_show(0, "状态更新成功");
        } else {
            return error_show(1002, "状态更新失败");
        }
    }
}