<?php

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

/**
 * 经纪人积分类
 *
 * @author wxj
 */
class GrowthCredit {
    /**
     *
     * @var TINY_ADODB 主数据库 
     */    
    private $db;
    
    /**
     *
     * @var type 从数据库
     */
    private $dbback;
    
    /**
     *
     * @var type 缓存对象
     */
    private $mc;

    /**
     *
     * @var String 关联的表名 
     */
    private $table;

    /**
     *
     * @var array   表中包含的字段 
     */
    private $fields = array('id','broker_id','credit','frozen','status');


    /**
     * 构造函数，初始化db memcache 等。
     * @param string $table 表名，可以使用默认值。
     */
    public function __construct($table = null) {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = 'growth_credit';
        $this->mc = $container->get('mc');
        $sqlfield = "DESC $this->table";
        $fieldsMKey = $sqlfield;
        $this->fields = $this->mc->get($fieldsMKey);
        //modified by bianshiwu 20141218 用empty($this->fields) 不用‘===’，因为出现过$this->fields为空的情况
        if(empty($this->fields)) {
            $fields = $this->dbback->getAll($sqlfield);
            foreach ($fields as $field) {
                $this->fields[] = $field['Field'];
            }
            $this->mc->set($fieldsMKey, $this->fields, 3600);
        }
    }
    
    /**
     * 通过id查找一条积分数据
     * @param type $id 积分表中的Id
     * @return array 返回一个包含一条积分数据的数组 
     */
    public function findOne($id) {
        //$sql = "select id,broker_id,credit,status from $this->table where id = $id";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "id = '$id'");
        return $this->dbback->getRow($sql);
    }

    /**
     * 通过id查找一条积分数据，是findOne的别名函数
     * @param type $id 积分表中的Id
     * @return type 返回一个包含一条积分数据的数组 
     */
    public function findById($id) {
        return $this->findOne($id);
    }
    
    /**
     * 根据经纪人id查找经纪人积分
     * @param type $brokerId 经纪人id
     * @return array 返回一个包含一条积分数据的数组 
     */
    public function findByBrokerId($brokerId) {
        //$sql = "select id,broker_id,credit,status from $this->table where id = $brokerId";
        $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "broker_id = '$brokerId'");
        return $this->db->getRow($sql);
    }
    
    /**
     * 根据经纪人Id增加积分或减少积分
     * @param type $brokerId 经纪人Id
     * @param type $creditValue 增加的分值，负值表示减少
     * @return boolean 失败返回false 成功返回true
     */
    public function addCreditToOne($brokerId,$creditValue) {
        //$sql = "update $this->table set credit = credit+$creditValue where broker_id=$brokerId";
        $creditValue = $creditValue > 0 ? '+' . $creditValue : $creditValue;
        $fieldData['credit'] =   $creditValue;
		
        $sql = SqlCombinHelper::updateSelf_sql($this->table, $fieldData, "broker_id='$brokerId'");

        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 根据提供的经纪人ID数组 增加或减少积分
     * @param type $brokerIdArray 经纪人Id组成的数组
     * @param type $creditValue 积分值
     * @return boolean 成功返回true 失败返回false
     */
    public function addCredit($brokerIdArray,$creditValue) {
        //$sql = "update $this->table set credit = credit+$creditValue where broker_id in ('".  implode("','", $brokerIdArray)."')";
        $strBrokerId = implode("','", $brokerIdArray);
        $creditValue = $creditValue > 0 ? '+' . $creditValue : $creditValue;
        $fieldData['credit'] =   $creditValue;
        $sql = SqlCombinHelper::updateSelf_sql($this->table, $fieldData, "broker_id in ('$strBrokerId')");
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据经纪人id设置积分值
     * @param type $brokerId
     * @param type $creditValue
     * @return boolean 失败返回false 成功返回true
     */
    public function setCreditToOne($brokerId,$creditValue) {
        //$sql = "update $this->table set credit = $creditValue where broker_id=$brokerId";
        $fieldData = array('credit'=>$creditValue);
        $sql = SqlCombinHelper::update_sql($this->table, $fieldData, "broker_id='$brokerId'");
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 根据提供的经纪人ID数组 设置积分值
     * @param type $brokerIdArray 经纪人Id组成的数组
     * @param type $creditValue 积分值
     * @return boolean
     */
    public function setCredit($brokerIdArray,$creditValue) {
        //$sql = "update $this->table set credit = $creditValue where broker_id in ('".  implode("','", $brokerIdArray)."')";
        $fieldData['credit'] = $creditValue;
        $sql = SqlCombinHelper::update_sql($creditValue, $fieldData, "broker_id in ('".  implode("','", $brokerIdArray)."')");
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 插入一条积分记录
     * @param type $brokerId 经纪人id
     * @param type $credit  积分值
     * @param type $status 状态，默认值是1
     * @return boolean 成功返回true，失败返回false
     */
    public function insertData($brokerId,$credit,$status=1) {
        //$sql = "insert into $this->table (broker_id,credit,status) value ($brokerId,$credit,$status)";
        $fieldData = array();
        $fieldData['broker_id'] = $brokerId;
        $fieldData['credit'] = $credit;
        $fieldData['status'] = $status;
        $sql = SqlCombinHelper::insert_sql($this->table, $fieldData);
        return $this->db->execute($sql);
    }
    
    /**
     * 批量增加积分
     * @param array $credits 数据数组，包含broker_id,credit,status。
     * @return boolean 成功返回true，失败返回false
     */
    public function insertMutl(array $credits) {
        /*
        $fields = array('broker_id','credit','status');
        $s = array();
        foreach ($credits as $value) {
            $brokerId = $value['brokerId'];
            $credit = $value['credit'];
            if (array_key_exists('status', $value)) {
                $status = $value['status'];
            } else {
                $status = 1;
            }
            $s[] = "($brokerId,$credit,$status)";
        }
        $sql = "insert into $this->table (broker_id,credit,status) values ".implode(',', $s);
        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
         * 
         */
        $sql = SqlCombinHelper::insertMutil_sql($this->table, $credits);
        $this->db->execute($sql);
        return $this->db->Affected_Rows();
    }
    
    /**
     * 根据id删除积分数据
     * @param integer $id 积分id
     * @return boolean 成功返回true，失败返回false
     */
    public function delById($id) {
        $sql = SqlCombinHelper::delete_sql($this->table, "id='$id'");
        return $this->db->execute($sql);
    }
    
    /**
     * 根据经纪人id删除积分数据
     * @param integer $brokerId
     * @return boolean 成功返回true，失败返回false
     */
    public function delByBrokerId($brokerId) {
        $sql = SqlCombinHelper::delete_sql($this->table, "broker_id='$brokerId'");
        return $this->db->execute($sql);
    }
    /**
     * 根据条件删除多条积分数据
     * @param string $field 字段名
     * @param array $fieldData 要删除的值
     */
    public function delMult($field,$fieldData) {
        if(!in_array($field, $this->fields)) {
            return false;
        }
        $strWhere = "$field in ('".  implode("','", $fieldData)."')";
        $sql = SqlCombinHelper::delete_sql($this->table, $strWhere);
    }
    
    /**
     * 根据经济id的数组成批删除经纪人
     * @param array $brokerIds 经纪人id的数组
     * @return type
     */
    public function delMultByBroker($brokerIds) {
        return $this->delMult('broker_id', $brokerIds);
    }
    
    /**
     * 获取表的字段数组
     * @return array    返回fields属性
     */
    public function getFields() {
        return $this->fields;
    }
    
    /**
     * 
     * @return string 返回表名
     */
    public function getTable() {
        return $this->table;
    }
    
    /**
     * 冻结id为$brokerId的经纪人的数目未$score的积分
     * @param type $brokerId
     * @param type $score
     */
    public function frozen($brokerId,$score) {
        $sql = SqlCombinHelper::updateSelf_sql($this->table, array('frozen'=>$score), "broker_id='$brokerId'");
        $this->db->Execute($sql);
        return $this->db->Affected_Rows();
    }
    
    /**
     * $score 小于0表示解冻的分数
     * @param int $brokerId
     * @param int $score
     * @return type
     */
    public function unfreeze($brokerId,$score) {
        return $this->frozen($brokerId,$score);
    }
    
    /**
     * 返回可用积分数
     */
    public function getValidCredit($brokerId) {
        $res = $this->findByBrokerId($brokerId);
        if($res) {
            return $res['credit']-$res['frozen'];
        } else {
            return 0;
        }
    }
}

?>
