<?php

/***************************************************************\
|                                                               |
| 开单大师（专业的房产ERP管理系统）                             |
|                                                               |
| © 2018 http://fs.ytsofts.cn All rights reserved.        |
|                                                               |
| 本系统由淮南市银泰软件科技有限公司提供技术支持                |
|                                                               |
| QQ号：984784483                                               |
|                                                               |
\***************************************************************/

namespace Admin\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        $company_id = session('gongsiid');
        //p($menu);die;
        if(session('admin') == 1){
            $role_id=M('role_user')->where(array('gongsiid'=>session("gongsiid"),'user_id'=>session('uid')))->getField('role_id');
            $sql = "select a.* from  __MENU__  as a join  __NODES__  as b where a.status=1 and b.company_id = {$company_id} and a.node_id=b.node_id order by a.listorder asc";
            $Model = new \Think\Model();
            $smenu=$Model->query($sql);
            $pmenu=M('Menu')->where(array('type'=>array('neq',2),'status'=>1))->select();
            $menu=array_merge_recursive($smenu,$pmenu);
            $menu=array_sort($menu,'listorder','asc');
        }else{
            $role_id=M('role_user')->where(array('gongsiid'=>session("gongsiid"),'user_id'=>session('uid')))->getField('role_id');
            $sql = "select a.* from  __MENU__ as a join __ACCESS__  as b join __NODES__  as c where a.status=1 and c.company_id = {$company_id} and a.node_id=b.node_id and b.node_id = c.node_id and b.role_id = '{$role_id}' order by a.listorder asc";
            $Model = new \Think\Model();
            $smenu=$Model->query($sql);
            $pmenu=M('Menu')->where(array('type'=>array('neq',2),'status'=>1))->select();
            $menu=array_merge_recursive($smenu,$pmenu);
            $menu=array_sort($menu,'listorder','asc');
        }
        $result = $menu;
        foreach ($result as $n=> $r) {
            if ($r['type']==2) {
                $name = ucwords($r['app']);
                $model = ucwords($r['model']);
                $action = $r['action'];
                //附带参数
                $params = "";
                if ($r['data']) {
                    $params = "?" . htmlspecialchars_decode($r['data']);
                }
                $result[$n]['url'] = U("{$name}/{$model}/{$action}{$params}");
            }elseif ($r['type']==3) {
                if ($r['blank']) {
                    $params =  htmlspecialchars_decode($r['blank']);
                }
                $result[$n]['url'] = $params;
            }
        }
        $tree = new \Org\Util\Tree();
        $tree->init($result);
        $categorys = $tree->get_tree_array(0);

        //去除空数组
        foreach ($categorys as $key => $value) {
            foreach ($value['child'] as $k => $v) {
                if (count($v['child']) == 0) {
                    unset($categorys[$key]['child'][$k]);
                }
            }
            if (count($categorys[$key]['child']) == 0) {
                unset($categorys[$key]);
            }
        }
        $this->user = M('yonghu')->where('id='.session('uid'))->find();
        $this->categorys = $categorys;
        //p($categorys);die;
        /*$this->pkzt();
        $this->zdkp();
        $this->fkypz();*/
        $this->display();
    }
    public function pkzt(){
        $jintian=date("Y-m-d",time());
        $jinri=strtotime($jintian);
        $zhpksj=M("fkypz")->where(array('gongsiid'=>session("gongsiid")))->getField('zhpksj');
        if ($jinri != $zhpksj) {
            $pkks=M("pkgame")->where(array('gongsiid'=>session("gongsiid"),'btime'=>$jinri))->select();
            foreach ($pkks as $key => $value) {
                M("pkgame")->where(array('gongsiid'=>session("gongsiid"),'id'=>$value['id']))->setField('pkstatus',2);
            }
            $pkjs=M("pkgame")->where(array('gongsiid'=>session("gongsiid"),'etime'=>$jinri))->select();
            foreach ($pkjs as $key => $value) {
                M("pkgame")->where(array('gongsiid'=>session("gongsiid"),'id'=>$value['id']))->setField('pkstatus',3);
            }
        }
        M("fkypz")->where(array('gongsiid'=>session("gongsiid")))->setField('zhpksj',$jinri);
    }
    public function zdkp(){
        $jintian=date("Y-m-d",time());
        $jinri=strtotime($jintian);
        $zhkpsj=M("fkypz")->where(array('gongsiid'=>session("gongsiid")))->getField('zhkpsj');
        if ($jinri != $zhkpsj) {
            $fpkp=M("fpkp")->where(array('gongsiid'=>session("gongsiid"),'endunix'=>$jinri,'czlx'=>1,'type'=>array('neq',2)))->select();
            foreach ($fpkp as $key => $value) {
                if ($value['zylx'] == 1) {
                    $zyb=M('fangyuan');
                } else if ($value['zylx'] == 2) {
                    $zyb=M('keyuan');
                }
                if ($value['fkplx'] == 1) {
                    $fpzd="fpdh";
                } else if ($value['fkplx'] == 2) {
                    $fpzd="fpdz";
                } else if ($value['fkplx'] == 3) {
                    $fpzd="fpdj";
                } else if ($value['fkplx'] == 4) {
                    $fpzd="fpdq";
                } else if ($value['fkplx'] == 5) {
                    $fpzd="fpck";
                }
                $zyb->where(array('gongsiid'=>session("gongsiid"),'id'=>$value['zyid']))->setField($fpzd,2);
                $zyb->where(array('gongsiid'=>session("gongsiid"),'id'=>$value['zyid']))->setField('fengpanrenid','');
            }
        }
        M("fkypz")->where(array('gongsiid'=>session("gongsiid")))->setField('zhkpsj',$jinri);
    }
    public function fkypz(){
        $fkypz=M("fkypz")->where(array('gongsiid'=>session("gongsiid")))->find();
        $xzshijian=time();
        $jintian=date("Y-m-d",$xzshijian);
        $jinri=strtotime($jintian);
        $options_arr = get_options('fkyzt_config',0);
        if ($jinri != $fkypz['zhzxsj']) {
            if($fkypz['csgj'] == 1){
                $csgjsj=$xzshijian-$fkypz['csgjsj'];
                $csgjwcj=M("fangyuan")->where("gongsiid = ".session('gongsiid')." and leixing = 1 and zhuangtai > 0 and zhuangtai not in (".$options_arr['fyfengpan_zhuangtai'].") and zhgjsj <".$csgjsj." and isdel = 2")->setField('zhuangtai',$options_arr['fygongpan_zhuangtai']);
            }
            if(($fkypz['csgj'] == 0) && ($fkypz['csxz'] == 1)){
                $csxzsj=$xzshijian-$fkypz['csxzsj'];
                $csxzwcj=M("fangyuan")->where("gongsiid = ".session('gongsiid')." and leixing = 1 and zhuangtai > 0 and zhuangtai not in (".$options_arr['fyfengpan_zhuangtai'].") and xiugaisj <".$csxzsj." and isdel = 2")->setField('zhuangtai',$options_arr['fygongpan_zhuangtai']);
            }
            if(($fkypz['csgj'] == 0) && ($fkypz['csxz'] == 0) && ($fkypz['cslr'] == 1)){
                $cslrsj=$xzshijian-$fkypz['cslrsj'];
                $cslrwcj=M("fangyuan")->where("gongsiid = ".session('gongsiid')." and leixing = 1 and zhuangtai > 0 and zhuangtai not in (".$options_arr['fyfengpan_zhuangtai'].") and lurusj <".$cslrsj." and isdel = 2")->setField('zhuangtai',$options_arr['fygongpan_zhuangtai']);
            }
            if($fkypz['czgj'] == 1){
                $czgjsj=$xzshijian-$fkypz['czgjsj'];
                $czgjwcj=M("fangyuan")->where("gongsiid = ".session('gongsiid')." and leixing = 2 and zhuangtai > 0 and zhuangtai not in (".$options_arr['fyfengpan_zhuangtai'].") and zhgjsj <".$czgjsj." and isdel = 2")->setField('zhuangtai',$options_arr['fygongpan_zhuangtai']);
            }
            if(($fkypz['czgj'] == 0) && ($fkypz['czxz'] == 1)){
                $czxzsj=$xzshijian-$fkypz['czxzsj'];
                $czxzwcj=M("fangyuan")->where("gongsiid = ".session('gongsiid')." and leixing = 2 and zhuangtai > 0 and zhuangtai not in (".$options_arr['fyfengpan_zhuangtai'].") and xiugaisj <".$czxzsj." and isdel = 2")->setField('zhuangtai',$options_arr['fygongpan_zhuangtai']);
            }
            if(($fkypz['czgj'] == 0) && ($fkypz['czxz'] == 0) && ($fkypz['czlr'] == 1)){
                $czlrsj=$xzshijian-$fkypz['czlrsj'];
                $czlrwcj=M("fangyuan")->where("gongsiid = ".session('gongsiid')." and leixing = 2 and zhuangtai > 0 and zhuangtai not in (".$options_arr['fyfengpan_zhuangtai'].") and lurusj <".$czlrsj." and isdel = 2")->setField('zhuangtai',$options_arr['fygongpan_zhuangtai']);
            }
            if($fkypz['qggj'] == 1){
                $qggjsj=$xzshijian-$fkypz['qggjsj'];
                $qggjwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 1 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and zhgjsj <".$qggjsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if(($fkypz['qggj'] == 0) && ($fkypz['qgxz'] == 1)){
                $qgxzsj=$xzshijian-$fkypz['qgxzsj'];
                $qgxzwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 1 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and xiugaisj <".$qgxzsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if(($fkypz['qggj'] == 0) && ($fkypz['qgxz'] == 0) && ($fkypz['qglr'] == 1)){
                $qglrsj=$xzshijian-$fkypz['qglrsj'];
                $qglrwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 1 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and lurusj <".$qglrsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if($fkypz['qzgj'] == 1){
                $qzgjsj=$xzshijian-$fkypz['qzgjsj'];
                $qzgjwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 2 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and zhgjsj <".$qzgjsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if(($fkypz['qzgj'] == 0) && ($fkypz['qzxz'] == 1)){
                $qzxzsj=$xzshijian-$fkypz['qzxzsj'];
                $qzxzwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 2 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and xiugaisj <".$qzxzsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if(($fkypz['qzgj'] == 0) && ($fkypz['qzxz'] == 0) && ($fkypz['qzlr'] == 1)){
                $qzlrsj=$xzshijian-$fkypz['qzlrsj'];
                $qzlrwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 2 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and lurusj <".$qzlrsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if($fkypz['ysgj'] == 1){
                $ysgjsj=$xzshijian-$fkypz['ysgjsj'];
                $ysgjwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 3 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and zhgjsj <".$ysgjsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if(($fkypz['ysgj'] == 0) && ($fkypz['ysxz'] == 1)){
                $ysxzsj=$xzshijian-$fkypz['ysxzsj'];
                $ysxzwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 3 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and xiugaisj <".$ysxzsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            if(($fkypz['ysgj'] == 0) && ($fkypz['ysxz'] == 0) && ($fkypz['yslr'] == 1)){
                $yslrsj=$xzshijian-$fkypz['yslrsj'];
                $yslrwcj=M("keyuan")->where("gongsiid = ".session('gongsiid')." and mmzl = 3 and zhuangtai > 0 and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].") and lurusj <".$yslrsj." and isdel = 2")->setField('zhuangtai',$options_arr['kygongpan_zhuangtai']);
            }
            M("fkypz")->where(array('gongsiid'=>session("gongsiid")))->setField('zhzxsj',$jinri);
        }
    }
    public function xiaoxi(){
        $Model = new \Think\Model();
        $is_rbac = _rbac_node_show(452);
        if ($is_rbac) {
            $map = "gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(452,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";
            }
            $sqlcount = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0";
            $countb=$Model->query($sqlcount);
            $sh_count=$countb[0]['count(*)'];
            $arr['sh_count']=$sh_count;
        }
        $is_rbac1 = _rbac_node_show(591);
        if ($is_rbac1) {
            $map1 = "gongsiid=".session('gongsiid');
            $x1 = _rbac_lists_where(591,0);
            if ($x1 && $x1 != "all" && $x1 != "me") {
                $map1 .= " and bumen in ({$x1})";
            }elseif ($x1 == "me") {
                $map1 .= " and id=".session('uid');
            }elseif ($x1 == false) {
                $map1 .= " and id=0";
            }
            $sqlcount1 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map1}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=1";
            $countb1=$Model->query($sqlcount1);
            $sh_count1=$countb1[0]['count(*)'];
            $arr['sh_count1']=$sh_count1;
        }
        $is_rbac2 = _rbac_node_show(594);
        if ($is_rbac2) {
            $map2 = "gongsiid=".session('gongsiid');
            $x2 = _rbac_lists_where(594,0);
            if ($x2 && $x2 != "all" && $x2 != "me") {
                $map2 .= " and bumen in ({$x2})";
            }elseif ($x2 == "me") {
                $map2 .= " and id=".session('uid');
            }elseif ($x2 == false) {
                $map2 .= " and id=0";
            }
            $sqlcount2 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map2}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=2";
            $countb2=$Model->query($sqlcount2);
            $sh_count2=$countb2[0]['count(*)'];
            $arr['sh_count2']=$sh_count2;
        }
        $is_rbac3 = _rbac_node_show(597);
        if ($is_rbac3) {
            $map3 = "gongsiid=".session('gongsiid');
            $x3 = _rbac_lists_where(597,0);
            if ($x3 && $x3 != "all" && $x3 != "me") {
                $map3 .= " and bumen in ({$x3})";
            }elseif ($x3 == "me") {
                $map3 .= " and id=".session('uid');
            }elseif ($x3 == false) {
                $map3 .= " and id=0";
            }
            $sqlcount3 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map3}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=3";
            $countb3=$Model->query($sqlcount3);
            $sh_count3=$countb3[0]['count(*)'];
            $arr['sh_count3']=$sh_count3;
        }
        $is_rbac4 = _rbac_node_show(600);
        if ($is_rbac4) {
            $map4 = "gongsiid=".session('gongsiid');
            $x4 = _rbac_lists_where(600,0);
            if ($x4 && $x4 != "all" && $x4 != "me") {
                $map4 .= " and bumen in ({$x4})";
            }elseif ($x4 == "me") {
                $map4 .= " and id=".session('uid');
            }elseif ($x4 == false) {
                $map4 .= " and id=0";
            }
            $sqlcount4 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map4}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=4";
            $countb4=$Model->query($sqlcount4);
            $sh_count4=$countb4[0]['count(*)'];
            $arr['sh_count4']=$sh_count4;
        }
        $is_rbac5 = _rbac_node_show(603);
        if ($is_rbac5) {
            $map5 = "gongsiid=".session('gongsiid');
            $x5 = _rbac_lists_where(603,0);
            if ($x5 && $x5 != "all" && $x5 != "me") {
                $map5 .= " and bumen in ({$x5})";
            }elseif ($x5 == "me") {
                $map5 .= " and id=".session('uid');
            }elseif ($x5 == false) {
                $map5 .= " and id=0";
            }
            $sqlcount5 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map5}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=5";
            $countb5=$Model->query($sqlcount5);
            $sh_count5=$countb5[0]['count(*)'];
            $arr['sh_count5']=$sh_count5;
        }
        $is_rbac6 = _rbac_node_show(606);
        if ($is_rbac6) {
            $map6 = "gongsiid=".session('gongsiid');
            $x6 = _rbac_lists_where(606,0);
            if ($x6 && $x6 != "all" && $x6 != "me") {
                $map6 .= " and bumen in ({$x6})";
            }elseif ($x6 == "me") {
                $map6 .= " and id=".session('uid');
            }elseif ($x6 == false) {
                $map6 .= " and id=0";
            }
            $sqlcount6 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map6}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=6";
            $countb6=$Model->query($sqlcount6);
            $sh_count6=$countb6[0]['count(*)'];
            $arr['sh_count6']=$sh_count6;
        }
        $is_rbac7 = _rbac_node_show(609);
        if ($is_rbac7) {
            $map7 = "gongsiid=".session('gongsiid');
            $x7 = _rbac_lists_where(609,0);
            if ($x7 && $x7 != "all" && $x7 != "me") {
                $map7 .= " and bumen in ({$x7})";
            }elseif ($x7 == "me") {
                $map7 .= " and id=".session('uid');
            }elseif ($x7 == false) {
                $map7 .= " and id=0";
            }
            $sqlcount7 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map7}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=7";
            $countb7=$Model->query($sqlcount7);
            $sh_count7=$countb7[0]['count(*)'];
            $arr['sh_count7']=$sh_count7;
        }
        $is_rbac8 = _rbac_node_show(612);
        if ($is_rbac8) {
            $map8 = "gongsiid=".session('gongsiid');
            $x8 = _rbac_lists_where(612,0);
            if ($x8 && $x8 != "all" && $x8 != "me") {
                $map8 .= " and bumen in ({$x8})";
            }elseif ($x8 == "me") {
                $map8 .= " and id=".session('uid');
            }elseif ($x8 == false) {
                $map8 .= " and id=0";
            }
            $sqlcount8 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map8}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=8";
            $countb8=$Model->query($sqlcount8);
            $sh_count8=$countb8[0]['count(*)'];
            $arr['sh_count8']=$sh_count8;
        }
        $is_rbac9 = _rbac_node_show(615);
        if ($is_rbac9) {
            $map9 = "gongsiid=".session('gongsiid');
            $x9 = _rbac_lists_where(615,0);
            if ($x9 && $x9 != "all" && $x9 != "me") {
                $map9 .= " and bumen in ({$x9})";
            }elseif ($x9 == "me") {
                $map9 .= " and id=".session('uid');
            }elseif ($x9 == false) {
                $map9 .= " and id=0";
            }
            $sqlcount9 = "select count(*) FROM __YWSHENPI__ as a join (select id from __YONGHU__ where {$map9}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and sh_zhuangtai=0 and yw_type=9";
            $countb9=$Model->query($sqlcount9);
            $sh_count9=$countb9[0]['count(*)'];
            $arr['sh_count9']=$sh_count9;
        }
        $map10 = "gongsiid=".session('gongsiid');
        $map10 .= " and txuid=".session('uid');
        $map10 .= " and txshijian<".time();
        $map10 .= " and isyidu=0";
        $sqlcount10 = "select count(*) FROM __KYTIXING__ where {$map10}";
        $countb10=$Model->query($sqlcount10);
        $sh_count10=$countb10[0]['count(*)'];
        $arr['sh_count10']=$sh_count10;
        $map11 = "gongsiid=".session('gongsiid');
        $map11 .= " and txuid=".session('uid');
        $map11 .= " and txshijian<".time();
        $map11 .= " and isyidu=0";
        $sqlcount11 = "select count(*) FROM __FJTIXING__ where {$map11}";
        $countb11=$Model->query($sqlcount11);
        $sh_count11=$countb11[0]['count(*)'];
        $arr['sh_count11']=$sh_count11;
        $is_rbac12 = _rbac_node_show(841);
        if ($is_rbac12) {
            $map12 = "gongsiid=".session('gongsiid');
            $x12 = _rbac_lists_where(841,0);
            if ($x12 && $x12 != "all" && $x12 != "me") {
                $map12 .= " and bumen in ({$x12})";
            }elseif ($x12 == "me") {
                $map12 .= " and id=".session('uid');
            }elseif ($x12 == false) {
                $map12 .= " and id=0";
            }
            $sqlcount12 = "select count(*) FROM __FPKP__ as a join (select id from __YONGHU__ where {$map12}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and type=3";
            $countb12=$Model->query($sqlcount12);
            $sh_count12=$countb12[0]['count(*)'];
            $arr['sh_count12']=$sh_count12;
        }
        $is_rbac13 = _rbac_node_show(844);
        if ($is_rbac13) {
            $map13 = "gongsiid=".session('gongsiid');
            $x13 = _rbac_lists_where(844,0);
            if ($x13 && $x13 != "all" && $x13 != "me") {
                $map13 .= " and bumen in ({$x13})";
            }elseif ($x13 == "me") {
                $map13 .= " and id=".session('uid');
            }elseif ($x13 == false) {
                $map13 .= " and id=0";
            }
            $sqlcount13 = "select count(*) FROM __FPKP__ as a join (select id from __YONGHU__ where {$map13}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and zylx=1 and czlx=1 and type=3";
            $countb13=$Model->query($sqlcount13);
            $sh_count13=$countb13[0]['count(*)'];
            $arr['sh_count13']=$sh_count13;
        }
        $is_rbac14 = _rbac_node_show(847);
        if ($is_rbac14) {
            $map14 = "gongsiid=".session('gongsiid');
            $x14 = _rbac_lists_where(847,0);
            if ($x14 && $x14 != "all" && $x14 != "me") {
                $map14 .= " and bumen in ({$x14})";
            }elseif ($x14 == "me") {
                $map14 .= " and id=".session('uid');
            }elseif ($x14 == false) {
                $map14 .= " and id=0";
            }
            $sqlcount14 = "select count(*) FROM __FPKP__ as a join (select id from __YONGHU__ where {$map14}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and zylx=1 and czlx=2 and type=3";
            $countb14=$Model->query($sqlcount14);
            $sh_count14=$countb14[0]['count(*)'];
            $arr['sh_count14']=$sh_count14;
        }
        $is_rbac15 = _rbac_node_show(850);
        if ($is_rbac15) {
            $map15 = "gongsiid=".session('gongsiid');
            $x15 = _rbac_lists_where(850,0);
            if ($x15 && $x15 != "all" && $x15 != "me") {
                $map15 .= " and bumen in ({$x15})";
            }elseif ($x15 == "me") {
                $map15 .= " and id=".session('uid');
            }elseif ($x15 == false) {
                $map15 .= " and id=0";
            }
            $sqlcount15 = "select count(*) FROM __FPKP__ as a join (select id from __YONGHU__ where {$map15}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and zylx=2 and czlx=1 and type=3";
            $countb15=$Model->query($sqlcount15);
            $sh_count15=$countb15[0]['count(*)'];
            $arr['sh_count15']=$sh_count15;
        }
        $is_rbac16 = _rbac_node_show(853);
        if ($is_rbac16) {
            $map16 = "gongsiid=".session('gongsiid');
            $x16 = _rbac_lists_where(853,0);
            if ($x16 && $x16 != "all" && $x16 != "me") {
                $map16 .= " and bumen in ({$x16})";
            }elseif ($x16 == "me") {
                $map16 .= " and id=".session('uid');
            }elseif ($x16 == false) {
                $map16 .= " and id=0";
            }
            $sqlcount16 = "select count(*) FROM __FPKP__ as a join (select id from __YONGHU__ where {$map16}) as b on a.uid=b.id where gongsiid = ".session("gongsiid")." and zylx=2 and czlx=2 and type=3";
            $countb16=$Model->query($sqlcount16);
            $sh_count16=$countb16[0]['count(*)'];
            $arr['sh_count16']=$sh_count16;
        }
        if ($sh_count) {
            $ywshzs=$sh_count;
        } else {
            $ywshzs=$sh_count1+$sh_count2+$sh_count3+$sh_count4+$sh_count5+$sh_count6+$sh_count7+$sh_count8+$sh_count9;
        }
        if ($sh_count12) {
            $fkpshzs=$sh_count12;
        } else {
            $fkpshzs=$sh_count13+$sh_count14+$sh_count15+$sh_count16;
        }
        $arr['sh_countz']=$ywshzs+$sh_count10+$sh_count11+$fkpshzs;
        $data=array('data'=>$arr);
        header("Content-type: application/json");
        exit(json_encode($data));
    }
    public function validatepass(){
        if (trim(I('passwd'))=='') {
            $this->error('密码不能为空');
        }
        $passwd = md5(trim(I('passwd')));
        $uid = session('uid');
        $r = M('Yonghu')->where(array('gongsiid'=>session("gongsiid"),'id'=>$uid,'mima'=>$passwd))->getField('id');
        //返回查询结果到异步json
        $data=array('r'=>$r);
        header("Content-type: application/json");
        exit(json_encode($data)); 
    }
    public function updatepass(){
        if (IS_POST) {
            $ymima=I('ymima');
            $xmima=I('xmima');
            $qrmima=I('qrmima');
            $yymima=M('Yonghu')->where(array('gongsiid'=>session("gongsiid"),'id'=>session('uid')))->getField('mima');
            if ($yymima!=md5($ymima)) {
                $this->error('原密码错误!');
            }
            if ($xmima!=$qrmima) {
                $this->error('两次密码不匹配!');
            }
            $passwd = md5($xmima);
            $uid = session('uid');
            $r = M('Yonghu')->where(array('gongsiid'=>session("gongsiid"),'id'=>$uid))->setField(array('mima'=>$passwd));
            if ($r) {
                $data_rizhi['zy_type']=8;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['gongsiid']=session("gongsiid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=2;
                $data_rizhi['neirong']="修改密码";
                M('rizhi')->add($data_rizhi);
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->display();
        }
    }
    public function welcome(){
        //$this->display('test');
        //die;
        
        //p($_SESSION);

        //本月第一天开始时间
        $gstmb=strtotime(date('Y-m-01',time()));
        //个人最新成交
        $this->chengjiao=M('chengjiao')->where(array('gongsiid'=>session("gongsiid"),'cjuid'=>session('uid')))->field('id,cjbianhao,dizhi,cjtime')->order('cjtime desc')->limit('6')->select();
        //门店最新成交
        $mdchengjiao="select id,cjbianhao,cjuid,b.ygmingcheng,dizhi,cjtime from (select * from __CHENGJIAO__ where gongsiid = ".session("gongsiid").") a join (select id as b_id,ygmingcheng,bumen from __YONGHU__ where gongsiid = ".session("gongsiid")." and bumen=".yhbm(session('uid'),1).") b on a.cjuid=b.b_id order by cjtime desc limit 6";
        $Model = new \Think\Model;
        $mdchengjiao=$Model->query($mdchengjiao);
        $this->assign('mdchengjiao',$mdchengjiao);
        //用户信息显示
        $this->yhtx=M('yonghu')->where(array('gongsiid'=>session("gongsiid"),'id'=>session('uid')))->getField('touxiang');
        //公告输出
        $map .= " where gongsiid=".session('gongsiid');
        $x = _rbac_lists_where(143,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";
        }
        $list="select a.*,e.ygmingcheng from (select * from __GSGONGGAO__ where gongsiid = ".session("gongsiid")." and ggleibie < 7) a join (select id as e_id,ygmingcheng,bumen from __YONGHU__ {$map}) e on a.ggren=e.e_id order by ggshijian desc limit 7 ";
        $Model = new \Think\Model;
        $Gsgonggao=$Model->query($list);
        $this->assign('gonggao',$Gsgonggao);
        //过户进度
        $guohu="select id,cjbianhao,cjuid,khname,dizhi,cjtime,a.count,c.count1 from (select * from __CHENGJIAO__ where gongsiid = ".session("gongsiid")." and cjuid=".session('uid').") b join (select cjbianhao as a_cjbianhao,count(*) as count from __GUOHU__ where gongsiid = ".session("gongsiid")." group by cjbianhao) a on a.a_cjbianhao=b.cjbianhao join (select cjbianhao as a_cjbianhao,count(*) as count1 from __GUOHU__ where gongsiid = ".session("gongsiid")." and zhuangtai=1 group by cjbianhao) c on b.cjbianhao=c.a_cjbianhao order by b.cjtime desc limit 6 ";
        $guohu=$Model->query($guohu);
        $this->assign('guohu',$guohu);
        //待办过户
        $guohu1="select id,cjbianhao,cjuid,khname,dizhi,cjtime,a.count from (select * from __CHENGJIAO__ where gongsiid = ".session("gongsiid")." and cjuid=".session('uid').") b left join (select cjbianhao as a_cjbianhao,count(*) as count from __GUOHU__ where gongsiid = ".session("gongsiid")." group by cjbianhao) a on a.a_cjbianhao=b.cjbianhao where a.count is null order by b.cjtime desc limit 6";
        $guohu1=$Model->query($guohu1);
        $this->assign('guohu1',$guohu1);
        //准意向客户
        $tmb=time()-7*24*3600;
        $tme=time();
        $yixiang="select id,khxingming,xqbianhao,a.count from (select * from __KEYUAN__ where gongsiid = ".session("gongsiid").") b join (select count(*) as count,kyid,dkuid,dkshijian from __KYDAIKAN__ where gongsiid = ".session("gongsiid")." and dkuid=".session('uid')." and dkshijian>".$tmb." and dkshijian<".$tme." group by kyid) a on a.kyid=b.id order by a.count desc limit 6 ";
        $yixiang=$Model->query($yixiang);
        $this->assign('yixiang',$yixiang);
        //公司业绩排行
        $gsyeji="select id,ygmingcheng,d.sum from (select * from __YONGHU__ where gongsiid = ".session("gongsiid").") c join (select uid, SUM( fcyj ) as sum, cjbianhao from (select * from __YEJI__ where gongsiid = ".session("gongsiid").") a join (select cjbianhao as b_cjbianhao,cjtime from __CHENGJIAO__ where gongsiid = ".session("gongsiid")." and cjtime >=".$gstmb." and zhuangtai =1) b on a.cjbianhao = b.b_cjbianhao group by a.uid) d on c.id = d.uid order by d.sum desc limit 5";
        $gsyeji=$Model->query($gsyeji);
        $this->assign('gsyeji',$gsyeji);
        //门店业绩排行
        $mdyeji="select id, ygmingcheng, d.sum from (select * from __YONGHU__ where gongsiid = ".session("gongsiid").") c join (select uid, SUM( fcyj ) as sum, cjbianhao from (select * from __YEJI__ where gongsiid = ".session("gongsiid").") a join (select cjbianhao as b_cjbianhao,cjtime from __CHENGJIAO__ where gongsiid = ".session("gongsiid")." and cjtime >=".$gstmb." and zhuangtai =1) b on a.cjbianhao = b.b_cjbianhao group by a.uid) d on c.id = d.uid where bumen=".session('bumen')." order by d.sum desc";
        $mdyeji=$Model->query($mdyeji);
        $this->assign('mdyeji',$mdyeji);
        //本月数据-客源（按分店统计）
        $kybmtj_sql="SELECT distinct syky.lry AS llry,byky.lry AS nlry,
        syky.bm, syky.kysl AS lky
        , ifnull(byky.kysl, 0) AS nky
        , round((ifnull(byky.kysl, 0) - ifnull(syky.kysl,0)) / ifnull(syky.kysl,1) * 100, 2) AS tbzz
        FROM (
            SELECT month(FROM_UNIXTIME(ky.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS kysl
            FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE ky.weihurenid = yh.id
            AND FROM_UNIXTIME(ky.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
            AND FROM_UNIXTIME(ky.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
            GROUP BY lry, yh.bumen
        ) syky
        LEFT JOIN (
            SELECT month(FROM_UNIXTIME(ky.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS kysl
            FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE ky.weihurenid = yh.id
            AND FROM_UNIXTIME(ky.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
            GROUP BY lry, yh.bumen
        ) byky
        ON syky.bm = byky.bm
        union
        SELECT distinct syky.lry AS llry,byky.lry AS nlry,
        byky.bm, syky.kysl AS lky
        , ifnull(byky.kysl, 0) AS nky
        , round((ifnull(byky.kysl, 0) - ifnull(syky.kysl,0)) / ifnull(syky.kysl,1) * 100, 2) AS tbzz
        FROM (
            SELECT month(FROM_UNIXTIME(ky.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS kysl
            FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE ky.weihurenid = yh.id
            AND FROM_UNIXTIME(ky.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
            AND FROM_UNIXTIME(ky.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
            GROUP BY lry, yh.bumen
        ) syky
        RIGHT JOIN (
            SELECT month(FROM_UNIXTIME(ky.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS kysl
            FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE ky.weihurenid = yh.id
            AND FROM_UNIXTIME(ky.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
            GROUP BY lry, yh.bumen
        ) byky
        ON syky.bm = byky.bm
        ORDER BY nky DESC";
        $kybmtj_result =$Model->query($kybmtj_sql);
        $kybmtj['ky']=$kybmtj_result;
        $kybmtj['bm']=session('bumen');
        $this->assign('kybmtj',$kybmtj);
        //本月数据-房源（按分店统计）
        $fybmtj_sql="SELECT syfy.lry, syfy.bm, syfy.fysl AS lfy
        , ifnull(byfy.fysl, 0) AS nfy
        , round((ifnull(byfy.fysl, 0) - ifnull(syfy.fysl,0)) / ifnull(syfy.fysl,1) * 100, 2) AS tbzz
        FROM (
            SELECT month(FROM_UNIXTIME(fy.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS fysl
            FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE fy.weihurenid = yh.id
            AND FROM_UNIXTIME(fy.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
            AND FROM_UNIXTIME(fy.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
            GROUP BY lry, yh.bumen
        ) syfy
        LEFT JOIN (
            SELECT month(FROM_UNIXTIME(fy.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS fysl
            FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE fy.weihurenid = yh.id
            AND FROM_UNIXTIME(fy.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
            GROUP BY lry, yh.bumen
        ) byfy
        ON syfy.bm = byfy.bm
        union
        SELECT byfy.lry, byfy.bm, syfy.fysl AS lfy
        , ifnull(byfy.fysl, 0) AS nfy
        , round((ifnull(byfy.fysl, 0) - ifnull(syfy.fysl,0)) / ifnull(syfy.fysl,1) * 100, 2) AS tbzz
        FROM (
            SELECT month(FROM_UNIXTIME(fy.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS fysl
            FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE fy.weihurenid = yh.id
            AND FROM_UNIXTIME(fy.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
            AND FROM_UNIXTIME(fy.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
            GROUP BY lry, yh.bumen
        ) syfy
        RIGHT JOIN (
            SELECT month(FROM_UNIXTIME(fy.lurusj)) AS lry, yh.bumen AS bm
            , COUNT(*) AS fysl
            FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE fy.weihurenid = yh.id
            AND FROM_UNIXTIME(fy.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
            GROUP BY lry, yh.bumen
        ) byfy
        ON syfy.bm = byfy.bm
        ORDER BY nfy DESC";
        $fybmtj_result =$Model->query($fybmtj_sql);
        $fybmtj['fy']=$fybmtj_result;
        $fybmtj['bm']=session('bumen');
        $this->assign('fybmtj',$fybmtj);
        //本月数据-客源带看（按分店统计）
        $dkbmtj_sql="select sydk.bm,sydk.dkcs ldk,ifnull(bydk.dkcs,0) ndk, round((ifnull(bydk.dkcs,0)-sydk.dkcs)/sydk.dkcs*100,2) tbzz from (select month(FROM_UNIXTIME(kydk.dkshijian)) dksj, yh.bumen bm,count(*) dkcs from (select * from __KYDAIKAN__ where gongsiid = ".session("gongsiid").") as kydk,(select * from __YONGHU__ where gongsiid = ".session("gongsiid").") as yh where kydk.dkuid=yh.id and FROM_UNIXTIME(kydk.dkshijian)>=date_add(date_sub(curdate(),interval 1 month),interval - day(curdate()) + 1 day) and FROM_UNIXTIME(kydk.dkshijian)<=date_sub(curdate(),interval 1 month) group by dksj,yh.bumen) as sydk left join (select month(FROM_UNIXTIME(kydk.dkshijian)) dksj, yh.bumen bm,count(*) dkcs from (select * from __KYDAIKAN__ where gongsiid = ".session("gongsiid").") as kydk,(select * from __YONGHU__ where gongsiid = ".session("gongsiid").") as yh where kydk.dkuid=yh.id and FROM_UNIXTIME(kydk.dkshijian)>=date_add(curdate(), interval - day(curdate()) + 1 day) group by dksj,yh.bumen) as bydk on sydk.bm=bydk.bm order by ndk desc";
        $dkbmtj_sql = "SELECT sydk.bm,sydk.dkcs ldk,ifnull(bydk.dkcs,0) ndk, round((ifnull(bydk.dkcs,0)-ifnull(sydk.dkcs,0))/ifnull(sydk.dkcs,1)*100,2) tbzz
        FROM (
            SELECT month(FROM_UNIXTIME(kydk.dkshijian)) AS dksj, yh.bumen AS bm
            , COUNT(*) AS dkcs
            FROM (SELECT * FROM __KYDAIKAN__ WHERE gongsiid = ".session("gongsiid").") kydk, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE kydk.dkuid = yh.id
            AND FROM_UNIXTIME(kydk.dkshijian) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
            AND FROM_UNIXTIME(kydk.dkshijian) <= date_sub(curdate(), INTERVAL 1 MONTH)
            GROUP BY dksj, yh.bumen
        ) sydk
        LEFT JOIN (
            SELECT month(FROM_UNIXTIME(kydk.dkshijian)) AS dksj, yh.bumen AS bm
            , COUNT(*) AS dkcs
            FROM (SELECT * FROM __KYDAIKAN__ WHERE gongsiid = ".session("gongsiid").") kydk, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE kydk.dkuid = yh.id
            AND FROM_UNIXTIME(kydk.dkshijian) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
            GROUP BY dksj, yh.bumen
        ) bydk
        ON sydk.bm=bydk.bm 
        union
        SELECT sydk.bm,sydk.dkcs ldk,ifnull(bydk.dkcs,0) ndk, round((ifnull(bydk.dkcs,0)-ifnull(sydk.dkcs,0))/ifnull(sydk.dkcs,1)*100,2) tbzz
        FROM (
            SELECT month(FROM_UNIXTIME(kydk.dkshijian)) AS dksj, yh.bumen AS bm
            , COUNT(*) AS dkcs
            FROM (SELECT * FROM __KYDAIKAN__ WHERE gongsiid = ".session("gongsiid").") kydk, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE kydk.dkuid = yh.id
            AND FROM_UNIXTIME(kydk.dkshijian) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
            AND FROM_UNIXTIME(kydk.dkshijian) <= date_sub(curdate(), INTERVAL 1 MONTH)
            GROUP BY dksj, yh.bumen
        ) sydk
        RIGHT JOIN (
            SELECT month(FROM_UNIXTIME(kydk.dkshijian)) AS dksj, yh.bumen AS bm
            , COUNT(*) AS dkcs
            FROM (SELECT * FROM __KYDAIKAN__ WHERE gongsiid = ".session("gongsiid").") kydk, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
            WHERE kydk.dkuid = yh.id
            AND FROM_UNIXTIME(kydk.dkshijian) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
            GROUP BY dksj, yh.bumen
        ) bydk
        ON sydk.bm=bydk.bm 
        ORDER BY ndk DESC";
        $dkbmtj_result =$Model->query($dkbmtj_sql);
        $dkbmtj['dk']=$dkbmtj_result;
        $dkbmtj['bm']=session('bumen');
        $this->assign('dkbmtj',$dkbmtj);
        //本月数据-客源、房源（按分店用户统计）
        $kyfytj_sql="SELECT kyyhtj.bm, kyyhtj.uid, kyyhtj.ygmingcheng
        , ifnull(kyyhtj.lky, 0) AS lky
        , ifnull(kyyhtj.nky, 0) AS nky
        , round((ifnull(kyyhtj.nky, 0) - ifnull(kyyhtj.lky, 0)) / ifnull(kyyhtj.lky, 1) * 100, 2) AS kytbzj
        , ifnull(fyyhtj.lfy, 0) AS lfy
        , ifnull(fyyhtj.nfy, 0) AS nfy
        , round((ifnull(fyyhtj.nfy, 0) - ifnull(fyyhtj.lfy, 0)) / ifnull(fyyhtj.lfy, 1) * 100, 2) AS fytbzj
        FROM (
            SELECT byky.bm, byky.uid, byky.ygmingcheng, syky.kysl AS lky, byky.kysl AS nky
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id  AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byky
            LEFT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(ky.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syky
            ON byky.uid = syky.uid
            AND byky.bm = syky.bm
            UNION
            SELECT syky.bm, syky.uid, syky.ygmingcheng, syky.kysl AS lky, byky.kysl AS nky
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byky
            RIGHT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(ky.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syky
            ON byky.uid = syky.uid
            AND byky.bm = syky.bm
        ) kyyhtj
        LEFT JOIN (
        SELECT syfy.bm, syfy.uid, syfy.ygmingcheng, syfy.fysl AS lfy, byfy.fysl AS nfy
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(fy.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syfy
            LEFT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byfy
            ON syfy.uid = byfy.uid
            AND syfy.bm = byfy.bm
            UNION
            SELECT byfy.bm, byfy.uid, byfy.ygmingcheng, syfy.fysl AS lfy, byfy.fysl AS nfy
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(fy.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syfy
            RIGHT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byfy
            ON syfy.uid = byfy.uid
            AND syfy.bm = byfy.bm
        ) fyyhtj
        ON kyyhtj.bm = fyyhtj.bm
        AND kyyhtj.uid = fyyhtj.uid        
        UNION
        SELECT fyyhtj.bm, fyyhtj.uid, fyyhtj.ygmingcheng
        , ifnull(kyyhtj.lky, 0) AS lky
        , ifnull(kyyhtj.nky, 0) AS nky
        , round((ifnull(kyyhtj.nky, 0) - ifnull(kyyhtj.lky, 0)) / ifnull(kyyhtj.lky, 1) * 100, 2) AS kytbzj
        , ifnull(fyyhtj.lfy, 0) AS lfy
        , ifnull(fyyhtj.nfy, 0) AS nfy
        , round((ifnull(fyyhtj.nfy, 0) - ifnull(fyyhtj.lfy, 0)) / ifnull(fyyhtj.lfy, 1) * 100, 2) AS fytbzj
        FROM (
            SELECT byky.bm, byky.uid, byky.ygmingcheng, syky.kysl AS lky, byky.kysl AS nky
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byky
            LEFT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(ky.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syky
            ON byky.uid = syky.uid
            AND byky.bm = syky.bm
            UNION
            SELECT syky.bm, syky.uid, syky.ygmingcheng, syky.kysl AS lky, byky.kysl AS nky
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byky
            RIGHT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS kysl
                FROM (SELECT * FROM __KEYUAN__ WHERE gongsiid = ".session("gongsiid").") ky, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE ky.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(ky.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(ky.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syky
            ON byky.uid = syky.uid
            AND byky.bm = syky.bm
        ) kyyhtj
        RIGHT JOIN (
            SELECT syfy.bm, syfy.uid, syfy.ygmingcheng, syfy.fysl AS lfy, byfy.fysl AS nfy
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(fy.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syfy
            LEFT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byfy
            ON syfy.uid = byfy.uid
            AND syfy.bm = byfy.bm
            UNION
            SELECT byfy.bm, byfy.uid, byfy.ygmingcheng, syfy.fysl AS lfy, byfy.fysl AS nfy
            FROM (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(date_sub(curdate(), INTERVAL 1 MONTH), INTERVAL -day(curdate()) + 1 DAY)
                AND FROM_UNIXTIME(fy.lurusj) <= date_sub(curdate(), INTERVAL 1 MONTH)
                GROUP BY yh.id, yh.bumen
            ) syfy
            RIGHT JOIN (
                SELECT yh.bumen AS bm, yh.id AS uid, yh.ygmingcheng, COUNT(*) AS fysl
                FROM (SELECT * FROM __FANGYUAN__ WHERE gongsiid = ".session("gongsiid").") fy, (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") yh
                WHERE fy.weihurenid = yh.id AND yh.bumen=".session('bumen')."
                AND FROM_UNIXTIME(fy.lurusj) >= date_add(curdate(), INTERVAL -day(curdate()) + 1 DAY)
                GROUP BY yh.id, yh.bumen
            ) byfy
            ON syfy.uid = byfy.uid
            AND syfy.bm = byfy.bm
        ) fyyhtj
        ON kyyhtj.bm = fyyhtj.bm 
        AND kyyhtj.uid = fyyhtj.uid order by fytbzj desc limit 6";
        $kyfytj_result =$Model->query($kyfytj_sql);
        $this->assign('fykytj',$kyfytj_result);
            //公司业绩排名
        $gsyjpm_sql = "SELECT 
        bmrs.bmid AS bumen,
        bmrs.bmming,
        bumyj.zyj AS bmyj,
        ROUND(bumyj.zyj / bmrs.rs 
        , 2) AS pjyj
        FROM
        (SELECT 
            bm.id 
            AS bmid,
            bm.bmming,
            IF(COUNT(yh.id 
            ) = 0, 1, COUNT(yh.id 
            )) AS rs
            FROM
            (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") AS yh
            LEFT JOIN (SELECT * FROM __BUMEN__ WHERE gongsiid = ".session("gongsiid").") AS bm ON yh.bumen = bm.id 
            WHERE
            yh.lock1 = 0 AND yh.status = 1
            GROUP BY bm.id 
        ) AS bmrs
        JOIN
        (SELECT 
            bmyh.id 
            AS bmid,
            bmyh.bmming,
            SUM(IFNULL(cjyj.fcyj, 0)) AS zyj
            FROM
            (SELECT 
                bm.id 
                , bm.bmming, yh.id 
                AS uid
                FROM
                (SELECT * FROM __YONGHU__ WHERE gongsiid = ".session("gongsiid").") AS yh
                LEFT JOIN (SELECT * FROM __BUMEN__ WHERE gongsiid = ".session("gongsiid").") AS bm ON yh.bumen = bm.id 
                WHERE
            yh.lock1 = 0 AND yh.status = 1) AS bmyh
            LEFT JOIN (SELECT 
                yj.uid, yj.fcyj
                FROM
                (SELECT * FROM __YEJI__ WHERE gongsiid = ".session("gongsiid").") AS yj
                JOIN (SELECT * FROM __CHENGJIAO__ WHERE gongsiid = ".session("gongsiid").") AS cj ON yj.cjbianhao = cj.cjbianhao
                WHERE
                cj.zhuangtai = 1
            AND MONTH(FROM_UNIXTIME(cj.cjtime)) = MONTH(NOW())) AS cjyj ON bmyh.uid = cjyj.uid
            GROUP BY bmyh.id 
        ) AS bumyj
        WHERE
        bmrs.bmid = bumyj.bmid
        ORDER BY pjyj DESC limit 5";
        $gsyjpm_result=$Model->query($gsyjpm_sql);
        $this->assign('gsyjpm',$gsyjpm_result);
        $this->remark=M('role')->where(array('gongsiid'=>session("gongsiid"),'id'=>session('role')))->getField('remark');
        //显示模板
        $this->display();
    }
    //显示头像页面
    public function avatar(){
        $this->display();
    }
    //计算器
    public function tool(){
        $this->mianji=I('mianji');
        $this->shoujia=I('shoujia');
        $this->display();
    }

    public function test()
    {
        die;
        $py = new \Lib\Pinyin\Pinyin;
        $count = M('xiaoqu')->where()->count();
        
        $data = M('xiaoqu')->where('pinyinjs is null')->limit()->field('id,xiaoqum')->select();
        foreach ($data as $key => $value) {
            $txt = $value['xiaoqum'];
            $s = $py->qupinyin($txt ,true);
            $q = $py->qupinyin($txt);
            $h = $s.' '.$q.' '.$txt;
            M('xiaoqu')->where(array('id'=>$value['id']))->setField('pinyinjs',$h);
            p($key);
        }
                
        
    }
}