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

class guide_project_model extends MY_Model {

    private $tbl = 'guide_project';
    /**
     * 缓存key
     * @var string
     */
    private $_mem_key ;

    //构造函数
    public function __construct()
    {
        parent::__construct(1, 1, 1);
//        $this->_mem_key = GB_CITY.'_guide_project';
    }
    /**获取所有专题总数
     */
    public function getGuideProjectTotal(){
        $time = time();
        $this->dbback->select("*");
        $this->dbback->where("project_solicitation_start <= $time AND project_solicitation_end >= $time");
        $result = $this->dbback->get('guide_project')->result_array();
        if($result){
            $result_count = count($result);
        }else{
            $result_count = 0;
        }
        return $result_count;
    }

    /**获取所有专题总数
     */
    public function getRealHouseTotal($uid){
        $this->dbback->select("*");
        $this->dbback->where("is_real_house =1 AND uid = '$uid' AND esta=1");
        $result = $this->dbback->get('sell')->result_array();
        if($result){
            $result_count = count($result);
        }else{
            $result_count = 0;
        }
        return $result_count;
    }

    /**获取放心看房源信息
     */
    public function getRealHouseInfo($uid,$offset,$limit,$guide_project_id){
        $time    = time();
        $this->dbback->select("id,blockid,room,hall,toilet,buildarea");
        $this->dbback->where("is_real_house =1 AND uid = '$uid' AND esta=1");
        $this->dbback->order_by('lucupdatetime','DESC');
        $this->dbback->limit($limit,$offset);
        $result = $this->dbback->get('sell')->result_array();
//        echo $this->dbback->last_query();die;
        if($result){
            for($i=0;$i<count($result);$i++){
                $blockInfo = $this->dbback->select('blockname,district,streetid')->from('block')->where(array('id'=>$result[$i]['blockid']))->get()->row_array();
                $streetInfo= $this->dbback->select('district,streetname')->from('street')->where(array('id'=>$blockInfo['streetid']))->get()->row_array();
                $result[$i]['blockname'] = $blockInfo['blockname'];
                $result[$i]['houseInfo']  = '';
                if($result[$i]['room']){
                    $result[$i]['houseInfo']  .= $result[$i]['room'].'室';
                }
                if($result[$i]['hall']){
                    $result[$i]['houseInfo']  .= $result[$i]['hall'].'厅';
                }
                if($result[$i]['toilet']){
                    $result[$i]['houseInfo']  .= $result[$i]['toilet'].'卫';
                }
                $result[$i]['buildarea'] = (intval($result[$i]['buildarea'])==$result[$i]['buildarea'])?intval($result[$i]['buildarea']):$result[$i]['buildarea'];
                $result[$i]['houseInfo']     .= '/'.$result[$i]['buildarea'].'㎡/'.$blockInfo['district'].'/'.$streetInfo['streetname'];
                //判断是否已经申请这个专题
                $isApplyInfo = $this->dbback->from('guide_project_apply')->where(array('guide_project_id'=>$guide_project_id,'uid'=>$uid,'house_id'=>$result[$i]['id']))->get()->row_array();
                if($isApplyInfo){
                    $result[$i]['is_apply'] = 1;
                }else{
                    $result[$i]['is_apply'] = 0;
                }
            }
        }
        return $result;
    }

    /**获取所有专题信息
     */
    public function getGuideProject($uid,$offset,$limit){
        $time    = time();
        $this->dbback->select("*");
        $this->dbback->where("project_solicitation_start <= '$time' AND project_solicitation_end >= '$time'");
        $this->dbback->order_by('create_time','DESC');
        $this->dbback->limit($limit,$offset);
        $result = $this->dbback->get('guide_project')->result_array();

//      echo $this->dbback->last_query();die;
        if($result){
            for($i=0;$i<count($result);$i++){
                $applyInfo  = $this->get_apply_info($uid,$result[$i]['id']);
                if($applyInfo){
                    $result[$i]['apply_status'] = '已申请';
                }else{
                    $result[$i]['apply_status'] = '未申请';
                }
                $result[$i]['apply_time']= date('Y-m-d',$result[$i]['project_solicitation_start']).' - '.date('Y-m-d',$result[$i]['project_solicitation_end']);
            }
        }
        return $result;
    }

    /**获取当前经纪人专题下申请信息
     */
    public function get_apply_info($uid,$id)
    {

        $result = $this->dbback->from('guide_project_apply')->where(array('uid'=>$uid,'guide_project_id'=>$id))->get()->result_array();
        return $result;
    }

    /*查看此经纪人是否是放心看
     */
    public function is_real_status($uid){
        $result = $this->dbback->select('real_status')->from('broker')->where(array('uid'=>$uid))->get()->result_array();
        return $result;
    }

    /*查看专题信息
     */
    public function guide_project_info($guide_project_id){
        $result = $this->dbback->from('guide_project')->where(array('id'=>$guide_project_id))->get()->row_array();
        if($result){
            $result['project_solicitation_start'] = date('Y-m-d',$result['project_solicitation_start']);
            $result['project_solicitation_end']   = date('Y-m-d',$result['project_solicitation_end']);
        }
        return $result;
    }

    /*查看此经纪人此专题已申请总数
     */
    public function applyTotal($guide_project_id,$uid){
        $ApplyNum = $this->dbback->from('guide_project_apply')->where(array('guide_project_id'=>$guide_project_id,'uid'=>$uid))->count_all_results();
        return $ApplyNum;

    }
    public function insert($tbl, $insert_data)
    {
        if ($this->db->insert($tbl, $insert_data))
        {
//            return $this->db->insert_id();
            $this->db->set('projrct_application_num','projrct_application_num + 1',FALSE)->where(array('id'=>$insert_data['guide_project_id']))->update('guide_project');
        }
    }

    /*查看此经纪人已申请推广的总数
     */
    public function applyAllTotal($uid){
        $ApplyNum = $this->dbback->from('guide_project_apply')->where(array('uid'=>$uid))->group_by('guide_project_id')->get()->result_array();
        return count($ApplyNum);
    }

    /*查看此经纪人已申请推广信息
     */
    public function applyAllInfo($uid,$offset,$limit){
        $applyInfo = $this->dbback->select('guide_project_id,group_concat(house_id) as houseArr')->from('guide_project_apply')->where(array('uid'=>$uid))->order_by('create_time','DESC')->group_by('guide_project_id')->limit($limit,$offset)->get()->result_array();
        if($applyInfo){
            for($i=0;$i<count($applyInfo);$i++){
                $guide_info = $this->guide_project_info($applyInfo[$i]['guide_project_id']);
                $applyInfo[$i]['project_title'] = $guide_info['project_title'];
                $applyInfo[$i]['hosue_id']      = explode(',',$applyInfo[$i]['houseArr']);
            }
        }
        return $applyInfo;
    }



}