From d68c61517a3a6d4ec4d47844c21bdc143f99cbab Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期三, 17 四月 2024 22:58:05 +0800
Subject: [PATCH] 大屏接口

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbFieldCategoryController.java |  143 ++++++++++++++++++++++++++++-------------------
 1 files changed, 86 insertions(+), 57 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbFieldCategoryController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbFieldCategoryController.java
index c0b5ffd..96dd3dd 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbFieldCategoryController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbFieldCategoryController.java
@@ -1,25 +1,22 @@
 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.common.basic.PageDTO;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.exception.ServiceException;
 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 com.ruoyi.system.vo.FieldCategoryVO;
 import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiImplicitParam;
 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.*;
-
-import javax.validation.constraints.NotNull;
-import java.util.List;
 
 /**
  * <p>
@@ -29,58 +26,53 @@
  * @author mitao
  * @since 2024-03-13
  */
-@Api(tags = "字段分类管理")
-@Validated
+@Slf4j
 @RestController
 @RequestMapping("/field-category")
 @RequiredArgsConstructor
+@Api(tags = "字段分类管理相关接口")
 public class TbFieldCategoryController {
+
     private final TbFieldCategoryService tbFieldCategoryService;
 
     /**
      * 添加
      * @param dto 字段分类数据传输对象
-     * @return AjaxResult
+     * @return R
      */
     @PostMapping("/add")
     @ApiOperation("添加")
-    public AjaxResult add(@RequestBody FieldCategoryDTO dto) {
+    public R<Object> add(@RequestBody @Validated FieldCategoryDTO dto) {
         try {
             tbFieldCategoryService.add(dto);
         } catch (Exception e) {
-            throw new RuntimeException("操作失败");
+            if (e instanceof ServiceException) {
+                return R.fail(e.getMessage());
+            }
+            log.error("添加异常", e);
+            return R.fail();
         }
-        return AjaxResult.success();
+        return R.ok();
     }
 
     /**
      * 根据id获取字段分类详情
      * @param id 字段分类id
-     * @return AjaxResult
+     * @return R
      */
-    @GetMapping("/getById")
+    @GetMapping("/get-details")
     @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);
+    public R<FieldCategoryDetailVO> getById(@RequestParam(value = "id") Long id) {
+        try {
+            FieldCategoryDetailVO vo = tbFieldCategoryService.getDetailsById(id);
+            return R.ok(vo);
+        } catch (Exception e) {
+            if (e instanceof ServiceException) {
+                return R.fail(e.getMessage());
+            }
+            log.error("根据id获取字段分类详情异常", e);
+            return R.fail();
+        }
     }
 
     /**
@@ -90,59 +82,96 @@
      */
     @PostMapping("/page")
     @ApiOperation("分页条件查询")
-    public AjaxResult<PageVO<FieldCategoryDetailVO>> page(@RequestBody FieldCategoryQuery query) {
-        return AjaxResult.success(tbFieldCategoryService.queryPage(query));
+    public R<PageDTO<FieldCategoryVO>> page(@Validated @RequestBody FieldCategoryQuery query) {
+        try {
+            return R.ok(tbFieldCategoryService.queryPage(query));
+        } catch (Exception e) {
+            if (e instanceof ServiceException) {
+                return R.fail(e.getMessage());
+            }
+            log.error("分页条件查询异常", e);
+            return R.fail();
+        }
     }
 
     /**
      * 隐藏显示操作
      * @param dto 显示隐藏操作数据传输对象
-     * @return AjaxResult
+     * @return R
      */
     @PostMapping("/show-hide")
     @ApiOperation("隐藏显示操作")
-    public AjaxResult showHide(@RequestBody ShowHideDTO dto) {
+    public R<Void> showHide(@RequestBody ShowHideDTO dto) {
         try {
             tbFieldCategoryService.showHide(dto);
         } catch (Exception e) {
-            throw new RuntimeException("操作失败");
+            if (e instanceof ServiceException) {
+                return R.fail(e.getMessage());
+            }
+            log.error("隐藏显示操作异常", e);
+            return R.fail();
         }
-        return AjaxResult.success();
+        return R.ok();
     }
 
-    @GetMapping("/delete-children")
+    /**
+     * 删除分类子节点
+     * @param id id
+     * @return R
+     */
+    @DeleteMapping("/delete-children")
     @ApiOperation("编辑页面删除子字段分类")
-    @ApiImplicitParam(name = "id", value = "字段分类id", required = true)
-    public AjaxResult deleteChildren(@RequestParam(value = "id") @NotNull(message = "id不能为空") Integer id){
+    public R<Object> deleteChildren(@RequestParam(value = "id") Long id){
         try {
             tbFieldCategoryService.deleteChildren(id);
         } catch (Exception e) {
-            throw new RuntimeException("操作失败");
+            if (e instanceof ServiceException) {
+                return R.fail(e.getMessage());
+            }
+            log.error("编辑页面删除子字段分类异常", e);
+            return R.fail();
         }
-        return AjaxResult.success();
+        return R.ok();
     }
 
-    @GetMapping("/delete")
+    /**
+     * 删除分类
+     * @param id id
+     * @return R
+     */
+    @DeleteMapping("/delete")
     @ApiOperation("列表页面删除分类")
-    @ApiImplicitParam(name = "id", value = "字段分类id", required = true)
-    public AjaxResult delete(@RequestParam(value = "id") @NotNull(message = "id不能为空") Integer id){
+    public R<Void> delete(@RequestParam(value = "id") Long id){
         try {
             tbFieldCategoryService.delete(id);
         } catch (Exception e) {
-            throw new RuntimeException("操作失败");
+            if (e instanceof ServiceException) {
+                return R.fail(e.getMessage());
+            }
+            log.error("列表页面删除分类异常", e);
+            return R.fail();
         }
-        return AjaxResult.success();
+        return R.ok();
     }
 
-    @PostMapping("/edit")
+    /**
+     * 编辑分类
+     * @param dto 字段分类更新数据传输对象
+     * @return R
+     */
+    @PutMapping("/edit")
     @ApiOperation("编辑")
-    public AjaxResult edit(@RequestBody FieldCategoryUpdateDTO dto) {
+    public R<Object> edit(@RequestBody FieldCategoryUpdateDTO dto) {
         try {
             tbFieldCategoryService.edit(dto);
         } catch (Exception e) {
-            throw new RuntimeException("操作失败");
+            if (e instanceof ServiceException) {
+                return R.fail(e.getMessage());
+            }
+            log.error("编辑异常", e);
+            return R.fail();
         }
-        return AjaxResult.success();
+        return R.ok();
     }
 
 }

--
Gitblit v1.7.1