<?php

class ComplainRecord{
     /**
     *
     * @var TINY_ADODB 主数据库
     */
    protected $db;
    /**
     *
     * @var TINY_ADODB 从数据库
     */
    protected $dbback;
    /**
     *
     * @var Memcache 缓存
     */
    protected $mc;
    /**
     *
     * @var string 表名
     */
    protected $table;
    /**
     *
     * @var 表中字段数组 
     */
    protected $fields;
    
    /**
     * 初始化类
     * @param string $table 表名，默认为complain_record
     */
    public function __construct($table='complain_record') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = $table;
        $this->mc = $container->get('mc');
        $sqlfield = "DESC $this->table";
        $fieldsMKey = $sqlfield;
        $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);
        }
    }
    
    /**
     * findByHouseId
     * 根据房源id查找记录
     * @param int $houseId 房源ID
     * @return array
     */    
    public function findByHouseId($houseId){
          $sql = SqlCombinHelper::select_sql($this->table, $this->fields,
                "house_id = $houseId");
        return $this->dbback->getAll($sql);
    }



	/**
     * findByHouseId
     * 根据房源id查找记录
     * @param int $houseId 房源ID
     * @return array
     */    
    public function findByHouseIdFromDb($houseId){
          $sql = SqlCombinHelper::select_sql($this->table, $this->fields,
                "house_id = $houseId");
        return $this->db->getAll($sql);
    }
    
    
    /**
     * findCountByHouseId
     * 根据房源id统计数量
     * @param int $houseId
     * @return int 
     */
    public function findCountByHouseId($houseId){
        $sql = SqlCombinHelper::count_sql($this->table, "house_id=$houseId");
        return $this->dbback->getOne($sql);
    }
    
    
    
     /*
     * 根据条件查找记录findByConf
     * findByConf
     */
     public function findByConf($conf,$order='',$offset='',$limit='',$groupby='') {      
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, $conf);
        if($groupby){
            $sql .= "GROUP BY $groupby";
        }
        if($order) {
            $sql .= " ORDER BY $order";
        }
        if($limit && $offset){
          $sql .= " LIMIT ".$offset.",".$limit;
         }elseif($limit) {
            $sql .= " LIMIT $limit";
        }

        return $this->db->GetAll($sql);
    }
    
    
    /*
     * 根据条件查找记录findByConfGroupHaving
     * findByConf
     */
     public function findByConfGroupHaving($conf,$order='',$offset='',$limit='',$groupby='',$having) {      
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, $conf);
        if($groupby){
            $sql .= " GROUP BY $groupby";
        }
        if($order) {
            $sql .= " ORDER BY $order";
        }
        if($having){
            $sql .= "HAVING $having";            
        }
        if($limit && $offset){
          $sql .= " LIMIT ".$offset.",".$limit;
         }elseif($limit) {
            $sql .= " LIMIT $limit";
        }

        return $this->db->GetAll($sql);
    }
        /*
     * 根据条件查找记录findByConfGroupHaving
     * findByConf
     */
     public function countByConfGroupHaving($conf,$groupby='',$having) {      
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, $conf);
        if($groupby){
            $sql .= "GROUP BY $groupby";
        }
        if($having){
            $sql .= "HAVING $having";            
        }
        $sql = SqlCombinHelper::count_sql($this->table, $conf);
        return $this->dbback->getOne($sql);
    }
    
    /*
     * 根据条件统计
     * findCountByConf
     */
	public function findCountByConf($conf,$groupby='') {   
        if($groupby){
            $conf .= " GROUP BY $groupby";
        }
        
		$sql = SqlCombinHelper::count_sql($this->table, $conf);
        return $this->dbback->getOne($sql);
	}  
    

    /*
     * 根据经纪人id查找记录
     * findByBrokerAndStatus
     */
     public function findByBrokerAndStatus($broker_id,$status='',$order='',$offset='',$limit='') {
        $conf =  $status!= '' ?  "broker_id='$broker_id' AND status= $status" : "broker_id='$broker_id'";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, $conf);
        if($order) {
            $sql .= " ORDER BY $order";
        }
        if($limit && $offset){
          $sql .= " LIMIT ".$offset.",".$limit;
         }elseif($limit) {
            $sql .= " LIMIT $limit";
        }

        return $this->db->GetAll($sql);
    }
    
    
    /*
     * 根据经纪人id和状态统计
     * findCountByBrokerAndStatus
     */
	public function findCountByBrokerAndStatus($uid,$status='') {
        $conf =  $status!= '' ?  "broker_id='$uid' AND status= $status" : "broker_id='$uid'";
		$sql = SqlCombinHelper::count_sql($this->table, $conf); 
        return $this->dbback->getOne($sql);
	}  
    
    
    
    
    
    /**
     * 获取某条房源是否被某紧急人举报
     * @param type $bid
     * @param type $houseId
     * @return type
     */
    public function findByBrokerAndHouseId($uid, $houseId) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,
                "broker_id = $uid  AND house_id = $houseId ");
        return $this->dbback->getRow($sql);
    }
   
    
    /*
     * 根据id查找
     */
    public function findById($id) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,"id='$id'");
        return $this->dbback->GetRow($sql);
    }
    
    

    public function insert($fieldData) {
        if($this->checkField($fieldData)){
            $sql = SqlCombinHelper::insert_sql($this->table, $fieldData);           
            $this->db->execute($sql);
            return $this->db->Insert_ID();
        } else {
            return false;
        }
    }
    
   

    /**
     * 
     * @param array $fieldData
     * @param string $conf
     * @return boolean
     */
    public function update($fieldData,$conf) {
        if($this->checkField($fieldData)){
            $sql = SqlCombinHelper::update_sql($this->table, $fieldData, $conf);
            $this->db->Execute($sql);
            return $this->db->Affected_Rows();
        } else {
            return false;
        }
    }

   
    /**
     * 检查数组是否正确
     * @param array $fields 要检查的数组
     * @return boolean 正确返回true，失败返回false
     */
    public function checkField($fieldData) {
        $fields = array_keys($fieldData);
        if(array_intersect($fields, $this->fields)==$fields){
            return true;
        } else {
            return false;
        }
    }
    
 
    
    public function getTable() {
        return $this->table;
    } 

  
    
    
    public function getFields() {
        return $this->fields;
    }
    /**
     * 清空表
     */
    public function emptyAll() {
        $sql = "DELETE FROM $this->table";
        return $this->db->Execute($sql);
    }
    
    public function insertMutl(array $filedData) {
        $sql = SqlCombinHelper::insertMutil_sql($this->table, $filedData);
        $this->db->execute($sql);
        return $this->db->Affected_Rows();
    }
    
    
    
    
}
