* @version : HUOSDK 8.0 */ namespace huo\model\agent; use huo\model\common\CommonModel; use huo\model\game\GameextModel; use huo\model\game\GameModel; use huo\model\rate\AgentGameRateModel; use huo\model\rate\GameRateModel; use huo\model\user\UserModel; use huolib\constant\AgentConst; use huolib\constant\CacheConst; use huolib\constant\GameConst; use huomp\model\game\GameMiniModel; use think\Cache; class AgentGameModel extends CommonModel { protected $name = 'agent_game'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $agent_cache_tag = CacheConst::TAG_AGENT_GAME_LIST; protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_PREFIX; /** * 连接游戏 * * @return \think\model\relation\BelongsTo */ public function game() { return $this->belongsTo(GameModel::className(), 'app_id', 'id', 'game')->setEagerlyType(0); } /** * 关联game_version表 * * @return \think\model\relation\HasMany */ public function gv() { return $this->hasMany('huo\model\game\GameversionModel', 'app_id', 'app_id')->field('app_id,package_url'); } /** * 连接游戏折扣 */ public function gamerate() { return $this->belongsTo(GameRateModel::className(), 'app_id', 'app_id')->field('app_id,benefit_type'); } /** * 连接游戏扩展 */ public function gameext() { return $this->belongsTo(GameextModel::className(), 'app_id', 'app_id')->setEagerlyType(0); } /** * 连接小程序游戏 */ public function gamemini() { return $this->belongsTo(GameMiniModel::className(), 'app_id', 'app_id'); } /** * 连接渠道 * * @return \think\model\relation\BelongsTo */ public function agent() { return $this->belongsTo(UserModel::className(), 'agent_id', 'id')->field('id,user_login,user_nicename,role_id,parent_id'); } /** * 关联折扣 * * @return \think\model\relation\HasOne */ public function rate() { return $this->hasOne(AgentGameRateModel::className(), 'ag_id', 'id'); } public function getBenefitTypeAttr($value) { if (GameConst::GAME_PROMOTE_SWITCH_NO == $value) { return '不可推广'; } elseif (GameConst::GAME_PROMOTE_SWITCH_CAN == $value) { return '可推广'; } elseif (GameConst::GAME_PROMOTE_SWITCH_CHECK == $value) { return '需审核'; } return '不可推广'; } /** * 添加渠道游戏 * * @param $data * * @return bool|string */ public function addData($data) { if (empty($data)) { return false; } if ($_obj = self::create($data, true)) { Cache::clear($this->agent_cache_tag); return $_obj->id; } else { return false; } } /** * 更新渠道游戏 * * @param $ag_data * @param $ag_id * * @return bool */ public function updateData($ag_data, $ag_id) { $_map['id'] = $ag_id; $_data = $ag_data; $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } else { Cache::clear($this->agent_cache_tag); return true; } } /** * 获取渠道ID * * @param string $agent_game * * @return bool|int|mixed */ public function getAgentIdByAg($agent_game = '') { if (empty($agent_game)) { return 0; } $_map['agent_game'] = $agent_game; $_agent_id = $this->where($_map)->value('agent_id'); if (false === $_agent_id) { return false; } return $_agent_id; } /** * 获取AG_ID * * @param string $agent_game * * @return bool|int|mixed */ public function getAgIdByAg($agent_game = '') { if (empty($agent_game)) { return 0; } $_map['agent_game'] = $agent_game; $_ag_id = $this->where($_map)->value('id'); if (false === $_ag_id) { return false; } return $_ag_id; } /** * 获取渠道ID * * @param int $ag_id * * @return bool|int|mixed */ public function getAgentIdByAgId($ag_id = 0) { if (empty($ag_id)) { return 0; } $_map['id'] = $ag_id; $_agent_id = $this->where($_map)->value('agent_id'); if (false === $_agent_id) { return false; } return $_agent_id; } /** * 通过渠道ID 获取已申请的游戏 * * @param $agent_id * * @return array */ public function getGameIdsByAgentId($agent_id) { if (empty($agent_id)) { return []; } $_map['agent_id'] = $agent_id; $_game_ids = $this->where($_map)->column('app_id'); return $_game_ids; } /** * @param $app_id * @param $agent_id * * @return int */ public function getAgIdByAgentIdAppId($agent_id, $app_id) { $_map['agent_id'] = $agent_id; $_map['app_id'] = $app_id; $_ag_id = $this->where($_map)->value('id'); if (false == $_ag_id) { $_ag_id = 0; } return $_ag_id; } /** * @param $agent_id * * @param $app_id * * @return array|bool|false|\PDOStatement|string|\think\Model */ public function getInfoByAgentIdAppId($agent_id, $app_id) { $_map['agent_id'] = $agent_id; $_map['app_id'] = $app_id; $_ag_info = $this->where($_map)->find(); if (false === $_ag_info) { return false; } if (is_object($_ag_info)) { $_ag_info = $_ag_info->toArray(); } return $_ag_info; } /** * @param $ag_id * * @return array|bool|false */ public function getAgInfoByAgId($ag_id) { if (empty($ag_id)) { return false; } $_map['id'] = $ag_id; $_ag_info = $this->where($_map)->find(); if (false === $_ag_info) { return false; } if (is_object($_ag_info)) { $_ag_info = $_ag_info->toArray(); } return $_ag_info; } /** * 获取单条记录缓存key * * @param int $agent_id 渠道ID * @param int $app_id 应用ID * * @return string */ protected function getCacheKeyByAgentApp($agent_id, $app_id) { return $this->cache_key_prefix.'a_'.$agent_id.'g_'.$app_id; } /** * 根据Agent_id app_id 获取ID * * @param int $agent_id 渠道ID * @param int $app_id 应用ID * * @return int|mixed */ public function getIdByAgentApp($agent_id, $app_id) { /* 缓存操作 */ $_cache_key = $this->getCacheKeyByAgentApp($agent_id, $app_id); $_id = Cache::get($_cache_key); if (!empty($_id)) { return $_id; } $_map = [ 'agent_id' => $agent_id, 'app_id' => $app_id, ]; $_id = $this->where($_map)->value('id'); if (empty($_id)) { return 0; } Cache::set($_cache_key, $_id); return $_id; } /** * 根据Agent_id app_id 获取数据 * * @param int $agent_id 渠道ID * @param int $app_id 应用ID * * @return int|mixed */ public function getInfoByAgentApp($agent_id, $app_id) { $_id = $this->getIdByAgentApp($agent_id, $app_id); if (empty($_id)) { return []; } return $this->getInfoById($_id); } /** * 获取渠道游戏是否切量 * * @param int $agent_id 渠道ID * @param int $app_id 应用ID * * @return int */ public function getIsSwitchByAgentApp($agent_id, $app_id) { $_info = $this->getInfoByAgentApp($agent_id, $app_id); return get_val($_info, 'is_switch', AgentConst::USER_SWITCH_NO); } }