<?php

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

/**
 * 经纪人成长值类
 *
 * @author sun
 */
class GrowthExp {
    
    /**
     * 主数据库
     */
    private $db;

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

    /**
     * 缓存对象
     */
    private $mc;
    
    /**
     * 关联的表名
     * @var string
     */
    private $table;
    
    /**
     * 经纪人的表名
     * @var string
     */
    private $table_broker;
    
    /**
     * 表中包含字段
     * @var array
     */
    private static $fields = array('id','broker_id','exp','status');
    
    private static $fields_broker = array('username', 'truename');

    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = 'growth_exp';
        $this->table_broker = 'broker';
    }
    
    /**
     * 根据经纪人编号获取一条经验值记录
     * @param int $brokerId 经纪人编号
     * @return array 返回一个包含经验值数据的数组 
     */
    public function findOneByBrokerId($brokerId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields,
                "broker_id = $brokerId");
        return $this->db->getRow($sql);
    }

    /**
     * 根据经纪人编号获取所有的经验值记录
     * @return mixed
     */
    public function findAllByBrokerId($brokerId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields,
                "broker_id = $brokerId");
        return $this->dbback->getAll($sql);
    }

    /**
     * 根据经纪人编号设置经验值
     * @param int $brokerId 经纪人编号
     * @param int $score 经验值
     * @return boolean 失败返回false 成功返回true
     */
    public function setExpToOneByBrokerId($brokerId, $score) {
        $arrField['exp'] =  $score;
        $sql = SqlCombinHelper::update_sql($this->table, $arrField, 
                "broker_id = $brokerId");
        return $this->db->execute($sql);
    }

    /**
     * 根据经纪人编号数组设置经验值
     * @param array $arrBrokerId 经纪人编号数组
     * @param int $score 经验值
     * @return boolean 失败返回false 成功返回true
     */
    public function setExpByBrokerId($arrBrokerId, $score) {
        $strBrokerId = implode(',', $arrBrokerId);
        $arrField['exp'] =  $score;
        $sql = SqlCombinHelper::update_sql($this->table, $arrField, 
                "broker_id IN ($strBrokerId)");
        return $this->db->execute($sql);
    }

    /**
     * 根据经纪人编号增加减少经验值
     * @param int $brokerId 经纪人编号
     * @param int $score 经验值
     * @return boolean 成功返回true，失败返回false
     */
    public function addExpToOneBrokerId($brokerId, $score) {
        $score = $score > 0 ? '+' . $score : $score;
        $arrField['exp'] =   $score;
        $sql = SqlCombinHelper::updateSelf_sql($this->table, $arrField, 
                "broker_id = $brokerId");      
        return $this->db->execute($sql);
    }
    
    /**
     * 根据经纪人数组编号增加减少经验值
     * @param array $arrBrokerId 经纪人编号数组
     * @param int $score 经验值
     * @return boolean 成功返回true，失败返回false
     */
    public function addExpBrokerId($arrBrokerId, $score) {
        $strBrokerId = implode(',', $arrBrokerId);
        $score = $score > 0 ? '+' . $score : $score;
        $arrField['exp'] =   $score;
        $sql = SqlCombinHelper::updateSelf_sql($this->table, $arrField, 
                "broker_id IN ($strBrokerId)");
        return $this->db->execute($sql);
    }
    
    /**
     * 插入一条数据
     * @param int $brokerId 经纪人编号
     * @param int $score 经验值
     * @param int $status 状态，默认值是1 
     * @return boolean 成功返回true，失败返回false
     */
    public function insertData($brokerId, $score, $status=1) {
        $arrFieldsValue = array();
        $arrFieldsValue['broker_id'] = $brokerId;
        $arrFieldsValue['exp'] = $score;
        $arrFieldsValue['status'] = $status;
        $sql = SqlCombinHelper::insert_sql($this->table, $arrFieldsValue);
        return $this->db->execute($sql);
    }
    
    /**
     * 删除经纪人数据
     * @param array $arrBrokerId 经纪人编号数组
     * @return boolean
     */
    public function delOneDataByBrokerId($brokerId) {
        $sql = SqlCombinHelper::delete_sql($this->table, 
                "broker_id = $brokerId");
        return $this->db->execute($sql);
    }

    /**
     * 批量删除经纪人数据
     * @param array $arrBrokerId 经纪人编号数组
     * @return boolean
     */
    public function delMutilDataByBrokerId($arrBrokerId) {
        $strBrokerId = implode(',', $arrBrokerId);
        $sql = SqlCombinHelper::delete_sql($this->table, 
                "broker_id IN ($strBrokerId)");
        return $this->db->execute($sql);
    }
    
    /**
     * 给表名中的字段加上表名为前缀
     * @param string $tablename 表名
     * @param array $arrFields  表名中的字段数组
     * @return array  array($tablename.$arrFields[0])
     */
    private function getRelatedTblNField($tablename, $arrFields) {
        $arrCombinFields = array();
        foreach($arrFields as $key => $value) {
            $arrCombinFields[] = $tablename . '.' . $value;
        }
        return $arrCombinFields;
    }
    
    /**
     * 拼装grow_exp和broker为查询字段，并返回
     * @return string
     */
    private function getInnerSearchByBetweenTable() {
        $searchField = '';
        $searchField .= implode(',', $this->getRelatedTblNField(
                $this->table_broker, self::$fields_broker));
        $brkerFields = implode(',', $this->getRelatedTblNField(
                $this->table, self::$fields));
        $searchField = $searchField . ',' . $brkerFields;
        return $searchField;
    }
    
    /**
     * 返回在此区间成长值经纪人的总数
     * @param int $down_exp 成长值介于下限边值
     * @param int $up_exp 成长值介于上限边值
     * @return int 经纪人的数目
     */
    public function findBrokerNumByBetweenExp($down_exp, $up_exp) {
        $sql = SqlCombinHelper::innerCount_sql($this->table_broker, $this->table,
                "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1 AND {$this->table}.exp "
                        . ">= $down_exp AND {$this->table}.exp <= $up_exp");
        return $this->dbback->getOne($sql);
    }
    
    /**
     * 返回大于此成长值经纪人的总数
     * @param int $exp 成长值
     * @return int 经纪人的数目
     */
    public function findBrokerNumByGlExp($exp) {
        $sql = SqlCombinHelper::innerCount_sql($this->table_broker, $this->table,
                "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1 AND {$this->table}.exp "
                        . ">= $exp");
        return $this->dbback->getOne($sql);
    }
    
    /**
     * 返回小于此成长值经纪人的总数
     * @param int $exp 成长值
     * @return int 经纪人的数目
     */
    public function findBrokerNumByLtExp($exp) {
        $sql = SqlCombinHelper::innerCount_sql($this->table_broker, $this->table,
                "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1 AND {$this->table}.exp "
                        . "<= $exp");
        return $this->dbback->getOne($sql);
    }
    
    /**
     * 返回在此区间成长值经纪人数组
     * @param int $down_exp 成长值介于下限边值
     * @param int $up_exp 成长值介于上限边值
     * @return array 经纪人数组
     */
    public function findAllBrokerByBetweenExp($down_exp, $up_exp) {
        $searchField = $this->getInnerSearchByBetweenTable();
        $sql = SqlCombinHelper::innerSelect_sql($this->table_broker, $this->table,
                $searchField, "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1 AND {$this->table}.exp "
                        . ">= $down_exp AND {$this->table}.exp <= $up_exp");
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 返回大于此成长值经纪人数组
     * @param int $exp 成长值
     * @return array 经纪人数组
     */
    public function findAllBrokerByGlExp($exp) {
        $searchField = $this->getInnerSearchByBetweenTable();
        $sql = SqlCombinHelper::innerSelect_sql($this->table_broker, $this->table,
                $searchField, "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1 AND {$this->table}.exp "
                        . ">= $exp");
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 返回小于此成长值经纪人数组
     * @param int $exp 成长值
     * @return array 经纪人数组
     */
    public function findAllBrokerByLtExp($exp) {
        $searchField = $this->getInnerSearchByBetweenTable();
        $sql = SqlCombinHelper::innerSelect_sql($this->table_broker, $this->table,
                $searchField, "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1 AND {$this->table}.exp "
                        . "<= $exp");
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 返回小于此成长值经纪人数组
     * @param int $exp 成长值
     * @return array 经纪人数组
     */
    public function findCountBrokerByLtExp($exp) {
        $searchField = "count(*)";
        $sql = SqlCombinHelper::innerSelect_sql($this->table_broker, $this->table,
                $searchField, "{$this->table_broker}.uid = {$this->table}.broker_id",
                "{$this->table_broker}.valid_flag =1 AND {$this->table}.exp "
                . "<= $exp");
        $allBrokerExpNum = $this->mc->get($sql);
        if ($allBrokerExpNum === false) {
            $allBrokerExpNum = $this->dbback->getOne($sql);
            $this->mc->set($sql, $allBrokerExpNum,300); 
        }
        return $allBrokerExpNum;
    }

    /**
     * 查找所有经纪人和基本信息及成长值 
     * @return array
     */
    public function findAllBrokerAndExpInfo() {
        $searchField = $this->getInnerSearchByBetweenTable();
        $sql = SqlCombinHelper::innerSelect_sql($this->table_broker, $this->table,
                $searchField, "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1");
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 查找所有经纪人和基本信息及成长值 
     * @return array
     */
    public function findCountBrokerAndExpInfo() {
        $searchField = "count(*)";
        $sql = SqlCombinHelper::innerSelect_sql($this->table_broker, $this->table,
                $searchField, "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1");
        $allBrokerExpNum = $this->mc->get($sql);
        if ($allBrokerExpNum === false) {
            $allBrokerExpNum = $this->dbback->getOne($sql);
            $this->mc->set($sql, $allBrokerExpNum,300); 
        }
        return $allBrokerExpNum;
    }

    /**
     * 查找所有经纪人和基本信息及成长值 
     * @return array
     */
     /**
    public function findAllBrokerAndExpCount() {
        $searchField = "count(*)";
        $sql = SqlCombinHelper::innerSelect_sql($this->table_broker, $this->table,
                $searchField, "{$this->table_broker}.uid = {$this->table}.broker_id",
                        "{$this->table_broker}.valid_flag =1");
        return $this->dbback->getOne($sql);
    }**/

    /**
     * 批量插入多记录
     * @param array $exps
     * @return type
     */
    public function insertMutl(array $exps) {
        $sql = SqlCombinHelper::insertMutil_sql($this->table, $exps);
        $this->db->execute($sql);
        return $this->db->Affected_Rows();
    }
    
    public function emptyTable() {
        $sql = "TRUNCATE TABLE $this->table";
        $this->db->Execute($sql);
    }
    /**
     * 返回表名
     */
    public function getTable() {
        return $this->table;
    }
}
