mitao
2024-07-09 07c83c163675e24252de05d029cef2eab046e583
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.finance.web.controller.api;
 
 
import com.finance.common.basic.PageDTO;
import com.finance.common.core.domain.R;
import com.finance.common.exception.ServiceException;
import com.finance.common.utils.BeanUtils;
import com.finance.system.domain.TbBasicDataCategory;
import com.finance.system.dto.BasicDataCategoryDTO;
import com.finance.system.dto.ShowHideDTO;
import com.finance.system.dto.update.BasicDataCategoryUpdateDTO;
import com.finance.system.query.BasicDataCategoryQuery;
import com.finance.system.service.TbBasicDataCategoryService;
import com.finance.system.vo.BasicDataCategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * <p>
 * 基础数据分类表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-03-13
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = {"基础数据分类相关接口"})
@RequestMapping("/basic-data-category")
public class TbBasicDataCategoryController {
 
    private final TbBasicDataCategoryService tbBasicDataCategoryService;
 
    /**
     * 添加
     *
     * @param dto 基础数据分类传输对象
     * @return R<Void>
     */
    @PostMapping("/add")
    @ApiOperation("添加")
    public R<Void> add(@RequestBody @Validated BasicDataCategoryDTO dto) {
        try {
            tbBasicDataCategoryService.add(dto);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("添加异常", e);
            return R.fail();
        }
        return R.ok();
    }
 
    /**
     * 编辑
     *
     * @param dto 基础数据分类更新传输对象
     * @return R<Void>
     */
    @PostMapping("/edit")
    @ApiOperation("编辑")
    public R<Void> add(@RequestBody @Validated BasicDataCategoryUpdateDTO dto) {
        if (dto.getId() <= 17) {
            throw new ServiceException("该基础数据分类不能修改");
        }
        try {
            tbBasicDataCategoryService.update(dto);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("编辑异常", e);
            return R.fail();
        }
        return R.ok();
    }
 
    /**
     * 删除
     *
     * @param id id
     * @return R<Void>
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public R<Void> delete(@RequestParam(value = "id") Long id) {
        if (id <= 17) {
            throw new ServiceException("该基础数据分类不能删除");
        }
        try {
            tbBasicDataCategoryService.removeById(id);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("删除异常", e);
            return R.fail();
        }
        return R.ok();
    }
 
    /**
     * 根据id获取详情
     *
     * @param id id
     * @return R<BasicDataCategoryVO>
     */
    @GetMapping("/get-details")
    @ApiOperation("根据id获取详情")
    public R<BasicDataCategoryVO> getDetails(@RequestParam(value = "id") Long id) {
        try {
            TbBasicDataCategory basicDataCategory = tbBasicDataCategoryService.getById(id);
            BasicDataCategoryVO vo = BeanUtils.copyBean(basicDataCategory,
                    BasicDataCategoryVO.class);
            return R.ok(vo);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("根据id获取详情异常", e);
            return R.fail();
        }
    }
 
    /**
     * 隐藏显示操作
     *
     * @param dto 显示隐藏操作数据传输对象
     * @return R<Void>
     */
    @PostMapping("/show-hide")
    @ApiOperation("隐藏显示操作")
    public R<Void> showHide(@RequestBody ShowHideDTO dto) {
        if (dto.getId() <= 17) {
            throw new ServiceException("该基础数据分类不能隐藏");
        }
        try {
            tbBasicDataCategoryService.showHide(dto);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("隐藏显示操作异常", e);
            return R.fail();
        }
        return R.ok();
    }
 
    /**
     * 分页条件查询
     *
     * @param query 基础数据分类条件查询对象
     * @return R<PageDTO < BasicDataCategoryVO>>
     */
    @PostMapping("/page")
    @ApiOperation("分页条件查询")
    public R<PageDTO<BasicDataCategoryVO>> page(
            @Validated @RequestBody BasicDataCategoryQuery query) {
        try {
            return R.ok(tbBasicDataCategoryService.queryPage(query));
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("分页条件查询异常", e);
            return R.fail();
        }
    }
}