FlowerTicket.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace App\Http\Controllers\PartnerPay;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Models\FlowerTicketModel;
  6. use Illuminate\Http\Request;
  7. class FlowerTicket extends Controller
  8. {
  9. /**
  10. * @param Request $request
  11. * @return array
  12. * @api {Post} /api/msy/flowerticket/store 创建兑换卡
  13. * @apiName 兑换卡生成
  14. * @apiGroup flowerticket
  15. *
  16. * @apiParam {int} validity_at 过期时间
  17. * @apiParam {int} red_flower 红花数量
  18. * @apiParam {int} gold_flower 金花数量
  19. * @apiParam {int} jsk 解锁卡
  20. * @apiParam {int} count 生成数量
  21. *
  22. * @apiSuccess {json}
  23. * {
  24. * 'code': 200,
  25. * 'message': 'success',
  26. * 'data': {
  27. * 'success': 2,
  28. * 'fail': 1,
  29. * 'ticket_ids': [1, 2, 3]
  30. * }
  31. * }
  32. */
  33. public function store(Request $request)
  34. {
  35. $this->validate($request, [
  36. 'validity_at' => 'required',
  37. 'red_flower' => 'required',
  38. 'gold_flower' => 'required',
  39. 'jsk' => 'required',
  40. 'count' => 'required',
  41. ]);
  42. $count = $request->input('count');
  43. $ticketids = array();
  44. for ($i = 0; $i < $count; $i++) {
  45. try {
  46. $data = array(
  47. 'validity_at' => $request->input('validity_at'),
  48. 'code' => bin2hex(\openssl_random_pseudo_bytes(4)),
  49. 'red_flower' => $request->input('red_flower'),
  50. 'gold_flower' => $request->input('gold_flower'),
  51. 'jsk' => $request->input('jsk')
  52. );
  53. $flowerTicketModel = new FlowerTicketModel();
  54. $ticket = $flowerTicketModel->fill($data);
  55. if ($ticket->save()) {
  56. array_push($ticketids, $ticket->id);
  57. }
  58. } catch (\Exception $e) {
  59. }
  60. }
  61. return array(
  62. 'code' => 200,
  63. 'message' => 'success',
  64. 'data' => [
  65. 'success' => count($ticketids),
  66. 'fail' => $count - count($ticketids),
  67. 'ticket_ids' => $ticketids
  68. ],
  69. );
  70. }
  71. /**
  72. * @param Request $request
  73. * @param int $ticket_id
  74. * @return array
  75. * @api {Post} /api/msy/flowerticket/:ticket_id/update ticket_id 兑换码id
  76. * @apiName 更新兑换码
  77. * @apiGroup flowerticket
  78. *
  79. * @apiParam {int} validity_at 过期时间
  80. * @apiParam {int} red_flower 红花数量
  81. * @apiParam {int} gold_flower 金花数量
  82. * @apiParam {int} jsk 解锁卡
  83. * @apiParam {int} count 生成数量
  84. *
  85. * @apiSuccess {json}
  86. * {
  87. * 'code': 200,
  88. * 'message': 'success'
  89. * }
  90. */
  91. public function update(Request $request, int $ticket_id)
  92. {
  93. $this->validate($request, [
  94. 'validity_at' => '',
  95. 'red_flower' => '',
  96. 'gold_flower' => '',
  97. 'jsk' => '',
  98. ]);
  99. $flowerTicketModel = new FlowerTicketModel();
  100. $ticket = $flowerTicketModel->find($ticket_id);
  101. if (collect($ticket)->isEmpty()) {
  102. return array(
  103. 'code' => 101,
  104. 'message' => '参数错误'
  105. );
  106. }
  107. $ticket->fill($request->toArray());
  108. if ($ticket->save()) {
  109. return array(
  110. 'code' => 200,
  111. 'message' => 'success'
  112. );
  113. } else {
  114. return array(
  115. 'code' => 501,
  116. 'message' => '数据库错误'
  117. );
  118. }
  119. }
  120. /**
  121. * @param int $ticket_id
  122. * @return array
  123. * @throws \Exception
  124. * @api {Get} /api/msy/flowerticket/:ticket_id/delete ticket_id 兑换码id
  125. * @apiName 删除兑换码
  126. * @apiGroup flowerticket
  127. *
  128. * @apiSuccess {json}
  129. * {
  130. * 'code': 200,
  131. * 'message': 'success'
  132. * }
  133. *
  134. * @apiFail {json}
  135. * {
  136. * 'code': 102,
  137. * 'message': '该兑换码已被领取'
  138. * }
  139. */
  140. public function delete(int $ticket_id)
  141. {
  142. $flowerTicketModel = new FlowerTicketModel();
  143. $ticket = $flowerTicketModel->find($ticket_id);
  144. if (collect($ticket)->isEmpty()) {
  145. return array(
  146. 'code' => 101,
  147. 'message' => '参数错误'
  148. );
  149. }
  150. if (!empty($ticket->uid)) {
  151. return array(
  152. 'code' => 102,
  153. 'message' => '该兑换码已被领取'
  154. );
  155. }
  156. if ($ticket->delete()) {
  157. return array(
  158. 'code' => 200,
  159. 'message' => 'success'
  160. );
  161. } else {
  162. return array(
  163. 'code' => 501,
  164. 'message' => '数据库错误'
  165. );
  166. }
  167. }
  168. /**
  169. * @param int $page
  170. * @return array
  171. * @api {Get} /api/msy/flowerticket/list/:page page 页数
  172. * @apiName 获取兑换码列表
  173. * @apiGroup flowerticket
  174. *
  175. * @apiSuccess {json}
  176. * {
  177. * 'code': 200,
  178. * 'message': 'success',
  179. * 'data': {
  180. * 'count': '总数',
  181. * 'page': '页数',
  182. * 'limit': '每页数量',
  183. * 'tickets': [{
  184. * 'id': '兑换码id',
  185. * 'uid': '领取人uid',
  186. * 'code': '兑换码'
  187. * }]
  188. * }
  189. * }
  190. */
  191. public function getList(int $page = 1)
  192. {
  193. $flowerTicketModel = new FlowerTicketModel();
  194. $tickets = $flowerTicketModel->take(100)->skip(($page - 1) * 100)->get();
  195. $count = $flowerTicketModel->count();
  196. return array(
  197. 'code' => 200,
  198. 'message' => 'success',
  199. 'data' => [
  200. 'count' => $count,
  201. 'page' => $page,
  202. 'limit' => 100,
  203. 'tickets' => $tickets
  204. ]
  205. );
  206. }
  207. /**
  208. * @param Request $request
  209. * @return array
  210. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  211. * @api {Get} api/msy/flowerticket/claim 领取兑换码
  212. * @apiName 领取兑换码
  213. * @apiGroup flowerticket
  214. *
  215. * @apiParam {string} code 兑换码code
  216. *
  217. * @apiSuccess {json}
  218. * {
  219. * 'code': 200,
  220. * 'message': 'success'
  221. * }
  222. */
  223. public function claim(Request $request)
  224. {
  225. $uid = Auth::auth();
  226. $this->validate($request, [
  227. 'code' => 'required'
  228. ]);
  229. $code = $request->input('code');
  230. $flowerTicketModel = new FlowerTicketModel();
  231. $ticket = $flowerTicketModel->where('code', $code)->first();
  232. if (collect($ticket)->isEmpty()) {
  233. return array(
  234. 'code' => 101,
  235. 'message' => '兑换码错误'
  236. );
  237. }
  238. if ($ticket->validity_at < time() || !empty($ticket->uid)) {
  239. return array(
  240. 'code' => 102,
  241. 'message' => '兑换码失效'
  242. );
  243. }
  244. \DB::beginTransaction();
  245. try {
  246. \DB::table('kdgx_msy_flower_ticket')->where('id', $ticket->id)->update(['uid' => $uid]);
  247. \DB::table('kdgx_partner_charge_user')->where('uid', $uid)->increment('red_flower', $ticket->red_flower);
  248. \DB::table('kdgx_partner_charge_user')->where('uid', $uid)->increment('gold_flower', $ticket->gold_flower);
  249. $ticketController = new Ticket();
  250. for ($i = 0; $i < $ticket->jsk; $i++) {
  251. $ticketController->create(5, $uid);
  252. }
  253. \DB::table('kdgx_partner_charge_pay_logs')->create([
  254. 'uid' => $uid,
  255. 'create_at' => time(),
  256. 'type' => 11,
  257. 'red_flower' => $ticket->red_flower,
  258. 'gold_flower' => $ticket->gold_flower,
  259. 'jsk' => $ticket->jsk
  260. ]);
  261. \DB::commit();
  262. return array(
  263. 'code' => 200,
  264. 'message' => 'success'
  265. );
  266. } catch (\Exception $e) {
  267. \DB::rollBack();
  268. return array(
  269. 'code' => 501,
  270. 'message' => $e->getMessage()
  271. );
  272. }
  273. }
  274. }