<?php

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

namespace Admin\Controller;
use Think\Controller;
class IndexController extends CommonController {
    public function index(){
        $role_id=M('role_user')->where(array('user_id'=>session('uid')))->getField('role_id');
        $sql = "select a.* from  
            (select * from __MENU__ where status=1 ) as a , 
            (select * from __ACCESS__ where role_id = '".$role_id."') as b where 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');
        //p($menu);die;
        if(session('username') == C('RBAC_SUPERADMIN')){
            $result=M('menu')->where(array('status'=>1))->order('listorder asc')->select();
        }else{
            $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 $key1 => $value1) {
                if ($value1['child']==null) {
                    unset($categorys[$key]['child'][$key1]);
                }
            }
        }
        foreach ($categorys as $key => $value) {
            if ($value['child']==null) {
                unset($categorys[$key]);
            }
        }
        $this->categorys = $categorys;
        $this->user = M('yonghu')->where('id='.session('uid'))->find();
        $this->display('index1');
    }
    public function xiaoxi(){
        $Model = new \Think\Model();
        $map3 = "gongsiid=".session('gongsiid');
        $map3 .= " and txuid=".session('uid');
        $map3 .= " and txshijian<".time();
        $map3 .= " and isyidu=0";
        $sqlcount3 = "select count(*) FROM __KYTIXING__ where {$map3}";
        $countb3=$Model->query($sqlcount3);
        $sh_count3=$countb3[0]['count(*)'];
        $arr['sh_count3']=$sh_count3;
        $map4 = "gongsiid=".session('gongsiid');
        $map4 .= " and txuid=".session('uid');
        $map4 .= " and txshijian<".time();
        $map4 .= " and isyidu=0";
        $sqlcount4 = "select count(*) FROM __FJTIXING__ where {$map4}";
        $countb4=$Model->query($sqlcount4);
        $sh_count4=$countb4[0]['count(*)'];
        $arr['sh_count4']=$sh_count4;
        $arr['sh_countz']=$sh_count1+$sh_count2+$sh_count3+$sh_count4+$sh_count5;
        $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('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('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('id'=>$uid))->setField(array('mima'=>$passwd));
            if ($r) {
                $data_rizhi['zy_type']=8;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=2;
                $neirong="修改密码";
                $data_rizhi['neirong']=$neirong;
                M('rizhi')->add($data_rizhi);
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }else{
            $this->display();
        }
    }
    public function welcome(){
        //输出个人信息
        $this->useri=M('Yonghu')->where(array('id'=>session('uid')))->field('id,ygmingcheng,dianhua,touxiang,ygbianhao')->find();
        $this->remark=M('role')->where(array('id'=>session('role')))->getField('remark');
        $ygbm=M('yonghu')->where(array('id'=>session('uid')))->getField('bumen');
        $this->bmming=M('bumen')->where(array('id'=>$ygbm))->getField('bmming');
        $this->jcpz=M('jcpz')->where('gongsiid=10000')->find();
        //准意向客户
        $tmb=time()-7*24*3600;
        $tme=time();
        $yixiang="select id,khxingming,xqbianhao,a.count from __KEYUAN__ b join (select count(*) as count,kyid,dkuid,dkshijian from __KYDAIKAN__ group by kyid) a on a.kyid=b.id where a.dkuid=".session('uid')." and a.dkshijian>{$tmb} and a.dkshijian<{$tme} order by a.count DESC limit 6";
        $yixiang=M('kydaikan')->query($yixiang);
        $this->assign('yixiang',$yixiang);
        //本月第一天开始时间
        $gstmb=strtotime(date('Y-m-01',time()));
        //本月数据-客源（按分店统计）
        $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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 = new \Think\Model();
        $kybmtj_result =$kybmtj_result->query($kybmtj_sql);
        $kybmtj['ky']=$kybmtj_result;
        $kybmtj['bm']=session('bumen');
        $this->assign('kybmtj',$kybmtj);
        //本月数据-客源带看（按分店统计）
        $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 __KYDAIKAN__ as kydk,__YONGHU__ 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 __KYDAIKAN__ as kydk,__YONGHU__ 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 __KYDAIKAN__ kydk, __YONGHU__ 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 __KYDAIKAN__ kydk, __YONGHU__ 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 __KYDAIKAN__ kydk, __YONGHU__ 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 __KYDAIKAN__ kydk, __YONGHU__ 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 = new \Think\Model();
        $dkbmtj_result =$dkbmtj_result->query($dkbmtj_sql);
        $dkbmtj['dk']=$dkbmtj_result;
        $dkbmtj['bm']=session('bumen');
        $this->assign('dkbmtj',$dkbmtj);
        //本月数据-客源、房源（按分店用户统计）
        $kytj_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
        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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 
        UNION
        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
        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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 __KEYUAN__ ky, __YONGHU__ 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 order by kytbzj desc limit 6"; 
        $kytj_result = new \Think\Model();
        $kytj_result =$kytj_result->query($kytj_sql);
        $this->assign('kytj',$kytj_result);
        $this->display();
    }
    //显示头像页面
    public function avatar(){
    	$this->display();
    }
    //计算器
    public function tool(){
        $this->mianji=I('mianji');
        $this->shoujia=I('shoujia');
        $this->display();
    }
}