* @version : HUOSDK 8.0 */ namespace huo\model\game; use huo\controller\game\GameCache; use huo\model\common\CommonModel; use huo\model\member\MemGameModel; use huo\model\rate\GameRateModel; use huo\model\slide\SlideModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\GameConst; use huolib\constant\MpConfConst; use huolib\constant\SlideConst; use huomp\model\game\GameMiniModel; use huomp\model\weixin\MpConfModel; use think\Cache; class GameModel extends CommonModel { protected $name = 'game'; //设置只读字段 // protected $readonly = ['app_key']; /* 自动写入 */ protected $insert = ['app_key']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $type = ['image' => 'array']; protected $cache_tag = 'game_id_name'; protected $list_tag = CacheConst::TAG_GAME_LIST; protected $agent_cache_tag = CacheConst::TAG_AGENT_GAME_LIST; /** * 基础查询 * * @param $query */ protected function base($query) { $query->where('delete_time', 0) ->where('is_delete', 2); } /** * @return \think\model\relation\HasMany */ public function gift() { return $this->hasMany('huo\model\game\GiftModel', 'app_id'); } /** * 关联游戏折扣表 */ public function rate() { return $this->hasOne(GameRateModel::className(), 'app_id', 'id', [], 'left')->setEagerlyType(0); } /** * 关联小游戏折扣表 */ public function mini() { return $this->hasOne(GameMiniModel::className(), 'app_id', 'id')->field('app_id,mini_app_id'); } /** * @return \think\model\relation\HasMany */ public function mg() { return $this->hasMany(MemGameModel::className(), 'app_id', 'id'); } /** * @return \think\model\relation\HasMany */ public function goods() { return $this->hasMany('huoAccountDeal\model\AccountGoodsModel', 'app_id', 'id'); } // // /** // * 关联游戏类型表 // */ // public function classify() { // return $this->hasOne('huo\model\game\GameclassifyModel', 'id', 'classify', [], 'left')->setEagerlyType(0); // } /** * 关联game_ext表 * * @return \think\model\relation\HasOne */ public function ext() { return $this->hasOne('huo\model\game\GameextModel', 'app_id', 'id', [], 'left')->setEagerlyType(0); } /** * 关联oa_game表 * * @return \think\model\relation\HasOne */ public function oa() { if (\huolib\oa\oa::hasOa()) { return $this->hasOne('huo\model\oa\OaGameModel', 'app_id', 'id', [], 'left')->setEagerlyType(0); } } /** * @return \think\model\relation\HasMany */ public function ag() { return $this->hasMany('huo\model\agent\AgentGameModel', 'app_id', 'id'); } /** * 关联game_version表 * * @return \think\model\relation\HasMany */ public function gv() { return $this->hasMany('huo\model\game\GameversionModel', 'app_id'); } public function cp() { return $this->hasOne('huo\model\game\CpModel', 'id', 'cp_id') ->field(['id', 'company_name']); } /** * @param string $value * * @param array $data * * @return string */ public function setAppKeyAttr($value = '', $data) { if (empty($value)) { return md5($value.$data['name'].time().uniqid()); } return $value; } /** * @param string $value * * @param array $data * * @return string */ public function setEnAbbrAttr($value = '', $data) { return huo_initials($value.$data['en_name']).'_'.$data['id']; } /** * 游戏游戏图标获取器 * * @param $value * * @return mixed */ public function getIconAttr($value) { if (!empty($value)) { return cmf_get_image_url($value); } return $value; } /** * 游戏游戏图标获取器 * * @param $value * * @return mixed */ public function getExtInfoAttr($value) { if (!empty($value)) { return json_decode($value, true); } return $value; } public function setExtInfoAttr($value) { if (!empty($value)) { return json_encode($value); } return $value; } /** * 游戏类型获取器 * * @param $value * * * @return mixed * */ public function getClassifyLabelAttr($value) { if (empty($value)) { return ''; } else { $_classify = substr($value, 0, 1); } if (GameConst::GAME_IOS == $_classify) { return '苹果'; } elseif (GameConst::GAME_ANDROID == $_classify) { return '安卓'; } elseif (GameConst::GAME_H5 == $_classify) { return 'H5'; } elseif (GameConst::GAME_MP_BOX == $value) { return '金币盒子'; } elseif (GameConst::GAME_MP_RPBOX == $value) { return '红包盒子'; } elseif (GameConst::GAME_MP == $_classify) { return '小游戏'; } else { return ''; } } /** * 游戏状态获取器 * * @param $value * * * @return mixed * */ public function getStatusLabelAttr($value) { if (GameConst::GAME_STATUS_ACCESS == $value) { return '接入中'; } elseif (GameConst::GAME_STATUS_ON == $value) { return '上线'; } elseif (GameConst::GAME_STATUS_OFF == $value) { return '下线'; } else { return ''; } } /** * 推广状态获取器 * * @param $value * * * @return mixed */ public function getAgPsLabel($value) { if (empty($value)) { return ''; } return GameConst::getPromotesMsg($value, false, true); } /** * 推广状态获取器 * * @param $value * * * @return mixed */ public function getPromoteSwitchLabelAttr($value) { if (empty($value)) { return ''; } return GameConst::getPromotesMsg($value); } /** * 通过苹果Id查找游戏ID * * @param string $apple_id 苹果ID * * @return int|mixed */ public function getAppIdByAppleId($apple_id = '') { if (empty($apple_id)) { return 0; } $_map['apple_id'] = $apple_id; $_app_id = $this->cache('apple_id'.$apple_id, CommonConst::CONST_DAY_SECONDS, $this->cache_tag)->where($_map) ->value('id'); if (empty($_app_id)) { return 0; } return $_app_id; } /** * 添加游戏 * * @param $data * * @return bool|mixed */ public function addGames($data) { if (empty($data)) { return false; } $_has_oa = false; if ($_obj = self::create($data, true)) { Cache::clear($this->cache_tag); Cache::clear($this->agent_cache_tag); $data['id'] = $_obj->id; $data['en_abbr'] = $_obj->setEnAbbrAttr('', $data); $_obj->isUpdate(true)->save($data); /* 写入扩展 */ $_ext_data['star_cnt'] = 5.0; $_obj->ext()->save($_ext_data); if (\huolib\oa\Oa::hasOa()) { $_oa_data = []; $_oa_data['app_id'] = $data['id']; $_obj->oa()->save($_oa_data); $_has_oa = true; } $_rate_data['app_id'] = $data['id']; $_obj->rate()->save($_rate_data); $_gv_model = new GameversionModel(); $_gv_data['app_id'] = $data['id']; $_gv_model->addVersion($_gv_data); Cache::clear($this->cache_tag); if ($_has_oa) { $_oa_re = $this->addOaGame($_obj->id); if ($_oa_re) { \think\Log::write( $_oa_re, 'error' ); } } /*小游戏 小程序*/ $_mp_array = [GameConst::GAME_MP, GameConst::GAME_MP_BOX, GameConst::GAME_MP_RPBOX, GameConst::GAME_MP_PERSONAL]; if (in_array($data['classify'], $_mp_array)) { $_data = ['app_id' => $_obj->id]; $_data['mini_app_id'] = $data['mp_id']; if (empty($_data['mini_app_id'])) { $_data['mini_app_id'] = 'test'.$_data['app_id']; } $_gm_mini_model = new GameMiniModel(); $_mini_data = $_gm_mini_model->getDataByMpAppId($_data['mini_app_id']); if (!empty($_mini_data)) { $_app_id = $_mini_data['app_id']; $_mini_data['app_id'] = $_obj->id; $_gm_mini_model->updateData($_mini_data, $_app_id); } else { $_gm_mini_model->addData($_data); $_mc_model = new MpConfModel(); $_data = $_mc_model->getDataByMpId($_data['mini_app_id']); if (empty($_data)) { $_mp_data['app_id'] = $_obj->id; $_mp_data['type'] = MpConfConst::MP_CONF_TYPE_6; $_mp_data['mp_id'] = $data['mp_id']; $_mp_data['wx_name'] = $data['name']; (new MpConfModel())->addData($_mp_data); } //盒子添加启动图 $_mpbox_array = [GameConst::GAME_MP_BOX, GameConst::GAME_MP_RPBOX]; if (in_array($data['classify'], $_mpbox_array)) { /* 添加广告列表_str */ $_slide = [ 'status' => SlideConst::SLIDE_STATUS_SHOW, 'code' => SlideConst::SLIDE_MP_SPLASH_IMAGE.$_obj->id, //code 固定前缀加盒子id 'name' => '小游戏盒子【'.$data['name'].'】闪屏图', 'remark' => '小游戏盒子【'.$data['name'].'】闪屏图', 'type_id' => SlideConst::SLIDE_TYPE_ONE_IMG, ]; (new SlideModel())->addData($_slide); /* 添加广告列表_end */ } } } return $_obj->id; } else { return false; } } /** * @return array */ public function oaSdkColumn() { $_oa_sdk_column_arr = array( 'app_id' => 'app_id', 'gamename' => 'name', 'classify' => 'classify', 'gameflag' => 'en_abbr', 'create_time' => 'create_time', 'status' => 'status', 'pinyin' => 'en_name', 'initial' => 'en_abbr', 'game_version' => 'version', 'update_time' => 'update_time', 'teststatus' => 'teststatus', 'icon' => 'icon', 'target_cnt' => 'standard_mem_cnt', 'target_level' => 'standard_level', 'run_time' => 'run_time', 'parent_id' => 'parent_id', ); return $_oa_sdk_column_arr; } /** * 游戏同步到oa * * @param int $id * * @return bool */ public function addOaGame($id = 0) { if (!$id) { return true; } $_map = array();/* 避免删除 */ $_map['id'] = $id; $_game_base = \think\Db::name($this->name)->where($_map)->find(); if (empty($_game_base)) { \think\Log::write( $_game_base, 'error' ); \think\Log::write( '避免删除', 'error' ); return true; } $_oa_map = array(); $_oa_map['app_id'] = $id; $_oa_game_info = \think\Db::name('oa_game')->where($_oa_map)->find(); if (!empty($_oa_game_info)) { $_game_base['standard_mem_cnt'] = $_oa_game_info['standard_mem_cnt']; /* 达标人数 */ $_game_base['standard_level'] = $_oa_game_info['standard_level'];/* 达标等级 */ } $_v_map = array(); $_v_map['app_id'] = $id; $_v_map['is_default'] = 2; $_game_base['version'] = \think\Db::name('game_version')->where($_v_map)->value('version'); if (!$_game_base['version']) { $_game_base['version'] = 1; } $_game_base['app_id'] = $id; $_send_data = array(); $_oa_sdk = $this->oaSdkColumn(); foreach ($_oa_sdk as $k => $v) { if (isset($_game_base[$v])) { $_send_data[$k] = $_game_base[$v]; } } $_re = \huolib\oa\OaGame::addOaGame($_send_data); if ($_re) { return $_re; } } /** * 更新游戏同步到oa * * @param int $app_id * @param array $data * * @return array|bool */ public function updateOaGame($app_id = 0, $data = array()) { $_send_data = array(); $_send_data['app_id'] = $app_id; $_oa_sdk = $this->oaSdkColumn(); foreach ($_oa_sdk as $k => $v) { if (isset($data[$v])) { $_send_data[$k] = $data[$v]; } } $_re = \huolib\oa\OaGame::updateOaGame($_send_data); if ($_re) { \think\Log::write($data, 'debug'); \think\Log::write($_re, 'debug'); } return $_re; } /** * 删除游戏 同步到oa * * @param int $app_id * * */ public function deleteOaGame($app_id = 0) { $delete_data = array('app_id' => $app_id); $_re = \huolib\oa\OaGame::deleteOaGame($delete_data); if ($_re) { } } /** * 还原游戏 同步到oa * * @param int $app_id * * */ public function restoreOaGame($app_id = 0) { $delete_data = array('app_id' => $app_id); $_re = \huolib\oa\OaGame::restoreOaGame($delete_data); if ($_re) { } } /** * 获取游戏列表 * * @param array $fields * @param array $where * * @return false|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getGames($fields = [], $where = []) { $_model = []; if (!empty($fields)) { $_model = $this->field($fields); } if (!empty($where)) { $_model = $this->where($where); } return $_model->select(); } public function getNameById($app_id = 0) { if (empty($app_id)) { return ''; } $_map['id'] = $app_id; return $this->where($_map)->value('name'); } /** * 更新游戏 * * @param array $game_data 父游戏信息 * @param string $app_id 子游戏ID * * @return bool */ public function updateGame($game_data, $app_id) { $_map['id'] = $app_id; $_data = $game_data; $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } else { Cache::clear($this->cache_tag); Cache::clear($this->list_tag); Cache::clear($this->agent_cache_tag); if (\huolib\oa\Oa::hasOa()) { $this->updateOaGame($app_id, $_data); } return true; } } /** * 通过游戏ID获取AppleId * * @param int $app_id * * @return mixed|string */ public function getAppleIdById($app_id = 0) { $_data = $this->getInfoById($app_id); return get_val($_data, 'apple_id', ''); } /** * 游戏ID名称对应表 * * @param int $status 是否上线 * @param int $is_delete 是否删除 2 删除 * @param int $is_sdk 是否是SDK * @param int $classify 游戏平台 * @param bool $game_flag * * @param bool $add_plat * * @param array $where * @param string $page * * @return array * @internal param bool $option */ public function getIdNames( $status = 0, $is_delete = 0, $is_sdk = 0, $classify = 0, $game_flag = false, $add_plat = false, $where = [], $page = '' ) { $_map = $where; if (!empty($status)) { $_map['status'] = $status; } if (!empty($is_delete)) { $_map['is_delete'] = $is_delete; } if (!empty($is_sdk)) { $_map['is_sdk'] = $is_sdk; } $_sub_classify = substr($classify, 0, 1); if (!empty($classify)) { if (3 == $_sub_classify) { $_map['classify'] = [ ['eq', 3], ['between', [300, 399]], 'or' ]; } elseif (4 == $_sub_classify && 401 != $classify) { $_map['classify'] = [ ['eq', 4], ['between', [400, 499]], 'or' ]; } elseif (in_array($classify, [601, 602])) { $_map['classify'] = ['in', [601, 602]]; } elseif (in_array($classify, [603])) { $_map['classify'] = ['in', [603]]; } elseif (6 == $_sub_classify) { $_map['classify'] = [ ['eq', 6], ['between', [604, 699]], 'or' ]; } else { $_map['classify'] = $classify; } } if (empty($_map['id'])) { $_map['id'] = ['not in', ['100']]; } $_tag = $this->cache_tag; $_cache_key = md5($_tag.json_encode($_map)); $_field = 'name'; if (true == $add_plat) { $_cache_key = 'plat_'.$_cache_key; $_field = "CONCAT(name,'-',CASE classify WHEN 3 THEN '安卓' WHEN 4 THEN 'IOS' WHEN 402 THEN 'IOS马甲' WHEN 5 THEN 'H5' WHEN 6 THEN '小游戏' WHEN 601 THEN '金币盒子' WHEN 602 THEN '红包盒子' WHEN 603 THEN '个人小程序' ELSE 'APPSTORE' END,CASE is_sdk WHEN 1 THEN '-CPS游戏' ELSE '' END) as name"; } if ($game_flag) { $_group = "parent_id"; $_games = $this->useGlobalScope(false) ->where($_map) ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag) ->group($_group) ->page($page) ->column($_field, 'id'); } else { $_games = $this->useGlobalScope(false) ->where($_map) ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag) ->column($_field, 'id'); } return $_games; } /** * 总计 * * @return int */ public function total() { return $this->count('0'); } /** * 今天新增 * * @return int */ public function todayCount() { return $this->getDayAddedCount(strtotime('today')); } /** * 昨天新增 * * @return int */ public function yesterdayCount() { return $this->getDayAddedCount(strtotime('yesterday')); } /** * 一天新增 * * @param int $time 时间戳,0点 * * @return int */ public function getDayAddedCount($time) { return $this->where('create_time', 'between', [$time, $time + CommonConst::CONST_DAY_SECONDS - 1])->count('0'); } /** * 接入游戏数量 * * @param array $map * * @return int */ public function onlineCount($map = []) { $_map = $map; $_map['status'] = GameConst::GAME_STATUS_ON; $_map['is_delete'] = CommonConst::CONST_NOT_DELETE; return $this->where($_map)->count('0'); } /** * 获取游戏AppID * * @param $name * * @return bool|mixed */ public function getAppIdByName($name) { if (empty($name)) { return false; } return $this->where(['name' => $name])->value('id'); } /* 实名认证添加 */ /** * 通过游戏ID获取游戏实名认证状态 * * @param int $app_id * * @return string */ public function getIsAuthById($app_id = 0) { if (empty($app_id)) { return GameConst::GAME_IDENTIFY_IS_YES; } $_data = GameCache::ins()->getInfoByAppId($app_id); return get_val($_data, 'is_auth', GameConst::GAME_IDENTIFY_IS_NO); } /** * 根据where获取id * * @param $where * * @return array */ public function getIdByWhere($where) { $_map = $where; return $this->where($_map)->value('id'); } /** * 根据where获取id * * @param $where * * @return array */ public function getIdsByWhere($where) { $_map = $where; return $this->useGlobalScope(false)->where($_map)->column('id'); } /** * 根据ID获取关联游戏ID * * @param int $id 游戏ID * * @return int 0 表示无关联游戏 */ public function getLinkAppId($id) { $_data = GameCache::ins()->getInfoByAppId($id); $_parent_id = get_val($_data, 'parent_id', CommonConst::CONST_ZERO); if (empty($_parent_id)) { return 0; } $_map = [ 'parent_id' => $_parent_id, ]; $_ids = $this->getIdsByWhere($_map); $_cnt = count($_ids); if (CommonConst::CONST_ONE >= $_cnt) { return 0; } foreach ($_ids as $_id) { if ($_id != $id) { return $_id; } } return 0; } /** * 获取游戏名称是否存在 * * @param array $where 查询条件 * * @return int|string */ public function getNameExist($where) { $_map = $where; $_cnt = $this->where($_map)->count(); return $_cnt; } /** * 获取类型下的游戏ids * * @param $classify * * @return array */ public function getIdsByClassify($classify) { $_map = ['classify' => $classify]; return $this->getIdsByWhere($_map); } /** * 获取马甲包关联游戏ID * * @return bool|mixed */ public function getMpVestReGameId() { $_map = ['apple_id' => ['>', 0]]; $_tag = $this->cache_tag; $_cache_key = md5($_tag.json_encode($_map)); return $this->where($_map)->group('apple_id')->cache($_cache_key, CommonConst::CONST_5_MINUTE_SECONDS)->column( 'apple_id' ); } public function linkgame() { return $this->belongsTo('huo\model\game\GameModel', 'apple_id'); } /** * 通过app_id获取ids,父子孙级 * @param int $app_id * @return array */ public function getGameIds(int $app_id): array { $ids = [$app_id]; $_data = $this->getInfoById($app_id); if ($_data['classify'] == 5) { // 判断是否父游戏 $son_id = $this->getLinkAppId($_data['id']); if ($son_id) $ids = array_merge($ids, [$son_id]); $child_ids = $this->getAppIdsByAppleId($son_id); if ($child_ids) { $ids = array_merge($ids, $child_ids); } } return $ids; } /** * 提供apple_id获取IDS * @param $apple_id * @return array|int|string */ public function getAppIdsByAppleId($apple_id) { if (empty($apple_id)) return 0; $_app_id = $this->cache('app_ids' . $apple_id, CommonConst::CONST_DAY_SECONDS, $this->cache_tag) ->where('apple_id', $apple_id) ->column('id'); if (empty($_app_id)) return 0; return $_app_id; } }