GiftLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * GiftLogic.php UTF-8
  4. * 礼包处理类
  5. *
  6. * @date : 2018/1/23 14:56
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : linjiebin <ljb@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace web\pc\logic;
  13. use huo\model\common\CommonModel;
  14. use huo\model\game\GiftcodeModel;
  15. use huo\model\game\GameversionModel;
  16. use huo\model\game\GiftModel as huoGiftModel;
  17. use huolib\constant\GameConst;
  18. use think\Db;
  19. class GiftLogic extends CommonModel {
  20. protected $base_field
  21. = [
  22. 'id' => 'giftid',
  23. 'app_id' => 'gameid',
  24. 'title' => 'gift_name',
  25. 'total_cnt' => 'total',
  26. 'remain_cnt' => 'remain',
  27. 'content' => 'content',
  28. 'start_time' => 'start_time',
  29. 'end_time' => 'end_time',
  30. 'scope' => 'scope',
  31. 'func' => 'func',
  32. 'name' => 'gamename',
  33. ];
  34. /**
  35. * 获取礼包列表
  36. * @param $mem_id
  37. * @param array $where
  38. * @param string $page
  39. *
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function getGifts($mem_id, $where = [], $page = '') {
  46. /* 查询玩家已经领取的礼包ID */
  47. if (!empty($mem_id)) {
  48. $_gc_map['mem_id'] = $mem_id;
  49. $_ids = GiftcodeModel::where($_gc_map)->column('gift_id');
  50. if (!empty($_ids)) {
  51. $where['id'] = ['not in', implode(',', $_ids)];
  52. }
  53. }else{
  54. $_map['remain_cnt'] = ['gt', 0];
  55. }
  56. $_map['end_time'] = ['gt', strtotime('-1 day')];
  57. $_map['classify'] = ['neq', GameConst::GAME_H5];
  58. $_join = [
  59. [
  60. '__GAME__ g', 'g.id = gf.app_id', 'LEFT'
  61. ],
  62. ];
  63. $_count = Db::name('gift')->alias('gf')->where($_map)->where($where)->join($_join)->count();
  64. if ($_count <= 0) {
  65. return [
  66. 'count' => 0,
  67. 'list' => []
  68. ];
  69. }
  70. $_field
  71. = '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';
  72. $_gifts = Db::name('gift')
  73. ->alias('gf')
  74. ->field($_field)
  75. ->where($_map)
  76. ->where($where)
  77. ->join($_join)
  78. ->page($page)
  79. ->order('hits_cnt desc')
  80. ->select()->toArray();
  81. $_list = [];
  82. foreach ($_gifts as $_gift) {
  83. $_data = null;
  84. $_data['icon'] = $_gift['icon'];
  85. foreach ($_gift as $_k => $_v) {
  86. if (empty($this->base_field[$_k])) {
  87. continue;
  88. }
  89. $_data[$this->base_field[$_k]] = $_v;
  90. }
  91. $_data['gift_name'] = $_gift['title'];
  92. $_data['gamename'] = $_gift['name'];
  93. $_version = GameversionModel::get(['app_id' => $_gift['app_id']]);
  94. $_data['down_url'] = $_version['package_url'];
  95. $_list[] = $_data;
  96. }
  97. $_rdata['count'] = $_count;
  98. $_rdata['list'] = $_list;
  99. return $_rdata;
  100. }
  101. /**
  102. * 获取玩家礼包
  103. *
  104. * @param $mem_id
  105. * @param string $page
  106. *
  107. * @return array|int
  108. */
  109. public function getMemGifts($mem_id, $page = '') {
  110. $_count = huoGiftModel::has('code', ['mem_id' => $mem_id])->count();
  111. if ($_count <= 0) {
  112. return [
  113. 'count' => 0,
  114. 'list' => []
  115. ];
  116. }
  117. $_field['code'] = 'gift_code';
  118. $_gifts = huoGiftModel::has('code', ['mem_id' => $mem_id])->with('game')
  119. ->field($_field)
  120. ->page($page)
  121. ->select()->toArray();
  122. $_list = [];
  123. foreach ($_gifts as $_gift) {
  124. $_data = null;
  125. $_data['gift_code'] = $_gift['gift_code'];
  126. $_data['icon'] = $_gift['game']['game_icon'];
  127. foreach ($_gift as $_k => $_v) {
  128. if (empty($this->base_field[$_k])) {
  129. continue;
  130. }
  131. $_data[$this->base_field[$_k]] = $_v;
  132. }
  133. $_data['gift_name'] = $_gift['title'];
  134. $_data['gamename'] = $_gift['game']['game_name'];
  135. // $_data['gift_name'] = empty($_gift['game']['game_name']) ? $_data['gift_name']
  136. // : $_gift['game']['game_name'].'-'.$_data['gift_name'];
  137. $_list[] = $_data;
  138. }
  139. $_rdata['count'] = $_count;
  140. $_rdata['list'] = $_list;
  141. return $_rdata;
  142. }
  143. /**
  144. * 领取礼包
  145. *
  146. * @param int $mem_id
  147. * @param int $gift_id
  148. *
  149. * @return array|int
  150. */
  151. public function setGift($mem_id, $gift_id) {
  152. /* 1 查询礼包剩余数量 */
  153. $_gift = huoGiftModel::with('game')->where('id', $gift_id)->find();
  154. // print_r($_gift);exit;
  155. if (empty($_gift)) {
  156. return 601;
  157. }
  158. $_data = $_gift->toArray();
  159. foreach ($_data as $_k => $_v) {
  160. if (empty($this->base_field[$_k])) {
  161. continue;
  162. }
  163. $_rdata[$this->base_field[$_k]] = $_v;
  164. }
  165. /* 查询是否已领取过礼包 */
  166. $_mem_gift = $_gift->code()->where('mem_id', $mem_id)->find();
  167. if (empty($_mem_gift)) {
  168. if ($_gift->remain_cnt <= 0) {
  169. return 601;
  170. }
  171. $_new_map['mem_id'] = 0;
  172. $_mem_gc = $_gift->code()->where('mem_id', 0)->find();
  173. if (empty($_mem_gc)) {
  174. return 601;
  175. }
  176. /* 事务处理 减少与领取同时进行 */
  177. $_gift->startTrans();
  178. try {
  179. $_gift->remain_cnt -= 1;
  180. $_gift->isUpdate(true)->save();
  181. $_mem_gc->mem_id = $mem_id;
  182. $_mem_gc->save();
  183. // 提交事务
  184. $_gift->commit();
  185. } catch (\Exception $e) {
  186. // 回滚事务
  187. $_gift->rollback();
  188. return 601;
  189. }
  190. $_rdata['gift_code'] = $_mem_gc->code;
  191. } else {
  192. return 602;
  193. $_rdata['gift_code'] = $_mem_gift->code;
  194. }
  195. $_rdata['gift_name'] = empty($_data['game']['game_name']) ? $_rdata['gift_name']
  196. : $_data['game']['game_name'].'-'.$_rdata['gift_name'];
  197. $_rdata['icon'] = $_data['game']['game_icon'];
  198. return $_rdata;
  199. }
  200. }