GiftLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * GiftLogic.php UTF-8
  4. * 礼包类
  5. *
  6. * @date : 2017/11/18 16:54
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\game;
  13. use huo\model\common\CommonModel;
  14. use huo\model\game\GameextModel;
  15. use huo\model\game\GameModel;
  16. use huo\model\game\GiftcodeModel;
  17. use huo\model\game\GiftModel;
  18. use huolib\status\GiftStatus;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\Exception;
  22. use think\exception\DbException;
  23. class GiftLogic extends CommonModel {
  24. public $base_field
  25. = [
  26. 'id' => 'gift_id',
  27. 'app_id' => 'game_id',
  28. 'title' => 'title',
  29. 'total_cnt' => 'total_cnt',
  30. 'remain_cnt' => 'remain_cnt',
  31. 'content' => 'content',
  32. 'start_time' => 'start_time',
  33. 'end_time' => 'end_time',
  34. 'dead_time' => 'dead_time',
  35. 'scope' => 'scope',
  36. 'func' => 'func',
  37. 'condition' => 'condition',
  38. 'need_vip' => 'need_vip',
  39. 'is_hot' => 'is_hot',
  40. 'is_luxury' => 'is_luxury',
  41. 'is_rmd' => 'is_rmd',
  42. 'hits_cnt' => 'hits_cnt',
  43. 'qq_id' => 'qq_id',
  44. ];
  45. public function getBaseField() {
  46. return $this->base_field;
  47. }
  48. /**
  49. * 获取礼包列表
  50. *
  51. * @param string $mem_id
  52. * @param array $where 条件
  53. * @param string $page
  54. *
  55. * @param bool $inc_me 是否包含自己已领取的礼包
  56. *
  57. * @return int|array
  58. * @throws DataNotFoundException
  59. * @throws DbException
  60. * @throws ModelNotFoundException
  61. */
  62. public function getGifts($mem_id, $where = [], $page = '', $inc_me = false) {
  63. /* 查询玩家已经领取的礼包ID */
  64. if (!empty($mem_id) && false == $inc_me) {
  65. $_gc_map['mem_id'] = $mem_id;
  66. $_gc_model = new GiftcodeModel();
  67. $_ids = $_gc_model->where($_gc_map)->column('gift_id');
  68. if (!empty($_ids)) {
  69. $where['id'] = ['not in', implode(',', $_ids)];
  70. }
  71. }
  72. $_gift_model = new GiftModel();
  73. $_map['remain_cnt'] = ['gt', 0];
  74. $_map['end_time'] = ['gt', time()];
  75. $_count = $_gift_model
  76. ->where($where)
  77. ->where($_map)
  78. ->count();
  79. if ($_count <= 0) {
  80. /* 查询是否领过此礼包 */
  81. if (!empty($mem_id) && true == $inc_me) {
  82. $_gc_map['mem_id'] = $mem_id;
  83. $_cnt = (new GiftcodeModel())->with('gift')->where($where)->where($_gc_map)->count();
  84. if ($_cnt > 0) {
  85. $_count = $_cnt;
  86. }
  87. }
  88. if ($_count < 0) {
  89. return GiftStatus::GIFT_CNT_ZERO;
  90. }
  91. }
  92. $_field = $this->base_field;
  93. // try {
  94. $_gifts = $_gift_model
  95. ->field($_field)
  96. ->where($where)
  97. ->where($_map)
  98. ->page($page)
  99. ->select();
  100. if (is_object($_gifts)) {
  101. $_gifts = $_gifts->toArray();
  102. }
  103. if (!empty($mem_id) && true == $inc_me && !empty($_gifts)) {
  104. $_gift_ids = [];
  105. foreach ($_gifts as $_k => $_v) {
  106. $_gift_ids[] = $_v['gift_id'];
  107. }
  108. $_gc_data = $this->getGiftCodes($_gift_ids, $mem_id);
  109. if (!empty($_gc_data)) {
  110. foreach ($_gifts as $_k => $_v) {
  111. if (!empty($_gc_data[$_v['gift_id']])) {
  112. $_gifts[$_k]['code'] = $_gc_data[$_v['gift_id']];
  113. } else {
  114. $_gifts[$_k]['code'] = null;
  115. }
  116. }
  117. }
  118. }
  119. $_rdata['count'] = $_count;
  120. $_rdata['list'] = $_gifts;
  121. return $_rdata;
  122. // } catch (DataNotFoundException $e) {
  123. // return GiftStatus::DATA_NOT_FOUND_EXCEPTION;
  124. // } catch (ModelNotFoundException $e) {
  125. // return GiftStatus::MODEL_NOT_FOUND_EXCEPTION;
  126. // } catch (DbException $e) {
  127. // return GiftStatus::DB_EXCEPTION;
  128. // }
  129. }
  130. /**
  131. * 获取礼包码
  132. *
  133. * @param int|array $gift_ids
  134. * @param int $mem_id
  135. *
  136. * @return false|array
  137. */
  138. public function getGiftCodes($gift_ids, $mem_id) {
  139. if (empty($mem_id)) {
  140. return false;
  141. }
  142. if (is_array($gift_ids)) {
  143. $_gc_map['gift_id'] = ['in', $gift_ids];
  144. } elseif (is_numeric($gift_ids)) {
  145. $_gc_map['gift_id'] = $gift_ids;
  146. } else {
  147. return false;
  148. }
  149. $_gc_map['mem_id'] = $mem_id;
  150. $_gc_data = (new GiftcodeModel())->where($_gc_map)->column('code', 'gift_id');
  151. return $_gc_data;
  152. }
  153. /**
  154. * 礼包ID 获取礼包详情
  155. *
  156. * @param $gift_id
  157. *
  158. * @return array|bool
  159. * @throws DataNotFoundException
  160. * @throws DbException
  161. * @throws Exception
  162. * @throws ModelNotFoundException
  163. */
  164. public function getInfoByGiftId($gift_id) {
  165. if (empty($gift_id)) {
  166. return false;
  167. }
  168. $_map['id'] = $gift_id;
  169. // try {
  170. $_gift_data = (new GiftModel())->where($_map)->find();
  171. if (is_object($_gift_data)) {
  172. $_gift_data = $_gift_data->toArray();
  173. }
  174. return $_gift_data;
  175. // } catch (DataNotFoundException $e) {
  176. // return false;
  177. // } catch (ModelNotFoundException $e) {
  178. // return false;
  179. // } catch (DbException $e) {
  180. // return false;
  181. // } catch (Exception $e) {
  182. // return false;
  183. // }
  184. }
  185. /**
  186. * 领取礼包
  187. *
  188. * @param int $mem_id
  189. * @param int $gift_id
  190. *
  191. * @return bool|string
  192. */
  193. public function setGift($mem_id, $gift_id) {
  194. /* 查询是否已领取过礼包 */
  195. $_map['mem_id'] = $mem_id;
  196. $_map['gift_id'] = $gift_id;
  197. $_gc_model = new GiftcodeModel();
  198. $_mg_code = $_gc_model->where($_map)->value('code');
  199. if (empty($_mg_code)) {
  200. $_map['mem_id'] = 0;
  201. // try {
  202. $_mem_gc = $_gc_model->where($_map)->find();
  203. if (is_object($_mem_gc)) {
  204. $_mem_gc = $_mem_gc->toArray();
  205. }
  206. if (empty($_mem_gc)) {
  207. return false;
  208. }
  209. $_mg_code = $_mem_gc['code'];
  210. $_mem_gc['mem_id'] = $mem_id;
  211. $_mem_gc['update_time'] = time();
  212. $_gc_model->isUpdate(true)->save($_mem_gc);
  213. // } catch (DataNotFoundException $e) {
  214. // return false;
  215. // } catch (ModelNotFoundException $e) {
  216. // return false;
  217. // } catch (DbException $e) {
  218. // return false;
  219. // } catch (Exception $e) {
  220. // return false;
  221. // }
  222. }
  223. return $_mg_code;
  224. }
  225. /**
  226. * 获取有礼包的游戏ID 合集
  227. *
  228. * @param $map
  229. * @param string $page
  230. * @param string $order
  231. *
  232. * @return int|array
  233. */
  234. public function getHasGiftAppIds($map, $page = '1,10', $order = 'run_time desc') {
  235. $_game_model = new GameModel();
  236. $_map = $map;
  237. $_count = $_game_model->with('ext')->where($_map)->where(
  238. function ($query) {
  239. $query->where(['real_gift_cnt' => ['>', 1]])->whereor(
  240. function ($queryx) {
  241. $queryx->where(['real_gift_cnt' => ['eq', 1], 'last_end_time' => ['gt', time()]]);
  242. }
  243. );
  244. }
  245. )->count();
  246. if (empty($_count)) {
  247. return GiftStatus::GIFT_CNT_ZERO;
  248. }
  249. $_ids = $_game_model->with('ext')->where($_map)->where(
  250. function ($query) {
  251. $query->where(['real_gift_cnt' => ['>', 1]])->whereor(
  252. function ($queryx) {
  253. $queryx->where(['real_gift_cnt' => ['eq', 1], 'last_end_time' => ['gt', time()]]);
  254. }
  255. );
  256. }
  257. )->order($order)->page($page)->column('id');
  258. $_rdata['count'] = $_count;
  259. $_rdata['list'] = $_ids;
  260. return $_rdata;
  261. }
  262. /**
  263. * @param $game_id
  264. *
  265. * @return array|int
  266. */
  267. public function getGameGiftByGameId($game_id) {
  268. if (empty($game_id)) {
  269. return GiftStatus::GIFT_CNT_ZERO;
  270. }
  271. $_map['app_id'] = $game_id;
  272. $_rdata = $this->getGifts(0, $_map);
  273. if (is_numeric($_rdata)) {
  274. return $_rdata;
  275. }
  276. return $_rdata;
  277. }
  278. /**
  279. * 获取玩家礼包
  280. *
  281. * @param $mem_id
  282. * @param string $page
  283. *
  284. * @return array|int
  285. */
  286. public function getMemGifts($mem_id, $page = '') {
  287. $_count = GiftModel::has('code', ['mem_id' => $mem_id])->count();
  288. if ($_count <= 0) {
  289. return GiftStatus::GIFT_CNT_ZERO;
  290. }
  291. $_field['code'] = 'gift_code';
  292. $_gifts = GiftModel::has('code', ['mem_id' => $mem_id])->with('game')
  293. ->field($_field)
  294. ->page($page)
  295. ->select()->toArray();
  296. $_list = [];
  297. foreach ($_gifts as $_gift) {
  298. $_data = null;
  299. $_data['gift_code'] = $_gift['gift_code'];
  300. $_data['icon'] = $_gift['game']['game_icon'];
  301. foreach ($_gift as $_k => $_v) {
  302. if (empty($this->base_field[$_k])) {
  303. continue;
  304. }
  305. $_data[$this->base_field[$_k]] = $_v;
  306. }
  307. $_data['gift_name'] = empty($_gift['game']['game_name']) ? $_data['gift_name']
  308. : $_gift['game']['game_name'].'-'.$_data['title'];
  309. $_list[] = $_data;
  310. }
  311. $_rdata['count'] = $_count;
  312. $_rdata['list'] = $_list;
  313. return $_rdata;
  314. }
  315. /***
  316. * 检查更新游戏实时礼包类型数量
  317. *
  318. * @param $gift_id
  319. */
  320. public function checkGameGift($gift_id) {
  321. $_gift_model = new giftModel();
  322. $_app_id = $_gift_model->where('id', $gift_id)->value('app_id');
  323. $_gx_data = [];
  324. $_map = [
  325. 'app_id' => $_app_id,
  326. 'remain_cnt' => ['gt', 0],
  327. 'end_time' => ['gt', time()],
  328. ];
  329. $_count = $_gift_model->where($_map)->count(); //获得有效的礼包剩余数量
  330. if ($_count < 1) {
  331. $_gx_data['real_gift_cnt'] = 0;
  332. $_gx_data['last_end_time'] = 0;
  333. } else {
  334. $_gx_data['real_gift_cnt'] = $_count;
  335. $_last_end_time = $_gift_model->where($_map)->order('end_time asc')->limit(1)->value(
  336. 'end_time'
  337. ); //获得有效的礼包剩余数量
  338. $_gx_data['last_end_time'] = $_last_end_time;
  339. }
  340. (new GameextModel())->updateData($_gx_data, $_app_id);
  341. }
  342. }