<?php

declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\BaseController;
use think\App;
use think\facade\Db;

class Role extends BaseController{

    public function __construct(App $app) {parent::__construct($app);}
    /**角色权限列表分页  page size
        * @return \think\response\Json|void
        * @throws \think\db\exception\DataNotFoundException
        * @throws \think\db\exception\DbException
        * @throws \think\db\exception\ModelNotFoundException
        */
    public function roleList(){
        $post=$this->post;
        $condition=[];
        isset($post['status'])&&$post['status']!=="" ? $condition[]=['a.status',"=",$post['status']]:"";
        isset($post['role_name'])&&$post['role_name']!=="" ? $condition[]=['a.role_name',"like","%".$post['role_name']."%"]:"";
        isset($post['level'])&&$post['level']!=="" ? $condition[]=["a.level","=",$post['level']]:"";
        $roleList = Db::name("role");
        $count =$roleList->alias("a")->where($condition)->count();
        $page = isset($post['page']) ? intval($post['page']) : 1;
        $size = isset($post['size']) ? intval($post['size']) : 10;
        $page >=ceil($count/$size) ? $page= (int)ceil($count/$size) :"";
        $list=$roleList->alias("a")->leftJoin("cfp_role_action t","a.id=t.role_id")->field("a.*,t.action_conllect as action,t.action_data")->where($condition)->page($page,$size)->select();

        return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
    }
    /** 角色新建
 * @role_name 角色名称
        * @level 角色等级
     * @action 功能权限
     * @action_data 操作权限
     * @private_data 私有权限
    * @return \think\response\Json|void
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
     */

    public function roleAdd(){
        $post=$this->post;
        $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
        if($rolename==""){
            return error_show(1002,"角色名称不能为空");
        }
        $isT=Db::name("role")->where(['role_name'=>$rolename])->find();
        if($isT){
            return error_show(1002,"角色名称已存在");
        }
        $level = isset($post['level']) ? intval($post['level']) : "";
        if($level==""){
            return error_show(1003,"角色等级不能为空");
        }
        $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : "";
        if($action==""){
            return error_show(1004,"功能权限不能为空");
        }
        $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): "";
        $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): "";
        Db::startTrans();
        try {
            $list = [
                "role_name"=>$rolename,
                "status"=>1,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s"),
                "level"=>$level,
            ];
            $role= Db::name("role")->insert($list,true);

            if($role>0){
                $role_action = [
                    "role_id"=>$role,
                    "action_conllect"=>$action,
                    "action_data"=>$data,
                    "private_data"=>$private_data,
                    "status"=>1,
                    "addtime"=>date("Y-m-d H:i:s"),
                    "updatetime"=>date("Y-m-d H:i:s"),
                ];
                $roleaction= Db::name("role_action")->insert($role_action,true);
                if($roleaction){
                    Db::commit();
                    return app_show(0,"新建成功");
                }else{
                    Db::rollback();
                    return app_show(1005,"权限录入失败");
                }
            }else{
                Db::rollback();
                return app_show(1006,"角色新建失败");
            }

        }catch (\Exception $e){
            Db::rollback();
            return error_show(1008,$e->getMessage());
        }
    }
    /**查询角色信息
     * @roleid 角色id
    * @return \think\response\Json|void
    * @throws \think\db\exception\DataNotFoundException
    * @throws \think\db\exception\DbException
    * @throws \think\db\exception\ModelNotFoundException
     */
    public function roleInfo(){
        $post=$this->post;
        $roleid= isset($post['roleid']) ? trim($post['roleid']) : "";
        if($roleid==""){
            return error_show(1001,'roleid不能为空');
        }
        $info =Db::name("role")->alias("a")->leftJoin("cfp_role_action t","a.id=t.role_id")->field("a.*,t.action_conllect,t.action_data,t.private_data")->where("a.id","=",$roleid)->find();
        if(!$info){
            return error_show(1002,"未找到对应的数据");
        }
        $info['action'] =explode(",",$info['action_conllect']);
        $info['action_data'] =$info['action_data']!=""?explode(",",$info['action_data']):"";
        $info['private_data'] =$info['private_data']!=""?explode(",",$info['private_data']) :"";
        return app_show(0,"获取成功",$info);
    }
    /** 角色信息编辑
         * @param role_name 角色名称
         * @param level 等级
         * @param action 功能权限
         * @param action_data 操作权限
         * @param roleid 角色id
         * @param private_data 私有权限
        * @return \think\response\Json|void
        * @throws \think\db\exception\DataNotFoundException
        * @throws \think\db\exception\DbException
        * @throws \think\db\exception\ModelNotFoundException
         */
    public function roleSave(){
        $post=$this->post;
        $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
        if($roleid==""){
            return error_show(1001,'roleid不能为空');
        }
        $info =Db::name("role")->where("id","=",$roleid)->find();
        if(!$info){
            return error_show(1002,"未找到对应的数据");
        }
        $rolename = isset($post['role_name']) ? trim($post['role_name']) : "";
        if($rolename==""){
            return error_show(1002,"角色名称不能为空");
        }
        $vers= Db::name("role")->where("id",'<>',$roleid)->where("role_name","=",$rolename)->find();

        if($vers){
            return error_show(1002,"角色名已存在");
        }
        $level = isset($post['level']) ? intval($post['level']) : "";
        if($level==""){
            return error_show(1003,"角色等级不能为空");
        }
        $action = isset($post['action'])&&is_array($post['action']) ? implode(",",$post['action']) : "";
        if($action==""){
            return error_show(1004,"功能权限不能为空");
        }
        $data = isset($post['action_data']) &&is_array($post['action_data'])?implode(",",$post['action_data']): "";
        $private_data = isset($post['private_data']) &&is_array($post['private_data'])?implode(",",$post['private_data']): "";
        Db::startTrans();
        try {
            $list = [
                "role_name"=>$rolename,
                "level"=>$level,
                "updatetime"=>date("Y-m-d H:i:s"),
                'id'=>$roleid
            ];
            $role= Db::name("role")->save($list);
            if($role){
                $role_action = [
                    "role_id"=>$roleid,
                    "action_conllect"=>$action,
                    "action_data"=>$data,
                    "private_data"=>$private_data,
                    "updatetime"=>date("Y-m-d H:i:s")
                ];
                $roleaction= Db::name("role_action")->where("role_id","=",$roleid)->update($role_action);
                if($roleaction){
                    Db::commit();
                    return app_show(0,"更新成功");
                }else{
                    Db::rollback();
                    return app_show(1005,"权限更新失败");
                }
            }else{
                Db::rollback();
                return app_show(1006,"角色更新失败");
            }

        }catch (\Exception $e){
            Db::rollback();
            return error_show(1008,$e->getMessage());
        }
    }

        /**
        * 角色状态更新
 * @roleid  角色id
 * @status  角色状态 0 /1
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function roleStatus(){
        $post=$this->post;
        $roleid= isset($post['roleid']) ? intval($post['roleid']) : "";
        if($roleid==""){
            return error_show(1001,'roleid不能为空');
        }
        $status = isset($post['status']) ? intval($post['status']) : "";
        if($status===""){
            return error_show(1001,'status不能为空');
        }
        if(!in_array($status,[0,1])){
            return error_show(1001,'status参数非法');
        }
        Db::startTrans();
        try {
            $dat=[
                'status'=>$status,'updatetime'=>date("Y-m-d H:i:s"),  'id'=>$roleid
            ];
            $re = Db::name("role")->save($dat);
            if($re){
                $dat2=[
                    'status'=>$status,'updatetime'=>date("Y-m-d H:i:s")
                ];
                $action =  Db::name("role_action")->where("role_id","=",$roleid)->update($dat2);
                if($action){
                    Db::commit();
                    return app_show(0,"状态更新成功");
                }
            }
            Db::rollback();
            return error_show(1003,"状态更新失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1002,$e->getMessage());
        }
    }

     /**
        * 获取所有角色
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function RoleAll(){
        $list =Db::name("role")->select();
        return app_show(0,"获取成功",$list);
    }
}