<?php
//贷款记录类
class ZsbLoanRecord{
     /**
     *
     * @var TINY_ADODB 主数据库
     */
    protected $db;
    /**
     *
     * @var TINY_ADODB 从数据库
     */
    protected $dbback;
    /**
     *
     * @var Memcache 缓存
     */
    protected $mc;
    /**
     *
     * @var string 表名
     */
    protected $table;
    /**
     *
     * @var 表中字段数组 
     */
    protected $fields;
    
    /**
     * 初始化类
     * @param string $table 表名，zsb_loan_record
     */
    public function __construct($table='zsb_loan_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);
        }
    }
    
    
    
    
    
    
    /*
     * 根据经纪人uid查找记录
     */
     public function findByBroker($uid,$order='',$offset='',$limit='') {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "broker_id='$uid'");
        if($order) {
            $sql .= " ORDER BY $order";
        }
        if($offset){
          $sql .= " LIMIT ".$offset.",".$limit;
         }elseif($limit) {
            $sql .= " LIMIT $limit";
        }

        return $this->db->GetAll($sql);
    }
    
    /*
     * 根据经纪人id和状态统计
     */
	public function findCountByBrokerAndStatus($uid,$status=1) {
		$sql = SqlCombinHelper::count_sql($this->table, "broker_id=$uid and status $status");
        return $this->dbback->getOne($sql);
	}  
    
    /**
     * 根据记录id获取记录类型
     */
    public function findTypeByRecordId($id){
        $sql = SqlCombinHelper::select_sql($this->table, 'type',
                "id = $id");
        return $this->dbback->getOne($sql);
    }
    
    /*
     * 根据条件统计数量
     */
    public function findNumberBy($conf) {
        $sql = SqlCombinHelper::count_sql($this->table, $conf);
        return $this->dbback->getOne($sql);
    }
    
    public function findBy($conf,$order='',$offset='',$limit='') {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,$conf);
       //$sql = SqlCombinHelper::innerSelect_sql($this->table, 'broker', implode(",", $this->fields).',agentcode' , "$this->table.broker_id=broker.uid", $conf);
        if($order) {
            $sql .= " ORDER BY $order";
        }
        if($limit && $offset){
          $sql .= " LIMIT ".$offset.",".$limit;
         }elseif($limit) {
            $sql .= " LIMIT $limit";
        }
     
        return $this->db->GetAll($sql);
    }
    
   /**
    * 插入记录insert
    * @param type $fieldData
    * @return boolean
    */
    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();
    }
    
    
   
    
}
