123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- /**
- * GiftLogic.php UTF-8
- * 礼包类
- *
- * @date : 2017/11/18 16:54
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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);
- }
- }
|