<?php
/**
 * 成长值和套餐的关系
 *
 * @author sun
 */
class GrowthExpControllPackage {
    /**
     * 主数据库
     */
    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->table = 'growth_exp_controll_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 findOneByWayAliasAndPackageId($wayAlias, $packageId) {
        $packageId = empty($packageId)?0:$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) {
        $packageId = empty($packageId)?0:$packageId;
        $arrRow = $this->findOneByWayAliasAndPackageId($wayAlias, $packageId);
        return $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 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);
    }
    
    /**
     * 通过别名和套餐id获取分值
     * @param type $wayAlias
     * @param type $packageId
     * @return type
     */
    public function findScoreByWayAliasPackageId($wayAlias, $packageId) {
        $sql = SqlCombinHelper::select_sql($this->table, 'score', "way_alias = "
                . "'$wayAlias' AND package_id = $packageId");
        return $this->dbback->getOne($sql);
    }
}
