1编写ego_api
修改com.ego.dubbo.service.TbContentCategoryDubboService
int deleteById(Long id) throws DaoException;
2编写ego_provider
修改com.ego.dubbo.service.impl.TbContentCategoryDubboServiceImpl
@Override
@Transactional(rollbackFor = Exception.class)
public int deleteById(Long id) throws DaoException {
TbContentCategory category = new TbContentCategory();
category.setId(id);
Date date = new Date();
category.setUpdated(date);
category.setStatus(2);
// 当前分类逻辑删除功能
int index = tbContentCategoryMapper.updateByPrimaryKeySelective(category);
if(index==1){
// 判断当前节点的父节点是否还有其他正常状态的子节点。
// 查询当前节点,能查询出当前节点的父节点是谁
TbContentCategory currContentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
TbContentCategoryExample example = new TbContentCategoryExample();
example.createCriteria().andParentIdEqualTo(currContentCategory.getParentId()).andStatusEqualTo(1);
List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
// 父分类已经没有正常状态的子节点,修改is_parent为false
if(list!=null&&list.size()==0){
TbContentCategory parent = new TbContentCategory();
parent.setIsParent(false);
parent.setId(currContentCategory.getParentId());
parent.setUpdated(date);
int indexParent = tbContentCategoryMapper.updateByPrimaryKeySelective(parent);
if(indexParent!=1){
throw new DaoException("删除分类 - 修改父分类失败");
}
}
if(currContentCategory.getIsParent()){
deleteChildrenById(id,date);
}
return 1;
}
throw new DaoException("删除分类 - 失败");
}
/**
* 递归
* @param id 父id
* @param date 更新时间
* @throws DaoException
*/
private void deleteChildrenById(Long id, Date date) throws DaoException{
TbContentCategoryExample example = new TbContentCategoryExample();
example.createCriteria().andParentIdEqualTo(id);
List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
for(TbContentCategory category : list){
TbContentCategory updateCategory = new TbContentCategory();
updateCategory.setId(category.getId());
updateCategory.setStatus(2);
updateCategory.setUpdated(date);
int index = tbContentCategoryMapper.updateByPrimaryKeySelective(updateCategory);
if(index==1){
// 如果是父才继续进行修改子节点
if(category.getIsParent()) {
deleteChildrenById(category.getId(), date);
}
}else{
throw new DaoException("删除分类 - 更新分类状态失败");
}
}
}
3编写ego_manage
3.1修改service
修改com.ego.service.TbContentCategoryService及实现类
EgoResult delete(Long id);
@Override
public EgoResult delete(Long id) {
try {
int index = tbContentCategoryDubboService.deleteById(id);
if(index==1){
return EgoResult.ok();
}
} catch (DaoException e) {
e.printStackTrace();
}
return EgoResult.error(“删除失败”);
}
3.2修改controller
修改com.ego.controller.TbContentCategoryController
@RequestMapping(“/content/category/delete”)
public EgoResult delete(Long id) {
return tbContentCategoryService.delete(id);
}