Gift.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace app\api\controller\v2;
  3. use app\api\controller\Api;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Model;
  7. class Gift extends Api
  8. {
  9. public function giftList()
  10. {
  11. $checkResult = $this->validate($this->input,
  12. [
  13. ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
  14. ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
  15. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  16. ]);
  17. if (true !== $checkResult) {
  18. $this->jsonResult('', 0, $checkResult, 'json');
  19. }
  20. $gameid = $this->input['gameid'];
  21. $where = [
  22. 'a.isdelete' => 0,
  23. 'a.endtime' => ['egt', NOW_TIMESTAMP - 86400],
  24. ];
  25. $gameInfo = model('game')->alias('b')->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
  26. ->where(['b.cooperation_status' => ['in', [0, 1, 2]]])
  27. ->field('info.mobileicon,b.name')->column('info.mobileicon,b.name', 'b.id');
  28. $orderStr = 'a.create_time desc';
  29. $list = Db::table('cy_libaoinfo')->alias('a')
  30. ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,a.description,a.consume')
  31. ->where($where)
  32. ->where(function ($query) use ($gameid) {
  33. if (isset($gameid) && !empty($gameid)) {
  34. $query->whereRaw(sprintf('FIND_IN_SET(%s,gameid)', $gameid));
  35. }
  36. })
  37. // ->whereIn('gameid',array_keys($gameInfo))
  38. // ->where('a.total > a.used')
  39. ->orderRaw($orderStr)
  40. // ->page($data['page'], $data['pageSize'])
  41. ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']])
  42. ->toArray();
  43. if (!$list) {
  44. $this->jsonResult([], 1, '发送成功');
  45. }
  46. $ids = array_column($list['data'], 'id');
  47. $libaolog = model('libao')->whereIn('infoid', $ids)->where('user_id', '=', $this->input['userid'])
  48. ->column('code', 'infoid');
  49. foreach ($list['data'] as $k => $v) {
  50. $used = $v['used'];
  51. $total = $v['total'];
  52. $list['data'][$k]['percent'] = '100';
  53. if ($used >= $total) {
  54. $list['data'][$k]['percent'] = '0';
  55. } else if ($used && $total) {
  56. $list['data'][$k]['percent'] = (bcsub(1, bcdiv($used, $total, 2), 2) * 100);
  57. }
  58. $game_ids = explode(',', $v['gameid']);
  59. $tmp = [];
  60. foreach ($game_ids as $kk => $vv) {
  61. $tmp[] = isset($gameInfo[$vv]['name']) ? $gameInfo[$vv]['name'] : '';
  62. }
  63. $list['data'][$k]['name'] = implode(' ', $tmp);
  64. $list['data'][$k]['mobileicon'] = STATIC_DOMAIN . '/' . $gameInfo[$game_ids[0]]['mobileicon'];
  65. // $list['data'][$k]['name'] = $gameInfo[$v['gameid']]['name'];
  66. $list['data'][$k]['starttime'] = date('Y-m-d H:i:s', $v['starttime']);
  67. $list['data'][$k]['endtime'] = date('Y-m-d H:i:s', $v['endtime'] + 86399);
  68. $list['data'][$k]['status'] = isset($libaolog[$v['id']]) ? '2' : '1'; //1 未领取 2已领取
  69. if ($v['consume'] > 0) {
  70. $list['data'][$k]['consumeStr'] = '累计充值达到' . $v['consume'] . '元可领取';
  71. $pay = model('pay')->cache('v2:gift:giftList:pay:' . $this->input['userid'] . ':' . $gameid, 30)
  72. ->where(['userid' => $this->input['userid'], 'gameid' => $gameid, 'status' => 1])->sum('amount');
  73. if ($pay >= $v['consume']) {
  74. $list['data'][$k]['eligibility'] = 1;//领取资格 1有资格 2无资格
  75. } else {
  76. $list['data'][$k]['eligibility'] = 2;//领取资格 1有资格 2无资格
  77. }
  78. } else {
  79. $list['data'][$k]['consumeStr'] = '进游领取';
  80. $list['data'][$k]['eligibility'] = 1;//领取资格 1有资格 2无资格
  81. }
  82. }
  83. $this->jsonResult($list, 1, '发送成功');
  84. }
  85. public function giftPack()
  86. {
  87. $checkResult = $this->validate($this->input,
  88. [
  89. ['id', 'require|integer|egt:1', '礼包id不能为空|礼包id必须是整数礼包id必须大于1'],
  90. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  91. ]);
  92. if (true !== $checkResult) {
  93. $this->jsonResult('', 0, $checkResult, 'json');
  94. }
  95. $data['id'] = $this->input['id'];
  96. $data['gameid'] = $this->input['gameid'];
  97. // 1 判断是否领取过
  98. $hasReceive = Db::table('cy_libao')->alias('a')
  99. ->field('a.code, a.id as logid')
  100. ->where(['a.infoid' => $data['id'], 'a.user_id' => $this->input['userid']])
  101. ->find();
  102. if (!empty($hasReceive)) {
  103. $this->jsonResult(['code' => $hasReceive['code']], 0, '您已领取过该礼包');
  104. }
  105. $info = Db::table('cy_libaoinfo')->alias('a')
  106. ->join('cy_libao b', 'a.id = b.infoid', 'left')
  107. ->field('b.code,a.endtime,a.used,a.total,b.id as lid,a.consume, a.title')
  108. ->where(['a.id' => $data['id'], 'b.status' => 0])->find();
  109. if ($info['used'] >= $info['total'] || empty($info['code'])) {
  110. $this->jsonResult([], 0, '该礼包已被领取完');
  111. }
  112. if ($info['endtime'] <= NOW_TIMESTAMP - 86400) {
  113. $this->jsonResult([], 0, '该礼包已过期');
  114. }
  115. if ($info['consume'] > 0) {
  116. $pay = model('pay')->cache('v2:gift:giftList:pay:' . $this->input['userid'] . ':' . $data['gameid'], 30)
  117. ->where(['userid' => $this->input['userid'], 'gameid' => $data['gameid'], 'status' => 1])->sum('amount');
  118. if ($pay < $info['consume']) {
  119. $this->jsonResult([], 0, '暂无资格领取');
  120. }
  121. }
  122. Db::startTrans();
  123. try {
  124. $logData = [
  125. 'username' => $this->input['username'],
  126. 'title' => $info['title'],
  127. 'create_time' => NOW_TIMESTAMP,
  128. 'libaoid' => $data['id'],
  129. 'code' => $info['code'],
  130. ];
  131. Db::table('cy_libao')->where(array('id' => $info['lid']))->update(array('status' => 1, 'update_time' => NOW_TIMESTAMP, 'user_id' => $this->input['userid']));
  132. Db::table('cy_libaoinfo')->where(array('id' => $data['id']))->setInc('used');
  133. Db::table('cy_libaolog')->insert($logData);
  134. // 提交事务
  135. Db::commit();
  136. } catch (\Exception $e) {
  137. // 回滚事务
  138. Db::rollback();
  139. $this->jsonResult([], 0, '该礼包该礼包暂时无法领取或已被领取完 ' . $e);
  140. }
  141. $this->jsonResult(['code' => $info['code']], 1, '领取成功!');
  142. }
  143. public function getMyGiftList()
  144. {
  145. $checkResult = $this->validate($this->input,
  146. [
  147. ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
  148. ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
  149. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  150. ['type', 'require|integer|in:1,2', '类型不能为空|类型必须是整数|类型必须大于1'],
  151. ]);
  152. if (true !== $checkResult) {
  153. $this->jsonResult('', 0, $checkResult, 'json');
  154. }
  155. $data['page'] = $this->input['page'];
  156. $data['gameid'] = $this->input['gameid'];
  157. $where = [
  158. 'll.user_id' => $this->input['userid'],
  159. // 'a.gameid' => $data['gameid'],
  160. 'a.isdelete' => 0,
  161. ];
  162. $gameInfo = model('game')->alias('b')->join('cy_gameinfo info', 'b.id=info.game_id', 'left')
  163. ->where(['b.cooperation_status' => ['in', [0, 1, 2]]])
  164. ->field('info.mobileicon,b.name')->column('info.mobileicon,b.name', 'b.id');
  165. if ($this->input['type'] == 1) { //1 未过期 2已过期
  166. $where['a.endtime'] = ['egt', NOW_TIMESTAMP - 86400];
  167. } else {
  168. $where['a.endtime'] = ['lt', NOW_TIMESTAMP - 86400];
  169. }
  170. $orderStr = 'a.create_time desc';
  171. $list = model('libao')->alias('ll')
  172. ->join('cy_libaoinfo a', 'll.infoid=a.id and ll.user_id="' . $this->input['userid'] . '"')
  173. ->field('a.id,a.gameid,a.title,a.content,a.total,a.used,a.starttime,a.endtime,a.description,a.consume,ll.code,ll.update_time')
  174. ->where($where)
  175. ->orderRaw($orderStr)
  176. ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']])
  177. ->toArray();
  178. if (!$list) {
  179. $this->jsonResult([], 1, '发送成功');
  180. }
  181. foreach ($list['data'] as $k => $v) {
  182. $game_ids = explode(',', $v['gameid']);
  183. $tmp = [];
  184. foreach ($game_ids as $kk => $vv) {
  185. $tmp[] = isset($gameInfo[$vv]['name']) ? $gameInfo[$vv]['name'] : '';
  186. }
  187. $list['data'][$k]['name'] = implode(' ', $tmp);
  188. $list['data'][$k]['mobileicon'] = STATIC_DOMAIN . '/' . $gameInfo[$game_ids[0]]['mobileicon'];
  189. $list['data'][$k]['starttime'] = date('Y-m-d H:i:s', $v['starttime']);
  190. $list['data'][$k]['endtime'] = date('Y-m-d H:i:s', $v['endtime'] + 86399);
  191. $list['data'][$k]['update_time'] = date('Y-m-d H:i:s', $v['update_time']);
  192. }
  193. $this->jsonResult($list, 1, '发送成功');
  194. }
  195. }