123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909 |
- <?php
- 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 $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;
-
- protected function base($query) {
- $query->where('delete_time', 0)
- ->where('is_delete', 2);
- }
-
- 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');
- }
-
- public function mg() {
- return $this->hasMany(MemGameModel::className(), 'app_id', 'id');
- }
-
- public function goods() {
- return $this->hasMany('huoAccountDeal\model\AccountGoodsModel', 'app_id', 'id');
- }
-
- public function ext() {
- return $this->hasOne('huo\model\game\GameextModel', 'app_id', 'id', [], 'left')->setEagerlyType(0);
- }
-
- public function oa() {
- if (\huolib\oa\oa::hasOa()) {
- return $this->hasOne('huo\model\oa\OaGameModel', 'app_id', 'id', [], 'left')->setEagerlyType(0);
- }
- }
-
- public function ag() {
- return $this->hasMany('huo\model\agent\AgentGameModel', 'app_id', 'id');
- }
-
- 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']);
- }
-
- public function setAppKeyAttr($value = '', $data) {
- if (empty($value)) {
- return md5($value.$data['name'].time().uniqid());
- }
- return $value;
- }
-
- public function setEnAbbrAttr($value = '', $data) {
- return huo_initials($value.$data['en_name']).'_'.$data['id'];
- }
-
- public function getIconAttr($value) {
- if (!empty($value)) {
- return cmf_get_image_url($value);
- }
- return $value;
- }
-
- 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;
- }
-
- 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 '';
- }
- }
-
- 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 '';
- }
- }
-
- public function getAgPsLabel($value) {
- if (empty($value)) {
- return '';
- }
- return GameConst::getPromotesMsg($value, false, true);
- }
-
- public function getPromoteSwitchLabelAttr($value) {
- if (empty($value)) {
- return '';
- }
- return GameConst::getPromotesMsg($value);
- }
-
- 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;
- }
-
- 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)) {
-
- $_slide = [
- 'status' => SlideConst::SLIDE_STATUS_SHOW,
- 'code' => SlideConst::SLIDE_MP_SPLASH_IMAGE.$_obj->id,
- 'name' => '小游戏盒子【'.$data['name'].'】闪屏图',
- 'remark' => '小游戏盒子【'.$data['name'].'】闪屏图',
- 'type_id' => SlideConst::SLIDE_TYPE_ONE_IMG,
- ];
- (new SlideModel())->addData($_slide);
-
- }
- }
- }
- return $_obj->id;
- } else {
- return false;
- }
- }
-
- 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;
- }
-
- 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;
- }
- }
-
- 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;
- }
-
- public function deleteOaGame($app_id = 0) {
- $delete_data = array('app_id' => $app_id);
- $_re = \huolib\oa\OaGame::deleteOaGame($delete_data);
- if ($_re) {
- }
- }
-
- public function restoreOaGame($app_id = 0) {
- $delete_data = array('app_id' => $app_id);
- $_re = \huolib\oa\OaGame::restoreOaGame($delete_data);
- if ($_re) {
- }
- }
-
- 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');
- }
-
- 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;
- }
- }
-
- public function getAppleIdById($app_id = 0) {
- $_data = $this->getInfoById($app_id);
- return get_val($_data, 'apple_id', '');
- }
-
- 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;
- }
-
- public function total() {
- return $this->count('0');
- }
-
- public function todayCount() {
- return $this->getDayAddedCount(strtotime('today'));
- }
-
- public function yesterdayCount() {
- return $this->getDayAddedCount(strtotime('yesterday'));
- }
-
- public function getDayAddedCount($time) {
- return $this->where('create_time', 'between', [$time, $time + CommonConst::CONST_DAY_SECONDS - 1])->count('0');
- }
-
- 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');
- }
-
- public function getAppIdByName($name) {
- if (empty($name)) {
- return false;
- }
- return $this->where(['name' => $name])->value('id');
- }
-
-
- 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);
- }
-
- public function getIdByWhere($where) {
- $_map = $where;
- return $this->where($_map)->value('id');
- }
-
- public function getIdsByWhere($where) {
- $_map = $where;
- return $this->useGlobalScope(false)->where($_map)->column('id');
- }
-
- 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;
- }
-
- public function getNameExist($where) {
- $_map = $where;
- $_cnt = $this->where($_map)->count();
- return $_cnt;
- }
-
- public function getIdsByClassify($classify) {
- $_map = ['classify' => $classify];
- return $this->getIdsByWhere($_map);
- }
-
- 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');
- }
-
- 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;
- }
-
- 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;
- }
- }
|