BaseController.java 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.charles7c.cnadmin.common.base;

import java.util.List;

21 22
import javax.servlet.http.HttpServletResponse;

23 24 25 26 27 28 29 30 31 32
import lombok.NoArgsConstructor;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

33 34
import cn.hutool.core.lang.tree.Tree;

35
import top.charles7c.cnadmin.common.model.query.PageQuery;
36
import top.charles7c.cnadmin.common.model.query.SortQuery;
37
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
38 39 40 41 42 43 44 45 46 47 48 49 50 51
import top.charles7c.cnadmin.common.model.vo.R;

/**
 * 控制器基类
 *
 * @param <S>
 *            业务接口
 * @param <V>
 *            列表信息
 * @param <D>
 *            详情信息
 * @param <Q>
 *            查询条件
 * @param <C>
52
 *            创建或修改信息
53 54 55 56
 * @author Charles7c
 * @since 2023/1/26 10:45
 */
@NoArgsConstructor
57
public abstract class BaseController<S extends BaseService<V, D, Q, C>, V, D, Q, C extends BaseRequest> {
58 59 60 61 62 63 64 65 66 67 68

    @Autowired
    protected S baseService;

    /**
     * 分页查询列表
     *
     * @param query
     *            查询条件
     * @param pageQuery
     *            分页查询条件
69
     * @return 分页信息
70 71
     */
    @Operation(summary = "分页查询列表")
72
    @ResponseBody
73
    @GetMapping
74 75 76
    protected R<PageDataVO<V>> page(@Validated Q query, @Validated PageQuery pageQuery) {
        PageDataVO<V> pageDataVO = baseService.page(query, pageQuery);
        return R.ok(pageDataVO);
77 78
    }

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    /**
     * 查询树列表
     *
     * @param query
     *            查询条件
     * @param sortQuery
     *            排序查询条件
     * @return 树列表信息
     */
    @Operation(summary = "查询树列表")
    @ResponseBody
    @GetMapping("/tree")
    protected R<List<Tree<Long>>> tree(@Validated Q query, @Validated SortQuery sortQuery) {
        List<Tree<Long>> list = baseService.tree(query, sortQuery, false);
        return R.ok(list);
    }

96 97 98 99 100
    /**
     * 查询列表
     *
     * @param query
     *            查询条件
101 102
     * @param sortQuery
     *            排序查询条件
103 104 105
     * @return 列表信息
     */
    @Operation(summary = "查询列表")
106
    @ResponseBody
107 108 109
    @GetMapping("/list")
    protected R<List<V>> list(@Validated Q query, @Validated SortQuery sortQuery) {
        List<V> list = baseService.list(query, sortQuery);
110 111 112 113 114 115 116 117 118 119
        return R.ok(list);
    }

    /**
     * 查看详情
     *
     * @param id
     *            ID
     * @return 详情信息
     */
120
    @Operation(summary = "查看详情")
121
    @Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
122
    @ResponseBody
123
    @GetMapping("/{id}")
124 125
    protected R<D> get(@PathVariable Long id) {
        D detail = baseService.get(id);
126 127 128 129 130 131 132 133 134 135
        return R.ok(detail);
    }

    /**
     * 新增
     *
     * @param request
     *            创建信息
     * @return 自增 ID
     */
136
    @Operation(summary = "新增数据")
137
    @ResponseBody
138
    @PostMapping
139
    protected R<Long> add(@Validated(BaseRequest.Add.class) @RequestBody C request) {
140
        Long id = baseService.add(request);
141 142 143 144 145 146 147 148 149 150
        return R.ok("新增成功", id);
    }

    /**
     * 修改
     *
     * @param request
     *            修改信息
     * @return /
     */
151
    @Operation(summary = "修改数据")
152 153 154 155
    @ResponseBody
    @PutMapping
    protected R update(@Validated(BaseRequest.Update.class) @RequestBody C request) {
        baseService.update(request);
156 157 158 159 160 161 162 163 164 165
        return R.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param ids
     *            ID 列表
     * @return /
     */
166
    @Operation(summary = "删除数据")
167
    @Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
168
    @ResponseBody
169 170 171 172 173
    @DeleteMapping("/{ids}")
    protected R delete(@PathVariable List<Long> ids) {
        baseService.delete(ids);
        return R.ok("删除成功");
    }
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    /**
     * 导出
     *
     * @param query
     *            查询条件
     * @param sortQuery
     *            排序查询条件
     * @param response
     *            响应对象
     */
    @Operation(summary = "导出数据")
    @GetMapping("/export")
    protected void export(@Validated Q query, @Validated SortQuery sortQuery, HttpServletResponse response) {
        baseService.export(query, sortQuery, response);
    }
190
}