Commit bf363bda authored by msgroup's avatar msgroup

修复子栏目删除是,父栏目leaf标识不对bug

parent 954b380a
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so, * the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions: * subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all * The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software. * copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
...@@ -48,271 +48,273 @@ import java.util.List; ...@@ -48,271 +48,273 @@ import java.util.List;
* 创建日期:2019-11-28 15:12:32<br/> * 创建日期:2019-11-28 15:12:32<br/>
* 历史修订:<br/> * 历史修订:<br/>
*/ */
@Service("cmscategoryBizImpl") @Service("cmscategoryBizImpl")
@Transactional(rollbackFor = RuntimeException.class) @Transactional(rollbackFor = RuntimeException.class)
public class CategoryBizImpl extends BaseBizImpl<ICategoryDao, CategoryEntity> implements ICategoryBiz { public class CategoryBizImpl extends BaseBizImpl<ICategoryDao, CategoryEntity> implements ICategoryBiz {
@Autowired @Autowired
private ICategoryDao categoryDao; private ICategoryDao categoryDao;
@Autowired
private IContentDao contentDao;
@Autowired
private IContentDao contentDao;
@Override
protected IBaseDao getDao() {
// TODO Auto-generated method stub
return categoryDao;
}
@Override @Override
protected IBaseDao getDao() { public List<CategoryEntity> queryChilds(CategoryEntity category) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return categoryDao; return categoryDao.queryChildren(category);
} }
@Override @Override
public List<CategoryEntity> queryChilds(CategoryEntity category) { public void saveEntity(CategoryEntity categoryEntity) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return categoryDao.queryChildren(category); String pingYin = PinYinUtil.getPingYin(categoryEntity.getCategoryTitle());
} //如果用户自己填入了拼音则使用用户的
@Override if (StrUtil.isNotBlank(categoryEntity.getCategoryPinyin())) {
public void saveEntity(CategoryEntity categoryEntity) { pingYin = categoryEntity.getCategoryPinyin();
// TODO Auto-generated method stub }
String pingYin = PinYinUtil.getPingYin(categoryEntity.getCategoryTitle()); CategoryEntity category = new CategoryEntity();
//如果用户自己填入了拼音则使用用户的 category.setCategoryPinyin(pingYin);
if (StrUtil.isNotBlank(categoryEntity.getCategoryPinyin())) { Object categoryBizEntity = getEntity(category);
pingYin = categoryEntity.getCategoryPinyin(); setParentId(categoryEntity);
} categoryEntity.setCategoryPinyin(pingYin);
CategoryEntity category=new CategoryEntity(); //更新新的父级
category.setCategoryPinyin(pingYin); if (StrUtil.isNotBlank(categoryEntity.getCategoryId()) && !"0".equals(categoryEntity.getCategoryId())) {
Object categoryBizEntity = getEntity(category); CategoryEntity parent = getById(categoryEntity.getCategoryId());
setParentId(categoryEntity); //如果之前是叶子节点就更新
categoryEntity.setCategoryPinyin(pingYin); if (parent.getLeaf()) {
//更新新的父级 parent.setLeaf(false);
if(StrUtil.isNotBlank(categoryEntity.getCategoryId())&&!"0".equals(categoryEntity.getCategoryId())){ updateById(parent);
CategoryEntity parent = getById(categoryEntity.getCategoryId()); }
//如果之前是叶子节点就更新 }
if(parent.getLeaf()){ categoryEntity.setLeaf(false);
parent.setLeaf(false); //如果是新增栏目一定是叶子节点
updateById(parent); if (StrUtil.isEmpty(categoryEntity.getId())) {
} categoryEntity.setLeaf(true);
} }
categoryEntity.setLeaf(false); super.save(categoryEntity);
//如果是新增栏目一定是叶子节点 //拼音存在则拼接id
if (StrUtil.isEmpty(categoryEntity.getId())) { if (categoryBizEntity != null) {
categoryEntity.setLeaf(true); categoryEntity.setCategoryPinyin(pingYin + categoryEntity.getId());
} }
super.save(categoryEntity); CategoryEntity parentCategory = null;
//拼音存在则拼接id if (StringUtils.isNotBlank(categoryEntity.getCategoryId())) {
if(categoryBizEntity!=null){ parentCategory = (CategoryEntity) getById(categoryEntity.getCategoryId());
categoryEntity.setCategoryPinyin(pingYin+categoryEntity.getId()); }
} //保存链接地址
CategoryEntity parentCategory = null; String path = ObjectUtil.isNotNull(parentCategory) ? parentCategory.getCategoryPath() : "";
if (StringUtils.isNotBlank(categoryEntity.getCategoryId())) { categoryEntity.setCategoryPath(path + "/" + categoryEntity.getCategoryPinyin());
parentCategory = (CategoryEntity)getById(categoryEntity.getCategoryId()); setTopId(categoryEntity);
} super.updateById(categoryEntity);
//保存链接地址 }
String path=ObjectUtil.isNotNull(parentCategory)?parentCategory.getCategoryPath():"";
categoryEntity.setCategoryPath( path+"/" + categoryEntity.getCategoryPinyin());
setTopId(categoryEntity);
super.updateById(categoryEntity);
}
private void setParentId(CategoryEntity categoryEntity) { private void setParentId(CategoryEntity categoryEntity) {
String path = ""; String path = "";
if(StringUtils.isNotEmpty(categoryEntity.getCategoryId())&&Long.parseLong(categoryEntity.getCategoryId())>0) { if (StringUtils.isNotEmpty(categoryEntity.getCategoryId()) && Long.parseLong(categoryEntity.getCategoryId()) > 0) {
CategoryEntity category = (CategoryEntity)getById(categoryEntity.getCategoryId()); CategoryEntity category = (CategoryEntity) getById(categoryEntity.getCategoryId());
path = category.getCategoryPath(); path = category.getCategoryPath();
if(StringUtils.isEmpty(category.getCategoryParentIds())) { if (StringUtils.isEmpty(category.getCategoryParentIds())) {
categoryEntity.setCategoryParentIds(category.getId()); categoryEntity.setCategoryParentIds(category.getId());
} else { } else {
categoryEntity.setCategoryParentIds(category.getCategoryParentIds()+","+category.getId()); categoryEntity.setCategoryParentIds(category.getCategoryParentIds() + "," + category.getId());
} }
}else { } else {
categoryEntity.setCategoryParentIds(null); categoryEntity.setCategoryParentIds(null);
} }
//保存时先保存再修改链接地址,修改时直接修改 //保存时先保存再修改链接地址,修改时直接修改
if(StringUtils.isNotBlank(categoryEntity.getId())) { if (StringUtils.isNotBlank(categoryEntity.getId())) {
categoryEntity.setCategoryPath(path+ "/" + categoryEntity.getCategoryPinyin()); categoryEntity.setCategoryPath(path + "/" + categoryEntity.getCategoryPinyin());
} }
} }
private void setChildParentId(CategoryEntity categoryEntity, String topId) {
CategoryEntity category=new CategoryEntity();
category.setCategoryId(categoryEntity.getId());
List<CategoryEntity> list = categoryDao.query(category);
list.forEach(x->{
if(StringUtils.isEmpty(categoryEntity.getCategoryParentIds())) {
x.setCategoryParentIds(categoryEntity.getId());
} else {
x.setCategoryParentIds(categoryEntity.getCategoryParentIds()+","+categoryEntity.getId());
}
//更新topid
x.setTopId(topId);
String path=categoryEntity.getCategoryPath();
//判断是否有parentIds
x.setCategoryPath(path+"/"+x.getCategoryPinyin());
//去除多余的/符号
super.updateEntity(x);
setChildParentId(x, topId);
});
}
@Override private void setChildParentId(CategoryEntity categoryEntity, String topId) {
public void updateEntity(CategoryEntity entity) { CategoryEntity category = new CategoryEntity();
setParentId(entity); category.setCategoryId(categoryEntity.getId());
String pingYin =entity.getCategoryPinyin(); List<CategoryEntity> list = categoryDao.query(category);
if(StrUtil.isNotBlank(pingYin)){ list.forEach(x -> {
CategoryEntity category=new CategoryEntity(); if (StringUtils.isEmpty(categoryEntity.getCategoryParentIds())) {
category.setCategoryPinyin(pingYin); x.setCategoryParentIds(categoryEntity.getId());
CategoryEntity categoryBizEntity = (CategoryEntity)getEntity(category); } else {
//拼音存在则拼接id x.setCategoryParentIds(categoryEntity.getCategoryParentIds() + "," + categoryEntity.getId());
if(categoryBizEntity!=null&&!categoryBizEntity.getId().equals(entity.getId())){ }
entity.setCategoryPinyin(pingYin+entity.getId()); //更新topid
} x.setTopId(topId);
} String path = categoryEntity.getCategoryPath();
setParentLeaf(entity); //判断是否有parentIds
setTopId(entity); x.setCategoryPath(path + "/" + x.getCategoryPinyin());
//如果父级栏目和父级id为空字符串则转化成null //去除多余的/符号
if (StringUtils.isEmpty(entity.getCategoryId())) { super.updateEntity(x);
entity.setCategoryId(null); setChildParentId(x, topId);
} });
if (StringUtils.isEmpty(entity.getCategoryParentIds())) { }
entity.setCategoryParentIds(null);
}
categoryDao.updateEntity(entity);
//更新子节点所有父节点id和topid
//如果本节点的topid为0(顶级栏目),则把自身的id作为子栏目的topid,非0所有的子栏目和本栏目使用同一个topid
String topId = entity.getTopId();
if (topId.equals("0")) {
topId = entity.getId();
}
setChildParentId(entity, topId);
}
@Override
public void updateEntity(CategoryEntity entity) {
setParentId(entity);
String pingYin = entity.getCategoryPinyin();
if (StrUtil.isNotBlank(pingYin)) {
CategoryEntity category = new CategoryEntity();
category.setCategoryPinyin(pingYin);
CategoryEntity categoryBizEntity = (CategoryEntity) getEntity(category);
//拼音存在则拼接id
if (categoryBizEntity != null && !categoryBizEntity.getId().equals(entity.getId())) {
entity.setCategoryPinyin(pingYin + entity.getId());
}
}
setParentLeaf(entity);
setTopId(entity);
//如果父级栏目和父级id为空字符串则转化成null
if (StringUtils.isEmpty(entity.getCategoryId())) {
entity.setCategoryId(null);
}
if (StringUtils.isEmpty(entity.getCategoryParentIds())) {
entity.setCategoryParentIds(null);
}
categoryDao.updateEntity(entity);
//更新子节点所有父节点id和topid
//如果本节点的topid为0(顶级栏目),则把自身的id作为子栏目的topid,非0所有的子栏目和本栏目使用同一个topid
String topId = entity.getTopId();
if (topId.equals("0")) {
topId = entity.getId();
}
setChildParentId(entity, topId);
}
@Override
public void update(CategoryEntity entity) {
super.updateEntity(entity);
}
@Override @Override
public void delete(String categoryId) { public void update(CategoryEntity entity) {
// TODO Auto-generated method stub super.updateEntity(entity);
CategoryEntity category = (CategoryEntity) categoryDao.selectById(categoryId); }
//删除父类
if(category != null){
category.setCategoryParentIds(null);
List<CategoryEntity> childrenList = categoryDao.queryChildren(category);
List<String> ids = new ArrayList<>();
for(int i = 0; i < childrenList.size(); i++){
//删除子类
ids.add(childrenList.get(i).getId());
}
categoryDao.deleteBatchIds(ids);
// 删除文章
contentDao.deleteEntityByCategoryIds(ids.toArray(new String[ids.size()]));
//获取被删节点的父节点 @Override
CategoryEntity parentNode = categoryDao.selectById(category.getCategoryId()); public void delete(String categoryId) {
//获取被删节点的所属栏目的其他节点 // TODO Auto-generated method stub
List<CategoryEntity> childNode = categoryDao.queryChildren(parentNode); CategoryEntity category = (CategoryEntity) categoryDao.selectById(categoryId);
//判断删除的是否为主节点 //删除父类
if (parentNode != null) { if (category != null) {
UpdateWrapper<CategoryEntity> updateWrapper = new UpdateWrapper<>(); category.setCategoryParentIds(null);
List<CategoryEntity> childrenList = categoryDao.queryChildren(category);
List<String> ids = new ArrayList<>();
for (int i = 0; i < childrenList.size(); i++) {
//删除子类
ids.add(childrenList.get(i).getId());
}
categoryDao.deleteBatchIds(ids);
// 删除文章
contentDao.deleteEntityByCategoryIds(ids.toArray(new String[ids.size()]));
//是否还有子节点 //获取被删节点的父节点
if (childNode.size() > 1) CategoryEntity parentNode = categoryDao.selectById(category.getCategoryId());
updateWrapper.eq("id", parentNode.getId()).set("leaf", 0); //清空CategoryParentIds,避免查找不必要的数据,只需要当前的父级栏目
else parentNode.setCategoryParentIds(null);
updateWrapper.eq("id", parentNode.getId()).set("leaf", 1); //获取被删节点的所属栏目的其他节点
List<CategoryEntity> childNode = categoryDao.queryChildren(parentNode);
//判断删除的是否为主节点
if (parentNode != null) {
UpdateWrapper<CategoryEntity> updateWrapper = new UpdateWrapper<>();
//如果没有子节点进行更新代码
if (childNode.size() == 1) {
updateWrapper.eq("id", parentNode.getId()).set("leaf", 1);
categoryDao.update(null, updateWrapper);
}
categoryDao.update(null, updateWrapper); }
}
} }
} }
/** /**
* 设置父级叶子节点 * 设置父级叶子节点
* @param entity * @param entity
*/ */
private void setParentLeaf(CategoryEntity entity){ private void setParentLeaf(CategoryEntity entity) {
CategoryEntity categoryEntity = getById(entity.getId()); CategoryEntity categoryEntity = getById(entity.getId());
//如果父级不为空并且修改了父级则需要更新父级 //如果父级不为空并且修改了父级则需要更新父级
if(entity.getCategoryId() != null && !entity.getCategoryId().equals(categoryEntity.getCategoryId())){ if (entity.getCategoryId() != null && !entity.getCategoryId().equals(categoryEntity.getCategoryId())) {
//更新旧的父级 //更新旧的父级
if(StrUtil.isNotBlank(categoryEntity.getCategoryId())&&!"0".equals(categoryEntity.getCategoryId())){ if (StrUtil.isNotBlank(categoryEntity.getCategoryId()) && !"0".equals(categoryEntity.getCategoryId())) {
CategoryEntity parent = getById(categoryEntity.getCategoryId()); CategoryEntity parent = getById(categoryEntity.getCategoryId());
//如果修改了父级则需要判断父级是否还有子节点 //如果修改了父级则需要判断父级是否还有子节点
boolean leaf = parent.getLeaf(); boolean leaf = parent.getLeaf();
//查找不等于当前更新的分类子集,有则不是叶子节点 //查找不等于当前更新的分类子集,有则不是叶子节点
QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
parent.setLeaf(count(queryWrapper.eq("category_id",parent.getId()).ne("id",entity.getId()))==0); parent.setLeaf(count(queryWrapper.eq("category_id", parent.getId()).ne("id", entity.getId())) == 0);
if(leaf!=parent.getLeaf()){ if (leaf != parent.getLeaf()) {
updateById(parent); updateById(parent);
} }
} }
//更新新的父级 //更新新的父级
if(StrUtil.isNotBlank(entity.getCategoryId())&&!"0".equals(entity.getCategoryId())){ if (StrUtil.isNotBlank(entity.getCategoryId()) && !"0".equals(entity.getCategoryId())) {
CategoryEntity parent = getById(entity.getCategoryId()); CategoryEntity parent = getById(entity.getCategoryId());
//如果之前是叶子节点就更新 //如果之前是叶子节点就更新
if(parent.getLeaf()){ if (parent.getLeaf()) {
parent.setLeaf(false); parent.setLeaf(false);
updateById(parent); updateById(parent);
} }
} }
} }
} }
/** /**
* 设置顶级id * 设置顶级id
* @param entity * @param entity
*/ */
private void setTopId(CategoryEntity entity){ private void setTopId(CategoryEntity entity) {
String categoryParentId = entity.getCategoryParentIds(); String categoryParentId = entity.getCategoryParentIds();
if(StrUtil.isNotBlank(categoryParentId)){ if (StrUtil.isNotBlank(categoryParentId)) {
String[] ids = categoryParentId.split(","); String[] ids = categoryParentId.split(",");
//如果有ParentId就取第一个 //如果有ParentId就取第一个
if(ids.length>0){ if (ids.length > 0) {
entity.setTopId(ids[0]); entity.setTopId(ids[0]);
return; return;
} }
} }
entity.setTopId("0"); entity.setTopId("0");
} }
@Override @Override
public void copyCategory(CategoryEntity category) { public void copyCategory(CategoryEntity category) {
String oldId = category.getId(); String oldId = category.getId();
//先保存被复制第一层栏目,因为第一层栏目不需要变更父级栏目 //先保存被复制第一层栏目,因为第一层栏目不需要变更父级栏目
category = getById(oldId); category = getById(oldId);
//id、拼音和路径按照原来的业务逻辑生成 //id、拼音和路径按照原来的业务逻辑生成
category.setId(null); category.setId(null);
category.setCategoryPinyin(null); category.setCategoryPinyin(null);
category.setCategoryPath(null); category.setCategoryPath(null);
saveEntity(category); saveEntity(category);
//传入简要被复制子栏目的id和复制后的生成的id,复制的子栏目全部使用 //传入简要被复制子栏目的id和复制后的生成的id,复制的子栏目全部使用
recursionCopyChilds(oldId, category.getId()); recursionCopyChilds(oldId, category.getId());
} }
/* /*
* 递归复制子栏目 * 递归复制子栏目
* @param oldParentId:被复制的父级栏目id(需要数据库原来存在该数据) * @param oldParentId:被复制的父级栏目id(需要数据库原来存在该数据)
* @param newParentId:复制栏目后新父级的id(新插入数据的id) * @param newParentId:复制栏目后新父级的id(新插入数据的id)
* */ * */
private void recursionCopyChilds(String oldParentId, String newParentId) { private void recursionCopyChilds(String oldParentId, String newParentId) {
CategoryEntity _category = new CategoryEntity(); CategoryEntity _category = new CategoryEntity();
_category.setCategoryId(oldParentId); _category.setCategoryId(oldParentId);
List<CategoryEntity> childs = query(_category); List<CategoryEntity> childs = query(_category);
for (CategoryEntity child : childs) { for (CategoryEntity child : childs) {
String childId = child.getId(); String childId = child.getId();
//id、拼音和路径按照原来的业务逻辑生成 //id、拼音和路径按照原来的业务逻辑生成
child.setId(null); child.setId(null);
child.setCategoryPinyin(null); child.setCategoryPinyin(null);
child.setCategoryPath(null); child.setCategoryPath(null);
child.setCategoryId(newParentId); child.setCategoryId(newParentId);
saveEntity(child); saveEntity(child);
//如果该栏目下还有子栏目则继续复制该栏目里的子栏目 //如果该栏目下还有子栏目则继续复制该栏目里的子栏目
recursionCopyChilds(childId, child.getId()); recursionCopyChilds(childId, child.getId());
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment