左侧树:保存分类

This commit is contained in:
huangge1199 2025-08-06 11:08:15 +08:00
parent af17053ee5
commit 8bc44ffc64
3 changed files with 44 additions and 9 deletions

View File

@ -31,8 +31,6 @@ public class CategoryController {
/**
* 查询分类
* @param params 分类参数
* @return 结果
*/
@Operation(summary = "查询分类")
@PostMapping("/showCategory")
@ -41,4 +39,14 @@ public class CategoryController {
List<Category> result = categoryService.showCategory(category);
return R.ok(result);
}
/**
* 保存分类
*/
@Operation(summary = "保存分类")
@PostMapping("/saveCategory")
public R<?> addCategory(@RequestBody JSONObject params) {
categoryService.addCategory(params);
return R.ok();
}
}

View File

@ -1,5 +1,6 @@
package iet.ustb.sf.service;
import com.alibaba.fastjson.JSONObject;
import iet.ustb.sf.domain.Category;
import com.baomidou.mybatisplus.extension.service.IService;
@ -13,4 +14,6 @@ import java.util.List;
public interface CategoryService extends IService<Category> {
List<Category> showCategory(Category category);
void addCategory(JSONObject params);
}

View File

@ -1,5 +1,6 @@
package iet.ustb.sf.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iet.ustb.sf.domain.Category;
@ -8,12 +9,12 @@ import iet.ustb.sf.exception.ThrowUtils;
import iet.ustb.sf.mapper.TargetMapper;
import iet.ustb.sf.service.CategoryService;
import iet.ustb.sf.mapper.CategoryMapper;
import iet.ustb.sf.util.CheckUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
/**
* @author hyy
@ -50,17 +51,40 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
} else {
queryWrapper.eq("topic", category.getTopic());
}
if (StringUtils.isEmpty(category.getParentId())) {
queryWrapper.isNull("parent_id");
} else {
queryWrapper.eq("parent_id", category.getParentId());
}
queryWrapper.orderByDesc("update_time");
categoryList = categoryMapper.selectList(queryWrapper);
for (Category tmp : categoryList) {
tmp.setOrganization(category.getOrganization());
}
return categoryList;
}
@Override
public void addCategory(JSONObject params) {
Category category = JSONObject.parseObject(params.toString(), Category.class);
ThrowUtils.throwIf(category == null, ErrorCode.PARAMS_ERROR, "传入分类不正确!");
CheckUtils.checkEmpty(category.getNameCn(), "中文分类");
CheckUtils.checkEmpty(category.getTopic(), "主题");
List<String> topicList = Arrays.asList("质量", "生产", "能源", "运维", "成本", "测试");
ThrowUtils.throwIf(!topicList.contains(category.getTopic()), ErrorCode.PARAMS_ERROR, "主题名称错误!");
if ("".equals(category.getId())) {
category.setId(UUID.randomUUID().toString());
}
List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().eq("name_en", category.getNameCn()));
for (Category tmp : categoryList) {
boolean bl = tmp.getNameCn().equals(category.getNameCn()) && !tmp.getId().equals(category.getId());
ThrowUtils.throwIf(bl, ErrorCode.PARAMS_ERROR, "分类名不能重复!");
}
Date now = new Date();
if (category.getCreateTime() == null) {
category.setCreateTime(now);
}
category.setUpdateTime(now);
categoryMapper.insert(category);
}
}