// +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Date: 2017-5-25 // +---------------------------------------------------------------------- namespace huo\model\slide; use huo\controller\game\GameCache; use huo\model\common\CommonModel; use huolib\constant\CacheConst; use huolib\constant\GameConst; use huolib\constant\SlideConst; use huomp\model\game\GameMiniModel; use think\Cache; class SlideItemModel extends CommonModel { CONST STATUS_SHOW = '显示'; // 2显示, CONST STATUS_HIDE = '隐藏'; // 1不显示 // /** // * @param $query // */ // protected function base($query) { // $query->where('status', 2); // } public function slide() { return $this->belongsTo(SlideModel::className(), 'slide_id', 'id', [], 'left') ->setEagerlyType(0); } public function getStatusLabelAttr() { if (SlideConst::SLIDE_STATUS_SHOW == $this->status) { return self::STATUS_SHOW; } return self::STATUS_HIDE; } /** * image 自动转化 * * * @param $value * * @return array */ public function getImageAttr($value) { if (!empty($value)) { return cmf_get_image_url($value); } return $value; } /** * image 自动转化 * * * @param $value * * @return array */ public function setImageAttr($value) { if (!empty($value)) { return str_replace(STATICSITE.'/upload/', '', $value); } return $value; } public function addData($data) { if (empty($data)) { return false; } if ($_obj = self::create($data, true)) { return $_obj->id; } else { return false; } } /** * 获取广告列表 * * @param array $map * @param string $page * * @param string $order * * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getList($map = [], $page = '1,10', $order = '+list_order') { $_redata = ['count' => 0, 'list' => []]; $_count = self::where($map)->count('id'); if (empty($_count)) { return $_redata; } $_order = self::orderFilter($order); $_list = self::with('slide')->where($map)->order($_order)->page($page)->select(); if (is_object($_list)) { $_list = $_list->toArray(); } foreach ($_list as $_k => $_v) { $_list[$_k]['status_label'] = SlideConst::SLIDE_STATUS_SHOW == $_v['status'] ? self::STATUS_SHOW : self::STATUS_HIDE; } $_redata['count'] = $_count; $_redata['list'] = $_list; return $_redata; } /** * 删除 * * @param $id * * @return int|mixed */ public function deleteItem($id) { $_data = parent::getInfoById($id); $_rs = self::where(['id' => $id])->delete(); if ($_rs != false && !empty($id)) { if (!empty($_data['slide_id'])) { $this->ClCacheBySlideId($_data['slide_id']); } } return $_rs; } /** * 更新 * * @param array $map * @param array $data * * @return SlideItemModel */ public function updateData($map = [], $data = []) { $_rs = self::where($map)->update($data); if ($_rs != false && !empty($map['id'])) { $_data = parent::getInfoById($map['id']); if (!empty($_data['slide_id'])) { $this->ClCacheBySlideId($_data['slide_id']); } } return $_rs; } /** * * @param $code * * @return array */ public function getAdsByCode($code) { $_cache_key = CacheConst::CACHE_SLIDE_CODE_PREFIX.$code; $_rdata = json_decode(Cache::get($_cache_key), true); if (!empty($_rdata)) { return $_rdata; } $_slide_id = (new SlideModel())->getSlideIdByCode($code); $_datas = $this ->where('table_name', '=', GameConst::GAME_MP_GAME) ->where('slide_id', '=', $_slide_id) ->where('status', '=', SlideConst::SLIDE_STATUS_SHOW) ->order('list_order DESC') ->select(); if (is_object($_datas)) { $_datas = $_datas->toArray(); } $_app_cache = new GameCache(); $_game_mini_logic = new GameMiniModel(); $_rdata = []; foreach ($_datas as $_k => $_v) { $_app_info = $_app_cache->getInfoByAppId($_v['target_id']); if (empty($_app_info)) { continue; } if ($_app_info['classify'] != GameConst::GAME_MP) { continue; } $_game_mini_info = $_game_mini_logic->getDataByAppId($_app_info['id']); if (empty($_game_mini_info)) { continue; } $_rdata[] = [ 'game_id' => $_app_info['id'], 'mini_app_id' => $_game_mini_info['mini_app_id'], 'need_popup' => $_game_mini_info['need_popup'], 'entrance_image' => cmf_get_image_preview_url($_game_mini_info['entrance_image']), 'gamename' => !empty($_app_info) ? $_app_info['name'] : '', 'oneword' => !empty($_app_info) ? $_app_info['publicity'] : '', 'image' => $_v['image'], ]; } if (!empty($_rdata)) { Cache::set($_cache_key, json_encode($_rdata)); } return $_rdata; } public function getAdByCode($code) { $_cache_key = CacheConst::CACHE_SLIDE_CODE_PREFIX.$code; $_rdata = json_decode(Cache::get($_cache_key), true); if (!empty($_rdata)) { return $_rdata; } $_slide_id = (new SlideModel())->getSlideIdByCode($code); $_datas = $this ->where('table_name', '=', GameConst::GAME_MP_GAME) ->where('slide_id', '=', $_slide_id) ->where('status', '=', SlideConst::SLIDE_STATUS_SHOW) ->order('list_order DESC') ->select(); if (is_object($_datas)) { $_datas = $_datas->toArray(); } $_app_cache = new GameCache(); $_game_mini_logic = new GameMiniModel(); $_rdata = []; foreach ($_datas as $_k => $_v) { $_app_info = $_app_cache->getInfoByAppId($_v['target_id']); $_game_mini_info = $_game_mini_logic->getDataByAppId($_app_info['id']); $_rdata[] = [ 'game_id' => empty($_app_info['id']) ? 0 : $_app_info['id'], 'mini_app_id' => empty($_game_mini_info['mini_app_id']) ? '' : $_game_mini_info['mini_app_id'], 'image' => $_v['image'], ]; } if (!empty($_rdata)) { Cache::set($_cache_key, json_encode($_rdata)); } return $_rdata; } /** * 根据slide_id 清理缓存 * * @param $slide_id * * @return bool */ public function ClCacheBySlideId($slide_id) { $_code = (new SlideModel())->getCodeById($slide_id); if (empty($_code)) { return false; } return $this->ClCacheByCode($_code); } /** * 根据code 清理缓存 * * @param $code * * @return bool */ public function ClCacheByCode($code) { if (empty($code)) { return false; } $_cache_key = CacheConst::CACHE_SLIDE_CODE_PREFIX.$code; Cache::rm($_cache_key); return true; } }