<?php

/**
 * core\lib\growth\GrowthBidPeriods.php
 */

/**
 * 竞投期次
 *
 * @author Administrator
 */
class GrowthBidPeriods {
    
    const BID_PERIODS_STATUS_ON = 1;//进行中
    const BID_PERIODS_STATUS_NOT_START = 2;//未开始
    const BID_PERIODS_STATUS_FINISH = 3;//已结束

    /**
     *
     * @var TINY_ADODB 主数据库
     */
    private $db;
    /**
     *
     * @var TINY_ADODB 从数据库
     */
    private $dbback;
    /**
     *
     * @var MCache 缓存
     */
    private $mc;
    /**
     *
     * @var string 表名
     */
    private $table;
    /**
     *
     * @var 表中字段数组 
     */
    private $fields;
    
    /**
     * 竞投类型数组
     * @var type 
     */
    private static $bidType = array(
        'blockexpert' => 1, 'house' => 2 
    );
    
    /**
     * 初始化类
     * @param string $table 表名，默认为growth_bid_periods
     */
    public function __construct($table = 'growth_bid_periods') {
        $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);
        }
    }
    
    /**
     * 根据id查找竞投期次信息
     * @param int $id 期次id
     * @return array 期次数据，包括开始时间，结束时间等
     */
    public function findById($id) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,"id='$id'");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据指定条件查找批次
     * @param string $conf 条件
     * @return type
     */
    public function findBy($conf='',$order = '',$limit = '') {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,$conf);
        if($order) {
            $sql .= " order by $order";
        }
        if($limit) {
            $sql .= " limit $limit";
        }
        return $this->dbback->getAll($sql);
    }
    
    public function findAll($conf='') {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields,$conf);
        return $this->dbback->GetAssoc($sql);
    }

    /**
     * 把数据$fieldData插入到表中
     * @param array $fieldData
     * @return boolean 成功返回插入id，失败返回false
     */
    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 $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 static function formatTime($startTime,$endTime,$format="Y.m.d") {
        $stime = strtotime($startTime);
        $etime = strtotime($endTime);
        return date($format,$stime)."-".date($format,$etime);
    }

    /**
     * 获取小于某一期之前的上一期id
     * @param type $type
     * @param type $periodsId
     */
    public function upBidPeriods($type = 1, $periodsId) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, 
                "id < $periodsId AND type = $type ORDER BY id DESC");
        return $this->dbback->getRow($sql);
    }
    
    public function currentBidPeriods($type = 1) {
        //最新一期的id
        $confgbp = "type=$type";
        $order = "ctime desc";
        $limit = "0,1";
        $bidp = $this->findBy($confgbp, $order, $limit);
        if($bidp) {
            $bidp= $bidp[0];
        }
        $bidpid = $bidp['id'];
        //判断本期的状态
        $time = time();
        $start_time = strtotime($bidp['start_time']);
        $end_time = strtotime($bidp['end_time']);
        if($time>=$start_time&&$time<$end_time) {//进行中
            $bidp['status'] = '1';
        } else if($time<$start_time){
            $bidp['status'] = '2';//未开始
        } elseif($time>=$end_time) {
            $bidp['status'] = '3';//竞投结束
        }
        return $bidp;
    }
    
    /**
     * 判断期次是否在竞投进行中
     * @param array $bidp 期次数组
     * @return boolean true 表示在竞投中，false 表示不在竞投中
     */
    public static function isBidOn($bidp) {
        $time = time();
        $start_time = strtotime($bidp['start_time']);
        $end_time = strtotime($bidp['end_time']);
        if($time>=$start_time&&$time<$end_time) {//进行中
            return true;
        } else{
            return false;//未开始
        } 
    }
}

?>
