<?php

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

/**
 * 积分获取方式和套餐类型对应的积分值类
 * @author wxj
 */
class GrowthCreditWayPackage {
    /**
     *
     * @var Mixed 主数据库
     */
    private $db;
    
    /**
     *
     * @var Mixed 从数据库
     */
    private $dbback;
    
    /**
     *
     * @var Memcache memcache缓存
     */
    private $mc;
    
    /**
     *
     * @var array 表中的字段
     */
    private $fields = array('id','way_alias','package_id','score');
    
    /**
     *
     * @var String 表名
     */
    private $table;
    
    /**
     *
     * @var type 积分的数组
     */
    private $data;
    
    /**
     * 构造函数，初始化db，memcache，表名等
     */
    public function __construct() {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->table = 'growth_credit_way_package';
        $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 String $wayAlias 获取方式的别名
     * @return array
     */
    public function getByWayAlias($wayAlias) {
        $this->setData();
        $res = array();
        foreach ($this->data as $value) {
            if($value['way_alias']==$wayAlias) {
                $res[] = $value;
            }
        }
        return $res;
        //$sql = "select ".$this->fields()." from $this->table where way_id = $wayId";
        //return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据获取方式别名和套餐id获得积分值数据
     * @param String $wayAlias 获取方式的别名
     * @param Integer $packageId
     * @return Array
     */
    public function getScoreByWayAliasAndPackageId($wayAlias,$packageId) {
        $this->setData();
        $res = 0;
        foreach ($this->data as $value) {
            if($value['way_alias']==$wayAlias&&$value['package_id']==$packageId) {
                $res = $value['score'];
                break;;
            }
        }
        return $res;
    }


    /**
     * 根据套餐id获得所有获取方式的积分数据
     * @param int $packageId 套餐id
     * @return array
     */
    public function getByPackageId($packageId) {
        $this->setData();
        $res = array();
        foreach ($this->data as $value) {
            if($value['package_id']==$packageId) {
                $res[] = $value;
            }
        }
        return $res;
        //$sql = "select ".$this->fields()." from $this->table where package_id = $packageId";
        //return $this->dbback->getAll($sql);
        
    }
    
    /**
     * 查找所有方式对应的积分数据
     * @return array
     */
    public function findAll() {
        $this->setData();
        return $this->data;
    }
    
    /**
     * 设置Data data包含所有对应的积分值
     */
    private function setData() {
        if(!$this->data) {
            //$sql = "select id,way_alias,package_id,score from $this->table";
            $sql = SqlCombinHelper::select_sql($this->table, $this->fields, "");
            //print_r($this->dbback->GetAll($sql));
            $res = $this->dbback->GetAll($sql);
            if($res) {
                $this->data = $res;
            } else {
                $this->data = array();
            }
        }
    }

    /**
     * 返回以逗号链接的字段名字符串
     * @return string
     */
    private function fields() {
        return implode(',', $this->fields);
    }
    /**
     * 获得表名
     * @return string 表名
     */
    public function getTableName() {
        return $this->table;
    }
    
    /**
     * 保存积分值设置
     * @param array $fields
     * @return boolean
     */
    public function save($fields) {
        $keys = array_keys($fields);
        if(array_key_exists('score', $fields)) {
            $score = $fields['score'];
            //$sql = "replace into $this->table (".  implode(",", $keys).") value ('".  implode("','", $fields)."')";
            $sql = "insert into $this->table (".  implode(",", $keys).") value ('".  implode("','", $fields)."') ON DUPLICATE KEY UPDATE score='$score'";
            if($this->db->execute($sql)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
        
    }
}

?>
