<?php

namespace app\admin\controller;

use app\common\model\apply\ApiClass;
use app\common\model\apply\ApiPath;
use app\common\model\apply\ApplyClass;
use app\common\model\base\org\Org;
use app\common\model\base\org\OrgRole;
use app\common\model\base\org\OrgType;
use app\common\model\base\OrgTypeClass;
use app\common\model\base\other\Area;
use app\common\model\basic\BasicSchool;
use daorui\platform\platformAuth;
use daorui\weixin\work\workAuth;

/**
 * 公告参数获取
 */
class Comapi extends Base
{
    protected $noNeedLogin = [

    ];
    protected $noNeedAuth = [
        'getApiClassPage', 'getOrgRolePage', 'getOrgTypeClassCode', 'getOrgPage', 'getApplyClassPage', 'getArea', 'getSchoolNature',
    ];

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * @title       :[获取学校性质]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2024/12/27 16:34
     */
    public function getSchoolNature()
    {
        $info = BasicSchool::schoolNatureList();
        $list = [];
        if ($info) {
            foreach ($info as $k => $v) {
                $list[] = [
                    'label' => $v,
                    'value' => $k,
                ];
            }
        }
        return pageRes(1, '', count($list), $list);
    }

    /**
     * @title       :[获取应用权限]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @param       {int}       {pid}         {0}      {父级id}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2025/1/2 10:12
     */
    public function getPermission()
    {
        $pid   = input('pid/d', 0);
        $where = [];
        if ($pid >= 0) {
            $where[] = ['pid', '=', $pid];
        }
        $list = ApiClass::field('id,pid,class_name')
            ->where($where)
            ->select()
            ->each(function ($itme) {
                $w                  = [
                    ['class_id', '=', $itme['id']],
                ];
                $itme['permission'] = ApiPath::field('id,api_name')->where($w)->select();
                if ($itme['pid'] == 0) {
                    $itme['hasChildren'] = true;
                } else {
                    $itme['hasChildren'] = false;
                    $itme['children']    = null;
                }
            });
        if ($pid >= 0) {
            return res(1, '', $list);
        } else {
            $list = $list ? $list->toArray() : [];
            $list = array2tree($list, 'pid', 'id', 0, 'children');
            return res(1, '', $list);
        }
    }

    /**
     * @title       :[接口分类]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @param       {int}       {page}        {1}      {页码}
     * @param       {int}       {limit}       {10}     {条数}
     * @param       {string}    {kw}          {''}     {关键词}
     * @param       {int}       {org_id}      {''}     {所属组织}
     * @param       {array}     {val}         {''}     {查询指定值}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2024/12/31 8:52
     */
    public function getApiClassPage()
    {
        $kw    = input('kw/s', '');
        $page  = input('page/d', 1);
        $limit = input('limit/d', 10);
        $pid   = input('pid/d', 0);
        // 查询指定值
        $val   = input('val/a', []);
        $where = [];
        if ($val) {
            if (is_array($val)) {
                // 查询指定值
                $where[] = ['id', 'IN', $val];
            } else {
                // 查询指定值
                $where[] = ['id', '=', $val];
            }
            $data = ApiClass::field('class_name AS label,id AS value')->where($where)->select();
            return pageRes(1, '', count($data), $data);
        } else {
            // 父级id
            if ($pid >= 0) {
                $where[] = ['pid', '=', $pid];
            }
            // 查询分页
            if ($kw) {
                $where[] = ['org_type_name|code', 'LIKE', "%$kw%"];
            }
            $res = ApiClass::field('class_name AS label,id AS value,pid')
                ->where($where)
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->each(function ($item) {
                    $item['hasChildren'] = $item['pid'] == 0 ? true : false;
                });
            return pageRes(1, '', $res->total(), $res->items());
        }
    }

    /**
     * @title       :[获取组织角色]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @param       {int}       {page}        {1}      {页码}
     * @param       {int}       {limit}       {10}     {条数}
     * @param       {string}    {kw}          {''}     {关键词}
     * @param       {int}       {org_id}      {''}     {所属组织}
     * @param       {array}     {val}         {''}     {查询指定值}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2024/12/30 11:17
     */
    public function getOrgRolePage()
    {
        $kw     = input('kw/s', '');
        $org_id = input('org_id/d', 0);
        $page   = input('page/d', 1);
        $limit  = input('limit/d', 10);
        // 查询指定值
        $val   = input('val/a', []);
        $where = [];
        if ($val) {
            // 查询指定值
            $where[] = ['role_id', 'IN', $val];
            $data    = OrgRole::field('name AS label,role_id AS value')
                ->where($where)
                ->select()
                ->each(function ($item) {
                    $item['right'] = OrgRole::getOrgPath($item['value']);
                });
            return pageRes(1, '', count($data), $data);
        } else {
            // 查询分页
            if ($kw) {
                $where[] = ['name|code', 'LIKE', "%$kw%"];
            }
            // 所属企业
            if ($org_id) {
                $where[] = ['org_id', '=', $org_id];
            }
            $res = OrgRole::field('name AS label,role_id AS value')
                ->where($where)
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->each(function ($item) {
                    $item['right'] = OrgRole::getOrgPath($item['value']);
                });
            return pageRes(1, '', $res->total(), $res->items());
        }
    }

    /**
     * @title       :[获取组织分类code值]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @param       {string}    {type_class}  {''}     {关键词}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2024/12/27 16:34
     */
    public function getOrgTypeClassCode()
    {
        $inCode = (new platformAuth())->interfaceRequest("getSchoolCode", []);
        $inCode = array_column($inCode['data'], 'code');
        $list   = OrgType::where('code', 'in', $inCode)->column('code as value,org_type_name as label');
        return pageRes(1, '', count($list), $list);
    }

    /**
     * @title       :[获取组织结构]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @param       {int}       {page}        {1}      {页码}
     * @param       {int}       {limit}       {10}     {条数}
     * @param       {string}    {kw}          {''}     {关键词}
     * @param       {array}     {val}         {''}     {查询指定值}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2024/12/23 17:45
     */
    public function getOrgTypePage()
    {
        $kw    = input('kw/s', '');
        $page  = input('page/d', 1);
        $limit = input('limit/d', 10);
        // 查询指定值
        $val   = input('val/a', []);
        $where = [];
        if ($val) {
            // 查询指定值
            $where[] = ['code', 'IN', $val];
            $data    = OrgType::field('org_type_name AS label,code AS value')->where($where)->select();
            return pageRes(1, '', count($data), $data);
        } else {
            // 查询分页
            if ($kw) {
                $where[] = ['org_type_name|code', 'LIKE', "%$kw%"];
            }
            $res = OrgType::field('org_type_name AS label,code AS value')->where($where)->paginate(['list_rows' => $limit, 'page' => $page]);
            return pageRes(1, '', $res->total(), $res->items());
        }
    }


    /**
     * @title       :[获取应用类型]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @param       {int}       {page}        {1}      {页码}
     * @param       {int}       {limit}       {10}     {条数}
     * @param       {string}    {kw}          {''}     {关键词}
     * @param       {array}     {val}         {''}     {查询指定值}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2024/12/24 17:24
     */
    public function getApplyClassPage()
    {
        $kw    = input('kw/s', '');
        $page  = input('page/d', 1);
        $limit = input('limit/d', 10);
        // 查询指定值
        $val   = input('val/a', []);
        $where = [];
        if ($val) {
            // 查询指定值
            $where[] = ['id', 'IN', $val];
            $data    = OrgType::field('class_name AS label,id AS value')->where($where)->select();
            return pageRes(1, '', count($data), $data);
        } else {
            // 查询分页
            if ($kw) {
                $where[] = ['class_name|class_code', 'LIKE', "%$kw%"];
            }
            $res = ApplyClass::field('class_name AS label,id AS value')->where($where)->paginate(['list_rows' => $limit, 'page' => $page]);
            return pageRes(1, '', $res->total(), $res->items());
        }
    }

    /**
     * @title       :[获取地区]
     * @param       {参数类型}   {参数字段名}   {默认值}  {描述}
     * @param       {int}       {page}        {1}      {页码}
     * @param       {int}       {limit}       {10}     {条数}
     * @param       {string}    {kw}          {''}     {关键词}
     * @param       {array}     {val}         {''}     {查询指定值}
     * @return json|{*}
     * @Author      : byl
     * @Date        :2024/12/27 9:36
     */
    public function getArea()
    {
        $kw     = input('kw/s', '');
        $pid    = input('pid/d', 1);
        $status = input('status/d', 0);
        $lazy   = input('lazy/b', true);
        // 查询指定值
        $val   = input('val/a', []);
        $where = [];
        if ($val) {
            // 查询指定值
            $where[] = ['id', 'IN', $val];
        } else {
            // 动态加载,则一级一级加载输出.非动态则输出全部
            if ($lazy) {
                $where[] = ['pid', '=', $pid];
            }
            if ($status) {
                $where[] = ['status', '=', $status];
            }
            if ($kw) {
                $where[] = ['name', 'LIKE', "%$kw%"];
            }
        }
        if ($lazy) {
            $data = Area::field('name AS label,id AS value,common,id,pid')
                ->where($where)
                ->order(['common' => 'DESC'])
                ->select();
            return res(1, '', $data);
        } else {
            $data     = Area::where($where)
                ->order(['common' => 'DESC'])
                ->column('name AS label,id AS value,common,id,pid');
            $treeData = [];
            if ($data) {
                $treeData = array2tree($data, 'pid', 'id', $pid);
            }
            return res(1, '', $treeData);
        }
    }
}