mitao
2024-03-22 e69dec94fe9763d04425756370760698850f926f
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TbFieldCategoryServiceImpl.java
@@ -6,6 +6,7 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageDTO;
import com.ruoyi.common.enums.ShowStatusEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.common.utils.CollUtils;
import com.ruoyi.common.utils.StringUtils;
@@ -16,6 +17,7 @@
import com.ruoyi.system.mapper.TbFieldCategoryMapper;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -85,10 +87,7 @@
    }
    private void updateCategoryAndChildren(Integer id, ShowStatusEnum status) {
        TbFieldCategory category = this.getById(id);
        if (Objects.isNull(category)) {
            throw new RuntimeException("非法id");
        }
        TbFieldCategory category = this.validateParam(id);
        this.lambdaUpdate()
                .eq(TbFieldCategory::getId, id)
                .set(TbFieldCategory::getStatus, status)
@@ -122,10 +121,7 @@
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        //一级分类
        TbFieldCategory category = this.getById(id);
        if (Objects.isNull(category)) {
            throw new RuntimeException("非法参数");
        }
        validateParam(id);
        //查询是否有二级分类
        List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list();
        if (CollectionUtils.isNotEmpty(threeCategoryList)) {
@@ -144,8 +140,17 @@
        this.removeById(id);
    }
    private TbFieldCategory validateParam(Integer id) {
        TbFieldCategory category = this.getById(id);
        if (Objects.isNull(category)) {
            throw new ServiceException("非法参数");
        }
        return category;
    }
    @Override
    public void edit(FieldCategoryUpdateDTO dto) {
        validateParam(dto.getId());
        //更新一级分类
        updateCategory(dto);
        List<FieldCategoryUpdateDTO> children = dto.getChildren();
@@ -180,4 +185,26 @@
                .list();
        return BeanUtils.copyList(list, FieldCategoryVO.class);
    }
    @Override
    public FieldCategoryDetailVO getDetailsById(Integer id) {
        TbFieldCategory oneCategory = this.getById(id);
        if (Objects.isNull(oneCategory)) {
            return new FieldCategoryDetailVO();
        }
        FieldCategoryDetailVO vo = BeanUtils.copyBean(oneCategory, FieldCategoryDetailVO.class);
        //根据一级分类id,查询二级分类
        List<TbFieldCategory> twoCategoryList = this.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 = this.lambdaQuery().eq(TbFieldCategory::getParentId, item.getId()).list();
            threeCategoryList.forEach(threeCategory->{
                FieldCategoryDetailVO threeCategoryVO = BeanUtils.copyBean(threeCategory, FieldCategoryDetailVO.class);
                twoCategoryVO.getChildren().add(threeCategoryVO);
            });
        });
        return vo;
    }
}