<?php
/**
 * 充值活动设置
 * @author sun
 */
class PayRechargeActive {
    
    /**
     * 主数据库 
     * @var TINY_ADODB 
     */    
    private $db;
    
    /**
     * 从数据库
     * @var type
     */
    private $dbback;
    
    /**
     * 缓存对象
     * @var type
     */
    private $mc;

    /**
     * 关联的表名 
     * @var String
     */
    private $table;
    
    /**
     * 房豆和钱的比例
     * @var type 
     */
    public static $rate = '0.1';
    
    /**
     * 查询区间范围
     * @param int $start 开始值
     * @param int $limit 每页显示多少条
     * @return string
     */
    private function getLimitCond($start, $limit) {
        $start = $start > 0 ? $start : 0;
        $limit_cond = ($start == 0 && $limit == 0) ? '' : ' LIMIT '
                      . $start . ' , ' . $limit;
        return $limit_cond;
    }
    
    /**
     * 验证字段是否合法
     * @param type $field
     * @return type
     */
    private function validateFiled($field) {
        return in_array($field, $this->fields);
    }
    
    /**
     * 排序规则
     * @param string $orderField 字段
     * @param string $order 排序规则
     * @return string
     */
    private function getOrder($orderField, $order = 'ASC') {
        $strOrder = '';
        if ($this->validateFiled($orderField)) {
            $strOrder = "ORDER BY $orderField $order";
        }
        return $strOrder;
    }
    
    /**
     * 构造函数，初始化db memcache 等。
     * @param string $table 表名，可以使用默认值。
     */
    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = 'pay_housebean_recharge_active';
        $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);
        }
    }
    
    /**
     * 插入充值记录
     * @param int $num 数量
     * @param string $operator 操作人
     * @param int $isActive 是否参与活动
     * @param int $price 价格
     * @param date $starttime 开始时间
     * @param date $endtime 结束时间
     * @return true or false
     */
    public function insert($num, $operator, $isActive = 0, $price = '', 
            $starttime = '', $endtime = '') {
        $fieldData = array();
        $fieldData['num'] = $num;
        $fieldData['operator'] = $operator;
        $fieldData['isactive'] = $isActive;
        $fieldData['price'] = $price;
        $fieldData['starttime'] = $starttime;
        $fieldData['endtime'] = $endtime;
        $fieldData['ctime'] = date('Y-m-d H:i:s');
        $sql = SqlCombinHelper::insert_sql($this->table, $fieldData);
        return $this->db->execute($sql);
    }
    
    /**
     * 根据id获取某条充值记录
     * @param int $id 编号
     * @return array
     */
    public function getOneById($id) {
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "id = '$id'");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 设置充值状态
     * @param int $id 编号
     * @param int $status 状态
     * @return return or false
     */
    public function setStatus($id, $status, $operator) {
        $fieldData = array('status' => $status, 'operator' => $operator);
        $sql = SqlCombinHelper::update_sql($this->table, $fieldData, "id='$id' and isactive =1");
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 统计条数
     * @return int
     */
    public function getCount() {
       $sql = SqlCombinHelper::count_sql($this->table);
       return $this->dbback->getOne($sql);
    }
    
    /**
     * 展示充值记录
     * @param int $start 开始页
     * @param int $pagesize 每页显示条数
     * @param string $order 升序还是降序
     * @return array
     */
    public function getList($start = null, $pagesize = 20, $order = 'desc') {
        if($start !== null && $pagesize !== null) {
            $limit = $this->getLimitCond($start, $pagesize);
        } else {
            $limit = '';
        }
		if($order !== null){
		    $order = $this->getOrder('id', $order);
        } else {
            $order = '';
        }
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, 
                "1 = 1 {$order} {$limit} ");
        return $this->dbback->getAll($sql);
    }
}
