BaseController.java 6.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
import cn.dev33.satoken.stp.StpUtil;
34
import cn.hutool.core.lang.tree.Tree;
35
import cn.hutool.core.util.StrUtil;
36

37
import top.charles7c.cnadmin.common.annotation.CrudRequestMapping;
38
import top.charles7c.cnadmin.common.constant.StringConsts;
39
import top.charles7c.cnadmin.common.model.query.PageQuery;
40
import top.charles7c.cnadmin.common.model.query.SortQuery;
41
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
42 43 44 45 46 47 48 49 50 51 52 53 54 55
import top.charles7c.cnadmin.common.model.vo.R;

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

    @Autowired
    protected S baseService;

    /**
     * 分页查询列表
     *
     * @param query
     *            查询条件
     * @param pageQuery
     *            分页查询条件
73
     * @return 分页信息
74 75
     */
    @Operation(summary = "分页查询列表")
76
    @ResponseBody
77
    @GetMapping
78
    public R<PageDataVO<V>> page(Q query, @Validated PageQuery pageQuery) {
79
        this.checkPermission("list");
80 81
        PageDataVO<V> pageDataVO = baseService.page(query, pageQuery);
        return R.ok(pageDataVO);
82 83
    }

84 85 86 87 88 89 90 91 92 93 94 95
    /**
     * 查询树列表
     *
     * @param query
     *            查询条件
     * @param sortQuery
     *            排序查询条件
     * @return 树列表信息
     */
    @Operation(summary = "查询树列表")
    @ResponseBody
    @GetMapping("/tree")
96
    public R<List<Tree<Long>>> tree(Q query, SortQuery sortQuery) {
97
        this.checkPermission("list");
98 99 100 101
        List<Tree<Long>> list = baseService.tree(query, sortQuery, false);
        return R.ok(list);
    }

102 103 104 105 106
    /**
     * 查询列表
     *
     * @param query
     *            查询条件
107 108
     * @param sortQuery
     *            排序查询条件
109 110 111
     * @return 列表信息
     */
    @Operation(summary = "查询列表")
112
    @ResponseBody
113
    @GetMapping("/list")
114
    public R<List<V>> list(Q query, SortQuery sortQuery) {
115
        this.checkPermission("list");
116
        List<V> list = baseService.list(query, sortQuery);
117 118 119 120 121 122 123 124 125 126
        return R.ok(list);
    }

    /**
     * 查看详情
     *
     * @param id
     *            ID
     * @return 详情信息
     */
127
    @Operation(summary = "查看详情")
128
    @Parameter(name = "id", description = "ID", in = ParameterIn.PATH)
129
    @ResponseBody
130
    @GetMapping("/{id}")
131
    public R<D> get(@PathVariable Long id) {
132
        this.checkPermission("list");
133
        D detail = baseService.get(id);
134 135 136 137 138 139 140 141 142 143
        return R.ok(detail);
    }

    /**
     * 新增
     *
     * @param request
     *            创建信息
     * @return 自增 ID
     */
144
    @Operation(summary = "新增数据")
145
    @ResponseBody
146
    @PostMapping
147
    public R<Long> add(@Validated(BaseRequest.Add.class) @RequestBody C request) {
148
        this.checkPermission("add");
149
        Long id = baseService.add(request);
150 151 152 153 154 155 156 157
        return R.ok("新增成功", id);
    }

    /**
     * 修改
     *
     * @param request
     *            修改信息
158 159
     * @param id
     *            ID
160 161
     * @return /
     */
162
    @Operation(summary = "修改数据")
163
    @ResponseBody
164
    @PutMapping("/{id}")
165
    public R update(@Validated(BaseRequest.Update.class) @RequestBody C request, @PathVariable Long id) {
166
        this.checkPermission("update");
167
        baseService.update(request, id);
168 169 170 171 172 173 174 175 176 177
        return R.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param ids
     *            ID 列表
     * @return /
     */
178
    @Operation(summary = "删除数据")
179
    @Parameter(name = "ids", description = "ID 列表", in = ParameterIn.PATH)
180
    @ResponseBody
181
    @DeleteMapping("/{ids}")
182
    public R delete(@PathVariable List<Long> ids) {
183
        this.checkPermission("delete");
184 185 186
        baseService.delete(ids);
        return R.ok("删除成功");
    }
187 188 189 190 191 192 193 194 195 196 197 198 199

    /**
     * 导出
     *
     * @param query
     *            查询条件
     * @param sortQuery
     *            排序查询条件
     * @param response
     *            响应对象
     */
    @Operation(summary = "导出数据")
    @GetMapping("/export")
200
    public void export(Q query, SortQuery sortQuery, HttpServletResponse response) {
201
        this.checkPermission("export");
202 203
        baseService.export(query, sortQuery, response);
    }
204 205 206 207 208 209 210 211 212 213

    /**
     * 权限认证
     *
     * @param subPermission
     *            部分权限码
     */
    private void checkPermission(String subPermission) {
        CrudRequestMapping crudRequestMapping = this.getClass().getDeclaredAnnotation(CrudRequestMapping.class);
        String path = crudRequestMapping.value();
214
        String permissionPrefix = String.join(StringConsts.COLON, StrUtil.splitTrim(path, StringConsts.SLASH));
215 216
        StpUtil.checkPermission(String.format("%s:%s", permissionPrefix, subPermission));
    }
217
}