mitao
2024-04-02 4403a428edc18482c5aa561e603b26ebe13d328f
ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/TbFieldCategoryController.java
@@ -3,8 +3,7 @@
import com.ruoyi.common.basic.PageDTO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.system.domain.TbFieldCategory;
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;
@@ -18,9 +17,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
/**
 * <p>
@@ -50,8 +46,11 @@
        try {
            tbFieldCategoryService.add(dto);
        } catch (Exception e) {
            log.error("添加分类异常", e);
            throw new RuntimeException("操作失败");
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("添加异常", e);
            return R.fail();
        }
        return R.ok();
    }
@@ -61,27 +60,19 @@
     * @param id 字段分类id
     * @return R
     */
    @GetMapping("/getById")
    @GetMapping("/get-details")
    @ApiOperation("根据id获取字段分类详情")
    public R<FieldCategoryDetailVO> getById(@RequestParam(value = "id") Integer id) {
        TbFieldCategory oneCategory = tbFieldCategoryService.getById(id);
        if (Objects.isNull(oneCategory)) {
            return R.ok(new FieldCategoryDetailVO());
    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();
        }
        FieldCategoryDetailVO vo = BeanUtils.copyBean(oneCategory, FieldCategoryDetailVO.class);
        //根据一级分类id,查询二级分类
        List<TbFieldCategory> twoCategoryList = tbFieldCategoryService.lambdaQuery().eq(TbFieldCategory::getParentId, oneCategory.getId()).list();
        twoCategoryList.forEach(item->{
            FieldCategoryDetailVO twoCategoryVO = BeanUtils.copyBean(item, FieldCategoryDetailVO.class);
            vo.getChildren().add(twoCategoryVO);
            //根据二级分类id,查询三级分类
            List<TbFieldCategory> threeCategoryList = tbFieldCategoryService.lambdaQuery().eq(TbFieldCategory::getParentId, item.getId()).list();
            threeCategoryList.forEach(threeCategory->{
                FieldCategoryDetailVO threeCategoryVO = BeanUtils.copyBean(threeCategory, FieldCategoryDetailVO.class);
                twoCategoryVO.getChildren().add(threeCategoryVO);
            });
        });
        return R.ok(vo);
    }
    /**
@@ -92,7 +83,15 @@
    @PostMapping("/page")
    @ApiOperation("分页条件查询")
    public R<PageDTO<FieldCategoryVO>> page(@RequestBody FieldCategoryQuery query) {
        return R.ok(tbFieldCategoryService.queryPage(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();
        }
    }
    /**
@@ -100,10 +99,18 @@
     * @param dto 显示隐藏操作数据传输对象
     * @return R
     */
    @PutMapping("/show-hide")
    @PostMapping("/show-hide")
    @ApiOperation("隐藏显示操作")
    public R<Object> showHide(@RequestBody ShowHideDTO dto) {
        tbFieldCategoryService.showHide(dto);
    public R<Void> showHide(@RequestBody ShowHideDTO dto) {
        try {
            tbFieldCategoryService.showHide(dto);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("隐藏显示操作异常", e);
            return R.fail();
        }
        return R.ok();
    }
@@ -114,12 +121,15 @@
     */
    @DeleteMapping("/delete-children")
    @ApiOperation("编辑页面删除子字段分类")
    public R<Object> deleteChildren(@RequestParam(value = "id") Integer id){
    public R<Object> deleteChildren(@RequestParam(value = "id") Long id){
        try {
            tbFieldCategoryService.deleteChildren(id);
        } catch (Exception e) {
            log.error("编辑页面删除子字段分类失败",e);
            throw new RuntimeException("操作失败");
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("编辑页面删除子字段分类异常", e);
            return R.fail();
        }
        return R.ok();
    }
@@ -131,8 +141,16 @@
     */
    @DeleteMapping("/delete")
    @ApiOperation("列表页面删除分类")
    public R<Void> delete(@RequestParam(value = "id") Integer id){
        tbFieldCategoryService.delete(id);
    public R<Void> delete(@RequestParam(value = "id") Long id){
        try {
            tbFieldCategoryService.delete(id);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("列表页面删除分类异常", e);
            return R.fail();
        }
        return R.ok();
    }
@@ -144,7 +162,15 @@
    @PutMapping("/edit")
    @ApiOperation("编辑")
    public R<Object> edit(@RequestBody FieldCategoryUpdateDTO dto) {
        tbFieldCategoryService.edit(dto);
        try {
            tbFieldCategoryService.edit(dto);
        } catch (Exception e) {
            if (e instanceof ServiceException) {
                return R.fail(e.getMessage());
            }
            log.error("编辑异常", e);
            return R.fail();
        }
        return R.ok();
    }