|
@@ -21,65 +21,65 @@ import java.util.List;
|
|
/**
|
|
/**
|
|
* @Description: 基础资料-知识-分类
|
|
* @Description: 基础资料-知识-分类
|
|
* @Author: jeecg-boot
|
|
* @Author: jeecg-boot
|
|
- * @Date: 2023-12-08
|
|
|
|
|
|
+ * @Date: 2023-12-08
|
|
* @Version: V1.0
|
|
* @Version: V1.0
|
|
*/
|
|
*/
|
|
@Service
|
|
@Service
|
|
public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category> implements ICategoryService {
|
|
public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category> implements ICategoryService {
|
|
|
|
|
|
- @Override
|
|
|
|
- public void addCategory(Category category) {
|
|
|
|
- //新增时设置hasChild为0
|
|
|
|
- category.setHasChild(ICategoryService.NOCHILD);
|
|
|
|
- if(oConvertUtils.isEmpty(category.getPid())){
|
|
|
|
- category.setPid(ICategoryService.ROOT_PID_VALUE);
|
|
|
|
- }else{
|
|
|
|
- //如果当前节点父ID不为空 则设置父节点的hasChildren 为1
|
|
|
|
- Category parent = baseMapper.selectById(category.getPid());
|
|
|
|
- if(parent!=null && !"1".equals(parent.getHasChild())){
|
|
|
|
- parent.setHasChild("1");
|
|
|
|
- baseMapper.updateById(parent);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- baseMapper.insert(category);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void updateCategory(Category category) {
|
|
|
|
- Category entity = this.getById(category.getId());
|
|
|
|
- if(entity==null) {
|
|
|
|
- throw new JeecgBootException("未找到对应实体");
|
|
|
|
- }
|
|
|
|
- String old_pid = entity.getPid();
|
|
|
|
- String new_pid = category.getPid();
|
|
|
|
- if(!old_pid.equals(new_pid)) {
|
|
|
|
- updateOldParentNode(old_pid);
|
|
|
|
- if(oConvertUtils.isEmpty(new_pid)){
|
|
|
|
- category.setPid(ICategoryService.ROOT_PID_VALUE);
|
|
|
|
- }
|
|
|
|
- if(!ICategoryService.ROOT_PID_VALUE.equals(category.getPid())) {
|
|
|
|
- baseMapper.updateTreeNodeStatus(category.getPid(), ICategoryService.HASCHILD);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- baseMapper.updateById(category);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
|
- public void deleteCategory(String id) throws JeecgBootException {
|
|
|
|
- //查询选中节点下所有子节点一并删除
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void addCategory(Category category) {
|
|
|
|
+ //新增时设置hasChild为0
|
|
|
|
+ category.setHasChild(ICategoryService.NOCHILD);
|
|
|
|
+ if (oConvertUtils.isEmpty(category.getPid())) {
|
|
|
|
+ category.setPid(ICategoryService.ROOT_PID_VALUE);
|
|
|
|
+ } else {
|
|
|
|
+ //如果当前节点父ID不为空 则设置父节点的hasChildren 为1
|
|
|
|
+ Category parent = baseMapper.selectById(category.getPid());
|
|
|
|
+ if (parent != null && !"1".equals(parent.getHasChild())) {
|
|
|
|
+ parent.setHasChild("1");
|
|
|
|
+ baseMapper.updateById(parent);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ baseMapper.insert(category);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void updateCategory(Category category) {
|
|
|
|
+ Category entity = this.getById(category.getId());
|
|
|
|
+ if (entity == null) {
|
|
|
|
+ throw new JeecgBootException("未找到对应实体");
|
|
|
|
+ }
|
|
|
|
+ String old_pid = entity.getPid();
|
|
|
|
+ String new_pid = category.getPid();
|
|
|
|
+ if (!old_pid.equals(new_pid)) {
|
|
|
|
+ updateOldParentNode(old_pid);
|
|
|
|
+ if (oConvertUtils.isEmpty(new_pid)) {
|
|
|
|
+ category.setPid(ICategoryService.ROOT_PID_VALUE);
|
|
|
|
+ }
|
|
|
|
+ if (!ICategoryService.ROOT_PID_VALUE.equals(category.getPid())) {
|
|
|
|
+ baseMapper.updateTreeNodeStatus(category.getPid(), ICategoryService.HASCHILD);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ baseMapper.updateById(category);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public void deleteCategory(String id) throws JeecgBootException {
|
|
|
|
+ //查询选中节点下所有子节点一并删除
|
|
id = this.queryTreeChildIds(id);
|
|
id = this.queryTreeChildIds(id);
|
|
- if(id.indexOf(",")>0) {
|
|
|
|
|
|
+ if (id.indexOf(",") > 0) {
|
|
StringBuffer sb = new StringBuffer();
|
|
StringBuffer sb = new StringBuffer();
|
|
String[] idArr = id.split(",");
|
|
String[] idArr = id.split(",");
|
|
for (String idVal : idArr) {
|
|
for (String idVal : idArr) {
|
|
- if(idVal != null){
|
|
|
|
|
|
+ if (idVal != null) {
|
|
Category category = this.getById(idVal);
|
|
Category category = this.getById(idVal);
|
|
String pidVal = category.getPid();
|
|
String pidVal = category.getPid();
|
|
//查询此节点上一级是否还有其他子节点
|
|
//查询此节点上一级是否还有其他子节点
|
|
- List<Category> dataList = baseMapper.selectList(new QueryWrapper<Category>().eq("pid", pidVal).notIn("id",Arrays.asList(idArr)));
|
|
|
|
|
|
+ List<Category> dataList = baseMapper.selectList(new QueryWrapper<Category>().eq("pid", pidVal).notIn("id", Arrays.asList(idArr)));
|
|
boolean flag = (dataList == null || dataList.size() == 0) && !Arrays.asList(idArr).contains(pidVal) && !sb.toString().contains(pidVal);
|
|
boolean flag = (dataList == null || dataList.size() == 0) && !Arrays.asList(idArr).contains(pidVal) && !sb.toString().contains(pidVal);
|
|
- if(flag){
|
|
|
|
|
|
+ if (flag) {
|
|
//如果当前节点原本有子节点 现在木有了,更新状态
|
|
//如果当前节点原本有子节点 现在木有了,更新状态
|
|
sb.append(pidVal).append(",");
|
|
sb.append(pidVal).append(",");
|
|
}
|
|
}
|
|
@@ -89,33 +89,33 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
|
|
baseMapper.deleteBatchIds(Arrays.asList(idArr));
|
|
baseMapper.deleteBatchIds(Arrays.asList(idArr));
|
|
//修改已无子节点的标识
|
|
//修改已无子节点的标识
|
|
String[] pidArr = sb.toString().split(",");
|
|
String[] pidArr = sb.toString().split(",");
|
|
- for(String pid : pidArr){
|
|
|
|
|
|
+ for (String pid : pidArr) {
|
|
this.updateOldParentNode(pid);
|
|
this.updateOldParentNode(pid);
|
|
}
|
|
}
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
Category category = this.getById(id);
|
|
Category category = this.getById(id);
|
|
- if(category==null) {
|
|
|
|
|
|
+ if (category == null) {
|
|
throw new JeecgBootException("未找到对应实体");
|
|
throw new JeecgBootException("未找到对应实体");
|
|
}
|
|
}
|
|
updateOldParentNode(category.getPid());
|
|
updateOldParentNode(category.getPid());
|
|
baseMapper.deleteById(id);
|
|
baseMapper.deleteById(id);
|
|
}
|
|
}
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
public List<Category> queryTreeListNoPage(QueryWrapper<Category> queryWrapper) {
|
|
public List<Category> queryTreeListNoPage(QueryWrapper<Category> queryWrapper) {
|
|
List<Category> dataList = baseMapper.selectList(queryWrapper);
|
|
List<Category> dataList = baseMapper.selectList(queryWrapper);
|
|
List<Category> mapList = new ArrayList<>();
|
|
List<Category> mapList = new ArrayList<>();
|
|
- for(Category data : dataList){
|
|
|
|
|
|
+ for (Category data : dataList) {
|
|
String pidVal = data.getPid();
|
|
String pidVal = data.getPid();
|
|
//递归查询子节点的根节点
|
|
//递归查询子节点的根节点
|
|
- if(pidVal != null && !ICategoryService.NOCHILD.equals(pidVal)){
|
|
|
|
|
|
+ if (pidVal != null && !ICategoryService.NOCHILD.equals(pidVal)) {
|
|
Category rootVal = this.getTreeRoot(pidVal);
|
|
Category rootVal = this.getTreeRoot(pidVal);
|
|
- if(rootVal != null && !mapList.contains(rootVal)){
|
|
|
|
|
|
+ if (rootVal != null && !mapList.contains(rootVal)) {
|
|
mapList.add(rootVal);
|
|
mapList.add(rootVal);
|
|
}
|
|
}
|
|
- }else{
|
|
|
|
- if(!mapList.contains(data)){
|
|
|
|
|
|
+ } else {
|
|
|
|
+ if (!mapList.contains(data)) {
|
|
mapList.add(data);
|
|
mapList.add(data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -164,35 +164,38 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
|
|
return selectTreeModels;
|
|
return selectTreeModels;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 根据所传pid查询旧的父级节点的子节点并修改相应状态值
|
|
|
|
- * @param pid
|
|
|
|
- */
|
|
|
|
- private void updateOldParentNode(String pid) {
|
|
|
|
- if(!ICategoryService.ROOT_PID_VALUE.equals(pid)) {
|
|
|
|
- Long count = baseMapper.selectCount(new QueryWrapper<Category>().eq("pid", pid));
|
|
|
|
- if(count==null || count<=1) {
|
|
|
|
- baseMapper.updateTreeNodeStatus(pid, ICategoryService.NOCHILD);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 根据所传pid查询旧的父级节点的子节点并修改相应状态值
|
|
|
|
+ *
|
|
|
|
+ * @param pid
|
|
|
|
+ */
|
|
|
|
+ private void updateOldParentNode(String pid) {
|
|
|
|
+ if (!ICategoryService.ROOT_PID_VALUE.equals(pid)) {
|
|
|
|
+ Long count = baseMapper.selectCount(new QueryWrapper<Category>().eq("pid", pid));
|
|
|
|
+ if (count == null || count <= 1) {
|
|
|
|
+ baseMapper.updateTreeNodeStatus(pid, ICategoryService.NOCHILD);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
|
|
|
+ /**
|
|
* 递归查询节点的根节点
|
|
* 递归查询节点的根节点
|
|
|
|
+ *
|
|
* @param pidVal
|
|
* @param pidVal
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- private Category getTreeRoot(String pidVal){
|
|
|
|
- Category data = baseMapper.selectById(pidVal);
|
|
|
|
- if(data != null && !ICategoryService.ROOT_PID_VALUE.equals(data.getPid())){
|
|
|
|
|
|
+ private Category getTreeRoot(String pidVal) {
|
|
|
|
+ Category data = baseMapper.selectById(pidVal);
|
|
|
|
+ if (data != null && !ICategoryService.ROOT_PID_VALUE.equals(data.getPid())) {
|
|
return this.getTreeRoot(data.getPid());
|
|
return this.getTreeRoot(data.getPid());
|
|
- }else{
|
|
|
|
|
|
+ } else {
|
|
return data;
|
|
return data;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据id查询所有子节点id
|
|
* 根据id查询所有子节点id
|
|
|
|
+ *
|
|
* @param ids
|
|
* @param ids
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
@@ -201,13 +204,13 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
|
|
String[] idArr = ids.split(",");
|
|
String[] idArr = ids.split(",");
|
|
StringBuffer sb = new StringBuffer();
|
|
StringBuffer sb = new StringBuffer();
|
|
for (String pidVal : idArr) {
|
|
for (String pidVal : idArr) {
|
|
- if(pidVal != null){
|
|
|
|
- if(!sb.toString().contains(pidVal)){
|
|
|
|
- if(sb.toString().length() > 0){
|
|
|
|
|
|
+ if (pidVal != null) {
|
|
|
|
+ if (!sb.toString().contains(pidVal)) {
|
|
|
|
+ if (sb.toString().length() > 0) {
|
|
sb.append(",");
|
|
sb.append(",");
|
|
}
|
|
}
|
|
sb.append(pidVal);
|
|
sb.append(pidVal);
|
|
- this.getTreeChildIds(pidVal,sb);
|
|
|
|
|
|
+ this.getTreeChildIds(pidVal, sb);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -216,27 +219,29 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
|
|
|
|
|
|
/**
|
|
/**
|
|
* 递归查询所有子节点
|
|
* 递归查询所有子节点
|
|
|
|
+ *
|
|
* @param pidVal
|
|
* @param pidVal
|
|
* @param sb
|
|
* @param sb
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- private StringBuffer getTreeChildIds(String pidVal,StringBuffer sb){
|
|
|
|
|
|
+ private StringBuffer getTreeChildIds(String pidVal, StringBuffer sb) {
|
|
List<Category> dataList = baseMapper.selectList(new QueryWrapper<Category>().eq("pid", pidVal));
|
|
List<Category> dataList = baseMapper.selectList(new QueryWrapper<Category>().eq("pid", pidVal));
|
|
- if(dataList != null && dataList.size()>0){
|
|
|
|
- for(Category tree : dataList) {
|
|
|
|
- if(!sb.toString().contains(tree.getId())){
|
|
|
|
|
|
+ if (dataList != null && dataList.size() > 0) {
|
|
|
|
+ for (Category tree : dataList) {
|
|
|
|
+ if (!sb.toString().contains(tree.getId())) {
|
|
sb.append(",").append(tree.getId());
|
|
sb.append(",").append(tree.getId());
|
|
}
|
|
}
|
|
- this.getTreeChildIds(tree.getId(),sb);
|
|
|
|
|
|
+ this.getTreeChildIds(tree.getId(), sb);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return sb;
|
|
return sb;
|
|
}
|
|
}
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
- public List<String> getAllParentNodeById(String childId){
|
|
|
|
|
|
+ public List<String> getAllParentNodeById(String childId) {
|
|
List<String> parentNodeList = new ArrayList<>();
|
|
List<String> parentNodeList = new ArrayList<>();
|
|
Category category = baseMapper.selectById(childId);
|
|
Category category = baseMapper.selectById(childId);
|
|
- if(category != null){
|
|
|
|
|
|
+ if (category != null) {
|
|
parentNodeList.add(category.getId());
|
|
parentNodeList.add(category.getId());
|
|
parentNodeList.addAll(getAllParentNodeById(category.getPid()));
|
|
parentNodeList.addAll(getAllParentNodeById(category.getPid()));
|
|
}
|
|
}
|
|
@@ -246,9 +251,11 @@ public class CategoryServiceImpl extends ServiceImpl<KmsCategoryMapper, Category
|
|
@Override
|
|
@Override
|
|
public List<Category> queryParentNodeById(String id) {
|
|
public List<Category> queryParentNodeById(String id) {
|
|
List<Category> parentNodeList = new ArrayList<>();
|
|
List<Category> parentNodeList = new ArrayList<>();
|
|
- Category category = baseMapper.selectById(id);
|
|
|
|
- if(category != null){
|
|
|
|
- parentNodeList.add(category);
|
|
|
|
|
|
+ Category category = baseMapper.selectByIdWithoutDelete(id);
|
|
|
|
+ if (category != null) {
|
|
|
|
+ if (category.getDelFlag() == 0) {
|
|
|
|
+ parentNodeList.add(category);
|
|
|
|
+ }
|
|
parentNodeList.addAll(queryParentNodeById(category.getPid()));
|
|
parentNodeList.addAll(queryParentNodeById(category.getPid()));
|
|
}
|
|
}
|
|
return parentNodeList;
|
|
return parentNodeList;
|