<?php

namespace app\admin\api;

use app\admin\model\Role;
use app\admin\model\SysRelation;

/**
 * Description of RoleApi
 *
 * @author sgq
 */
class RoleApi {

    public static function getOne($id) {
        $res = Role::findOrEmpty($id);
        if ($res["pid"] > 0) {
            $res["pName"] = isNullOrEmpty(Role::where("id", "=", $res["pid"])->findOrEmpty()["name"]);
        }
        if ($res["deptid"] > 0) {
            $res["deptName"] = isNullOrEmpty(DeptApi::getOne($res["deptid"])["simplename"]);
        }
        return $res;
    }

    public static function getList($params) {
        $where = [];
        if (trim($params["roleName"])) {
            $where = [["name", "like", "%" . $params["roleName"] . "%"]];
        }
        $list = Role::where($where)->select()->toArray();
        foreach ($list as &$item) {
            if ($item["pid"] > 0) {
                $item["pName"] = isNullOrEmpty(Role::where("id", "=", $item["pid"])->findOrEmpty()["name"]);
            }
            if ($item["deptid"] > 0) {
                $item["deptName"] = isNullOrEmpty(DeptApi::getOne($item["deptid"])["simplename"]);
            }
        }
        return $list;
    }

    public static function create($params) {
        if (!isset($params["name"]))
            return ["msg" => "角色名不能为空"];
        if (!isset($params["tips"]))
            return ["msg" => "别名不能为空"];
        if (!isset($params["pid"]))
            return ["msg" => "请选择上级名称"];
        $data["name"] = $params["name"];
        $data["pid"] = $params["pid"];
        $data["deptid"] = $params["deptid"];
        $data["tips"] = $params["tips"];
        $data["num"] = $params["num"];
        Role::insert($data);
        return ["code" => 200, "msg" => "成功"];
    }

    public static function update($params) {
        if (!isset($params["id"]))
            return ["msg" => "没有角色信息"];
        if (!isset($params["name"]))
            return ["msg" => "角色名不能为空"];
        if (!isset($params["tips"]))
            return ["msg" => "别名不能为空"];
        if (!isset($params["pid"]))
            return ["msg" => "请选择上级名称"];
        $child_ids = Role::where("pid", "=", $params["id"])->column("id");
        if (in_array($params["pid"], $child_ids))
            return ["msg" => "不能选择自身下级作为上级"];
        $data["id"] = $params["id"];
        $data["name"] = $params["name"];
        $data["pid"] = $params["pid"];
        $data["deptid"] = $params["deptid"];
        $data["tips"] = $params["tips"];
        $data["num"] = $params["num"];
        Role::update($data);
        return ["code" => 200, "msg" => "成功"];
    }

    public static function set_authority($id, $mids) {
        SysRelation::where("roleid", "=", $id)->delete();
        $data = [];
        $mids = explode(",", $mids);
        for ($i = 0; $i < count($mids); $i++) {
            $data[] = [
                "menuid" => $mids[$i],
                "roleid" => $id
            ];
        }
        SysRelation::insertAll($data);
        return ["code" => 200, "msg" => "成功"];
    }

    public static function delete($id) {
        //不能删除超级管理员角色
        if ($id == 1)
            return ["msg" => "不能删除超级管理员角色"];
        Role::where(["id" => $id])->delete();
        return ["code" => 200, "msg" => "成功"];
    }

}
