<?php
/**
 * 成长值操作记录
 *
 * @author sun
 */
class GrowthExpRecord {
    /**
     * 主数据库
     */
    private $db;

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

    /**
     * 缓存对象
     */
    private $mc;
    
    /**
     * 关联的表名
     * @var string
     */
    private $table;
    
    /**
     * 表中包含字段
     * @var array
     */
    private static $fields = array('id','broker_id','way_alias','score','exp',
        'description', 'discard','ctime');
    
    public function __construct($strDate = null) {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        if ($strDate == '') {
            $strDate = date('Ymd');
        }
        $this->table = "growth_exp_record_" . $strDate;
        $this->exp = new GrowthExp();
    }
    
    /**
     * 创建表结构
     * @param type $strDate
     * @return type
     */
    public function createTable($tablename) {
        $sql = "CREATE TABLE IF NOT EXISTS `{$tablename}` (
            `id` int(10) unsigned NOT NULL auto_increment,
            `broker_id` int(11) NOT NULL COMMENT '经纪人ID',
            `way_alias` varchar(20) NOT NULL COMMENT '获取途径，关联exp_way表way_alias字段',
            `score` int(11) NOT NULL COMMENT '本次获取成长值分值，大于0表示获得，小于0表示扣除',
            `exp` int(11) NOT NULL COMMENT '本次获取后时的成长值',
            `description` varchar(256) character set gbk default NULL COMMENT '操作描述',
            `ip` varchar(30) NOT NULL COMMENT '记录操作人的IP地址',
            `ctime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP COMMENT '成长值变化的时间戳',
            `discard` tinyint(4) NOT NULL default '0' COMMENT '是否废弃，1废弃，0正常状态',
            PRIMARY KEY  (`id`),
            KEY `brokerid` (`broker_id`)
          ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='成长值变化记录表'";
        return $this->db->execute($sql);
    }
    
    /**
     * 删除表
     * @param type $tablename
     */
    public function dropTable($tablename) {
        $sql = "DROP TABLE  IF EXISTS  $tablename";
        return $this->db->execute($sql);
    }
    
    /**
     * 验证字段是否合法
     * @param type $field
     * @return type
     */
    private function validateFiled($field) {
        return in_array($field, self::$fields);
    }
    
    /**
     * 
     * @param type $start
     * @param type $limit
     * @return type
     */
    private function getLimitCond($start, $limit) {
        $start = $start > 0 ? $start : 0;
        $limit_cond = ($start == 0 && $limit == 0) ? '' : ' LIMIT '
                      . $start . ' , ' . $limit;
        return $limit_cond;
    }
    
    /**
     * 
     * @param type $orderField
     * @param type $order
     * @return string Description
     */
    private function getOrder($orderField, $order = 'ASC') {
        $strOrder = '';
        if ($this->validateFiled($orderField)) {
            $strOrder = 'ORDER BY $orderField $order';
        }
        return $strOrder;
    }
    
    
    /**
     * 
     * @param type $brokerId
     * @param type $wayAlias
     * @param type $score
     * @param type $description
     * @param type $discard
     * @return type
     */
    public function insertData($brokerId,$wayAlias,$score,$description,
            $discard = 0) {
        $arrFieldsValue = array();
        $expOne = $this->exp->findOneByBrokerId($brokerId);
        $arrFieldsValue['broker_id'] = $brokerId;
        $arrFieldsValue['way_alias'] = $wayAlias;
        $arrFieldsValue['score'] = $score;
        $arrFieldsValue['exp'] = $expOne['exp'];
        $arrFieldsValue['description'] = $description;
        $arrFieldsValue['ip'] = GetIp();;
        $arrFieldsValue['discard'] = $discard;
        $sql = SqlCombinHelper::insert_sql($this->table, $arrFieldsValue);
        return $this->db->execute($sql);
    }
    
    /**
     * 
     * @param type $wayAlias
     * @param type $brokerId
     */
    public function countNumData($wayAlias, $brokerId) {
        
    }
    
    /**
     * 
     * @param type $wayAlias
     * @param type $brokerId
     */
    public function findAllByWayAliasAndBrokerId($wayAlias, $brokerId, 
            $start, $limit) {
        
    }
    
    /**
     * 
     * @param type $brokerId
     * @param type $start
     * @param type $limit
     */
    public function findAllByBrokerId($brokerId, $start, $limit) {
        
    }
    
    /**
     * 
     * @param type $brokerId
     * @param type $start
     * @param type $limit
     */
    public function findRowByBrokerId($brokerId, $start, $limit) {
        
    }

	/**
     * 根据经纪人id 查找成长值变化记录数据
     * @param int $brokerId 经纪人id
     * @param int $start 开始的记录数
     * @param int $number 返回的条数
     * @return type
     */
    public function findByBrokerId($brokerId,$start=null,$number=null,$order=null) {
        $limit = '';
        if($start !== null&&$number !== null) {
            $limit = "limit $start,$number";
        }
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields,"broker_id = '$brokerId'");
		if($order !== null){
		    $sql .= "order by id $order ";
		}
        $sql .= $limit;
       return $this->dbback->getAll($sql);
    }
    
    public function findCountByBrokerId($brokerId) {
       $sql = SqlCombinHelper::select_sql($this->table, "count(id) as num","broker_id = '$brokerId'");
       return $this->dbback->getOne($sql);
    }
}
