<?php !defined('APP_DIR') ? die('acceess not defined') : '';
class actions_broker_sellwarning extends actions_action{
    
    public function __construct(){
        parent::__construct();
        if (!$this->loginuser){
            url::redirect('/login.php');
        }
    }
    
    /**
     * 预警房源列表
     *
     */
    public function index(){
        //翻页处理
        $page = intval($_GET['page']);
        $page = ($page > 0 ? $page : 1);
        
        //每页条数
        $perpage = 30;
        $offset = ($page-1)*$perpage;
        
        //城市的所有区属
        $districts = bm('city')->cityDistricts($this->city);
        
        //查询条件
        $where = array(
            'city' => $this->city,
        );
        
        //房源ID
        if ($_GET['id']){
            $where['id'] = intval($_GET['id']);
        }
        
        //小区
        if ($_GET['block_id']){
            $where['blockid'] = intval($_GET['block_id']);
        }
        //经纪人
        if ($_GET['broker_id']){
            $where['uid'] = intval($_GET['broker_id']);
        }
        //经纪人手机号
        if ($_GET['telno']){
            $where['telno'] = intval($_GET['telno']);
        }
        
        //区属
        if ($_GET['district']){
            $where['district'] = trim($_GET['district']);
        }
        
        //楼层
        if ($_GET['floor']){
            $where['floor'] = intval($_GET['floor']);
        }
        //总层数
        if ($_GET['totalfloor']){
            $where['totalfloor'] = intval($_GET['totalfloor']);
        }
        
        //户型
        if ($_GET['hall']){
            $where['hall'] = intval($_GET['hall']);
        }
        if ($_GET['area']){
            $where['buildarea >'] = intval($_GET['area'])-2;
            $where['buildarea <'] = intval($_GET['area'])+2;
        }
        //日期
        if ($_GET['time']){
            $where['creattime >='] = strtotime($_GET['time']);
            $where['creattime <'] = strtotime($_GET['time'])+86400;
        }
        
        
        //排序规则
        $orderby = array('id' => 'DESC');
        
        $query_struct = array(
            'where' => $where,
            'orderby' => $orderby,
            'limit' => array($offset,$perpage),
        );
        $warnings = bm('sellwarning')->get_all($query_struct);
        foreach ($warnings as $k => $v){
            $broker = bm('broker')->get($v['uid'],$this->city);
            $warnings[$k]['uname'] = $broker['username'];
        }
        
        //获取总数
        $total = bm('sellwarning')->count($query_struct);
        $pagenations = pagebar($total,tool::getStr('','',url::current_url()),$perpage);
        
        //模板赋值
        $tpl_data = array(
            'warnings' => $warnings,
            'pagenations'    => $pagenations,
            'where' => $where,
            'districts' => $districts
        );
        $body_tpl = array(
            'body_tpl' => new tpl('broker/sellwarning_index.php',$tpl_data),
        );
        $this->tpl_data = $body_tpl;
    }
    
    /**
     * 冻结房源
     *
     */
    public function dongjie(){
        $id = intval($_GET['id']);
        $query_struct = array(
            'where' => array(
                'city' => $this->city,
                'id' => $id,
            )
        );
       
        $data = bm('sellwarning')->get($query_struct);
		
        if ($data){
            //经纪人和门店信息
            $broker = bm('broker')->get($data['uid']);
			//print_r($broker);die;
            $agency = bm('broker')->agencyByCode($broker['agentcode']);
            
            //冻结房源疑似房源表操作记录
            bm('sellwarning')->refuse($data);
            
            //冻结二手房房源
            $_sell = array(
                'id' => $data['id'],
                'esta' => 5
            );
            bm('sell')->edit($_sell);
            
            //冻结房源的记录
            $record = array(
                'uid' => $broker['uid'],
                'username' => $broker['username'],
                'truename' => $broker['truename'],
                'tbl' => 'sell',
                'rowid' => $data['id'],
                'agentshortname' => $agency['agentname'],
                'source' => 2,
                'operator' => $this->loginuser['truename']?$this->loginuser['truename']:'系统',
                'ctime' => time(),
            );
            bm('broker')->freeze_record($record);
            
            //经纪人降为1星
            $_broker = array(
                'uid' => $broker['uid'],
                 //'starlevel' => 1,
				 'starlevel' => $broker['starlevel']-1,
                 'mistake_time' => time(),
            );
            bm('broker')->edit($_broker);
            
            //降星的记录
            $star_record = array(
                'uid' => $broker['uid'],
                'username' => $broker['username'],
                'truename' => $broker['truename'],
                'agentshortname' => $agency['agentname'],
                'type' => 2,
                'num' => 1,
                'operator' => $this->loginuser['truename']?$this->loginuser['truename']:'系统',
                'ip' => tool::ip(),
                'ctime' => time(),
            );
            bm('broker')->add_star_level_record($star_record);
        }
        die('success');
    }
    
    /**
     * 审核通过
     *
     */
    public function tongguo(){
        $id = intval($_GET['id']);
        $query_struct = array(
            'where' => array(
                'city' => $this->city,
                'id' => $id,
            )
        );
        $data = bm('sellwarning')->get($query_struct);
        bm('sellwarning')->pass($data);
        die('success');
    }
    
    /**
     * 数据统计
     *
     */
    public function tongji(){
        $t_s = time();
        $last_id = 0;
        $n=0;
        
        $tj_blocks = array();
        $tj_brokers = array();
        while (1){
            $query_struct = array(
                'where' => array(
                    'city' => $this->city,
                    'id >' => $last_id,
                ),
                'orderby' => array(
                    'id' => 'ASC',
                ),
                'limit' => 2000,
            );
            $warnings = bm('sellwarning')->get_all($query_struct);
            if (!$warnings){
                break;
            }
            foreach ($warnings as $_w){
                //按照小区统计
                if (!$tj_blocks[$_w['blockid']]){
                    $tj_blocks[$_w['blockid']] = array(
                        'block_id' => $_w['blockid'],
                        'block_name' => $_w['blockshowname'],
                        'warning_num' => 0,
                    );
                }
                $tj_blocks[$_w['blockid']]['warning_num'] += 1;
                
                //按照经纪人统计
                if (!$tj_brokers[$_w['uid']]){
                    $tj_brokers[$_w['uid']] = array(
                        'uid' => $_w['uid'],
                        'contactor' => $_w['contactor'],
                        'telno' => $_w['telno'],
                        'warning_num' => 0,
                    );
                }
                $tj_brokers[$_w['uid']]['warning_num'] += 1;
                
                
                //按照经纪公司统计
                $last_id = $_w['id'];
                $n+=1;
            }
        }
        
        $block_path = $this->city.'/sellwarning/tongji/blocks';
        $broker_path = $this->city.'/sellwarning/tongji/brokers';
        
        fcache::set($block_path,'',$tj_blocks,0);
        fcache::set($broker_path,'',$tj_brokers,0);
        $t_e = time();
        echo '完成小区和经纪人的数据统计，耗时：'.($t_e-$t_s).'秒<br>';die();
    }
    
    /**
     * 门店统计
     *
     */
    public function tongji_agency(){
        $t_s = time();
        $broker_path = $this->city.'/sellwarning/tongji/brokers';
        $tj_brokers = fcache::get($broker_path);
        $tj_agencies = array();
        foreach ($tj_brokers as $_tj_b){
            $uid = $_tj_b['uid'];
            $broker = bm('broker')->get($uid,$this->city);
            $agency = bm('broker')->agencyByCode($broker['agentcode'],$this->city);
            
            if (!$tj_agencies[$agency['agid']]){
                $tj_agencies[$agency['agid']] = array(
                    'agid' => $agency['agid'],
                    'agentcode' => $agency['agentcode'],
                    'agentname' => $agency['agentname'],
                    'warning_num' => 0,
                );
            }
            $tj_agencies[$agency['agid']]['warning_num'] += $_tj_b['warning_num'];
        }
        $agency_path = $this->city.'/sellwarning/tongji/agency';
        fcache::set($agency_path,'',$tj_agencies,0);
        $t_e = time();
        echo '完成门店的数据统计，耗时：'.($t_e-$t_s).'秒<br>';die();
    }
    
    /**
     * 门店的疑似房源统计
     *
     */
    public function show_tongji_agency(){
        $agency_path = $this->city.'/sellwarning/tongji/agency';
        $tj_agencies = fcache::get($agency_path);
        
        //模板赋值
        $tpl_data = array(
            'tj_agencies' => $tj_agencies,
        );
        $body_tpl = array(
            'body_tpl' => new tpl('broker/sellwarning_show_tongji_agency.php',$tpl_data),
        );
        $this->tpl_data = $body_tpl;
    }
    
    /**
     * 小区的疑似房源统计
     *
     */
    public function show_tongji_block(){
        $agency_path = $this->city.'/sellwarning/tongji/blocks';
        $tj_blocks = fcache::get($agency_path);
        
        //模板赋值
        $tpl_data = array(
            'tj_blocks' => $tj_blocks,
        );
        $body_tpl = array(
            'body_tpl' => new tpl('broker/sellwarning_show_tongji_block.php',$tpl_data),
        );
        $this->tpl_data = $body_tpl;
    }
    
    public function remove_disable(){
        $last_id = intval($_GET['last_id']);
        //删除超时房源
        if ($last_id==0){
            bm('sellwarning')->removeTimeout($this->city);
        }
        
        $query_struct = array(
            'fields' => 'id,uid',
            'where' => array(
                'city' => $this->city,
                'id >' => $last_id,
            ),
            'orderby' => array('id' => 'ASC'),
            'limit' => 500,
        );
        $warnings = bm('sellwarning')->get_all($query_struct,'id');
        
        if ( empty($warnings) ){
            die('完成全部检查');
        }
        
        $wids = array_keys($warnings);
        $query_struct = array(
            'fields' => 'id,uid,esta',
            'where' => array(
                'id' => $wids,
            ),
        );
        $sells = bm('sell')->get_all($query_struct,'id');
        
        $del_ids = array();
        foreach ($warnings as $k => $_warning){
            $last_id = $k;
            //删除已经删除房源的数据
            if( $sells[$k]['esta'] !=1 ){
                $del_ids[] = $k;
            }else {
                //删除一星经纪人的数据
                $broker = bm('broker')->get($_warning['uid']);
                if ($broker['starlevel']==1){
                    $del_ids[] = $k;
                }
            }
        }
        if ($del_ids){
            bm('sellwarning')->removeByIds($del_ids,$this->city);
        }
        echo "已检查到房源：$last_id";
        echo "<script>window.location.href='http://esfadmin.house365.com/365frame/index.php?ctrl=broker_sellwarning&method=remove_disable&last_id=$last_id';</script>";
        die();
    }
}