LhcNumber.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\LhcNumber as LhcNumberModel;
  5. use Exception;
  6. use App\Constants\HttpStatus;
  7. class LhcNumber extends Controller
  8. {
  9. /**
  10. * 号码管理列表
  11. */
  12. public function list()
  13. {
  14. try {
  15. $list = LhcNumberModel::get();
  16. // 2. 格式化树形结构
  17. $tree = [];
  18. foreach ($list as $item) {
  19. // 一级节点:game
  20. if (!isset($tree[$item->game])) {
  21. $tree[$item->game] = [
  22. 'name' => $item->game,
  23. 'children' => []
  24. ];
  25. }
  26. // 二级节点:gameplay
  27. if (!isset($tree[$item->game]['children'][$item->gameplay])) {
  28. $tree[$item->game]['children'][$item->gameplay] = [
  29. 'name' => $item->gameplay,
  30. 'children' => []
  31. ];
  32. }
  33. // 三级:把数据放入 children
  34. $tree[$item->game]['children'][$item->gameplay]['children'][] = ['id' => $item->id,'number' => $item->number, 'odds' => $item->odds];
  35. }
  36. // 重新索引,返回纯数组结构
  37. $tree = array_values($tree);
  38. } catch (Exception $e) {
  39. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  40. }
  41. return $this->success([ 'data' => $tree]);
  42. }
  43. //设置赔率
  44. public function setOdds()
  45. {
  46. try {
  47. $params = request()->validate([
  48. 'id' => ['nullable','integer'],
  49. 'game' => ['nullable','string'],
  50. 'gameplay' => ['nullable','string'],
  51. 'odds' => ['required','numeric']
  52. ]);
  53. $id = $params['id'] ?? 0;
  54. if ($id) {
  55. $info = LhcNumberModel::where('id', $id)->first();
  56. if (!$info) throw new Exception('数据不存在');
  57. } else if (!empty($params['game']) && !empty($params['gameplay'])) {
  58. $info = LhcNumberModel::where('game', $params['game'])->where('gameplay', $params['gameplay'])->first();
  59. if (!$info) throw new Exception('数据不存在');
  60. } else {
  61. throw new Exception('参数错误');
  62. }
  63. $info->odds = $params['odds'];
  64. $info->updated_by = auth()->id();
  65. $info->save();
  66. return $this->success();
  67. } catch (Exception $e) {
  68. return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
  69. }
  70. }
  71. }