* @version : HUOSDK 8.0 */ namespace huo\logic\game; use huo\model\common\CommonModel; use huo\model\game\GameextModel; use huo\model\game\GameModel; use huo\model\game\GiftcodeModel; use huo\model\game\GiftModel; use huolib\status\GiftStatus; use think\db\exception\DataNotFoundException; use think\db\exception\ModelNotFoundException; use think\Exception; use think\exception\DbException; class GiftLogic extends CommonModel { public $base_field = [ 'id' => 'gift_id', 'app_id' => 'game_id', 'title' => 'title', 'total_cnt' => 'total_cnt', 'remain_cnt' => 'remain_cnt', 'content' => 'content', 'start_time' => 'start_time', 'end_time' => 'end_time', 'dead_time' => 'dead_time', 'scope' => 'scope', 'func' => 'func', 'condition' => 'condition', 'need_vip' => 'need_vip', 'is_hot' => 'is_hot', 'is_luxury' => 'is_luxury', 'is_rmd' => 'is_rmd', 'hits_cnt' => 'hits_cnt', 'qq_id' => 'qq_id', ]; public function getBaseField() { return $this->base_field; } /** * 获取礼包列表 * * @param string $mem_id * @param array $where 条件 * @param string $page * * @param bool $inc_me 是否包含自己已领取的礼包 * * @return int|array * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function getGifts($mem_id, $where = [], $page = '', $inc_me = false) { /* 查询玩家已经领取的礼包ID */ if (!empty($mem_id) && false == $inc_me) { $_gc_map['mem_id'] = $mem_id; $_gc_model = new GiftcodeModel(); $_ids = $_gc_model->where($_gc_map)->column('gift_id'); if (!empty($_ids)) { $where['id'] = ['not in', implode(',', $_ids)]; } } $_gift_model = new GiftModel(); $_map['remain_cnt'] = ['gt', 0]; $_map['end_time'] = ['gt', time()]; $_count = $_gift_model ->where($where) ->where($_map) ->count(); if ($_count <= 0) { /* 查询是否领过此礼包 */ if (!empty($mem_id) && true == $inc_me) { $_gc_map['mem_id'] = $mem_id; $_cnt = (new GiftcodeModel())->with('gift')->where($where)->where($_gc_map)->count(); if ($_cnt > 0) { $_count = $_cnt; } } if ($_count < 0) { return GiftStatus::GIFT_CNT_ZERO; } } $_field = $this->base_field; // try { $_gifts = $_gift_model ->field($_field) ->where($where) ->where($_map) ->page($page) ->select(); if (is_object($_gifts)) { $_gifts = $_gifts->toArray(); } if (!empty($mem_id) && true == $inc_me && !empty($_gifts)) { $_gift_ids = []; foreach ($_gifts as $_k => $_v) { $_gift_ids[] = $_v['gift_id']; } $_gc_data = $this->getGiftCodes($_gift_ids, $mem_id); if (!empty($_gc_data)) { foreach ($_gifts as $_k => $_v) { if (!empty($_gc_data[$_v['gift_id']])) { $_gifts[$_k]['code'] = $_gc_data[$_v['gift_id']]; } else { $_gifts[$_k]['code'] = null; } } } } $_rdata['count'] = $_count; $_rdata['list'] = $_gifts; return $_rdata; // } catch (DataNotFoundException $e) { // return GiftStatus::DATA_NOT_FOUND_EXCEPTION; // } catch (ModelNotFoundException $e) { // return GiftStatus::MODEL_NOT_FOUND_EXCEPTION; // } catch (DbException $e) { // return GiftStatus::DB_EXCEPTION; // } } /** * 获取礼包码 * * @param int|array $gift_ids * @param int $mem_id * * @return false|array */ public function getGiftCodes($gift_ids, $mem_id) { if (empty($mem_id)) { return false; } if (is_array($gift_ids)) { $_gc_map['gift_id'] = ['in', $gift_ids]; } elseif (is_numeric($gift_ids)) { $_gc_map['gift_id'] = $gift_ids; } else { return false; } $_gc_map['mem_id'] = $mem_id; $_gc_data = (new GiftcodeModel())->where($_gc_map)->column('code', 'gift_id'); return $_gc_data; } /** * 礼包ID 获取礼包详情 * * @param $gift_id * * @return array|bool * @throws DataNotFoundException * @throws DbException * @throws Exception * @throws ModelNotFoundException */ public function getInfoByGiftId($gift_id) { if (empty($gift_id)) { return false; } $_map['id'] = $gift_id; // try { $_gift_data = (new GiftModel())->where($_map)->find(); if (is_object($_gift_data)) { $_gift_data = $_gift_data->toArray(); } return $_gift_data; // } catch (DataNotFoundException $e) { // return false; // } catch (ModelNotFoundException $e) { // return false; // } catch (DbException $e) { // return false; // } catch (Exception $e) { // return false; // } } /** * 领取礼包 * * @param int $mem_id * @param int $gift_id * * @return bool|string */ public function setGift($mem_id, $gift_id) { /* 查询是否已领取过礼包 */ $_map['mem_id'] = $mem_id; $_map['gift_id'] = $gift_id; $_gc_model = new GiftcodeModel(); $_mg_code = $_gc_model->where($_map)->value('code'); if (empty($_mg_code)) { $_map['mem_id'] = 0; // try { $_mem_gc = $_gc_model->where($_map)->find(); if (is_object($_mem_gc)) { $_mem_gc = $_mem_gc->toArray(); } if (empty($_mem_gc)) { return false; } $_mg_code = $_mem_gc['code']; $_mem_gc['mem_id'] = $mem_id; $_mem_gc['update_time'] = time(); $_gc_model->isUpdate(true)->save($_mem_gc); // } catch (DataNotFoundException $e) { // return false; // } catch (ModelNotFoundException $e) { // return false; // } catch (DbException $e) { // return false; // } catch (Exception $e) { // return false; // } } return $_mg_code; } /** * 获取有礼包的游戏ID 合集 * * @param $map * @param string $page * @param string $order * * @return int|array */ public function getHasGiftAppIds($map, $page = '1,10', $order = 'run_time desc') { $_game_model = new GameModel(); $_map = $map; $_count = $_game_model->with('ext')->where($_map)->where( function ($query) { $query->where(['real_gift_cnt' => ['>', 1]])->whereor( function ($queryx) { $queryx->where(['real_gift_cnt' => ['eq', 1], 'last_end_time' => ['gt', time()]]); } ); } )->count(); if (empty($_count)) { return GiftStatus::GIFT_CNT_ZERO; } $_ids = $_game_model->with('ext')->where($_map)->where( function ($query) { $query->where(['real_gift_cnt' => ['>', 1]])->whereor( function ($queryx) { $queryx->where(['real_gift_cnt' => ['eq', 1], 'last_end_time' => ['gt', time()]]); } ); } )->order($order)->page($page)->column('id'); $_rdata['count'] = $_count; $_rdata['list'] = $_ids; return $_rdata; } /** * @param $game_id * * @return array|int */ public function getGameGiftByGameId($game_id) { if (empty($game_id)) { return GiftStatus::GIFT_CNT_ZERO; } $_map['app_id'] = $game_id; $_rdata = $this->getGifts(0, $_map); if (is_numeric($_rdata)) { return $_rdata; } return $_rdata; } /** * 获取玩家礼包 * * @param $mem_id * @param string $page * * @return array|int */ public function getMemGifts($mem_id, $page = '') { $_count = GiftModel::has('code', ['mem_id' => $mem_id])->count(); if ($_count <= 0) { return GiftStatus::GIFT_CNT_ZERO; } $_field['code'] = 'gift_code'; $_gifts = GiftModel::has('code', ['mem_id' => $mem_id])->with('game') ->field($_field) ->page($page) ->select()->toArray(); $_list = []; foreach ($_gifts as $_gift) { $_data = null; $_data['gift_code'] = $_gift['gift_code']; $_data['icon'] = $_gift['game']['game_icon']; foreach ($_gift as $_k => $_v) { if (empty($this->base_field[$_k])) { continue; } $_data[$this->base_field[$_k]] = $_v; } $_data['gift_name'] = empty($_gift['game']['game_name']) ? $_data['gift_name'] : $_gift['game']['game_name'].'-'.$_data['title']; $_list[] = $_data; } $_rdata['count'] = $_count; $_rdata['list'] = $_list; return $_rdata; } /*** * 检查更新游戏实时礼包类型数量 * * @param $gift_id */ public function checkGameGift($gift_id) { $_gift_model = new giftModel(); $_app_id = $_gift_model->where('id', $gift_id)->value('app_id'); $_gx_data = []; $_map = [ 'app_id' => $_app_id, 'remain_cnt' => ['gt', 0], 'end_time' => ['gt', time()], ]; $_count = $_gift_model->where($_map)->count(); //获得有效的礼包剩余数量 if ($_count < 1) { $_gx_data['real_gift_cnt'] = 0; $_gx_data['last_end_time'] = 0; } else { $_gx_data['real_gift_cnt'] = $_count; $_last_end_time = $_gift_model->where($_map)->order('end_time asc')->limit(1)->value( 'end_time' ); //获得有效的礼包剩余数量 $_gx_data['last_end_time'] = $_last_end_time; } (new GameextModel())->updateData($_gx_data, $_app_id); } }