mitao
2024-04-09 ec76c5defdd8018ce4efcc8795508498a84de4b7
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.ruoyi.web.controller.api;
 
 
import com.ruoyi.common.basic.PageDTO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.system.domain.TbField;
import com.ruoyi.system.dto.FieldDTO;
import com.ruoyi.system.dto.ShowHideDTO;
import com.ruoyi.system.dto.update.FieldUpdateDTO;
import com.ruoyi.system.query.FieldQuery;
import com.ruoyi.system.service.TbFieldCategoryService;
import com.ruoyi.system.service.TbFieldService;
import com.ruoyi.system.vo.FieldCategoryVO;
import com.ruoyi.system.vo.FieldVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
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
@RequestMapping("/field")
@Api(tags = "字段管理相关接口")
public class TbFieldController {
 
    private final TbFieldService tbFieldService;
    private final TbFieldCategoryService tbFieldCategoryService;
 
    /**
     * 获取分类列表
     *
     * @param id 分类id
     * @return 分类列表
     */
    @GetMapping("/categories")
    @ApiOperation(value = "获取分类列表", notes = "一级分类id传0,二级分类传一级分类id,三级分类同理")
    public R<List<FieldCategoryVO>> queryFieldCategories(@RequestParam Long id) {
        try {
            return R.ok(tbFieldCategoryService.queryFieldCategories(id));
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("获取分类列表异常", e);
            return R.fail();
        }
    }
 
    /**
     * 添加字段
     *
     * @param dto 字段数据传输对象
     * @return 响应状态
     */
    @PostMapping("/add")
    @ApiOperation("添加字段")
    public R<Void> add(@RequestBody @Validated FieldDTO dto) {
        try {
            tbFieldService.add(dto);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("添加字段异常", e);
            return R.fail();
        }
        return R.ok();
    }
 
    @GetMapping("/influenced-data")
    @ApiOperation("隐藏字段,获取同步隐藏的基础数据")
    public R<String> influencedData(@RequestParam Long id) {
        try {
            return R.ok(tbFieldService.influencedData(id));
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("获取同步隐藏的基础数据异常", e);
            return R.fail();
        }
    }
 
    /**
     * 隐藏显示操作
     *
     * @param dto 显示隐藏操作数据传输对象
     * @return R
     */
    @PostMapping("/show-hide")
    @ApiOperation("隐藏显示操作")
    public R<Void> showHide(@RequestBody ShowHideDTO dto) {
        try {
            tbFieldService.showHide(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 响应状态
     */
    @PostMapping("/edit")
    @ApiOperation("编辑字段")
    public R<Void> add(@RequestBody @Validated FieldUpdateDTO dto) {
        try {
            tbFieldService.update(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 PageVO<FieldCategoryDetailVO>
     */
    @PostMapping("/page")
    @ApiOperation("分页条件查询")
    public R<PageDTO<FieldVO>> page(@Validated @RequestBody FieldQuery query) {
        try {
            return R.ok(tbFieldService.queryPage(query));
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("分页条件查询异常", e);
            return R.fail();
        }
    }
 
    /**
     * 获取详情
     *
     * @param id id
     * @return FieldVO
     */
    @GetMapping("/get-details")
    @ApiOperation("获取详情")
    public R<FieldVO> getDetails(@RequestParam Long id) {
        try {
            TbField field = tbFieldService.getById(id);
            return R.ok(BeanUtils.copyBean(field, FieldVO.class));
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("获取详情", e);
            return R.fail();
        }
    }
 
    /**
     * 删除
     *
     * @param id id
     * @return 响应状态
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public R<Void> delete(@RequestParam Long id) {
        try {
            tbFieldService.removeById(id);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("获取详情", e);
            return R.fail();
        }
        return R.ok();
    }
 
    /**
     * 模板下载
     */
    @GetMapping("/download")
    @ApiOperation("模板下载")
    public void downloadImportTemplate() {
        try {
            tbFieldService.downloadImportTemplate();
        } catch (Exception e) {
            log.error("模板下载异常", e);
            throw new ServiceException("模板下载失败,请联系管理员!");
        }
    }
}