<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * zsb
 *
 * 基础类库
 *
 * @package         zsb
 * @author          EllisLab Dev Team
 * @copyright      Copyright (c) 2006 - 2014, HOUSE365, Inc.
 * @link            http://nj.sell.house.com
 * @since           Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * Tag_package_base_model CLASS
 *
 * 急推标签包管理类
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          xue
 */
//加载父类


class Tag_package_base_model extends MY_Model {
   
   
    
    /**
     * 房源标签包表
     * @var const
     */
    const TAG_PACKAGE_TBL = 'house_tag_package';
    
    /**
     * 缓存key
     * @var const
    */
    private $_mem_key;
    
    /**
     * 类初始化
     */
    public function __construct() 
    {
        parent::__construct(1,1,1);       
 
        $this->_mem_key = GB_CITY.'_house_tag_package_base_model';
    }
    
   
   public function get_now_time(){
	   return time();
   }

   public function get_month_begin_time(){
        return mktime(0,0,0,date('m'),1,date('Y'));
   }


    /**
     * 购买标签
     * @access  public
     * @param   int $package_id 购买包id
	 * @param   int $num 购买数量
	 * @param   int $expire_day 有效天数
     * @param  int $uid  用户uid
	 * @return  int $id  最后一个插入id
     */
	 public function add_tag_pacakage($package_id,$num,$expire_day,$uid){
		 $package_arr = array();
         $add_time = $this->get_now_time();
		 $end_time = strtotime( date("Y-m-d 23:59:59") ) + 86400 * ($expire_day-1);
		 if($num>1){
			 for($i=0;$i<$num;$i++){
			    $package_arr[$i] = array('tag_price_id'=>$package_id,'add_time'=>$add_time,'end_time'=>$end_time,'uid'=>$uid,'esta'=>0);			 
			 }		     
		 }elseif($num==1){
			 $package_arr = array('tag_price_id'=>$package_id,'add_time'=>$add_time,'end_time'=>$end_time,'uid'=>$uid,'esta'=>0);
		 
		 }else{
		    return false;
		 }   
		 return $this->insert_package($package_arr);
    		
	 }


	

	 /**
     * 获取当前可用标签
     *
     * @access  public
	 * @param int $uid 用户id
	 * @param int $clear_cache 是否清缓存
	 * @param int $force_db 是否强制走主库
     * @return  array   急推标签信息
     */
	 public function get_tag_valid($uid,$clear_cache = 0, $force_db = 0){
		 $data_info = array();
		 $mem_key = $this->_mem_key.'_get_tag_package_record_'.$uid;
		 if( $clear_cache == 1)
		 {
		 	$this->mc->delete($mem_key);
		 }
		 $cache = $this->mc->get($mem_key);
		 if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
		 {
			$data_info = $cache['data'];
		 }
		 else{
			 $nowtime = $this->get_now_time();
			 if($force_db == 1)
			 {				
				$cond_where = "uid = $uid AND esta =0 AND $nowtime<=end_time";
                $this->db->where($cond_where);
				//排序条件
                $this->db->order_by("end_time", "ASC");
                $data_info = $this->db->get( self::TAG_PACKAGE_TBL )->result_array();
				//echo $this->db->last_query();
			
			 }
			 else
			 {
				$cond_where = "uid = $uid AND esta =0 AND $nowtime<=end_time";
				
                $this->dbback->where($cond_where);
				//排序条件
                $this->dbback->order_by("end_time", "ASC");
                $data_info = $this->dbback->get( self::TAG_PACKAGE_TBL )->result_array();
				
			 }
			 $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$data_info),  600);
			
		 }
		 return $data_info;

	 }


	  /**
     * 获取当前可用标签数量
     *
     * @access  public
	 * @param int $uid 用户id
	 * @param int $clear_cache 是否清缓存
	 * @param int $force_db 是否强制走主库
     * @return  int  当前可用急推标签数量
     */
	 public function get_tag_valid_num($uid,$clear_cache = 0, $force_db = 0){

		 $mem_key = $this->_mem_key.'_get_tag_package_record_num_'.$uid;
		 if( $clear_cache == 1)
		 {
		 	$this->mc->delete($mem_key);
		 }
		 $this->mc->delete($mem_key);
		 $cache = $this->mc->get($mem_key);
		 if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
		 {
			$tag_num = $cache['data'];
		 }
		 else{
			 $nowtime = $this->get_now_time();
			 if($force_db == 1)
			 {
				$cond_where = "uid = $uid AND esta =0 AND $nowtime<=end_time";
                $this->db->where($cond_where);              
                $tag_num = $this->db->from( self::TAG_PACKAGE_TBL )->count_all_results();

			 }
			 else
			 {
				$cond_where = "uid = $uid AND esta =0 AND $nowtime<=end_time";
                $this->dbback->where($cond_where);
				
                $tag_num = $this->dbback->from( self::TAG_PACKAGE_TBL )->count_all_results();

                 if($_GET['zkg']){
                     var_dump($this->dbback->last_query());die;
                 }
			 }
			 $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$tag_num),  300);


		 }
		 return $tag_num;

	 }
	 
	  /**
     * 获取当月已使用标签数量
     *
     * @access  public
	 * @param int $uid 用户id
	 * @param int $clear_cache 是否清缓存
	 * @param int $force_db 是否强制走主库
     * @return  int  当前可用急推标签数量
     */
	 public function get_used_tag_valid_num($uid,$clear_cache = 0, $force_db = 0){
		 
		 $mem_key = $this->_mem_key.'_get_used_tag_package_record_num_'.$uid;
		 if( $clear_cache == 1)
		 {
		 	$this->mc->delete($mem_key);
		 }
		 $cache = $this->mc->get($mem_key);
		 if(isset($cache['is_ok']) && $cache['is_ok'] == 1 )
		 {
			$tag_num = $cache['data'];
		 }
		 else{
			 $nowtime = $this->get_now_time();
			 //$month_begin_time = $this->get_month_begin_time();
			 if($force_db == 1)
			 {
				
				$cond_where = "uid = $uid AND esta =1 AND $nowtime<=end_time";
				//$cond_where = "uid = $uid AND esta =1 AND $month_begin_time<=end_time";
                $this->db->where($cond_where);              
                $tag_num = $this->db->from( self::TAG_PACKAGE_TBL )->count_all_results();
			   
			 }
			 else
			 {
				$cond_where = "uid = $uid AND esta =1 AND $nowtime<=end_time";
				//$cond_where = "uid = $uid AND esta =1 AND $month_begin_time<=end_time";
                $this->dbback->where($cond_where);               
                $tag_num = $this->dbback->from( self::TAG_PACKAGE_TBL )->count_all_results();	
			   // echo $this->dbback->last_query();exit();
			 }
			 $this->mc->add($mem_key, array('is_ok'=>1,'data'=>$tag_num),  300);
			
		 }
		 return $tag_num;

	 }

     /**
     * 修改使用标签数量
     *
     * @access  public
	 * @param int $uid 用户id
     * @return boolean 是否设置成功	
     */
	 public function update_tag_record($uid){
		
		 $result = FALSE;
		 $nowtime = $this->get_now_time();
		 $update_data = array();
         $update_data['esta'] = 1;
         $cond_where = "uid = $uid AND esta =0 AND $nowtime<=end_time";
         $this->db->where($cond_where);
		 $this->db->order_by('end_time',"ASC");
         $this->db->limit(1);
         $this->db->update(self::TAG_PACKAGE_TBL, $update_data);
         // echo $this->db->last_query();exit();		
         $result = $this->db->affected_rows() > 0 ? TRUE : FALSE;
		 return $result;
	 }

     /**
     *  //到期提醒
     *
     * @access  public
	 * @param int $uid 用户id
	 * @param int $clear_cache 是否清缓存
	 * @param int $force_db 是否强制走主库
     * @return boolean 是否设置成功	
     */
	
	 public function expire_remind($uid,$clear_cache = 0, $force_db = 0){
		 $tag_valid_arr = $this->get_tag_valid($uid,$clear_cache,$force_db);
		 $time_count =array();
		 if(is_array($tag_valid_arr)&&!empty($tag_valid_arr)){
			 foreach($tag_valid_arr as $key=>$value){

				$time_arr[] = $this->tag_exipire_days($value['end_time']);			    
			 }
			 $time_count = array_count_values($time_arr);
		 
		 }
		 return $time_count;
		
	 }  
  
    
    
    /**
     * 获取房源标签剩余天数
     *
     * @access  public
     * @param   int $expiretime 到期时间
     * @return  strting  剩余天数
     */
    public function tag_exipire_days( $expiretime )
    {   
	$strRemainder = '';

        if($expiretime > 0) 
        {
            $nowtime = strtotime( date('Y-m-d 00:00:00') );
            $dif    = $expiretime - $nowtime;
            
            $strRemainder = ceil( $dif / 86400 );
        }
        
        return isset($strRemainder) ? $strRemainder.'天' : '';
    }


	 //插入包标签
	 private function insert_package($insert_data){
		if (isset($insert_data[0]) && is_array($insert_data[0]))
        {
            //批量插入
            if ($this->db->insert_batch(self::TAG_PACKAGE_TBL, $insert_data))
            {
                return $this->db->insert_id();
            }
        } 
        else 
        {
            //单条插入
            if ($this->db->insert(self::TAG_PACKAGE_TBL, $insert_data))
            {
                return $this->db->insert_id();
            }
        }
        return false;
		
	 }












}







/* End of file sell_tag_base_model.php */
/* Location: ./applications/models/sell_tag_base_model.php */