<?php

class infoAccessmanage_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->HT = $this->load->database('INFORMATION', TRUE);
        $this->HT229 = $this->load->database('HT', TRUE);
    }

    //获取用户结构列表
    public function get_user_structure(){
        $sql = "SELECT   is1.is_id AS id,
                         is1.is_parent_id AS pId,
                         OPI_Name as name,
                         1 AS status,
                         OPI_Code,
                         iam_id,
                         iam_author
                FROM     infoaccessmanage 
                         INNER JOIN infoStructures is1 ON iam_is_id = is1.is_id
                         LEFT JOIN tourmanager.dbo.OperatorInfo ON iam_author=OPI_Code
                ORDER BY is1.is_level ASC,
                         is1.is_sort ASC, 
                         is1.is_path ASC";
        $query = $this->HT->query($sql);
        $result=$query->result();

        //获取作者平台用户的用户名
        $author_user=$this->get_author_user_list();
        $author=array();
        foreach ($author_user as $v) {
            $author[$v->a_id]=$v->a_name;
        }
        foreach ($result as $r) {
            if(is_numeric($r->iam_author)) {
                $r->name=$author[$r->iam_author];
            }elseif (is_null($r->name)) {
                $r->name=$r->iam_author;
            }else{
                $r->name=$r->name.'('.$r->OPI_Code.')';
            }
        }

        return $query->result();
    }

    //根据用户标识iam_author获取权限
    public function get_user_access($iam_author,$site_code){
        $sql = "SELECT TOP 1
                         iam_id,
                         iam_author,
                         iam_is_id,
                         iao_id,
                         iao_read,
                         iao_write,
                         iao_role,
                         iao_site
                FROM     infoaccessmanage 
                         LEFT JOIN infoaccessownsite ON iao_author=iam_author AND iao_site=?
                WHERE    iam_author='$iam_author'";
        $query = $this->HT->query($sql, array($site_code));
        $result = $query->result();
        if (!empty($result)) {
            $result=$result[0];
        }
        return $result;
    }

    //根据用户的树结构ID获取用户权限数据
    public function get_user_by_isid($iam_is_id,$site_code){
        $sql = "SELECT TOP 1
                         iam_id,
                         iam_author as iao_author,
                         iam_is_id,
                         iao_id,
                         iao_read,
                         iao_write,
                         iao_role,
                         iao_site,
                         iao_other
                FROM     infoaccessmanage 
                         LEFT JOIN infoaccessownsite ON iao_author=iam_author AND iao_site=?
                WHERE    iam_is_id=?";
        $query = $this->HT->query($sql, array($site_code,$iam_is_id));
        $result = $query->result();
        if (!empty($result)) {
            $result=$result[0];
        }
        return $result;
    }
    //获取用户拥有权限的站点
    public function get_user_site_list($iao_author){
        $sql = "SELECT  iao_site
                FROM    infoaccessownsite
                WHERE    iao_author='$iao_author'";
        $query = $this->HT->query($sql);
        $result = $query->result();
        $sites='';
        if (!empty($result)) {
            foreach ($result as $s) {
                $sites.=$s->iao_site.',';
            }
        }
        return $sites;
    }

    public function get_user_site_list_by_isid($iam_is_id){
        $sql = "SELECT  iao_site
                FROM    infoaccessownsite
                        INNER JOIN infoaccessmanage ON iam_author=iao_author AND iam_is_id=?";
        $query = $this->HT->query($sql, array($iam_is_id));
        $result = $query->result();
        $sites='';
        if (!empty($result)) {
            foreach ($result as $s) {
                $sites.=$s->iao_site.',';
            }
        }
        return $sites;
    }

    //删除某个用户
    public function delete_user($iam_id){
        $sql = "DELETE FROM infoaccessmanage WHERE iam_id=?";
        $result=$this->HT->query($sql, array($iam_id));
        return $result;
    }

    //给用户添加可以访问的站点
    public function add_site($iao_author,$iao_site){
        $sql = "INSERT INTO infoaccessownsite (
                             iao_author,
                             iao_site
                    ) VALUES (?,?)";
        $query = $this->HT->query($sql, array($iao_author,$iao_site));
        return $this->HT->last_id('infoaccessownsite');
    }

    //删除用户在某一站点的权限
    public function delete_site($author,$site=''){
        $map="";
        if ($site!='') {
            $map=" AND iao_site='$site'";
        }
        $set_sql = "DELETE FROM infoaccessownsite WHERE iao_author=? $map";
        $result=$this->HT->query($set_sql, array($author));
        return $result;
    }

    //更新用户在某一站点的权限
    public function update_site($iao_author,$iao_site,$iao_other){
        $sql = "UPDATE  infoaccessownsite
                   SET  iao_other=?
                 WHERE  iao_author = ? AND iao_site = ?";
        $result = $this->HT->query($sql, array($iao_other,$iao_author,$iao_site));
        return $sql;
    }

    //添加用户
    public function add_access($iam_author,$iam_is_id){
        $sql = "INSERT INTO infoaccessmanage (
                             iam_author,
                             iam_is_id
                    ) VALUES (?,?)";
        $query = $this->HT->query($sql, array($iam_author,$iam_is_id));
        return $this->HT->last_id('infoaccessmanage');
    }

    //编辑用户权限
    public function update_access($iam_id,$iam_author){
        $set_sql = "UPDATE  infoaccessmanage
                    SET     iam_author=?
                    WHERE   iam_id = ?";
        $result=$this->HT->query($set_sql, array($iam_author,$iam_id));
        return $result;
    }

    //编辑用户可以操作的信息节点
    public function update_ownsite($iao_read,$iao_write,$iao_author,$iao_site,$iao_role){
        $set_sql = "UPDATE  infoaccessownsite
                    SET     iao_read=?,
                            iao_write=?,
                            iao_role=?
                    WHERE   iao_author = ? AND iao_site=?";
        $result=$this->HT->query($set_sql, array($iao_read,$iao_write,$iao_role,$iao_author,$iao_site));
        return $result;
    }

    //获取角色结构列表
    public function get_role_structure(){
        $sql = "SELECT   is1.is_id AS id,
                         is1.is_parent_id AS pId,
                         iar_name as name,
                         1 AS status,
                         iar_id,
                         is1.is_level
                FROM     infoaccessrole 
                         INNER JOIN infoStructures is1 ON iar_is_id = is1.is_id
                ORDER BY is1.is_level ASC,
                         is1.is_sort ASC, 
                         is1.is_path ASC";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    //添加角色
    public function add_role($iar_name,$iar_is_id){
        $sql = "INSERT INTO infoaccessrole (
                             iar_name,
                             iar_is_id
                    ) VALUES (?,?)";
        $query = $this->HT->query($sql, array($iar_name,$iar_is_id));
        return $this->HT->last_id('infoaccessrole');
    }

    //更新角色名称
    public function update_role($iar_id,$iar_name){
        $set_sql = "UPDATE infoaccessrole
                    SET    iar_name = ?
                    WHERE  iar_id = ?";
        $result=$this->HT->query($set_sql, array($iar_name,$iar_id));
        return $result;
    }

    //获取角色列表
    public function get_role($ian_iar_id){
        $sql = "SELECT  iar_id,
                        iar_name,
                        is1.is_level
                FROM    infoaccessrole
                        INNER JOIN infoStructures is1 ON iar_is_id = is1.is_id
                WHERE   iar_id IN ($ian_iar_id)";
        $query = $this->HT->query($sql);
        return $query->result();
    }
    //根据角色的树结构ID获取角色拥有的权限节点
    public function get_role_by_isid($iar_is_id){
        $sql = "SELECT  iar_id,iar_name,ian_iaa_id,iar_is_id
                FROM    infoaccessrole 
                        LEFT JOIN infoaccessnode
                        ON iar_id=ian_iar_id
                WHERE   iar_is_id=?";
        $query = $this->HT->query($sql,array($iar_is_id));
        $result=$query->result();
        if(!empty($result))
        {
            $result=$result[0];
        }
        return $result;
    }

    //删除角色,并删除与之相关的“角色-权限节点”关系
    public function delete_role($iar_id){
        $sql = "DELETE FROM infoaccessrole WHERE  iar_id = ?";
        $query=$this->HT->query($sql, array($iar_id));
        if ($query) {
            $node_sql = "DELETE FROM infoaccessnode WHERE  ian_iar_id = ?";
            $query=$this->HT->query($node_sql, array($iar_id));
        }
        return $query;
    }

    //添加新的 “角色-权限节点”关系
    public function add_node($ian_iar_id,$ian_iaa_ids){
        $sql = "INSERT INTO infoaccessnode (
                             ian_iar_id,
                             ian_iaa_id
                    ) VALUES (?,?)";
        $query = $this->HT->query($sql, array($ian_iar_id,$ian_iaa_ids));
        return $this->HT->last_id('infoaccessnode');
    }

    //更新“角色-权限节点”关系
    public function update_node($ian_iar_id,$ian_iaa_id){
        $set_sql = "UPDATE infoaccessnode
                    SET    ian_iaa_id = ?
                    WHERE  ian_iar_id = ?";
        $result=$this->HT->query($set_sql, array($ian_iaa_id,$ian_iar_id));
        return $result;
    }

    //获取某个角色拥有的权限节点ID
    public function get_node_list($ian_iar_id){
        $set_sql = "SELECT ian_iaa_id
                    FROM   infoaccessnode
                    WHERE  ian_iar_id IN ($ian_iar_id)";
        $query = $this->HT->query($set_sql);
        return $query->result();
    }

    //获取权限节点结构列表
    public function get_structure_list(){
        $sql = "SELECT   is1.is_id AS id,
                         is1.is_parent_id AS pId,
                         iaa_title as name,
                         1 AS status,
                         iaa_id
                FROM     infoaccessaction 
                         INNER JOIN infoStructures is1 ON iaa_is_id = is1.is_id
                ORDER BY is1.is_level ASC,
                         is1.is_sort ASC, 
                         is1.is_path ASC";
        $query = $this->HT->query($sql);
        return $query->result();
    }
    //根据权限节点树结构id数组获取其子节点数据
    public function get_sub_action_by_isids($iaa_is_id_arr){
        $map='1=2';
        foreach ($iaa_is_id_arr as $v) {
            $map.=' or '."is_path like '%$v%'";
        }
        $sql = "SELECT  iaa_id
                FROM    infoaccessaction
                        INNER JOIN infoStructures ON iaa_is_id=is_id
                WHERE   $map";
        $query = $this->HT->query($sql);
        $result = $query->result();
        return $result;
    }

    //根据树结构ID获取权限节点内容
    public function get_action_bytreeid($treeid){
        $sql = "SELECT TOP 1   
                         iaa_title,
                         iaa_controller,
                         iaa_action,
                         iaa_is_id,
                         iaa_id
                FROM     infoaccessaction 
                WHERE    iaa_is_id=?";
        $query = $this->HT->query($sql, array($treeid));
        $result = $query->result();
        if (!empty($result)) {
            $result=$result[0];
        }
        return $result;
    }

    //根据权限节点id字符串获取权限节点内容
    public function get_role_action_list($iaa_ids){
        $sql = "SELECT  iaa_id,
                        iaa_title,
                        iaa_controller,
                        iaa_action,
                        iaa_is_id as is_id
                FROM    infoaccessaction
                WHERE   iaa_id IN ($iaa_ids)";
        $query = $this->HT->query($sql);
        $result = $query->result();
        return $result;
    }

    //获取所有的权限节点列表
    public function get_action_list()
    {
        $sql = "SELECT  iaa_id,
                        iaa_title,
                        iaa_controller,
                        iaa_action,
                        iaa_is_id
                FROM    infoaccessaction";

        $query = $this->HT->query($sql);
        $result = $query->result();
        return $result;
    }

    //添加权限节点
    public function add_action($iam_author,$iam_is_id){
        $sql = "INSERT INTO infoaccessaction (
                             iaa_title,
                             iaa_is_id
                    ) VALUES (?,?)";
        $query = $this->HT->query($sql, array($iam_author,$iam_is_id));
        return $this->HT->last_id('infoaccessaction');
    }

    //更新权限节点
    public function update_action($iaa_title,$iaa_controller,$iaa_action,$iaa_id){
        $set_sql = "UPDATE infoaccessaction
                    SET    iaa_title=?,
                           iaa_controller=?,
                           iaa_action=?
                    WHERE  iaa_id = ?";
        $result=$this->HT->query($set_sql, array($iaa_title,$iaa_controller,$iaa_action,$iaa_id));
        return $result;
    }

    //删除某个权限节点
    public function delete_action($iaa_id){
        $sql = "DELETE FROM infoaccessaction WHERE  iaa_id = ?";
        $query=$this->HT->query($sql, array($iaa_id));
        return $query;
    }

    //获取某个站点的作者
    public function get_site_author($site){
        $sql="select iao_author from infoaccessownsite where iao_site=?";
        $query = $this->HT->query($sql,array($site));
        return $query->result();
    }

    //获取汉特用户列表
    public function get_ht_user_list($undefined=false){
        $undefined_sql='';
        if ($undefined) {
            $undefined_sql="AND OPI_Code NOT IN (SELECT iam_author FROM infoaccessmanage)";
        }
        $sql = "SELECT * FROM  tourmanager.dbo.OperatorInfo WHERE DeleteFlag=0 $undefined_sql";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    //获取作者平台用户列表
    public function get_author_user_list($undefined=false){
        $undefined_sql='WHERE a_active=1';
        if ($undefined) {
            //已经添加的用户id，不知道为啥直接嵌套报错
            $mapsql="SELECT iam_author FROM infoaccessmanage where ISNUMERIC(iam_author)=1";
            $map_query = $this->HT->query($mapsql);
            $authors='';
            foreach ($map_query->result() as $v) {
                if(trim($v->iam_author)!='')$authors.=$v->iam_author.',';
            }
            $authors=$authors.'0';

            $undefined_sql.=" AND a_id NOT IN ($authors)";
        }
        $sql = "SELECT * FROM  tourmanager.dbo.infoAuthors $undefined_sql";
        $query = $this->HT->query($sql);
        return $query->result();
    }
}