<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of broker_check
 *
 * @author Administrator
 */
class Broker_check extends MY_Model {

    public $uid;
    public $cityflag;
    public $score_baseinfo_limit = 20;  //基础信息分数
    public $score_houseMamage_limit = 40; //房源管理信息分数
    public $score_houseRefresh_limit = 40; //房源刷新信息分数
    public $score_min = 0;
    public $tbl_broker;
    public $tbl_certificate;
    public $tbl_sell;
    public $tbl_rent;
    public $tbl_housefreeze;
    public $tbl_picdel;
    public $tbl_refresh;
    public $start_time;
    public $end_time;
    public $brokerInfo = array();
    public $is_cache = 1;

    public function __construct() {
        parent::__construct(0,1,3);
        $this->load->model('broker_model');
        $this->load->model('Statistics','stat');
        $info = $this->broker_model->get_user_session();
        $this->uid = $info['uid'];
        $this->cityflag = 'nj';
        $this->tbl_broker = 'broker';
        $this->tbl_certificate = 'broker_certificate';
        $this->tbl_sell = 'sell';
        $this->tbl_rent = 'rent';
        $this->tbl_housefreeze = 'house_freeze';
        $this->tbl_picdel = 'upload_log';
        $this->tbl_refresh = 'log_refresh';
        $this->brokerInfo = $this->Get_BrokerInfo();
    }

    /**
     * 获取经纪人基础信息
     * @param 无
     * @return array 经纪人基础信息
     * @author xz 2013-6-20
     * */
    public function Get_brokerScore() {
        $mem_key = $this->cityflag . '_BrokerCheck_BrokerScore_' . $this->uid;
        $temp_brokerScore = $this->mc->get($mem_key);
        if ($temp_brokerScore['is_ok'] == '1') {
            $brokerScore = $temp_brokerScore['data'];
        } else {
            $brokerScore = array();
            //判断经纪人是否是付费套餐，如果不是则返回false.
            if ($this->brokerInfo['is_pay'] == 0) {
                $BaseInfo_Score = $this->Get_BaseInfo_Score();
                $HouseMamage_Score = $this->Get_HouseMamage_Score();
                $HouseRefresh_Score = $this->Get_HouseRefresh_Score();
                $total_score = $BaseInfo_Score + $HouseMamage_Score + $HouseRefresh_Score;
                $brokerScore = array(
                    'time' => time(),
                    'total_score' => $total_score,
                    'BaseInfo_Score' => $BaseInfo_Score,
                    'HouseMamage_Score' => $HouseMamage_Score,
                    'HouseRefresh_Score' => $HouseRefresh_Score
                );
            }
            $this->mc->add($mem_key, array('is_ok' => '1', 'data' => $brokerScore),  60);
        }
        return $brokerScore;
    }

    /**
     * 获取经纪人基础信息
     * @param 无
     * @return array 经纪人基础信息
     * @author xz 2013-6-20
     * */
    public function Get_BrokerInfo() {
        return $this->broker_model->get_broker_info();
    }

    /**
     * 获取经纪人基础信息得分
     * @param 无
     * @return int 经纪人基础检测得分
     * @author xz 2013-6-20
     * */
    public function Get_BaseInfo_Score() {

        //获取经纪人头像检测得分(已通过得6分，待审核得4分，无得0分)
        $score_photo = 0;
        $valid_flag_photo = $this->Get_SmallPhoto_Status();

        if ($valid_flag_photo == '0') {
            $score_photo = 3;
        } else if ($valid_flag_photo == '1') {
            $score_photo = 6;
        }

        //获取经纪人身份证得分(已通过得6分，待审核得4分，无得0分)
        $score_Idcard = 0;
        $valid_flag_Idcard = $this->Get_Identity_Card_Status();

        if ($valid_flag_Idcard == '0') {
            $score_Idcard = 3;
        } else if ($valid_flag_Idcard == '1') {
            $score_Idcard = 6;
        }

        //获取经纪人名片认证(已通过得4分，待审核得2分，无得0分)
        $score_Bcard = 0;
        $valid_flag_Bcard = $this->Get_Business_Card_Status();

        if ($valid_flag_Bcard == '0') {
            $score_Bcard = 2;
        } else if ($valid_flag_Bcard == '1') {
            $score_Bcard = 4;
        }

        //获取置业认证得分(已通过得4分，待审核得2分，无得0分)
        $score_Cert = 0;
        $valid_flag_Cert = $this->Get_Certification_Status();

        if ($valid_flag_Cert == '0') {
            $score_Cert = 2;
        } else if ($valid_flag_Cert == '1') {
            $score_Cert = 4;
        }

        //各部分得分相加
        @$score_baseinfo = $score_photo + $score_Idcard + $score_Bcard + $score_Cert;
        //限制最高得分
        $score_baseinfo = $this->score_baseinfo_limit >= $score_baseinfo ? $score_baseinfo : $this->score_baseinfo_limit;
        //取整数返回
        return intval($score_baseinfo);
    }

    /**
     * 获取经纪人房源管理部分得分
     * @param 无
     * @return int 获取经纪人房源管理部分得分
     * @author xz 2013-6-20
     * */
    public function Get_HouseMamage_Score() {

        //端口上限
        $postlimit = intval($this->brokerInfo['postlimit']);

        //激活房源数量
        $HouseNum = $this->Get_HouseNum($this->tbl_sell) + $this->Get_HouseNum($this->tbl_rent);

        //多图房源数量
        $StarHouseNum = $this->Get_StarHouseNum($this->tbl_sell) + $this->Get_StarHouseNum($this->tbl_rent);

        //激活房源率得分(发布率=激活房源数量/有效房源上限，得分=发布率*10)
        $house_score = $postlimit > 0 ? ( $HouseNum / $postlimit ) * 10 : 0;

        //多图房源率得分(多图率=多图房源数量/有效房源数量，得分=多图率*30)
        $star_house_score = $HouseNum > 0 ? ( $StarHouseNum / $HouseNum ) * 30 : 0;

        //赋值时间段
        $this->start_time = strtotime(date('Y-m-d'));
        $this->end_time = $this->start_time + 86399;

        //违规房源得分(当天被冻结1条扣2分，2条扣4分，3条扣6分，5条及其以上扣10分)
        $Violation_HouseNum = $this->Get_Violation_HouseNum();
        $Violation_House_score = $Violation_HouseNum * ( -2 );
        $Violation_House_score = $Violation_House_score >= -10 ? $Violation_House_score : -10;

        //删除图片得分(当天被一张扣1分，2张扣2分，3张扣3分…10张及其以上扣10分)
        $Violation_PicNum = $this->Get_Violation_PicNum();
        $Violation_Pic_score = $Violation_PicNum * ( -1 );
        $Violation_Pic_score = $Violation_Pic_score >= -10 ? $Violation_Pic_score : -10;

        $score_HouseMamage = $house_score + $star_house_score + $Violation_House_score + $Violation_Pic_score;

        //最小值为0
        $score_HouseMamage = $score_HouseMamage > 0 ? $score_HouseMamage : 0;

        //验证最大上限
        $score_HouseMamage = $this->score_houseMamage_limit > $score_HouseMamage ? $score_HouseMamage : $this->score_houseMamage_limit;

        return floor($score_HouseMamage);
    }

    /**
     * 获取经纪人房源管理部分得分
     * @param 无
     * @return int 获取经纪人房源管理部分得分
     * @author xz 2013-6-20
     * */
    public function Get_HouseRefresh_Score() {

        //端口上限
        $postlimit = intval($this->brokerInfo['postlimit']);
        $refresh_num = $this->Get_Refresh_Num();

        //刷新房源得分(刷新率=刷新次数/端口刷新次数上限，得分=刷新率*40)
        $refresh_score = $postlimit > 0 ? ( $refresh_num / ( $postlimit * 3 ) ) * 40 : 0;

        //验证得分上限
        $refresh_score = $this->score_houseRefresh_limit > $refresh_score ? $refresh_score : $this->score_houseRefresh_limit;
        //向下取证，保证各部分相加不会超过100分
        return floor($refresh_score);
    }

    /**
     * 获取经纪人头像认证信息状态
     * @param 无
     * @return string 经纪人头像认证信息状态
     * @author xz 2013-6-20
     * */
    public function Get_SmallPhoto_Status() {
        $kind = "p_2_2";
        return $this->get_cert_status($kind);
        //$valid_info = array();
        //$sql = "SELECT valid_flag FROM " . $this->tbl_certificate ." WHERE uid = " . $this->uid . " AND kind = 'p_2_2' ";
        //$valid_info = $this->dbback->GetAll($sql);
        /*
        $this->dbback->select("valid_flag")->from($this->tbl_certificate)
                ->where("uid = $this->uid AND kind = 'p_2_2'");
        $query = $this->dbback->get();
        $valid_info = $query->result_array();
        $valid_yx = 0;
        $valid_dl = 0;
        foreach ($valid_info as $value) {
            switch ($value['valid_flag']) {
                case '1':
                    $valid_yx++;
                    break;
                case '0':
                    $valid_dl++;
                    break;
                default:
                    break;
            }
        }
        if ($valid_yx > 0) {
            $valid_flag = 1;
        } elseif ($valid_dl > 0) {
            $valid_flag = 0;
        } else {
            $valid_flag = 2;
        }
        return $valid_flag;
         * 
         */
    }

    /**
     * 获取经纪人身份证认证信息状态
     * @param 无
     * @return string 身份证认证信息状态
     * @author xz 2013-6-20
     * */
    function Get_Identity_Card_Status() {
        return $this->get_cert_status("p_2_3");
    }

    /**
     * 获取经纪人名片认证信息状态
     * @param 无
     * @return string 身份证认证信息状态
     * @author xz 2013-6-20
     * */
    public function Get_Business_Card_Status() {
        return $this->get_cert_status("p_3");
    }

    /**
     * 获取经纪人全国置业认证认证信息状态
     * @param 无
     * @return string 经纪人全国置业认证认证信息状态
     * @author xz 2013-6-20
     * */
    public function Get_Certification_Status() {
        $kind = "p_4_2";
        return $this->get_cert_status($kind);
    }

    /**
     * 获取经纪人激活房源数量
     * @param $tbl 房源类型 sell\rent
     * @return int 经纪人激活房源数量
     * @author xz 2013-6-20
     * */
    public function Get_HouseNum($tbl) {
        if($tbl == 'rent') {
            return $this->stat->rentCount($this->uid);
        }elseif($tbl == 'sell') {
            return $this->stat->sellCount($this->uid);
        }
        /**
        $sql = "SELECT COUNT(*) FROM " . $tbl . " WHERE esta = 1 "
                . " AND agentcode = 'B000' AND uid = $this->uid ";
        $num = $this->dbback->GetOne($sql);
        $this->dbback->select("valid_flag")->from($this->tbl_certificate)
                ->where("uid = $this->uid AND kind = '$kind'");
        $query = $this->dbback->get();
        $valid_info = $query->result_array();
        return intval($num);
         * 
         */
    }

    /**
     * 获取经纪人多图房源数量
     * @param $tbl 房源类型 sell\rent
     * @return int 经纪人多图房源数量
     * @author xz 2013-6-20
     * */
    function Get_StarHouseNum($tbl) {
        if($tbl == 'sell') {
            return $this->stat->sellCountStar($this->uid);
        } elseif($tbl == 'rent') {
            return $this->stat->rentCountStar($this->uid);
        }
        /*
        $sql = "SELECT COUNT(*) FROM $tbl WHERE esta = 1 "
                . " AND agentcode = 'B000' AND uid = $this->uid AND isstar = 3 ";
        $num = $this->dbback->GetOne($sql);

        return intval($num);
         * 
         */
    }

    /**
     * 获取经纪人违规房源数量
     * @param 无
     * @return int 经纪人违规房源数量
     * @author xz 2013-6-20
     * */
    private function Get_Violation_HouseNum() {
        return $this->stat->countViolationHouse($this->uid,$this->start_time,$this->end_time);
        /*
        $cond = " WHERE agentcode = 'B000' AND uid = " . $this->uid;
        $cond .= ' AND freezetime BETWEEN ' . $this->start_time . ' AND ' . $this->end_time;

        $sql = "SELECT COUNT(*) FROM " . $this->tbl_housefreeze . " $cond ";
        $num = $this->dbback->GetOne($sql);

        return intval($num);
         * 
         */
    }

    /**
     * 获取经纪人违规图片数量
     * @param 无
     * @return int 经纪人违规图片数量
     * @author xz 2013-6-20
     * */
    private function Get_Violation_PicNum() {
        return $this->stat->countViolationPic($this->uid,  $this->start_time,  $this->end_time);
        /*
        $cond = " WHERE uid = '" . $this->uid . "' AND sort != '99'   ";
        $cond .= " AND optime >= '" . $this->start_time . "' AND optime <= '" . $this->end_time . "'";
        $sql = " SELECT COUNT(*) FROM " . $this->tbl_picdel . " $cond ";
        $num = $this->dbback->GetOne($sql);
        return intval($num);
        */
    }

    /**
     * 获取经纪人房源刷新次数
     * @param 无
     * @return int 经纪人房源刷新次数
     * @author xz 2013-6-20
     * */
    private function Get_Refresh_Num() {
        return $this->stat->refreshNumberToday($this->uid);
        /*
        $day = date('Y-m-d', time());
        $sql = " SELECT SUM(num) FROM $this->tbl_refresh WHERE "
                . " uid = '" . $this->uid . "' AND ymd = '" . $day . "'";
        $refresh_num = $this->dbback->GetOne($sql);
        return intval($refresh_num);
        */
    }
    
    private function get_cert_status($kind) {
        $this->dbback->select("valid_flag")->from($this->tbl_certificate)
                ->where("uid = $this->uid AND kind = '$kind'");
        $query = $this->dbback->get();
        $valid_info = $query->result_array();
        $valid_yx = 0;
        $valid_dl = 0;
        foreach ($valid_info as $value) {
            switch ($value['valid_flag']) {
                case '1':
                    $valid_yx++;
                    break;
                case '0':
                    $valid_dl++;
                    break;
                default:
                    break;
            }
        }
        if ($valid_yx > 0) {
            $valid_flag = 1;
        } elseif ($valid_dl > 0) {
            $valid_flag = 0;
        } else {
            $valid_flag = 2;
        }
        return $valid_flag;
    }

}

?>
