<?php
    namespace Admin\Controller;
    use Think\Controller;
    use Think\Model;
    class RbacController extends CommonController {
        //岗位列表
        public function Role(){
            $is_rbac = _rbac_node_show(18);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $sousuo=I('rolefl');
            $condition="where gongsiid = ".session('gongsiid');
            if ($sousuo == '全部') {//搜索
                $condition.="";
            }elseif($sousuo){
                $condition.=" and rolefl = $sousuo";
            }
            $Model = new \Think\Model();
            $count=$Model->query("select count(*) from __ROLE__ {$condition}");
            $Page=new \Think\Page($count['0']['count(*)'],30);
            $show=$Page->show();
            $list="select * from __ROLE__ {$condition} order by rolefl asc,xsshunxu asc limit ".$Page->firstRow.','.$Page->listRows;
            $Role=$Model->query($list);
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('role',$Role);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $this->rolefl=get_peizhi('rolefl',0,1);
            $this->display();
        }
    	//用户列表
        public function index(){
            $map = "gongsiid=".session('gongsiid');
            $status=I('status');
            $sousuo=I('sousuo');
            $x = _rbac_lists_where(17,0);
            if ($x && $x != "all" && $x != "me") {
                $map .= " and bumen in ({$x})";
            }elseif ($x == "me") {
                $map .= " and id=".session('uid');
            }elseif ($x == false) {
                $map .= " and id=0";
            }
            $tree = new \Org\Util\Tree();
            $bumen = I("get.bumen",0,'intval');
            if ($status==1) {
                $map .= " and status=1";
            }else {
                $map .= " and status=0";
            }
            if ($sousuo) {
                $map.=" and (dianhua like '%$sousuo%' or pinyinjs like '%$sousuo%' or ygmingcheng like '%$sousuo%' or ygbianhao like '%$sousuo%')";
            }
            if ($bumen) {
                $string = get_bm_sz($bumen,2);
                $map .= " and bumen in ($string)";
            }
            $count=D('UserRelation')->where($map)->count();
            $Page=new \Think\Page($count,30);
            $show=$Page->show();
            $Yonghu=D('UserRelation')->where($map)->limit($Page->firstRow.','.$Page->listRows)->field('mima',true)->relation('role')->order('pinyinjs asc')->select();
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('user',$Yonghu);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $select_categorys = bumen_option( $bumen, 17);
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
        public function stopUser(){
            $id = I('id',0,'intval');
            $is_rbac = _rbac_btn_show(190,0,$id);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $isok = M('yonghu')->where(array('id'=>$id))->setField('lock1',1);
            if ($isok) {
                $xiugai = "停用员工【".zym($id,5)."】";
                data_rizhi(8,21,$id,zym($id,15),$xiugai,"","用户");
                $this->success('已停用');
            }else{
                $this->error('停用失败');
            }
        }
        public function startUser(){
            $id = I('id',0,'intval');
            $is_rbac = _rbac_btn_show(192,0,$id);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $isok = M('yonghu')->where(array('id'=>$id))->setField('lock1',0);
            if ($isok) {
                $xiugai = "启用员工【".zym($id,5)."】";
                data_rizhi(8,21,$id,zym($id,15),$xiugai,"","用户");
                $this->success('已启用');
            }else{
                $this->error('启用失败');
            }
        }
        //添加岗位
        public function addRole(){
            $is_rbac = _rbac_node_show(99);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                //防止重复提交
                $yonghu=M('yonghu');
                if (!$yonghu->autoCheckToken($_POST)) {
                    $this->error('请不要重复添加');
                }
                if (session('gongsiid')=="") {
                    $this->error('请重新登录');
                }
                $data['remark']=I('remark');
                $data['rolefl']=I('rolefl');
                $data['miaoshu']=I('miaoshu');
                $data['xsshunxu']=I('xsshunxu');
                $data['gongsiid']=session('gongsiid');
                $isok=M('Role')->add($data);
                if ($isok) {
                    data_rizhi(6,1,$isok,"","新增岗位","","岗位");
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->rolefl=get_peizhi('rolefl',0,1);
                $this->display();
            }
        }
        //更新岗位
        public function editRole(){
            $is_rbac = _rbac_node_show(101);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $id = I('id');
                $data['remark'] = I('remark');
                $data['rolefl'] = I('rolefl');
                $data['miaoshu'] = I('miaoshu');
                $data['xsshunxu'] = I('xsshunxu');
                $role = M('role')->where(array('id'=>$id))->find();
                $xiugai = "";
                if ($role['rolefl'] != I('rolefl') && ($role['rolefl'] == true || I('rolefl') == true)) {
                    $xiugai .= "岗位分类：[".get_peizhi('rolefl',$role['rolefl'],3)."]=>[".get_peizhi('rolefl',I('rolefl'),3)."];";
                }
                if ($role['remark'] != I('remark') && ($role['remark'] == true || I('remark') == true)) {
                    $xiugai .= "岗位名称：[".$role['remark']."]=>[".I('remark')."];";
                }
                if ($role['xsshunxu'] != I('xsshunxu') && ($role['xsshunxu'] == true || I('xsshunxu') == true)) {
                    $xiugai .= "显示顺序：[".$role['xsshunxu']."]=>[".I('xsshunxu')."];";
                }
                if ($role['miaoshu'] != I('miaoshu') && ($role['miaoshu'] == true || I('miaoshu') == true)) {
                    $xiugai .= "岗位描述：[".$role['miaoshu']."]=>[".I('miaoshu')."];";
                }
                $isok = M('Role')->where(array('id'=>$id))->save($data);
                if ($isok) {
                    data_rizhi(6,2,$id,"",$xiugai,"","岗位");
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            }else{
                $this->role=M('Role')->where(array('id'=>I('id')))->find();
                $this->rolefl=get_peizhi('rolefl',0,1);
                $this->display();
            }
        }
        //删除岗位
        public function delRole(){
            $is_rbac = _rbac_node_show(103);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $id = I('id');
            $isok=M('Role')->where(array('id'=>$id))->delete();
            if ($isok) {
                data_rizhi(6,8,$id,"","删除岗位","","岗位");
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        //添加用户
        public function addUser(){
            $is_rbac = _rbac_node_show(104);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $bumen=I('bumen');
                $role=I('role_id');
                if (!session(C('ADMIN_AUTH_KEY'))) {
                    $kxrole=yhkxrole(0,2);
                    if (!in_array($role,$kxrole)) {
                        $this->error('异常错误！');
                    }
                }
                $tjbmsz=bumen_idsz(104);
                if (!in_array($bumen,$tjbmsz) && $tjbmsz[0] != "all"){
                    $this->error('异常错误！');
                }
                if(!preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[8-9])\d{8}$/",I('dianhua'))){  
                    $this->error('电话号码格式不正确!'); 
                }
                if (I('dianhua')) {
                    $user1=M('yonghu')->where(array('dianhua' =>I('dianhua'),'gongsiid'=>session('gongsiid')))->find();
                    if ($user1) {
                        $this->error('手机号已被使用');
                    }
                }else{
                    $this->error('手机号不能为空');
                }
                if (I('xingbie') == 1) {
                    $xingbie = 1;
                } elseif (I('xingbie') == 2) {
                    $xingbie = 2;
                } else {
                    $xingbie = 0;
                }
                $bhao = 'YH-';
                $bh = M('bianhao')->where(array('id'=>8))->setInc('bianhao');
                $bh1 = M('bianhao')->where(array('id'=>8))->getField('bianhao');
                $user = array(
                    'gongsiid'=>session('gongsiid'),
                    'ygmingcheng'=>I('ygmingcheng'),
                    'pinyinjs'=>I('pinyinjs'),
                    'dianhua'=>I('dianhua'),
                    'ygbianhao'=>$bhao.$bh1,
                    'xingbie'=>$xingbie,
                    'bumen'=>I('bumen'),
                    'mima'=>I('mima','','md5'),
                    'logintime'=>time(),
                    'loginip'=>get_client_ip(),
                );
                for ($i=1; $i < 7; $i++) { 
                    $user['company'.$i]=0;
                }
                $bumen_find = M('bumen')->where(array('id'=>I('bumen')))->find();
                $u = $bumen_find['level'];
                $user['company'.$bumen_find['level']]=$bumen_find['id'];
                for ($i=0; $i < $u; $i++) { 
                    $bumen_find = M('bumen')->where(array('id'=>$bumen_find['parentid']))->find();
                    if ($bumen_find['bmming']) {
                        $user['company'.$bumen_find['level']]=$bumen_find['id'];
                    }
                }
                $isok=M('yonghu')->add($user);
                if ($isok) {
                    $data_role =  array(
                        'role_id'=>$role,
                        'user_id'=>$isok
                    );
                    M('role_user')->add($data_role);
                    data_rizhi(8,1,$isok,$bhao.$bh1,"新增用户","","用户");
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                if (session(C('ADMIN_AUTH_KEY'))) {
                    $this->role=M('role')->where(array('gongsiid'=>session('gongsiid')))->order('rolefl asc,xsshunxu asc')->select();
                } else {
                    $this->role=yhkxrole(0,1,0);
                }
                $parentid = I("get.parentid",0,'intval');
                $select_categorys = bumen_option( $parentid, 104);
                $this->assign("select_categorys", $select_categorys);
                $this->display();
            }
        }
        //更新用户
        public function editUser(){
            $id=I('id',0,'intval');
            $is_rbac = _rbac_btn_show(106,0,$id);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $et = I('edittype');
            switch ($et) {
                case '1':
                    if (!IS_POST) {$this->redirect('Rbac/edituser');}
                    $yh=M('yonghu')->where(array('id'=>$id))->find();
                    $User=M("Yonghu");
                    if (I('xingbie') == 1) {
                        $xingbie = 1;
                    } elseif (I('xingbie') == 2) {
                        $xingbie = 2;
                    } else {
                        $xingbie = 0;
                    }
                    if (C('RBAC_SUPERADMIN') != $yh['dianhua']) {
                        $data['dianhua']=I('dianhua');
                    }
                    $data['ygmingcheng']=I('ygmingcheng');
                    $data['pinyinjs']=I('pinyinjs');
                    $data['xingbie']=$xingbie;
                    if (C('RBAC_SUPERADMIN') != $yh['dianhua']) {
                        if ($yh['dianhua']==I('dianhua') && $yh['ygmingcheng']==I('ygmingcheng') && $yh['pinyinjs']==I('pinyinjs')) {
                            $this->error('没有数据被更新');
                        }
                    } else {
                        if ($yh['ygmingcheng']==I('ygmingcheng') && $yh['pinyinjs']==I('pinyinjs')) {
                            $this->error('没有数据被更新');
                        }
                    }
                    $xiugai = "";
                    if (C('RBAC_SUPERADMIN') != $yh['dianhua']) {
                        if ($yh['dianhua']!=I('dianhua') && ($yh['dianhua']==true || I('dianhua')==true)) {
                            $xiugai.="手机号：[".$yh['dianhua']."]=>[".I('dianhua')."]；";
                        }
                    }
                    if ($yh['ygmingcheng']!=I('ygmingcheng') && ($yh['ygmingcheng']==true || I('ygmingcheng')==true)) {
                        $xiugai.="用户名称：[".$yh['ygmingcheng']."]=>[".I('ygmingcheng')."]；";
                    }
                    if ($yh['pinyinjs']!=I('pinyinjs') && ($yh['pinyinjs']==true || I('pinyinjs')==true)) {
                        $xiugai.="拼音检索：[".$yh['pinyinjs']."]=>[".I('pinyinjs')."]；";
                    }
                    if ($yh['xingbie']!=$xingbie && ($yh['xingbie']==true || $xingbie==true)) {
                        if ($yh['xingbie'] == 1) {
                            $xb = "男";
                        } elseif ($yh['xingbie'] == 2) {
                            $xb = "女";
                        } else {
                            $xb = "未知";
                        }
                        if ($xingbie == 1) {
                            $xb1 = "男";
                        } elseif ($xingbie == 2) {
                            $xb1 = "女";
                        } else {
                            $xb1 = "未知";
                        }
                        $xiugai.="性别：[".$xb."]=>[".$xb1."]；";
                    }
                    $isok = $User->where(array('id'=>$id))->save($data);
                    if ($isok){
                        if (C('RBAC_SUPERADMIN') != $yh['dianhua']) {
                            if (($yh['dianhua']!=I('dianhua') && ($yh['dianhua']==true || I('dianhua')==true)) && $yh['touxiang'] == 1) {
                                copy("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_30.jpg","./Upload/Avatar/".$yh['gongsiid']."/".I('dianhua')."_30.jpg");
                                copy("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_60.jpg","./Upload/Avatar/".$yh['gongsiid']."/".I('dianhua')."_60.jpg");
                                copy("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_100.jpg","./Upload/Avatar/".$yh['gongsiid']."/".I('dianhua')."_100.jpg");
                                copy("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_200.jpg","./Upload/Avatar/".$yh['gongsiid']."/".I('dianhua')."_200.jpg");
                                unlink("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_30.jpg");
                                unlink("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_60.jpg");
                                unlink("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_100.jpg");
                                unlink("./Upload/Avatar/".$yh['gongsiid']."/".$yh['dianhua']."_200.jpg");
                            }
                        }
                        data_rizhi(8,2,$id,$yh['ygbianhao'],$xiugai,"","用户");
                        $this->success('更新成功');
                    }else{
                        $this->error('更新失败');
                    }
                    break;
                case '2':
                    if (!IS_POST) {$this->redirect('Rbac/edituser');}
                    $uid = $id;
                    $mima = trim(I('mima'));
                    $isok = $mima==''? false : M('yonghu')->where(array('id'=>$uid))->setField(array('mima' =>md5($mima)));
                    if ($isok) {
                        data_rizhi(8,2,$uid,zym($uid,15),"密码重置","","用户");
                        $this->success('密码已重置为：'.$mima);
                    }else{
                        $this->error('密码未更改');
                    }
                    break;
                case '3':
                    if (!IS_POST) {$this->redirect('Rbac/edituser');}
                    $uid = $id;
                    $keyid = trim(I('keyid'));
                    if ($keyid=='') {
                        $this->error('加密锁ID不能为空');
                    }
                    $id = M('yonghu')->where(array('keyid'=>$keyid))->field('id')->find();
                    $isok = $id>0? false : M('yonghu')->where(array('id'=>$uid))->setField(array('keyid' =>$keyid));
                    if ($isok) {
                        data_rizhi(8,2,$uid,zym($uid,15),"更新加密锁ID","","用户");
                        $this->success('更新成功');
                    }else{
                        $this->error('加密锁ID未更改或已存在');
                    }
                    break;
                default:
                    $this->user=M('Yonghu')->where(array('id'=>$id))->find();
                    if (C('RBAC_SUPERADMIN') == $this->user['dianhua']) {
                        $this->iscg = 1;
                    } else {
                        $this->iscg = 2;
                    }
                    $this->display();
                break;
            }
        }
        //删除用户
        public function delUser(){
            $id = I('id',0,'intval');
            $is_rbac = _rbac_btn_show(108,0,$id);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $fyshu = M('fangyuan')->where(array('weihurenid'=>$id))->count();
            $kyshu = M('keyuan')->where(array('weihurenid'=>$id))->count();
            if ($fyshu || $kyshu) {
                $this->error('请先转移数据!');
            }
            $isok = M('yonghu')->where(array('id'=>$id))->setField('status',0);
            if ($isok){
                $xiugai = "删除员工【".zym($id,5)."】";
                data_rizhi(8,8,$id,zym($id,15),$xiugai,"","用户");
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        //恢复用户
        public function huifuUser(){
            $id = I('id',0,'intval');
            $is_rbac = _rbac_btn_show(518,0,$id);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $isok = M('Yonghu')->where(array('id'=>$id))->setField('status',1);
            if ($isok){
                $xiugai = "恢复员工【".zym($id,5)."】";
                data_rizhi(8,8,$id,zym($id,15),$xiugai,"","用户");
                $this->success('恢复成功');
            }else{
                $this->error('恢复失败');
            }
        }
        //配置权限
        public function access(){
            $is_rbac = _rbac_node_show(113);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $access1 = htmlspecialchars_decode(I('access'));
                $access2 = json_decode($access1,true);
                $access = array_unique(array_filter($access2));
                $rid = $access['rid'];
                $db = M('access');
                //清空原权限
                $db->where(array('role_id'=>$rid))->delete();
                //组合新权限
                unset($access['rid']);
                unset($access['__hash__']);
                $data = array();
                foreach ($access as $v) {
                    if ($v != "") {
                        $tmp = explode('_', $v);
                        if ($tmp[0] != 0) {
                            $data[]=array(
                            'role_id'=>$rid,
                            'node_id'=>$tmp[0],
                            'level'=>3,
                            'fanwei'=>$tmp[1],
                            'zhuangtai'=>$tmp[2],
                           );
                        }
                    }
                }
                $node = M('node')->where(array('level'=>array('in','1,2')))->select();
                foreach ($node as $v) {
                    $data[]=array(
                        'role_id'=>$rid,
                        'node_id'=>$v['id'],
                        'level'=>$v['level'],
                        'fanwei'=>0,
                        'zhuangtai'=>0,
                    );
                }
                //插入新权限
                if ($db->addALL($data)) {
                    data_rizhi(6,2,$rid,"","配置".zym($rid,8)."的权限","","岗位");
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            } else {
                $rid = I('rid',0,'intval');
                $arr = M('node_set')->where(array('status'=>1))->order(array("sort" => "ASC"))->select();
                foreach ($arr as $key => $value) {
                    $arr[$key]['pid'] = $value['parentid'];
                }
                $access = M('access')->where(array('level'=>3,'role_id'=>$rid))->select();
                $result = [];
                foreach($access as $k=>$v){
                    $result[$v['node_id']][] = $v;
                }
                $arr = node_merge($arr,null,0);
                foreach ($arr as $k1 => $v1) {
                    if ($v1['is_sign']) {
                        foreach ($v1['child'] as $k2 => $v2) {
                            $arr[$k1]['child'][$k2]['config'] = $result[$v2['node_id']];
                        }
                    } else {
                        foreach ($v1['child'] as $k2 => $v2) {
                            foreach ($v2['child'] as $key => $value) {
                                $arr[$k1]['child'][$k2]['child'][$key]['config'] = $result[$value['node_id']];
                            }
                        }
                    }
                }
                $this->categorys = $arr;
                $this->fyzhuangtai=get_peizhi('zhuangtai',0,1);
                $this->kyzhuangtai=get_peizhi('khzhuangtai',0,1);
                $this->bumen_arr_level = get_options('bumen_options','bumen_array');
                $this->rid=$rid;
                $this->display('access2');
            }
        }
        public function Bumen(){
            $is_rbac = _rbac_node_show(115);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $map['gongsiid']=session('gongsiid');
            $Data=M('bumen');
            $result = $Data->where($map)->order(array("id" => "ASC"))->select();
            $xiao = $result;
            $tree = new \Org\Util\Tree();
            $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            $newmenus=array();
            foreach ($result as $m){
                $newmenus[$m['id']]=$m;
            }
            $bumen_option_value_arr = get_options('bumen_options','bumen_array');
            $sc=new AjaxController();
            foreach ($result as $n=> $r) {
                $result[$n]['level_s'] = $bumen_option_value_arr[$sc->get_level($r['id'], $newmenus)];
                $result[$n]['str_manage'] = '<if condition="_rbac_node_show(116)"><a href="javascript:;" class="layui-btn layui-btn-mini" onclick="layer_show(\'添加部门\',\''.U('addBumen',array('parentid'=>$r['id'])).'\',\'480\',\'480\')" style="text-decoration:none"><i class="Hui-iconfont">&#xe604;</i> 添加</a></if>';
                $result[$n]['str_manage'] .='&nbsp;&nbsp;&nbsp;<if condition="_rbac_node_show(118)"><a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-mini" onclick="layer_show(\'更新部门\',\''.U('editbumen',array('id'=>$r['id'])).'\',\'480\',\'480\')" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i> 编辑</a></if>';
                $result[$n]['str_manage'] .='&nbsp;&nbsp;&nbsp;<if condition="_rbac_node_show(120)"><a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-mini" onclick="admin_bumen_del(this,\''.$r['id'].'\')" style="text-decoration:none;"><i class="Hui-iconfont">&#xe6df;</i> 删除</a></if>';
            }
            $tree->init($result);
            $str = "<tr id='node-\$id' \$parentid_node style='\$style'>
                        <td style='padding-left:20px;'>\$id</td>
                        <td>\$level-\$level_s</td>
                        <td>\$spacer\$bmming</td>
                        <td>\$bmdianhua</td>
                        <td>\$str_manage</td>
                    </tr>";
            $categorys = $tree->get_tree(0, $str);
            $this->assign("categorys", $categorys);
            $this->display();
        }
        public function addBumen(){
            $is_rbac = _rbac_node_show(116);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $s = count(get_options('bumen_options','bumen_array'));
                $data['parentid']=I('parentid',0,'intval');
                $level = M('bumen')->where(array('id'=>$data['parentid']))->getField('level');
                if ($s<=$level) {
                    $this->error('此部门已是最后一级部门，不能添加子部门。');
                }
                $data['level']=$level+1;//重要的等级
                $data['cjshijian']=time();
                $data['bmming']=I('bmming');
                $data['bmdianhua']=I('bmdianhua');
                $data['bmdizhi']=I('bmdizhi');
                $data['gongsiid']=session('gongsiid');
                $isok = M('bumen')->add($data);
                if($isok){
                    data_rizhi(6,1,$isok,"","添加部门","","部门");
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            } else {
                $parentid = I("get.parentid",0,'intval');
                $select_categorys = bumen_option( $parentid, 0);
                $this->assign("select_categorys", $select_categorys);
                $this->display();
            }
        }
        public function editBumen(){
            $is_rbac = _rbac_node_show(118);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $id=I('id');
                $_POST['parentid']=I('parentid',0,'intval');
                $level = M('bumen')->where(array('id'=>$_POST['parentid']))->getField('level');
                $_POST['level']=$level+1;//重要的等级
                if (M('bumen')->where(array('id'=>$id,'gongsiid'=>session('gongsiid')))->save($_POST)) {
                    $this->success('修改成功'); 
                }else{
                    $this->error('修改失败');
                }
            } else {
                $id = I("get.id",0,'intval');
                $this->bumen = M('bumen')->where(array('id'=>$id))->find();
                $parentid = $this->bumen['parentid'];
                $select_categorys = bumen_option( $parentid, 0);
                $this->assign("select_categorys", $select_categorys);
                $this->bm=M('bumen')->where(array('id'=>$id))->find();
                $this->yonghu=M('yonghu')->where(array('bumen'=>$id,'gongsiid'=>session('gongsiid')))->select();
                $this->display();
            }
        }
        public function delBumen(){
            $is_rbac = _rbac_node_show(120);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $id = I('id');
            $isok = M('bumen')->where(array('id'=>$id))->delete();
            if ($isok) {
                data_rizhi(6,1,$id,"","删除部门","","部门");
                $this->success('删除成功'); 
            }else{
                $this->error('删除失败');
            }
        }
        public function xxzhuanyi(){
            $is_rbac = _rbac_btn_show(414,0,I('id'));
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $xxzy=I('xxzy');
            if (IS_POST) {
                $yid = I('yid');
                $uid = I('uid1');
                $bumen = I('bumen');
                $fkyid = I('fkyid');
                if(!$uid){
                    $this->error("请选择客源维护人！");
                }
                if($yid == $uid){
                    $this->error("不能转移给自己！");
                }
                if($fkyid==''){
                    $this->error("请先选中");
                }
                $yh = zym($yid,5);
                $name = zym($uid,5);
                if(is_array($fkyid)){
                    $where = 'id in('.implode(',',$fkyid).')';
                }else{
                    $where = 'id='.$fkyid;
                }
                if($xxzy == 1) {
                    $xxzyb=M('fangyuan');
                    $zys=$xxzyb->where(array($where))->count();
                    $zy=$xxzyb->where(array($where))->select();
                    foreach ($zy as $key => $value) {
                        $zybh.=$value['bianhao'].';';
                    }
                    $zyzy="房源";
                } elseif ($xxzy == 2) {
                    $xxzyb=M('keyuan');
                    $zys=$xxzyb->where(array($where))->count();
                    $zy=$xxzyb->where(array($where))->select();
                    foreach ($zy as $key => $value) {
                        $zybh.=$value['xqbianhao'].';';
                    }
                    $zyzy="客源";
                }
                $xiugai = "将用户[".$yh."]的".$zyzy."[".$zybh."]转移给[".$name."]";
                data_rizhi(8,23,$yid,zym($yid,15),$xiugai,"","用户");
                $this->success("成功转移".$zyzy.$zys."条");
            }else{
                $uid=I('id');
                if (!$uid) {
                    $this->error('未找到用户');
                }
                if($xxzy == 1) {
                    $Data=M('fangyuan');
                    $field=",bianhao,xiaoqum";
                } elseif ($xxzy == 2) {
                    $Data=M('keyuan');
                    $field=",xqbianhao,khxingming,kehulx";
                }
                $condition=" weihurenid=".$uid;
                $shijian1=I('shijian1');
                $shijian2=I('shijian2');
                if ($shijian1) {
                    $this->shijian1=$shijian1;
                    $shijian3=strtotime($shijian1);
                }
                if ($shijian2) {
                    $this->shijian2=$shijian2;
                    $shijian4=strtotime($shijian2)+86400;
                }
                if ($shijian3 and $shijian4){
                    if ($shijian3 > $shijian4){
                        $condition.=" and lurusj > ".$shijian4." and lurusj < ".$shijian3;
                        $this->shijian1=$shijian2;
                        $this->shijian2=$shijian1;
                    } else {
                        $condition.=" and lurusj > ".$shijian3." and lurusj < ".$shijian4;
                    }
                } elseif ($shijian3) {
                    $condition.=" and lurusj > ".$shijian3;
                } elseif ($shijian4) {
                    $condition.=" and lurusj < ".$shijian4;
                }
                $count=$Data->where($condition)->count();
                $page= new\Think\Page($count,30);
                $show=$page->show();
                $fkyzy=$Data->where($condition)->field('id,weihurenid,lurusj'.$field)->order('lurusj desc')->limit($page->firstRow.','.$page->listRows)->select();
                $this->assign('firstRow',$Page->firstRow);
                $this->assign('fkyzy',$fkyzy);
                $this->assign('count',$count);
                $this->assign('page',$show);
                $bumen = I("get.bumen",0,'intval');
                $select_categorys = bumen_option( $bumen, 414);
                $this->assign("select_categorys", $select_categorys);
                $this->display();
            }
        }
        //用户日志
        public function rizhi(){
            if (I('is_ajax')) {
                /*权限控制开始*/
                $condition = "e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(442,0);
                if ($x && $x != "all" && $x != "me") {
                    $condition .= " and e.bumen in ({$x})";
                } elseif ($x == "me") {
                    $condition .= " and e.id=".session('uid');
                } elseif ($x == false) {
                    $condition .= " and e.id=0";
                }
                /*权限控制结束*/
                $sousuo = I('sousuo');
                $condition .= " and a.zy_type = 8";
                $shijian = I('shijian');
                if ($shijian) {
                    $time_s = explode(' - ',$shijian);
                    $time_s1 = strtotime($time_s[0]);
                    $time_s2 = strtotime($time_s[1])+86400;
                    $condition .= " and a.addtime>=".$time_s1." and a.addtime<".$time_s2;
                }
                if ($sousuo) {
                    $condition .= " and (e.pinyinjs like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or a.zy_remark like '%$sousuo%')";
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $rzpx = "order by a.".$field." ".$order;
                } else {
                    $rzpx = "order by a.addtime desc";
                }
                $Model = new \Think\Model();
                $count = $Model->query("select count(*) from __RIZHI__ a join __YONGHU__ e on a.uid=e.id where {$condition}");
                $Page = new \Think\Page($count['0']['count(*)'],$limit);
                $list = "select a.*,e.ygmingcheng,e.bumen from __RIZHI__ a join __YONGHU__ e on a.uid=e.id where {$condition} ".$rzpx." limit ".$Page->firstRow.','.$Page->listRows;
                $data = $Model->query($list);
                foreach ($data as $key => $value) {
                    switch ($value['cz_type']) {
                        case '1':
                            $data[$key]['cz_type'] = "新增";
                            break;
                        case '2':
                            $data[$key]['cz_type'] = "基础修改";
                            break;
                        case '8':
                            $data[$key]['cz_type'] = "删除";
                            break;
                        case '18':
                            $data[$key]['cz_type'] = "审核";
                            break;
                        case '21':
                            $data[$key]['cz_type'] = "停启用";
                            break;
                        case '23':
                            $data[$key]['cz_type'] = "信息转移";
                            break;
                        default:
                            $data[$key]['cz_type'] = "——";
                            break;
                    }
                    $data[$key]['bumen'] = zym($value['bumen'],6);
                    $data[$key]['addtime'] = date('Y-m-d H:i:s',$value['addtime']);
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count['0']['count(*)'],
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->display();// 输出模板
            }
        }
        public function mac(){
            $this->sqlist1=M('mac')->where('shenhe is null')->select();
            $this->sqlist2=M('mac')->where('shenhe = 1')->select();
            $this->display();
        }
        public function macShenhe(){
            $id=I('id');
            $shenhe=M('Mac')->where(array('id'=>$id))->setField('shenhe',1);
            if ($shenhe) {
                data_rizhi(6,18,$id,"","电脑审核","","加密锁");
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        public function macShanchu(){
            $id=I('id');
            $Shanchu=M('Mac')->where(array('id'=>$id))->delete();
            if ($Shanchu) {
                data_rizhi(6,8,$id,"","删除电脑","","加密锁");
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        //可选岗位
        public function kxrole(){
            $is_rbac = _rbac_node_show(36);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $id=I('id');
            if (IS_POST) {
                $roleids=I('duoxuan');
                foreach ($roleids as $key => $value) {
                    $arr[]=$value;
                }
                $ids=implode(",",$arr);
                $isok=M('role')->where(array('id'=>$id))->setField('kxrole',$ids);
                if ($isok) {
                    data_rizhi(6,2,$id,"","设置".zym($rid,8)."的可选岗位","","岗位");
                    $this->success('配置成功');
                }else{
                    $this->error('配置失败');
                }
            }else{
                $this->role=yhkxrole(0,1);
                $this->roleid=yhkxrole($id,2);
                $this->rolefl=get_peizhi('rolefl',0,1);
                $this->display();
            }
        }
    }
?>