forked from wangjiadong/comp
parent
303f10c000
commit
0ce0bab975
11 changed files with 776 additions and 116 deletions
@ -1,19 +1,204 @@ |
||||
package org.jeecg.modules.demo.cms.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import org.jeecg.common.constant.SymbolConstant; |
||||
import org.jeecg.common.exception.JeecgBootException; |
||||
import org.jeecg.common.system.vo.SelectTreeModel; |
||||
import org.jeecg.common.util.oConvertUtils; |
||||
import org.jeecg.modules.demo.cms.entity.CmsColumn; |
||||
import org.jeecg.modules.demo.cms.mapper.CmsColumnMapper; |
||||
import org.jeecg.modules.demo.cms.service.ICmsColumnService; |
||||
import org.springframework.stereotype.Service; |
||||
import org.springframework.transaction.annotation.Transactional; |
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Description: cms栏目 |
||||
* @Author: jeecg-boot |
||||
* @Date: 2023-10-11 |
||||
* @Date: 2023-10-11 |
||||
* @Version: V1.0 |
||||
*/ |
||||
@Service |
||||
public class CmsColumnServiceImpl extends ServiceImpl<CmsColumnMapper, CmsColumn> implements ICmsColumnService { |
||||
|
||||
@Override |
||||
public List<CmsColumn> queryTreeListNoPage(QueryWrapper<CmsColumn> queryWrapper) { |
||||
List<CmsColumn> dataList = baseMapper.selectList(queryWrapper); |
||||
List<CmsColumn> mapList = new ArrayList<>(); |
||||
for (CmsColumn data : dataList) { |
||||
String pidVal = data.getPid(); |
||||
//递归查询子节点的根节点
|
||||
if (pidVal != null && !ICmsColumnService.NOCHILD.equals(pidVal)) { |
||||
CmsColumn rootVal = this.getTreeRoot(pidVal); |
||||
if (rootVal != null && !mapList.contains(rootVal)) { |
||||
mapList.add(rootVal); |
||||
} |
||||
} else { |
||||
if (!mapList.contains(data)) { |
||||
mapList.add(data); |
||||
} |
||||
} |
||||
} |
||||
return mapList; |
||||
} |
||||
|
||||
@Override |
||||
public List<SelectTreeModel> queryListByCode(String parentCode) { |
||||
String pid = ROOT_PID_VALUE; |
||||
if (oConvertUtils.isNotEmpty(parentCode)) { |
||||
LambdaQueryWrapper<CmsColumn> queryWrapper = new LambdaQueryWrapper<>(); |
||||
queryWrapper.eq(CmsColumn::getPid, parentCode); |
||||
List<CmsColumn> list = baseMapper.selectList(queryWrapper); |
||||
if (list == null || list.size() == 0) { |
||||
throw new JeecgBootException("该编码【" + parentCode + "】不存在,请核实!"); |
||||
} |
||||
if (list.size() > 1) { |
||||
throw new JeecgBootException("该编码【" + parentCode + "】存在多个,请核实!"); |
||||
} |
||||
pid = list.get(0).getId(); |
||||
} |
||||
return baseMapper.queryListByPid(pid, null); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public List<SelectTreeModel> queryListByPid(String pid) { |
||||
if (oConvertUtils.isEmpty(pid)) { |
||||
pid = ROOT_PID_VALUE; |
||||
} |
||||
return baseMapper.queryListByPid(pid, null); |
||||
} |
||||
|
||||
@Override |
||||
@Transactional(rollbackFor = Exception.class) |
||||
public void delete(String ids) { |
||||
String allIds = this.queryTreeChildIds(ids); |
||||
String pids = this.queryTreePids(ids); |
||||
//1.删除时将节点下所有子节点一并删除
|
||||
this.baseMapper.deleteBatchIds(Arrays.asList(allIds.split(","))); |
||||
//2.将父节点中已经没有下级的节点,修改为没有子节点
|
||||
if (oConvertUtils.isNotEmpty(pids)) { |
||||
LambdaUpdateWrapper<CmsColumn> updateWrapper = new UpdateWrapper<CmsColumn>() |
||||
.lambda() |
||||
.in(CmsColumn::getId, Arrays.asList(pids.split(","))) |
||||
.set(CmsColumn::getHasChild, "0"); |
||||
this.update(updateWrapper); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 查询需修改标识的父节点ids |
||||
* |
||||
* @param ids |
||||
* @return |
||||
*/ |
||||
private String queryTreePids(String ids) { |
||||
StringBuffer sb = new StringBuffer(); |
||||
//获取id数组
|
||||
String[] idArr = ids.split(","); |
||||
for (String id : idArr) { |
||||
if (id != null) { |
||||
CmsColumn category = this.baseMapper.selectById(id); |
||||
//根据id查询pid值
|
||||
String metaPid = category.getPid(); |
||||
//查询此节点上一级是否还有其他子节点
|
||||
LambdaQueryWrapper<CmsColumn> queryWrapper = new LambdaQueryWrapper<>(); |
||||
queryWrapper.eq(CmsColumn::getPid, metaPid); |
||||
queryWrapper.notIn(CmsColumn::getId, Arrays.asList(idArr)); |
||||
List<CmsColumn> dataList = this.baseMapper.selectList(queryWrapper); |
||||
boolean flag = (dataList == null || dataList.size() == 0) && !Arrays.asList(idArr).contains(metaPid) |
||||
&& !sb.toString().contains(metaPid); |
||||
if (flag) { |
||||
//如果当前节点原本有子节点 现在木有了,更新状态
|
||||
sb.append(metaPid).append(","); |
||||
} |
||||
} |
||||
} |
||||
if (sb.toString().endsWith(SymbolConstant.COMMA)) { |
||||
sb = sb.deleteCharAt(sb.length() - 1); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
/** |
||||
* 根据所传pid查询旧的父级节点的子节点并修改相应状态值 |
||||
* |
||||
* @param pid |
||||
*/ |
||||
private void updateOldParentNode(String pid) { |
||||
if (!ICmsColumnService.ROOT_PID_VALUE.equals(pid)) { |
||||
Long count = baseMapper.selectCount(new QueryWrapper<CmsColumn>().eq("pid", pid)); |
||||
if (count == null || count <= 1) { |
||||
// baseMapper.updateTreeNodeStatus(pid, ICmsColumnService.NOCHILD);
|
||||
this.lambdaUpdate().set(CmsColumn::getPid, ICmsColumnService.NOCHILD) |
||||
.eq(CmsColumn::getPid, pid).update(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 递归查询节点的根节点 |
||||
* |
||||
* @param pidVal |
||||
* @return |
||||
*/ |
||||
private CmsColumn getTreeRoot(String pidVal) { |
||||
CmsColumn data = baseMapper.selectById(pidVal); |
||||
if (data != null && !ICmsColumnService.ROOT_PID_VALUE.equals(data.getPid())) { |
||||
return this.getTreeRoot(data.getPid()); |
||||
} else { |
||||
return data; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 根据id查询所有子节点id |
||||
* |
||||
* @param ids |
||||
* @return |
||||
*/ |
||||
private String queryTreeChildIds(String ids) { |
||||
//获取id数组
|
||||
String[] idArr = ids.split(","); |
||||
StringBuffer sb = new StringBuffer(); |
||||
for (String pidVal : idArr) { |
||||
if (pidVal != null) { |
||||
if (!sb.toString().contains(pidVal)) { |
||||
if (sb.toString().length() > 0) { |
||||
sb.append(","); |
||||
} |
||||
sb.append(pidVal); |
||||
this.getTreeChildIds(pidVal, sb); |
||||
} |
||||
} |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
/** |
||||
* 递归查询所有子节点 |
||||
* |
||||
* @param pidVal |
||||
* @param sb |
||||
* @return |
||||
*/ |
||||
private StringBuffer getTreeChildIds(String pidVal, StringBuffer sb) { |
||||
List<CmsColumn> dataList = baseMapper.selectList(new QueryWrapper<CmsColumn>().eq("pid", pidVal)); |
||||
if (dataList != null && dataList.size() > 0) { |
||||
for (CmsColumn tree : dataList) { |
||||
if (!sb.toString().contains(tree.getId())) { |
||||
sb.append(",").append(tree.getId()); |
||||
} |
||||
this.getTreeChildIds(tree.getId(), sb); |
||||
} |
||||
} |
||||
return sb; |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue