123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- /**
- * GiftLogic.php UTF-8
- * 礼包处理类
- *
- * @date : 2018/1/23 14:56
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : linjiebin <ljb@huosdk.com>
- * @version : HUOSDK 8.0
- */
- namespace web\pc\logic;
- use huo\model\common\CommonModel;
- use huo\model\game\GiftcodeModel;
- use huo\model\game\GameversionModel;
- use huo\model\game\GiftModel as huoGiftModel;
- use huolib\constant\GameConst;
- use think\Db;
- class GiftLogic extends CommonModel {
- protected $base_field
- = [
- 'id' => 'giftid',
- 'app_id' => 'gameid',
- 'title' => 'gift_name',
- 'total_cnt' => 'total',
- 'remain_cnt' => 'remain',
- 'content' => 'content',
- 'start_time' => 'start_time',
- 'end_time' => 'end_time',
- 'scope' => 'scope',
- 'func' => 'func',
- 'name' => 'gamename',
- ];
- /**
- * 获取礼包列表
- * @param $mem_id
- * @param array $where
- * @param string $page
- *
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getGifts($mem_id, $where = [], $page = '') {
- /* 查询玩家已经领取的礼包ID */
- if (!empty($mem_id)) {
- $_gc_map['mem_id'] = $mem_id;
- $_ids = GiftcodeModel::where($_gc_map)->column('gift_id');
- if (!empty($_ids)) {
- $where['id'] = ['not in', implode(',', $_ids)];
- }
- }else{
- $_map['remain_cnt'] = ['gt', 0];
- }
- $_map['end_time'] = ['gt', strtotime('-1 day')];
- $_map['classify'] = ['neq', GameConst::GAME_H5];
- $_join = [
- [
- '__GAME__ g', 'g.id = gf.app_id', 'LEFT'
- ],
- ];
- $_count = Db::name('gift')->alias('gf')->where($_map)->where($where)->join($_join)->count();
- if ($_count <= 0) {
- return [
- 'count' => 0,
- 'list' => []
- ];
- }
- $_field
- = 'gf.id,gf.app_id,gf.title,gf.total_cnt,gf.remain_cnt,gf.content,gf.start_time,gf.end_time,gf.scope,gf.func,g.name,g.icon';
- $_gifts = Db::name('gift')
- ->alias('gf')
- ->field($_field)
- ->where($_map)
- ->where($where)
- ->join($_join)
- ->page($page)
- ->order('hits_cnt desc')
- ->select()->toArray();
- $_list = [];
- foreach ($_gifts as $_gift) {
- $_data = null;
- $_data['icon'] = $_gift['icon'];
- foreach ($_gift as $_k => $_v) {
- if (empty($this->base_field[$_k])) {
- continue;
- }
- $_data[$this->base_field[$_k]] = $_v;
- }
- $_data['gift_name'] = $_gift['title'];
- $_data['gamename'] = $_gift['name'];
- $_version = GameversionModel::get(['app_id' => $_gift['app_id']]);
- $_data['down_url'] = $_version['package_url'];
- $_list[] = $_data;
- }
- $_rdata['count'] = $_count;
- $_rdata['list'] = $_list;
- return $_rdata;
- }
- /**
- * 获取玩家礼包
- *
- * @param $mem_id
- * @param string $page
- *
- * @return array|int
- */
- public function getMemGifts($mem_id, $page = '') {
- $_count = huoGiftModel::has('code', ['mem_id' => $mem_id])->count();
- if ($_count <= 0) {
- return [
- 'count' => 0,
- 'list' => []
- ];
- }
- $_field['code'] = 'gift_code';
- $_gifts = huoGiftModel::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'] = $_gift['title'];
- $_data['gamename'] = $_gift['game']['game_name'];
- // $_data['gift_name'] = empty($_gift['game']['game_name']) ? $_data['gift_name']
- // : $_gift['game']['game_name'].'-'.$_data['gift_name'];
- $_list[] = $_data;
- }
- $_rdata['count'] = $_count;
- $_rdata['list'] = $_list;
- return $_rdata;
- }
- /**
- * 领取礼包
- *
- * @param int $mem_id
- * @param int $gift_id
- *
- * @return array|int
- */
- public function setGift($mem_id, $gift_id) {
- /* 1 查询礼包剩余数量 */
- $_gift = huoGiftModel::with('game')->where('id', $gift_id)->find();
- // print_r($_gift);exit;
- if (empty($_gift)) {
- return 601;
- }
- $_data = $_gift->toArray();
- foreach ($_data as $_k => $_v) {
- if (empty($this->base_field[$_k])) {
- continue;
- }
- $_rdata[$this->base_field[$_k]] = $_v;
- }
- /* 查询是否已领取过礼包 */
- $_mem_gift = $_gift->code()->where('mem_id', $mem_id)->find();
- if (empty($_mem_gift)) {
- if ($_gift->remain_cnt <= 0) {
- return 601;
- }
- $_new_map['mem_id'] = 0;
- $_mem_gc = $_gift->code()->where('mem_id', 0)->find();
- if (empty($_mem_gc)) {
- return 601;
- }
- /* 事务处理 减少与领取同时进行 */
- $_gift->startTrans();
- try {
- $_gift->remain_cnt -= 1;
- $_gift->isUpdate(true)->save();
- $_mem_gc->mem_id = $mem_id;
- $_mem_gc->save();
- // 提交事务
- $_gift->commit();
- } catch (\Exception $e) {
- // 回滚事务
- $_gift->rollback();
- return 601;
- }
- $_rdata['gift_code'] = $_mem_gc->code;
- } else {
- return 602;
- $_rdata['gift_code'] = $_mem_gift->code;
- }
- $_rdata['gift_name'] = empty($_data['game']['game_name']) ? $_rdata['gift_name']
- : $_data['game']['game_name'].'-'.$_rdata['gift_name'];
- $_rdata['icon'] = $_data['game']['game_icon'];
- return $_rdata;
- }
- }
|