<?php

/***************************************************************\
|                                                               |
|   房佳（专业的新房分销ERP管理系统）                              |
|                                                               |
|   © 2019 http://fj.ytsofts.cn/ All rights reserved.           |
|                                                               |
|   本系统由淮南市银泰软件科技有限公司提供技术支持                 |
|                                                               |
|   QQ号：984784483                                               |
|                                                               |
\***************************************************************/

namespace Admin\Controller;
use Think\Controller;
class OptionsController extends CommonController {
    //用户关联
    public function yhgxbd(){
        $is_rbac = _rbac_node_show(916);
        if (!$is_rbac) {
            $this->redirect('Index/error');
        }
        $map = "a.gongsiid=".session('gongsiid')." and a.status=1 and a.lock1=0";
        $role=I('role');
        $sousuo=I('sousuo');
        if ($sousuo) {
            $map.=" and (a.dianhua like '%$sousuo%' or a.pinyinjs like '%$sousuo%' or a.ygmingcheng like '%$sousuo%' or a.ygbianhao like '%$sousuo%')";
        }
        if ($role == 2) {
            $map .= " and a.leixing = 2";
        } else if ($role == 3) {
            $map .= " and a.leixing = 3";
        } else {
            $map .= " and (a.leixing = 2 or a.leixing = 3)";
        }
        $count=M('yonghu')->query("select count(*) from __YONGHU__ a where {$map}");
        $Page=new \Think\Page($count['0']['count(*)'],30);
        $show=$Page->show();
        $list="select a.id,a.bumen,a.ygmingcheng,a.dianhua,a.ygbianhao,a.xingbie,a.leixing,c.remark from __YONGHU__ a left join __ROLE_USER__ as b on a.id=b.user_id left join __ROLE__ c on b.role_id=c.id where {$map} order by a.pinyinjs asc limit ".$Page->firstRow.','.$Page->listRows;
        $user=M('yonghu')->query($list);
        $this->assign('firstRow',$Page->firstRow);
        $this->assign('user',$user);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display();
    }
    //小区指定销售
    public function xqzdxs(){
        $is_rbac = _rbac_node_show(917);
        if (!$is_rbac) {
            $this->redirect('Index/error');
        }
        if (IS_POST) {
            $uid=I('uid');
            $zdids=I('zdids');
            foreach ($zdids as $key => $value) {
                $zdid=explode("-",$value);
                $xiaoqu=M('xiaoqu')->where(array('id'=>$zdid[0]))->field('adduid,xiaoqum,bdbumen')->find();
                $user=M('yonghu')->where(array('id'=>$xiaoqu['adduid']))->field('lock1,status')->find();
                $role=M('yonghu')->where(array('id'=>$zdid[1]))->field('bumen,leixing,lock1,status')->find();
                if ($user['lock1'] == 1) {
                    $this->error($xiaoqu['xiaoqum'].'负责人被停用！');
                }
                if ($user['status'] == 0) {
                    $this->error($xiaoqu['xiaoqum'].'负责人不存在！');
                }
                if ($role['lock1'] == 1) {
                    $this->error($xiaoqu['xiaoqum'].'指定销售被停用！');
                }
                if ($role['status'] == 0) {
                    $this->error($xiaoqu['xiaoqum'].'指定销售不存在！');
                }
                if ($role['bumen'] != $xiaoqu['bdbumen'] || $role['leixing'] != 2) {
                    $this->error($xiaoqu['xiaoqum'].'指定销售异常！');
                }
            }
            foreach ($zdids as $key => $value) {
                $zdid=explode("-",$value);
                $yhm=M('yonghu')->where(array('id'=>$uid))->getField('ygmingcheng');
                $xqbdxs=M('xqbdxs')->where(array('uid'=>$uid,'xqid'=>$zdid[0]))->getField('id');
                $xiaoqu=M('xiaoqu')->where(array('id'=>$zdid[0]))->field('adduid,xiaoqum')->find();
                $role=M('yonghu')->where(array('id'=>$zdid[1]))->field('dianhua,ygmingcheng')->find();
                $user=M('yonghu')->where(array('id'=>$xiaoqu['adduid'],'lock1'=>0,'status'=>1))->field('bumen,dianhua,ygmingcheng')->find();
                $data['uid']=$uid;
                $data['xqid']=$zdid[0];
                $data['xsid']=$zdid[1];
                $data['fzr']=$user['ygmingcheng'];
                $data['fzdh']=$user['dianhua'];
                $data['bumen']=$user['bumen'];
                $data['xsdh']=$role['dianhua'];
                $data['xsm']=$role['ygmingcheng'];
                $data['xqtjr']=$xiaoqu['adduid'];
                $data['xiaoqum']=$xiaoqu['xiaoqum'];
                $data['addtime']=time();
                $data['adduid']=session('uid');
                $data['gongsiid']=session('gongsiid');
                $xiugai.=$yhm."在小区".$xiaoqu['xiaoqum']."的指定销售为".$role['ygmingcheng']."；";
                if ($xqbdxs) {
                    $fzky=M('fzky')->where(array('adduid'=>$uid,'xqid'=>$zdid[0],'weihurenid'=>array('neq',$zdid[1])))->setField('weihurenid',$zdid[1]);
                    $isok=M("xqbdxs")->where(array('id'=>$xqbdxs))->save($data);
                } else {
                    $fzky=M('fzky')->where(array('adduid'=>$uid,'xqid'=>$zdid[0],'weihurenid'=>array('neq',$zdid[1])))->setField('weihurenid',$zdid[1]);
                    $isok=M("xqbdxs")->add($data);
                }
            }
            if ($isok) {
                $data_rizhi['zy_type']=8;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=27;
                $data_rizhi['zy_id']=$uid;
                $data_rizhi['neirong']=$xiugai;
                M('rizhi')->add($data_rizhi);
                $this->success('绑定成功！');
            }else{
                $this->error('绑定失败！');
            }
        } else {
            $uid=I('uid');
            $role=M('yonghu')->where(array('id'=>$uid))->field('bumen,leixing')->find();
            if ($role['leixing'] != 2 && $role['leixing'] != 3) {
                $this->error('用户数据异常');
            } else {
                $this->xqzdxs=1;
            }
            if ($role['leixing'] == 2) {
                $xqid=M('xiaoqu')->where(array('bdbumen'=>$role['bumen']))->getField('id');
                if (!$xqid) {
                    $this->error('小区数据异常');
                }
            }
            $map = "a.gongsiid=".session('gongsiid');
            $sousuo=I('sousuo');
            if ($sousuo) {
                $map.=" and (a.xiaoqum like '%$sousuo%' or a.pinyinjs like '%$sousuo%')";
            }
            if ($xqid) {
                $map.=" and a.id <> ".$xqid;
            }
            $list="select a.id,a.xiaoqum,b.ygmingcheng as xqfzr,b.dianhua as xqfzdh from __XIAOQU__ a left join __YONGHU__ b on a.adduid=b.id where {$map} order by a.pinyinjs asc ";
            $xiaoqu=M('xiaoqu')->query($list);
            foreach ($xiaoqu as $key => $value) {
                $xsid=M('xqbdxs')->where(array('uid'=>$uid,'xqid'=>$value['id']))->getField('xsid');
                $xiaoqu[$key]['xsid'] = $xsid;
            }
            $this->assign('xiaoqu',$xiaoqu);
            $this->ygmingcheng=M('yonghu')->where(array('id'=>$uid))->getField('ygmingcheng');
            $map1 = "a.gongsiid=".session('gongsiid')." and a.lock1=0 and a.status=1 and a.leixing=2";
            $listyh="select a.id,a.ygmingcheng,a.dianhua,b.id as xqid from __YONGHU__ a join __XIAOQU__ b on a.bumen=b.bdbumen where {$map1} order by a.pinyinjs asc ";
            $yonghu=M('yonghu')->query($listyh);
            $this->assign('yonghu',$yonghu);
            $this->display();
        }
    }
}