123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- /**
- * AgentGameModel.php UTF-8
- * 渠道游戏MOdel
- *
- * @date : 2018/4/26 14:53
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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);
- }
- }
|