Coupon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace app\api\controller\v2;
  3. use app\api\controller\Api;
  4. use app\common\model\Members;
  5. use app\common\model\PayType;
  6. use app\common\model\Payrule;
  7. use app\common\model\Game;
  8. use app\service\MemberCoinService;
  9. use think\Cache;
  10. use think\Db;
  11. use think\Model;
  12. class Coupon extends Api
  13. {
  14. /**
  15. * 用户代金券列表
  16. */
  17. public function getMyCoupon()
  18. {
  19. $checkResult = $this->validate($this->input,
  20. [
  21. ['type_id', 'require|integer|in:1,2,3', '类型不能为空|类型必须是整数|类型必须1,2,3'],
  22. ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
  23. ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
  24. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  25. ]);
  26. if (true !== $checkResult) {
  27. $this->jsonResult('', 0, $checkResult);
  28. }
  29. $where = [];
  30. $type_id = $this->input['type_id'];
  31. //查询参数
  32. $where['a.member_id'] = $this->input['userid'];
  33. $where['a.examine'] = 2;
  34. $couponMemberModel = model('CouponMember');
  35. if($type_id == 1){
  36. $order = "a.id desc";
  37. }elseif($type_id == 2){
  38. $order = "a.use_time desc";
  39. }else{
  40. $order = "c.end_time desc";
  41. }
  42. $list = $couponMemberModel
  43. ->alias('a')
  44. ->join('cy_coupon c', 'a.coupon_id=c.id')
  45. ->where($where)
  46. ->where(function ($query) use ($type_id) {
  47. $end_time = strtotime(date("Y-m-d"));
  48. //1未使用 2已使用 3已失效
  49. if ($type_id == 1) {
  50. $query->where([
  51. 'c.state' => 1,
  52. 'a.state' => 1,
  53. 'a.is_use' => 1,
  54. ])->whereRaw(sprintf('(c.type_id =1 and c.end_time>=%d) or (c.type_id=2 and a.end_time>=%d)', $end_time, $end_time));
  55. } else if ($type_id == 2) {
  56. $query->whereIn('a.is_use', [2, 3]);
  57. } else {
  58. $query->whereRaw(sprintf('(c.type_id =1 and c.end_time<%d) or (c.type_id=2 and a.end_time<%d) or c.state =2 or a.state = 2', $end_time, $end_time));
  59. }
  60. })
  61. ->field('a.*,c.name,c.money,c.min_money,c.start_time,c.end_time,a.start_time as coupon_start_time,a.end_time as coupon_end_time,c.type_id,c.game_id,c.state as coupon_state,c.content,a.use_time')
  62. ->order($order)
  63. ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']])
  64. ->toArray();
  65. if ($list) {
  66. $tmpGameList = model('Common/Game')->getAllByCondition('id,name,nickname');
  67. $gameList = array();
  68. foreach ($tmpGameList as $game) {
  69. $gameList[$game['id']] = $game;
  70. }
  71. foreach ($list['data'] as $k => $v) {
  72. if ($v['type_id'] == 2) {
  73. $list['data'][$k]['start_time'] = date('Y-m-d H:i:s',$v['coupon_start_time']);
  74. $list['data'][$k]['end_time'] = date('Y-m-d H:i:s',$v['coupon_end_time']);
  75. }else{
  76. $list['data'][$k]['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time']));
  77. }
  78. if ($v['game_id']) {
  79. $game_ids = explode(',', $v['game_id']);
  80. $tmp = [];
  81. foreach ($game_ids as $kk => $vv) {
  82. $tmp[] = $gameList[$vv]['name'];
  83. }
  84. $list['data'][$k]['game_name'] = implode(' ', array_unique($tmp));
  85. } else {
  86. $list['data'][$k]['game_name'] = '全部游戏';
  87. }
  88. }
  89. }
  90. $list['type_id'] = $type_id;
  91. $this->jsonResult($list, 1, '获取成功');
  92. }
  93. /**
  94. * 可领取代金券列表
  95. */
  96. public function getCoupon()
  97. {
  98. $checkResult = $this->validate($this->input,
  99. [
  100. ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'],
  101. ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'],
  102. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  103. ]);
  104. if (true !== $checkResult) {
  105. $this->jsonResult('', 0, $checkResult);
  106. }
  107. $list = model('Coupon')->alias('a')
  108. ->join(sprintf('(select * from cy_coupon_member where member_id=%d group by coupon_id ) b', $this->input['userid']), 'a.id=b.coupon_id', 'left')
  109. ->where('a.is_show', 2)
  110. ->whereRaw(sprintf('FIND_IN_SET(%d,a.game_id)',$this->input['gameid']))
  111. ->where('a.state', 1)->whereRaw(sprintf('(a.type_id=1 and a.end_time>=%d) or a.type_id=2', strtotime(date('Y-m-d'))))->order('a.id desc')
  112. ->field('a.*,if(b.member_id>0,b.member_id,0) as member_id,b.start_time as coupon_start_time,b.end_time as coupon_end_time,b.code')
  113. ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']])
  114. ->toArray();
  115. if ($list) {
  116. $tmpGameList = model('Common/Game')->getAllByCondition('id,name,nickname');
  117. $gameList = array();
  118. foreach ($tmpGameList as $game) {
  119. $gameList[$game['id']] = $game;
  120. }
  121. foreach ($list['data'] as $k => $v) {
  122. //1未领取 2已领取
  123. if ($v['member_id'] > 0) {
  124. $list['data'][$k]['receive'] = 2;
  125. if ($v['type_id'] == 2) {
  126. $list['data'][$k]['start_time'] = date('Y-m-d H:i:s',$v['coupon_start_time']);
  127. $list['data'][$k]['end_time'] = date('Y-m-d H:i:s',$v['coupon_end_time']+86399);
  128. }
  129. } else {
  130. $list['data'][$k]['receive'] = 1;
  131. }
  132. if ($v['grant_num'] > 0) {
  133. $unreceivedNum = model('Common/CouponMember')
  134. ->where('coupon_id', $v['id'])
  135. ->where('member_id', 0)
  136. ->count();
  137. if ($unreceivedNum <= 0) {
  138. $list['data'][$k]['percentage'] = 0;
  139. } else {
  140. $list['data'][$k]['percentage'] = (int)number_format(($v['grant_num'] - $v['receive_num']) / $v['grant_num'] * 100, 0, '.', '');
  141. }
  142. } else {
  143. $list['data'][$k]['percentage'] = 0;
  144. }
  145. if ($v['game_id']) {
  146. $game_ids = explode(',', $v['game_id']);
  147. $tmp = [];
  148. foreach ($game_ids as $kk => $vv) {
  149. $tmp[] = $gameList[$vv]['nickname'];
  150. }
  151. $list['data'][$k]['game_name'] = implode(' ', array_unique($tmp));
  152. } else {
  153. $list['data'][$k]['game_name'] = '全部游戏';
  154. }
  155. }
  156. }
  157. $this->jsonResult($list, 1, '获取成功');
  158. }
  159. // 代金卷领取
  160. public function receiveCoupon()
  161. {
  162. $checkResult = $this->validate($this->input,
  163. [
  164. ['id', 'require|integer|egt:1', '代金券ID不能为空|代金券ID必须是整数|代金券ID必须大于1'],
  165. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  166. ]);
  167. if (true !== $checkResult) {
  168. $this->jsonResult('', 0, $checkResult);
  169. }
  170. $coupon = model('Common/Coupon')->cache('v2:coupon:receiveCoupon' . $this->input['id'], 60)->where('id', $this->input['id'])
  171. ->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2) and state=1 and is_show=2', Db::quote(date('Y-m-d'))))->find();
  172. if (!$coupon) {
  173. $this->jsonResult('', 0, '代金券不存在');
  174. }
  175. if ($coupon['receive_num'] >= $coupon['grant_num']) {
  176. $this->jsonResult('', 0, '代金券已领完');
  177. }
  178. if (model('common/CouponMember')->where(['member_id' => $this->input['userid'], 'coupon_id' => $this->input['id']])->find()) {
  179. $this->jsonResult('', 0, '已领取', 'json');
  180. }
  181. model('Common/Coupon')->startTrans();
  182. if (!$couponMember = model('common/CouponMember')->where(['coupon_id' => $this->input['id'], 'state' => 1, 'is_use' => 1, 'member_id' => 0])->find()) {
  183. model('Common/Coupon')->rollback();
  184. $this->jsonResult('', 0, '代金券已领完');
  185. }
  186. if (!model('Common/Coupon')->where('id', $coupon['id'])->update(['receive_num' => $coupon['receive_num'] + 1])) {
  187. model('Common/Coupon')->rollback();
  188. $this->jsonResult('', 0, '领取失败,稍后再试 ');
  189. }
  190. if ($coupon['type_id'] == 1) {
  191. $start_time = 0;
  192. $end_time = 0;
  193. } else {
  194. $start_time = strtotime(date('Y-m-d'));
  195. $end_time = strtotime(date('Y-m-d')) + 86400 * $coupon['day'] - 1;
  196. }
  197. if (model('common/CouponMember')->where(['id' => $couponMember['id'], 'coupon_id' => $this->input['id'], 'state' => 1, 'is_use' => 1, 'member_id' => 0])->update(['member_id' => $this->input['userid'], 'update_time' => time(), 'start_time' => $start_time, 'end_time' => $end_time])) {
  198. model('Common/Coupon')->commit();
  199. $this->jsonResult(['code'=>$couponMember['code']], 1, '领取成功');
  200. } else {
  201. model('Common/Coupon')->rollback();
  202. $this->jsonResult('', 0, '领取失败,稍后再试');
  203. }
  204. }
  205. // 代金券码兑换
  206. public function exchangeCoupon()
  207. {
  208. $checkResult = $this->validate($this->input,
  209. [
  210. ['code', 'require', '代金券兑换码不能为空'],
  211. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  212. ]);
  213. if (true !== $checkResult) {
  214. $this->jsonResult('', 0, $checkResult);
  215. }
  216. if (!$couponMember = model('common/CouponMember')->where('code', trim($this->input['code']))->find()) {
  217. $this->jsonResult('', 0, '兑换码错误');
  218. }
  219. if ($couponMember['member_id'] > 0) {
  220. $this->jsonResult('', 0, '该兑换码已兑换');
  221. }
  222. $coupon = model('Common/Coupon')->cache('v2:coupon:receiveCoupon' . $couponMember['coupon_id'], 60)->where('id', $couponMember['coupon_id'])
  223. ->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2) and state=1 and is_show=2', Db::quote(date('Y-m-d'))))->find();
  224. if (!$coupon) {
  225. $this->jsonResult('', 0, '代金券不存在');
  226. }
  227. model('Common/Coupon')->startTrans();
  228. if (!model('Common/Coupon')->where('id', $coupon['id'])->update(['receive_num' => $coupon['receive_num'] + 1])) {
  229. model('Common/Coupon')->rollback();
  230. $this->jsonResult('', 0, '领取失败,稍后再试 ');
  231. }
  232. if ($coupon['type_id'] == 1) {
  233. $start_time = 0;
  234. $end_time = 0;
  235. if($coupon['end_time']>time()){
  236. $this->jsonResult('', 0, '代金券已过期');
  237. }
  238. } else {
  239. $start_time = strtotime(date('Y-m-d'));
  240. $end_time = strtotime(date('Y-m-d')) + 86400 * $coupon['day'] - 1;
  241. }
  242. if (model('common/CouponMember')->where(['id' => $couponMember['id'], 'coupon_id' => $coupon['id'], 'state' => 1, 'is_use' => 1, 'member_id' => 0])->update(['member_id' => $this->input['userid'], 'update_time' => time(), 'start_time' => $start_time, 'end_time' => $end_time])) {
  243. model('Common/Coupon')->commit();
  244. $this->jsonResult('', 1, '领取成功');
  245. } else {
  246. model('Common/Coupon')->rollback();
  247. $this->jsonResult('', 0, '领取失败,稍后再试');
  248. }
  249. }
  250. /**
  251. * 支付可用代金券获取
  252. */
  253. public function getPayCoupon()
  254. {
  255. $checkResult = $this->validate($this->input,
  256. [
  257. ['money', 'require|integer|egt:1', '金额不能为空|金额必须是整数|金额必须大于0'],
  258. ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'],
  259. ]);
  260. if (true !== $checkResult) {
  261. $this->jsonResult('', 0, $checkResult);
  262. }
  263. $list = model('common/CouponMember')->getPayCoupon($this->input['userid'], $this->input['gameid'], $this->input['money']);
  264. $this->jsonResult(['list' => $list], 1, '获取成功');
  265. }
  266. }