<?php
namespace app\admin\server\menu;

use app\admin\model\SystemMenu;
use app\admin\model\SystemRole;
use support\Db;
use support\Redis;

class MenuServer
{
    /**
     * Notes:获取菜单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getMenuList(string $keywords)
    {
        if (!empty($keywords)){
            $list =  SystemMenu::getMenuList($keywords);
        }else{
            $list = SystemMenu::getMenus(0);
            if (!empty($list)){
                for ($i=0;$i<count($list);$i++){
//                    if ($list[$i]['menu_is_show'] == 1){
//                        $list[$i]['menu_is_show'] = true;
//                    }else{
//                        $list[$i]['menu_is_show'] = false;
//                    }
                    $list[$i]['children'] = SystemMenu::getMenus($list[$i]['menu_id']);
                    if (!empty($list[$i]['children'])){
                        for ($s=0;$s<count($list[$i]['children']);$s++){
//                            if ($list[$i]['children'][$s]['menu_is_show'] == 1){
//                                $list[$i]['children'][$s]['menu_is_show'] = true;
//                            }else{
//                                $list[$i]['children'][$s]['menu_is_show'] = false;
//                            }
                            $list[$i]['children'][$s]['children'] = SystemMenu::getMenus($list[$i]['children'][$s]['menu_id']);
//                            if (!empty($list[$i]['children'][$s]['children'])){
//                                for ($j=0;$j<count($list[$i]['children'][$s]['children']);$j++){
//                                    if ($list[$i]['children'][$s]['children'][$j]['menu_is_show'] == 1){
//                                        $list[$i]['children'][$s]['children'][$j]['menu_is_show'] = true;
//                                    }else{
//                                        $list[$i]['children'][$s]['children'][$j]['menu_is_show'] = false;
//                                    }
//                                }
//                            }
                        }
                    }
                }
            }
        }
        return $list;
    }

    /**
     * Notes:修改角色
     * @param string $menu_name
     * @param int $menu_id
     * @param int $menu_pid
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function updateMenu($menu_id, $menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show)
    {
        SystemMenu::affairBegin();
        try {
            $where = [];
            $where['menu_id'] = $menu_id;
            $data = [];
            $data['menu_name'] = $menu_name;
            $data['level'] = 1;
            if (!empty($menu_pid)){
                $data['level'] = SystemMenu::getParentValue($menu_pid) + 1;
            }
            $data['menu_pid'] = $menu_pid;
            $data['menu_path'] = $menu_path;
            $data['menu_icon'] = $menu_icon;
            $data['menu_route'] = $menu_route;
            $data['menu_params'] = $menu_params;
            $data['menu_sort'] = $menu_sort;
            $data['menu_is_menu'] = $menu_is_menu;
            $data['menu_is_show'] = $menu_is_show;
            $result = SystemMenu::where($where)->update($data);
            if ($result !== false){
                SystemMenu::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemMenu::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除角色
     * @param int $menu_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function delMenu($menu_id)
    {
        SystemMenu::affairBegin();
        try {
            $where = [];
            $where['menu_id'] = $menu_id;
            $data['menu_status'] = 2;
            $result = SystemMenu::where($where)->update($data);
            if (!empty($result)){
                SystemMenu::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemMenu::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加菜单
     * @param string $menu_name
     * @param int $menu_pid
     * @return int
     * User: ZQ
     * Date: 2022/9/14
     */
    public static function insertMenu($menu_name, $menu_pid, $menu_path, $menu_icon, $menu_route, $menu_params, $menu_sort, $menu_is_menu, $menu_is_show)
    {

        SystemMenu::affairBegin();
        try {
            $data = [];
            $data['menu_name'] = $menu_name;
            $data['level'] = 1;
            if (!empty($menu_pid)){
                $data['level'] = SystemMenu::getParentValue($menu_pid) + 1;
            }
            $data['menu_pid'] = $menu_pid;
            $data['menu_path'] = $menu_path;
            $data['menu_icon'] = $menu_icon;
            $data['menu_route'] = $menu_route;
            $data['menu_params'] = $menu_params;
            $data['menu_sort'] = $menu_sort;
            $data['menu_is_menu'] = $menu_is_menu;
            $data['menu_is_show'] = $menu_is_show;
            $result =  SystemMenu::insertGetId($data);
            if (!empty($result)){
                SystemMenu::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemMenu::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询角色
     * @param int $menu_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function menuInfo($menu_id)
    {
        $SystemMenu = new SystemMenu();
        $where = [];
        $where['menu_id'] = $menu_id;
        $result = SystemMenu::where($where)->first();
        $result['menu_is_menu'] = (int)$result['menu_is_menu'];
        $result['menu_pid_arr'] = $SystemMenu->getParentMenuId($result['menu_pid']);
//        $result['menu_is_show'] = ($result['menu_is_show'] == 1 ? true : false);
        return $result;
    }

    /**
     * Notes:获取菜单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function menuLevel()
    {
        $list = SystemMenu::getLevel(0);
        if (!empty($list)){
            for ($i=0;$i<count($list);$i++){
                $list[$i]['children'] = SystemMenu::getLevel($list[$i]['value']);
                if (!empty($list[$i]['children'])){
                    for ($s=0;$s<count($list[$i]['children']);$s++){
                        $list[$i]['children'][$s]['children'] = SystemMenu::getLevel($list[$i]['children'][$s]['value']);
                    }
                }
            }
        }

        return $list;
    }
    /**
     * Notes:获取菜单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function secondLevel()
    {
        $list = SystemMenu::getLevel(0);
        if (!empty($list)){
            for ($i=0;$i<count($list);$i++){
                $list[$i]['children'] = SystemMenu::getLevel($list[$i]['value']);
            }
        }

        return $list;
    }

    /**
     * Notes:修改角色
     * @param int $menu_is_show
     * @param int $menu_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateStatus($menu_id, $menu_is_show)
    {
        SystemMenu::affairBegin();
        try {
            $where = [];
            $where['menu_id'] = $menu_id;
            $data = [];
            $data['menu_is_show'] = $menu_is_show;
            $result = SystemMenu::where($where)->update($data);
            if ($result !== false){
                SystemMenu::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemMenu::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:菜单列表
     * @param array $adminInfo
     * @return array
     * User: yym
     * Date: 2022/9/21
     */
    public static function getHomeMenuList(array $adminInfo)
    {
//        $redis_key = 'roles_' . $adminInfo['admin_roles'];
//        if(Redis::exists($redis_key))
//        {
//            $list = json_decode(Redis::get($redis_key), true);
//        }else{
            $menu_list_ids = array();
            if($adminInfo['admin_is_boos'] != 1)
            {
                $rules = SystemRole::getRuleList(explode(',', $adminInfo['admin_roles']));
                if(!empty($rules))
                {
                    foreach ($rules as $row)
                    {
                        $menu_list_ids = empty($menu_list_ids) ? explode(',', $row['role_rules']) : array_merge($menu_list_ids, explode(',', $row['role_rules']));
                    }
                }else{
                    $menu_list_ids = array(0);
                }
            }
            if(!empty($menu_list_ids))
            {
                $SystemMenu = new SystemMenu();
                //获取每个自己的父级
                foreach ($menu_list_ids as $item)
                {
                    if($item <= 0){
                        continue;
                    }
                    $menu_list_ids = array_merge($menu_list_ids, $SystemMenu->getParentPid($item));
                }
                $menu_list_ids = array_unique($menu_list_ids);
            }
            $list = SystemMenu::getHomeMenuList($menu_list_ids);
//            Redis::setEx($redis_key, 86400, json_encode($list));
//        }
        return $list;
    }
}
