<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class Priv_codeModel extends BaseModel
{
    protected $table = 'dc_priv_code';
    //更具ids获取权限代码
    public function getCode($ids)
    {
        $data = $this->select('priv_code')->whereIn('id', $ids)->get();
        if (!$data) {
            return -7520135522; //没有权限
        }
        $data = $data->toArray();
        $arr = array();
        foreach ($data as $v) {
            $arr[] = trim($v['priv_code']);
        }
        return $arr;
    }
    //获取多有的权限代码
    protected function getAllcode()
    {
        $data = $this->select('priv_code')->get();
        if (!$data) {
            return -7520135522; //没有权限
        }
        $data = $data->toArray();
        $arr = array();
        foreach ($data as $v) {
            $arr[] = trim($v['priv_code']);
        }
        return $arr;
    }
    public function hasCode($code)
    {
        $res = $this->where('priv_code', $code)->first();
        if (!$res) {
            return -7030050022;
        }
        return 1;
    }
    //获取管理员权限代码
    protected function getUserCode($admin_id)
    {
        //获取权限id
        $data = \App\Models\Priv_roleModel::getPrivID($admin_id);
        if ($data < 0) {
            return array();
        }
        $reData = $this->select('priv_code')->whereIn('id', $data)->get();
        if (!$reData) {
            return array();
        }
        $reData = $reData->toArray();
        $res = array();
        foreach ($reData as $v) {
            $res[] = trim($v['priv_code']);
        }
        return $res;
    }

    //获取非超级管理员的权限代码
    protected function getAdminCode($admin_id)
    {
        $sql = 'SELECT priv_code from dc_priv_code   WHERE id in    (SELECT priv_id FROM dc_priv_role   WHERE role_id in (  SELECT role_id FROM dc_role WHERE admin_id=' . $admin_id . '  ) )';
        $data = DB::select($sql);

        if (!$data) {
            return -802023003; //没有
        }
        $data = json_encode($data);
        $data = json_decode($data, 1);
        return $data;
    }
}
