<?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 house_tag
 *
 * @author Administrator
 */
class house_tag_price_model extends MY_Model {
   

    /**
     * 表名
     * @var type 表名
     */
    private $table;

    public function __construct() {
        parent::__construct(1,1,1); 
        $this->table = 'house_tag_price';
    }
    
    /**
     * 查找所有标签包
     * @param type $uid
     * @param type $startTime
     * @param type $endTime
     * @param type $start
     * @param type $number
     * @return type
     */
    public function findByAll() 
	{	
		$mem_key ="house_tag_price_find_by_all";

		$cache = $this->mc->get($mem_key);        
		
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $tag_info = $cache['data'];
        }
        else
        {
			$tag_info = $this->dbback->get($this->table)->result_array();
			$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$tag_info) ,  600);
		}

		return $tag_info;
    }
	

	/**
     * 查找经纪人当月购买的套餐数量
     * @param type $uid
     * @param type $startTime
     * @param type $endTime
     * @param type $start
     * @param type $number
     * @return type
     */
    public function findTagNumByUid($uid) 
	{
		
		$date_start  =strtotime(date('Y-m-01 00:00:00'));
		$date_end    =strtotime(date('Y-m-31 23:59:59'));
		$this->dbback->select('tag_price_id ,tag_num');
		$this->dbback->where("uid = $uid && date_add>=$date_start && date_add<=$date_end");
		
		$tag_info = $this->dbback->get('house_tag_record')->result_array();
		for($i=0;$i<count($tag_info);$i++)
		{
			$this->dbback->select('tag_num'); //tag_num 为house_tag_record  tag_num 字段
			$tag_price_id =$tag_info[$i]['tag_price_id'];
			
			$this->dbback->where("id =$tag_price_id");
			$tag_price_info =$this->dbback->get('house_tag_price')->result_array();
			$tag_info[$i]['tag_price_num'] =$tag_price_info[0]['tag_num']; //tag_price_num 为house_tag_price tag_num 字段
		}

		$sum =0;
		for ($j=0;$j<count($tag_info);$j++)
		{
			$sum +=$tag_info[$j]['tag_num']*$tag_info[$j]['tag_price_num'];
		}
		
        return $sum;
    }


	/**
     * 查找经纪人套餐数量
     * @param type $uid
     * @param type $startTime
     * @param type $endTime
     * @param type $start
     * @param type $number
     * @return type
     */
    public function findBrokerPageById($id) 
	{
		$mem_key ="find_broker_page_by_id_".$id;
		//$this->mc->delete($mem_key);
		$cache = $this->mc->get($mem_key);        
		
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $tag_info = $cache['data'];
        }
        else
        {
			$this->dbback->where("package_id = $id");
			$tag_info = $this->dbback->get('broker_package')->result_array();
			$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$tag_info) ,  600);
		}
        return $tag_info;
    }

    /**
     * 根据id，查找标签信息
     * @param type $uid
     * @param type $startTime
     * @param type $endTime
     * @return type
     */
    public function findById($id) 
	{	
		
		$mem_key ="find_house_tag_info_by_id_".$id;
		//$this->mc->delete($mem_key);
		$cache = $this->mc->get($mem_key);        
		
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $tag_info = $cache['data'];
        }
        else
        {
			$this->dbback->where("id = $id");
			$tag_info = $this->dbback->get($this->table)->result_array();
			$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$tag_info) ,  600);
		}
        return $tag_info;
    }


	/**
     * 根据master，客户经理
     * @param type $uid
     * @param type $startTime
     * @param type $endTime
     * @return type
     */
    public function findByMaster($master) 
	{	
		
		$mem_key ="find_master_info_by_id_".$master;
		//$this->mc->delete($mem_key);
		$cache = $this->mc->get($mem_key);        
		
        if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
        {
            $tag_info = $cache['data'];
        }
        else
        {
			$this->dbback->where("authname = '$master'");
			$tag_info = $this->dbback->get('home_auth')->result_array();
			$this->mc->add($mem_key , array('is_ok'=>1 , 'data'=>$tag_info) ,  36000);
		}
        return $tag_info;
    }
    

    
}
?>