|
|
@@ -15,34 +15,37 @@ class LhcNumber extends Controller
|
|
|
public function list()
|
|
|
{
|
|
|
try {
|
|
|
- $params = request()->validate([
|
|
|
- 'page' => ['nullable', 'integer', 'min:1'],
|
|
|
- 'limit' => ['nullable', 'integer', 'min:1'],
|
|
|
- 'game' => ['nullable', 'string'],
|
|
|
- 'gameplay' => ['nullable', 'string'],
|
|
|
- 'number' => ['nullable'],
|
|
|
- ]);
|
|
|
- $page = request()->input('page', 1);
|
|
|
- $limit = request()->input('limit', 15);
|
|
|
-
|
|
|
- $query = new LhcNumberModel();
|
|
|
- if (!empty($params['game'])) {
|
|
|
- $query = $query->where('game', $params['game']);
|
|
|
+ $list = LhcNumberModel::get();
|
|
|
+ // 2. 格式化树形结构
|
|
|
+ $tree = [];
|
|
|
+
|
|
|
+ foreach ($list as $item) {
|
|
|
+ // 一级节点:game
|
|
|
+ if (!isset($tree[$item->game])) {
|
|
|
+ $tree[$item->game] = [
|
|
|
+ 'name' => $item->game,
|
|
|
+ 'children' => []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 二级节点:gameplay
|
|
|
+ if (!isset($tree[$item->game]['children'][$item->gameplay])) {
|
|
|
+ $tree[$item->game]['children'][$item->gameplay] = [
|
|
|
+ 'name' => $item->gameplay,
|
|
|
+ 'children' => []
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 三级:把数据放入 children
|
|
|
+ $tree[$item->game]['children'][$item->gameplay]['children'][] = ['id' => $item->id,'number' => $item->number, 'odds' => $item->odds];
|
|
|
}
|
|
|
- if (!empty($params['gameplay'])) {
|
|
|
- $query = $query->where('gameplay', $params['gameplay']);
|
|
|
- }
|
|
|
- if (!empty($params['number'])) {
|
|
|
- $query = $query->where('number', $params['number']);
|
|
|
- }
|
|
|
- $count = $query->count();
|
|
|
- $list = $query
|
|
|
- ->forPage($page, $limit)
|
|
|
- ->get();
|
|
|
+
|
|
|
+ // 重新索引,返回纯数组结构
|
|
|
+ $tree = array_values($tree);
|
|
|
} catch (Exception $e) {
|
|
|
return $this->error(HttpStatus::CUSTOM_ERROR,$e->getMessage());
|
|
|
}
|
|
|
- return $this->success(['total' => $count, 'data' => $list]);
|
|
|
+ return $this->success([ 'data' => $tree]);
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -51,12 +54,21 @@ class LhcNumber extends Controller
|
|
|
{
|
|
|
try {
|
|
|
$params = request()->validate([
|
|
|
- 'id' => ['required','integer'],
|
|
|
+ 'id' => ['nullable','integer'],
|
|
|
+ 'game' => ['nullable','string'],
|
|
|
+ 'gameplay' => ['nullable','string'],
|
|
|
'odds' => ['required','numeric']
|
|
|
]);
|
|
|
- $id = $params['id'];
|
|
|
- $info = LhcNumberModel::where('id', $id)->first();
|
|
|
- if (!$info) throw new Exception('数据不存在');
|
|
|
+ $id = $params['id'] ?? 0;
|
|
|
+ if ($id) {
|
|
|
+ $info = LhcNumberModel::where('id', $id)->first();
|
|
|
+ if (!$info) throw new Exception('数据不存在');
|
|
|
+ } else if (!empty($params['game']) && !empty($params['gameplay'])) {
|
|
|
+ $info = LhcNumberModel::where('game', $params['game'])->where('gameplay', $params['gameplay'])->first();
|
|
|
+ if (!$info) throw new Exception('数据不存在');
|
|
|
+ } else {
|
|
|
+ throw new Exception('参数错误');
|
|
|
+ }
|
|
|
|
|
|
$info->odds = $params['odds'];
|
|
|
$info->updated_by = auth()->id();
|