<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use think\Request;
use think\App;
use think\facade\Db;

//日志
class Log extends Base
{


    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $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";
        $condition = [['type',"=",1]];
        $count = Db::name("action_log")->where($condition)->count();
        $total = ceil($count/$size);
        $page = $page >= $total ? intval($total) : $page;
        $list =Db::name("action_log")->where($condition)->page($page,$size)->order("addtime desc")->select();
        return app_show(0,"获取成功",['list'=>$list,"count"=>$count]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {
        $id = isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']) :"";
        if($id===""){
            return error_show(1004,"参数 id 不能为空");
        }
        $info = Db::name("action_log")->where(["id"=>$id])->find();
        if(empty($info)){
            return error_show(1004,"未找到数据");
        }
        return app_show(0,"获取成功",$info);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function msglist()
    {
        $param = $this->request->only(['token' => '', 'orderCode' => '', 'page' => 1, 'size' => 10, 'order_type' => '', 'is_read' => ''], 'post', 'trim');
        $apply_id = GetUserInfo($param['token']);
        if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "用户数据不存在");

        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : 0;
        $condition = [['om.uid', '=', $rm]];//自己看到自己的消息
        if ($param['orderCode'] != "") $condition[] = ["om.orderCode", "like", "%{$param['orderCode']}%"];
        if ($param['order_type'] != "") $condition[] = ["om.order_type", "=", $param['order_type']];
        if ($param['is_read'] !== "") $condition[] = ["om.is_read", "=", $param['is_read']];

        $count = Db::name("order_msg")
            ->alias('om')
            ->where($condition)
            ->count();

        $total = ceil($count / $param['size']);
        $page = $param['page'] >= $total ? $total : $param['page'];

        $list = Db::name("order_msg")
            ->alias('om')
            ->field('om.*,ap.order_name,p.process_name')
            ->where($condition)
            ->leftJoin('action_process ap', 'ap.order_type=om.order_type AND ap.order_process=om.order_status')
            ->leftJoin('process p', 'p.id=ap.pid')
            ->page(intval($page), intval($param['size']))
            ->order("om.addtime desc")
            ->select()
            ->toArray();

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

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        $id = isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']) :"";
        if($id===""){
            return error_show(1004,"参数 id 不能为空");
        }
        $info = Db::name("order_msg")->where(["id"=>$id])->find();
        if(empty($info)){
            return error_show(1004,"未找到数据");
        }
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        $apply_id = GetUserInfo($token);
        if (empty($apply_id) || $apply_id['code'] != 0) {
            return error_show(1002, "用户数据不存在");
        }
        $type = \think\facade\Config::get("order");
        $info['order_type_cn']=$type["order_type"][$info['order_type']];
        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
        if($rm==$info['uid'] && $info['is_read']==0){
            Db::name("order_msg")->where(["id"=>$id])->save(["is_read"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
        }
        return app_show(0,"获取成功",$info);
    }

    public function limit()
    {
        $id = isset($this->post['id'])&&$this->post['id']!=="" ? $this->post['id'] :[];
        if(empty($id)){
            return error_show(1004,"参数 id 不能为空");
        }
        $info = Db::name("order_msg")->where(["id"=>$id])->select()->toArray();
        if(empty($info)){
            return error_show(1004,"未找到数据");
        }
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        $apply_id = GetUserInfo($token);
        if (empty($apply_id) || $apply_id['code'] != 0) {
            return error_show(1002, "用户数据不存在");
        }
        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
        $read= Db::name("order_msg")->where(["id"=>$id,"uid"=>$rm])->save(["is_read"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
        return $read?app_show(0,"已读成功"):app_show(1003,"已读失败");
    }
    public function allread()
    {
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        $apply_id = GetUserInfo($token);
        if (empty($apply_id) || $apply_id['code'] != 0) {
            return error_show(1002, "用户数据不存在");
        }
        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
        $info = Db::name("order_msg")->where(["uid"=>$rm,"is_read"=>0])->select()->toArray();
        if(empty($info)){
            return error_show(1004,"暂无未读数据");
        }
        $read=Db::name("order_msg")->where(["uid"=>$rm,"is_read"=>0])->save(["is_read"=>1,"updatetime"=>date("Y-m-d 
        H:i:s")]);
        return $read?app_show(0,"全部已读成功"):app_show(1003,"全部已读失败");
    }
}