mitao
2024-03-14 717811c825b998c3001ad0145bd1fd8f46d1a796
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
package com.ruoyi.web.controller.api;
 
 
import com.ruoyi.common.basic.PageVO;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.TbFieldCategory;
import com.ruoyi.system.dto.FieldCategoryDTO;
import com.ruoyi.system.dto.ShowHideDTO;
import com.ruoyi.system.dto.update.FieldCategoryUpdateDTO;
import com.ruoyi.system.query.FieldCategoryQuery;
import com.ruoyi.system.service.TbFieldCategoryService;
import com.ruoyi.system.vo.FieldCategoryDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.constraints.NotNull;
import java.util.List;
 
/**
 * <p>
 * 字段分类表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-03-13
 */
@Api(tags = "字段分类管理")
@Validated
@RestController
@RequestMapping("/field-category")
@RequiredArgsConstructor
public class TbFieldCategoryController {
    private final TbFieldCategoryService tbFieldCategoryService;
 
    /**
     * 添加
     * @param dto 字段分类数据传输对象
     * @return AjaxResult
     */
    @PostMapping("/add")
    @ApiOperation("添加")
    public AjaxResult add(@RequestBody FieldCategoryDTO dto) {
        try {
            tbFieldCategoryService.add(dto);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return AjaxResult.success();
    }
 
    /**
     * 根据id获取字段分类详情
     * @param id 字段分类id
     * @return AjaxResult
     */
    @GetMapping("/getById")
    @ApiOperation("根据id获取字段分类详情")
    @ApiImplicitParam(name = "id", value = "字段分类id", required = true)
    public AjaxResult<FieldCategoryDetailVO> getById(@RequestParam(value = "id") @NotNull(message = "id不能为空") Integer id) {
        TbFieldCategory oneCategory = tbFieldCategoryService.getById(id);
        FieldCategoryDetailVO vo = new FieldCategoryDetailVO();
        BeanUtils.copyBeanProp(vo, oneCategory);
        //根据一级分类id,查询二级分类
        List<TbFieldCategory> twoCategoryList = tbFieldCategoryService.lambdaQuery().eq(TbFieldCategory::getParentId, oneCategory.getId()).list();
        twoCategoryList.forEach(item->{
            FieldCategoryDetailVO twoCategoryVO = new FieldCategoryDetailVO();
            BeanUtils.copyBeanProp(twoCategoryVO, item);
            vo.getChildren().add(twoCategoryVO);
            //根据二级分类id,查询三级分类
            List<TbFieldCategory> threeCategoryList = tbFieldCategoryService.lambdaQuery().eq(TbFieldCategory::getParentId, item.getId()).list();
            threeCategoryList.forEach(threeCategory->{
                FieldCategoryDetailVO threeCategoryVO = new FieldCategoryDetailVO();
                BeanUtils.copyBeanProp(threeCategoryVO, threeCategory);
                twoCategoryVO.getChildren().add(threeCategoryVO);
            });
            vo.getChildren().add(twoCategoryVO);
        });
        return AjaxResult.success(vo);
    }
 
    /**
     * 分页条件查询
     * @param query 部门条件查询对象
     * @return PageVO<FieldCategoryDetailVO>
     */
    @PostMapping("/page")
    @ApiOperation("分页条件查询")
    public AjaxResult<PageVO<FieldCategoryDetailVO>> page(@RequestBody FieldCategoryQuery query) {
        return AjaxResult.success(tbFieldCategoryService.queryPage(query));
    }
 
    /**
     * 隐藏显示操作
     * @param dto 显示隐藏操作数据传输对象
     * @return AjaxResult
     */
    @PostMapping("/show-hide")
    @ApiOperation("隐藏显示操作")
    public AjaxResult showHide(@RequestBody ShowHideDTO dto) {
        try {
            tbFieldCategoryService.showHide(dto);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return AjaxResult.success();
    }
 
    @GetMapping("/delete-children")
    @ApiOperation("编辑页面删除子字段分类")
    @ApiImplicitParam(name = "id", value = "字段分类id", required = true)
    public AjaxResult deleteChildren(@RequestParam(value = "id") @NotNull(message = "id不能为空") Integer id){
        try {
            tbFieldCategoryService.deleteChildren(id);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return AjaxResult.success();
    }
 
    @GetMapping("/delete")
    @ApiOperation("列表页面删除分类")
    @ApiImplicitParam(name = "id", value = "字段分类id", required = true)
    public AjaxResult delete(@RequestParam(value = "id") @NotNull(message = "id不能为空") Integer id){
        try {
            tbFieldCategoryService.delete(id);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return AjaxResult.success();
    }
 
    @PostMapping("/edit")
    @ApiOperation("编辑")
    public AjaxResult edit(@RequestBody FieldCategoryUpdateDTO dto) {
        try {
            tbFieldCategoryService.edit(dto);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return AjaxResult.success();
    }
 
}