<?php
/**
 * 成长值获取方式方式和套餐之间的关联
 *
 * @author sun
 */
class GrowthExpWayPackage {
    
    /**
     * 主数据库
     */
    private $db;

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

    /**
     * 缓存对象
     */
    private $mc;
    
    /**
     * 关联的表名
     * @var string
     */
    private $table;
    
    /**
     * 表中包含字段
     * @var array
     */
    private static $fields = array('id','way_alias','package_id','score');

    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_way_package';
    }
    
    /**
     * 查找所有套餐和获取成长值相关数据
     * @return array
     */
    public function findAllData() {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields);
        return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据获取方式和套餐获取相关数据
     * @param string $wayAlias 方式别名
     * @param int $packageId 套餐编号
     * @return array
     */
    public function findDataByWayAliasAndPackageId($wayAlias, $packageId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, 
                "way_alias = '$wayAlias' AND package_id = $packageId");
        return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据获取方式和套餐获相关分值
     * @param string $wayAlias 方式别名
     * @param type $packageId 套餐编号
     * @return int
     */
    public function findScoreByWayAliasAndPackageId($wayAlias, $packageId) {
        $arrRow = $this->findDataByWayAliasAndPackageId($wayAlias, $packageId);
        return !isset($arrRow['score']) || $arrRow['score'] == '' ? 0 : $arrRow['score'];
    }
    
    /**
     * 插入相关数据
     * @param string $way_alias 方式别名
     * @param int $package_id 套餐编号
     * @param int $score 分值
     * @return boolean
     */
    public function insertData($way_alias, $package_id, $score) {
        $arrFieldsValue = array();
        $arrFieldsValue['way_alias'] = $way_alias;
        $arrFieldsValue['package_id'] = $package_id;
        $arrFieldsValue['score'] = $score;
        $sql = SqlCombinHelper::insert_sql($this->table, $arrFieldsValue);
        return $this->db->execute($sql);
    }
    
    /**
     * 根据套餐编号删除相关数据
     * @param int $packageId 套餐编号
     * @return boolean
     */
    public function delDataByPackageId($packageId) {
        $sql = SqlCombinHelper::delete_sql($this->table, "package_id = "
                . "$packageId");
        return $this->db->execute($sql);
    }
    
    /**
     * 根据方式编号删除相关数据
     * @param string $wayAlias 方式别名
     * @return boolean
     */
    public function delDataByWayAlias($wayAlias) {
        $sql = SqlCombinHelper::delete_sql($this->table, 
                "way_alias = '$wayAlias'");
        return $this->db->execute($sql);
    }
    
    /**
     * 根据方式编号和套餐编号删除相关数据
     * @param string $wayAlias 方式别名
     * @param type $packageId 套餐编号
     * @return boolean
     */
    public function delDataByWayAliasAndPackageId($wayAlias, $packageId) {
        $sql = SqlCombinHelper::delete_sql($this->table, "way_alias = "
                . "'$wayAlias'" . "AND package_id = $packageId");
        return $this->db->execute($sql);
    }
    
    /**
     * 保存数据
     * @param array $fields 数据结构
     * @return boolean
     */
    public function saveData($fields) {
        $sql = SqlCombinHelper::replace_sql($this->table, $fields);
        return $this->db->execute($sql);
    }
}
