<?php
declare (strict_types=1);

namespace app\model\system;

use library\basic\BaseModel;
use library\utils\Arr;
use think\Model;

/**
 * @mixin \think\Model
 */
class RolePath extends BaseModel
{

    private $_list = [];

    /**
     * 获取分类数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArMenu($show = 0)
    {
        $menus = $this
            ->field("rp.*,(select title from table_admin_menu where id = rp.menu_id) as menu_title")
            ->alias("rp")
            ->when(!empty($show), function ($query) {
                $query->where('is_show', 1);
            })
            ->order("seq", "desc")
            ->select();
        $data  = [];
        foreach ($menus as $item) {
            $data[] = $item->getData();
        }
        $menuAr = Arr::getTree($data);
        return $menuAr;
    }

    /**
     * 删除栏目
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delMenu($id)
    {
        $mAr = $this->where('pid', $id)->select()->toArray();
        foreach ($mAr as $v) {
            $this->delMenu($v['id']);
        }
        $this->where('id', $id)->delete();
    }


    /**
     * 设置显示状态
     * @param $id 设置状态
     * @param $status
     * @return bool
     */
    public function setStatus($id, $status)
    {
        self::beginTrans();
        try {
            $this->where('id', $id)->save(['is_show' => $status]);
            $this->where('pid', $id)->save(['is_show' => $status]);
            self::commitTrans();
            return true;
        } catch (DbException $db) {
            self::rollbackTrans();
            return false;
        }

    }

    /**
     * 获取全部数据【select】
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAll()
    {
        $this->_list = $this->order("seq","desc")->select()->toArray();
        return $this->_list;
    }
    /**
     * 根据id 获取数据
     * @param $id
     */
    public function findData($id){
        foreach ($this->_list as $v) {
            if($id == $v['id']) return $v;
        }
        return null;
    }

    /**
     * 获取数据
     */
    public function getRoleData()
    {
        return $this->getChild(0);
    }

    /**
     * 获取关联属性
     * @param $pid
     */
    public function getMenus($id) : int
    {
        foreach ($this->_list as $v) {
            if($v['id'] == $id) return $v['menu_id'];
        }
        return  0;
    }

    /**
     * 下一集合
     * @param $pid
     */
    public function getChild($pid)
    {
        $tAr = [];
        foreach ($this->_list as $v) {
            if ($v['pid'] == $pid) {
                $d             = $v;
                $d['children'] = $this->getChild($v['id']);
                $tAr[]         = $d;
            }
        }

        return $tAr;
    }

    /**
     * 获取model数据
     * @param $moule
     */
    public static function getMoule($str){
        $module = !empty($str) ? json_decode($str,true) : [];
        if(empty($module)) return [];
        $idAr = [];
        foreach ($module as $k => $v) {
            $idAr[] = str_replace('check_','',$k);
        }
        $model = new static();
        $model->getAll();
        $data = [];
        foreach ($idAr as $v) {
            $d = $model->findData($v);
            if(!empty($d)) $data[] = $d;
        }
        //获取关联栏目
        foreach ($data as &$v) {
            $v['path_menu_id'] = $model->getMenus($v['pid']);
        }
        return $data;
    }

}
