树的上下移

@SuppressWarnings({ "unchecked" })
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Response<Boolean> moveDown(String id) {
		// TODO Auto-generated method stub
		// 获取要下移的那条数据的信息
		ItemEntity entity = itemDao.findById(id);
		//获取需要移动的子节点 这些都是需要更新ranking
		List<ItemEntity> findChildren = itemDao.findChildren(id);
		// 查询上一条记录
		ItemEntity entityNext = itemDao.moveDown(entity.getDocumentId(), entity.getParentId(), entity.getRanking().toString());
		
		// 最下面的记录不能下移
		if (entityNext == null) {
			return new Response<Boolean>().failure("已到最后一条,不能下移");

		}
		List<ItemEntity> targetChildren = itemDao.findChildren(entityNext.getId());

		changeRanking(findChildren, targetChildren,false);
		return new Response<Boolean>().success(true);
	}



private void changeRanking(List<ItemEntity> currentChildren, List<ItemEntity> targetChildren, boolean flag) {
		if(currentChildren ==null || currentChildren.size() == 0|| targetChildren ==null || targetChildren.size() ==0 ){
			return;
		}
		BigDecimal maxranking = flag ? currentChildren.get(currentChildren.size()-1).getRanking() : targetChildren.get(targetChildren.size()-1).getRanking();
		BigDecimal minranking = flag ? targetChildren.get(0).getRanking() : currentChildren.get(0).getRanking();
		
		BigDecimal commonSub = (maxranking.subtract(minranking)).divide(new BigDecimal(currentChildren.size() +targetChildren.size()),2,BigDecimal.ROUND_DOWN);
		
		if(flag){
			currentChildren.addAll(targetChildren);
			for (int i = 0; i<currentChildren.size(); i++) {
				currentChildren.get(i).setRanking(minranking.add(commonSub.multiply(new BigDecimal(i))));
			}
			itemDao.batchUpdate(currentChildren);
			return;
		}
		targetChildren.addAll(currentChildren);
		for (int i = 0; i<targetChildren.size(); i++) {
			targetChildren.get(i).setRanking(minranking.add(commonSub.multiply(new BigDecimal(i))));
		}
		itemDao.batchUpdate(targetChildren);
	
	}

@SuppressWarnings("unchecked")
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Response<Boolean> moveUp(String id) {
		// TODO Auto-generated method stub
		// 获取要上移的那条数据的信息
		ItemEntity entity = itemDao.findById(id);
		// 查询上一条记录
		ItemEntity lastArticleEntity = itemDao.moveUp(entity.getDocumentId(), entity.getParentId(),
				entity.getRanking().toString());
		// 最上面的记录不能上移
		if (lastArticleEntity == null) {
			return new Response<Boolean>().failure("已到第一条,不能上移");
		}
		//获取需要移动的子节点 这些都是需要更新ranking
		List<ItemEntity> findChildren = itemDao.findChildren(id);
		List<ItemEntity> targetChildren = itemDao.findChildren(lastArticleEntity.getId());
		changeRanking(findChildren, targetChildren,true);
		return new Response<Boolean>().success(true);
	
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值