PcIssue.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\PcIssueService;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Validation\ValidationException;
  8. use Exception;
  9. use App\Models\PcIssue as PcIssueModel;
  10. class PcIssue extends Controller
  11. {
  12. /**
  13. * @api {get} /admin/pcIssue 列表
  14. * @apiGroup 极速28
  15. *
  16. * @apiUse result
  17. * @apiUse header
  18. * @apiVersion 1.0.0
  19. *
  20. * @apiParam {int} [page=1]
  21. * @apiParam {int} [limit=10]
  22. * @apiParam {string} [issue_no] 期号
  23. */
  24. public function index()
  25. {
  26. try {
  27. $search = request()->validate([
  28. 'page' => ['nullable', 'integer', 'min:1'],
  29. 'limit' => ['nullable', 'integer', 'min:1'],
  30. 'issue_no' => ['nullable', 'string'],
  31. ]);
  32. $page = request()->input('page', 1);
  33. $limit = request()->input('limit', 15);
  34. $result['total'] = PcIssueModel::where(PcIssueModel::getWhere($search))->count();
  35. $result['data'] = PcIssueModel::where(PcIssueModel::getWhere($search))
  36. ->orderByDesc('id')->forPage($page, $limit)->get();
  37. } catch (ValidationException $e) {
  38. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  39. } catch (Exception $e) {
  40. return $this->error(intval($e->getCode()));
  41. }
  42. return $this->success($result);
  43. }
  44. /**
  45. * @api {post} /admin/pcIssue/preDraw 预开奖
  46. * @apiGroup 极速28
  47. *
  48. * @apiUse result
  49. * @apiUse header
  50. * @apiVersion 1.0.0
  51. *
  52. * @apiParam {string} issue_no 期号
  53. * @apiParam {int} he 预开奖号码
  54. * - 开奖号码取值范围:0-27
  55. *
  56. */
  57. public function preDraw()
  58. {
  59. DB::beginTransaction();
  60. try {
  61. request()->validate([
  62. 'issue_no' => ['required', 'string'],
  63. 'he' => ['required', 'integer', 'min:0', 'max:27'],
  64. ]);
  65. $issueNo = request()->input('issue_no');
  66. $he = request()->input('he');
  67. $he = intval($he);
  68. $pcIssue = PcIssueModel::where('issue_no', $issueNo)->first();
  69. if (!$pcIssue) throw new Exception('期号错误', HttpStatus::CUSTOM_ERROR);
  70. if (!in_array($pcIssue->status, [PcIssueModel::STATUS_BETTING, PcIssueModel::STATUS_CLOSE])) {
  71. throw new Exception('预开奖失败;状态不正确', HttpStatus::CUSTOM_ERROR);
  72. }
  73. $keno = PcIssueService::getMatchingNumbers($he);
  74. sort($keno);
  75. $pcIssue->advance_keno = json_encode($keno);
  76. $pcIssue->advance_code = $he;
  77. $pcIssue->save();
  78. $winningNumbers = PcIssueService::getWinningNumbers($keno);
  79. DB::commit();
  80. } catch (ValidationException $e) {
  81. DB::rollBack();
  82. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  83. } catch (Exception $e) {
  84. DB::rollBack();
  85. if ($e->getCode() == HttpStatus::CUSTOM_ERROR) {
  86. return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
  87. }
  88. return $this->error(intval($e->getCode()));
  89. }
  90. return $this->success($winningNumbers);
  91. }
  92. }