* @version : HUOSDK 8.5 */ namespace huoRate\model; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\GameConst; use think\Cache; class AgentGameRateModel extends CommonModel { protected $name = 'agent_game_rate'; protected $pk = 'ag_id'; protected $type = ['benefit_type' => 'int']; /* 开启自动写入时间戳字段 */ protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_RATE_PREFIX; protected $agent_cache_tag = CacheConst::TAG_AGENT_GAME_LIST; /** * 获取单条记录缓存key * * @param int $id ID * * @return string */ protected function getSingleCacheKey($id) { return $this->cache_key_prefix.$id; } /** * 获取单条记录缓存key * * @param int $app_id 应用ID * @param int $agent_id 渠道ID * * @return string */ protected function getCacheKeyByGameAgent($app_id, $agent_id) { return $this->cache_key_prefix.$app_id.'_'.$agent_id; } /** * 添加数据 * * @param array $data 需要添加的数据 * * @return false|int 添加失败返回 false 添加成功 返回添加的ID */ public function addData($data) { $_data = $data; $_id = parent::addData($_data); if (false === $_id) { return false; } { $_app_id = get_val($data, 'app_id', 0); $_agent_id = get_val($data, 'agent_id', 0); /* 添加数据 APP VIP 缓存 */ $_cache_key = $this->getCacheKeyByGameAgent($_app_id, $_agent_id); Cache::set($_cache_key, $_id); } /* TAG缓存操作 */ return $_id; } /** * 通过ID获取信息 * * @param int $id 主键ID * * @return array|false */ public function getInfoById($id) { /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); $_data = Cache::get($_single_cache_key); if (!empty($_data)) { return $_data; } $_data = parent::getInfoById($id); if (empty($_data)) { return []; } Cache::set($_single_cache_key, $_data); return $_data; } /** * 更新单条数据 * * @param array $data 数据 * @param int $id ID * * @return bool */ public function updateData($data, $id) { $_data = $data; $_rs = parent::updateData($_data, $id); if (false === $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); /* TAG缓存操作 */ Cache::clear($this->agent_cache_tag); /* 更新轮播图列表缓存数据 */ if (!empty($data['app_id'])) { //(new SlideItemModel())->clCacheByAppId($data['app_id']); } return true; } /** * 删除单条数据 * * @param int $id ID * @param bool $is_complete 是否完成删除 * * @return bool */ public function deleteData($id, $is_complete = false) { $_rs = parent::deleteData($id, $is_complete); if (false == $_rs) { return false; } /* 缓存操作 */ $_single_cache_key = $this->getSingleCacheKey($id); Cache::rm($_single_cache_key); /* TAG缓存操作 */ Cache::clear($this->agent_cache_tag); return $_rs; } /** * 更新多条数据 * * @param array $data 数据 * @param array $ids 主集ID * * @return bool */ public function updateDatas($data, $ids) { $_data = $data; foreach ($ids as $_id) { $_rs = $this->updateData($_data, $_id); if (false == $_rs) { return false; } } return true; } /** * 根据游戏删除所有数据 * * @param int $app_id 应用ID * * @return bool */ public function deleteDataByAppId($app_id) { $_map = [ 'app_id' => $app_id, ]; $_ids = $this->where($_map)->column('id'); foreach ($_ids as $_id) { $this->deleteData($_id); } return true; } /** * 根据应用渠道获取ID * * @param int $app_id 应用ID * @param int $agent_id 渠道ID * * @return int */ public function getIdByGameAgent($app_id, $agent_id) { if (empty($app_id) && empty($agent_id)) { return 0; } $_cache_key = $this->getCacheKeyByGameAgent($app_id, $agent_id); $_id = Cache::get($_cache_key); if (!empty($_id)) { if (CommonConst::CACHE_HAS_READ == $_id) { return 0; } return $_id; } $_map = [ 'app_id' => $app_id, 'agent_id' => $agent_id, ]; $_id = $this->where($_map)->value($this->pk); if (empty($_id)) { Cache::set($_cache_key, CommonConst::CACHE_HAS_READ); return 0; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据应用渠道获取信息 * * @param int $app_id 应用ID * @param int $agent_id 渠道ID * * * @return array|false */ public function getInfoByGameAgent($app_id, $agent_id) { $_id = $this->getIdByGameAgent($app_id, $agent_id); if (empty($_id)) { return false; } return $this->getInfoById($_id); } /** * 根据ID获取应用ID * * @param int $id ID * * * @return int */ public function getAppIdById($id) { $_data = $this->getInfoById($id); return get_val($_data, 'app_id', CommonConst::CONST_ZERO); } /** * 根据ID获取渠道ID * * @param int $id ID * * * @return int */ public function getAgentIdById($id) { $_data = $this->getInfoById($id); return get_val($_data, 'agent_id', CommonConst::CONST_ZERO); } /** * 根据应用渠道获取信息 * * @param int $app_id 应用ID * @param int $agent_id 渠道ID * * * @return array|false */ public function getPromoteSwitchByGameAgent($app_id, $agent_id = 0) { $_data = $this->getInfoByGameAgent($app_id, $agent_id); return get_val($_data, 'promote_switch', 0); } /** * 根据应用渠道游戏折扣 * * @param int $app_id 应用ID * @param int $agent_id 渠道ID * * * @return array|false */ public function getGameRateByGameAgent($app_id, $agent_id = 0) { $_data = $this->getInfoByGameAgent($app_id, 0); return get_val($_data, 'game_rate', CommonConst::CONST_ZERO); } /** * 根据应用渠道获取 优惠类型 * * @param int $app_id 应用ID * @param int $agent_id 渠道ID * * * @return int 优惠类型,0 无优惠 1 折扣 2 返利 */ public function getBenefitTypeByGameAgent($app_id, $agent_id = 0) { $_data = $this->getInfoByGameAgent($app_id, 0); return get_val($_data, 'benefit_type', GameConst::RATE_BENEFIT_NO); } /** * 关联game表 */ public function game() { return $this->belongsTo('huo\model\game\GameModel', 'app_id')->field('id,name,icon,classify'); } /** * 关联user表 */ public function user() { return $this->belongsTo('huo\model\user\UserModel', 'agent_id')->field('id,user_login,user_nicename,role_id'); } // // /** // * 根据应用渠道获取一级渠道返点比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getAgentRebateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'agent_rebate', CommonConst::CONST_ZERO); // } // // /** // * 根据应用渠道获取 二级渠道返点比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getSubAgentRebateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'sub_agent_rebate', CommonConst::CONST_ZERO); // } // // /** // * 根据应用渠道获取 一级渠道分成比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getAgentRateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'agent_rate', CommonConst::CONST_ONE); // } // // /** // * 根据应用渠道获取下级渠道分成比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getSubAgentRateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'sub_agent_rate', CommonConst::CONST_ONE); // } // // // /** // * 根据应用渠道获取 玩家折扣比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getMemRateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'mem_rate', CommonConst::CONST_ZERO); // } // // /** // * 根据应用渠道获取 玩家首充折扣比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getFirstMemRateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'first_mem_rate', CommonConst::CONST_ZERO); // } // // /** // * 根据应用渠道获取 玩家返利比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getMemRebateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'mem_rebate', CommonConst::CONST_ZERO); // } // // /** // * 根据应用渠道获取 玩家首充返利比例 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return FLOAT // */ // public function getFirstMemRebateByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'first_mem_rebate', CommonConst::CONST_ZERO); // } // // /** // * 根据应用渠道获取渠道奖励 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return int CPA计价 // */ // public function getAgentRewardByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'agent_reward', 0); // } // // /** // * 根据应用渠道获取玩家奖励 // * // * @param int $app_id 应用ID // * @param int $agent_id 渠道ID // * // * // * @return int CPA计价 // */ // public function getMemRewardByGameAgent($app_id, $agent_id) { // $_data = $this->getInfoByGameAgent($app_id, $agent_id); // // return get_val($_data, 'mem_reward', 0); // } }