mybaitis分级删除

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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值