<?php

/**
 * 
 */

/**
 * 经纪人成长体系发布房源等级日志（是否加积分）
 *
 * @author 
 */
class GrowthHouseLog {
    //put your code here
	private $db;
    private $dbback;       
    private $table;   
    private $fields;
    public function __construct() {      
       $container = Container::getInstance();
       $this->db = $container->get('db');
       $this->dbback = $container->get('dbback');	  
       $this->table = "growth_house_log";
       //$this->fields = "id,tbl,house_id,grade";
        $this->mc = $container->get('mc');
        $sqlfield = "DESC $this->table";
        $fieldsMKey = $sqlfield;
		//$this->mc->delete($fieldsMKey);
        $this->fields = $this->mc->get($fieldsMKey);
        if($this->fields === false) {
            $fields = $this->dbback->getAll($sqlfield);
            foreach ($fields as $field) {
                $this->fields[] = $field['Field'];
            }
            $this->mc->set($fieldsMKey, $this->fields, 3600);
        }

    }
    
     /**
     * 获取房源等级记录信息
     * @param $brokerId
     * @return array 
     * @author  2014-3-28
     */    
     public function findHouseGradeLog($houseId,$tbl,$grade,$brokerId){		 
         $sql = SqlCombinHelper::select_sql($this->table, $this->fields, 
                 "broker_id = $brokerId and tbl = '$tbl' and house_id = $houseId");        
         $result = $this->dbback->getRow($sql);	
		 if(!$result){			
		     $this->insertHouseGradeLog($houseId,$tbl,$grade,$brokerId);
			 //$this->findHouseGradeLog($houseId,$tbl,$grade,$brokerId);
			 $result = array('tbl'=>$tbl,'broker_id '=>$brokerId,'house_id'=>$houseId,'grade'=>$grade);
		 }
		 return $result;
     }
     
	 /**
     * 添加房源记录
     * @param 无
     * @return array 
     * @author  2014-3-28
     */    
     public function insertHouseGradeLog($houseId,$tbl,$grade, $brokerId){      
        $fieldData['tbl'] = $tbl;
        $fieldData['house_id'] = $houseId;	
        $fieldData['grade'] = $grade;
        $fieldData['broker_id'] = $brokerId;
        $fieldData['ptime'] = time();
        $sql = SqlCombinHelper::insert_sql($this->table, $fieldData);
        $this->db->execute($sql);		
     }
     
	 /**
	 *修改房源等级记录
	 *
	 *
	 */
   
     public function updateHouseGradeLog($houseId,$tbl,$grade,$brokerId = ''){       
		$fieldData['grade'] = $grade;	
        $fieldData['utime'] = time();	
	    $sql = SqlCombinHelper::update_sql($this->table, $fieldData, "broker_id = $brokerId and tbl= '$tbl' and house_id = $houseId");
        if($this->db->execute($sql)) {			
            return true;
        } else {
            return false;
        }
    		
     }
     
     
     public function findCountBy($brokerId, $startTime) {
         $sql = SqlCombinHelper::count_sql($this->table, "broker_id = $brokerId"
                 . "and tbl = 'sell' and $startTime <= ptime");
         $sellNum   = (int)$this->dbback->GetOne($sql);
         $sql = SqlCombinHelper::count_sql($this->table, "broker_id = $brokerId"
                 . "and tbl = 'rent' and $startTime <= ptime");
         $rentNum   = (int)$this->dbback->GetOne($sql);
         return $sellNum + $rentNum;
     }
    
}
