<?php
/**
 * 竞投房源虚假举报基类
 *
 */
class Complain {
    /**
     * 主数据库
     */
    public $db;

    /**
     * 从数据库
     */
    public $dbback;

    /**
     * 缓存对象
     */
    public $mc;
    
    /**
     * 关联的表名
     * @var string
     */
    public $table;
    
    /**
     * 表中包含字段
     * @var array
     */
    public $fields;

    public function __construct($table = '') {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
          
        $this->mc = $container->get('mc');       
        $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);
        }
    }
    
    /**
     * 获取所有记录
     * @return array
     */
    public function findAll() {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields);
        return $this->dbback->getAll($sql);
    }
    
        
    
    /**
     * 保存数据
     * @param array $fields 数据结构
     * @return boolean
     */
    public function saveData($fields) {
        $sql = SqlCombinHelper::replace_sql($this->table,$fields);
        return $this->db->execute($sql);
    }
    
    /*
     * 添加数据
     */
    public function insert($fieldData) {
        $sql = SqlCombinHelper::insert_sql($this->table, $fieldData);
        $this->db->Execute($sql);
        return $this->db->Insert_ID();
    }
    
    /*
     * 根据id查找
     */
    public function findById($id) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,"id='$id'");
        return $this->dbback->GetRow($sql);
    }
    
    /*
     * 根据条件统计数量
     */
    public function findNumberBy($conf) {
        $sql = SqlCombinHelper::count_sql($this->table, $conf);
        return $this->dbback->getOne($sql);
    }
    
    /*
     * 删除
     */
    public function delBy($conf) {
        $sql = SqlCombinHelper::delete_sql($this->table, $conf);
        $this->db->Execute($sql);
        return $this->db->Affected_Rows();
    }

    /*
     * 更新
     */
        public function update($fieldData,$conf) {
        $sql = SqlCombinHelper::update_sql($this->table, $fieldData, $conf);
        $this->db->Execute($sql);
        return $this->db->Affected_Rows();
    }
    
    
}
