大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全家桶1年46,售后保障稳定
@ApiOperation(value = "板块拖动排序")
@ApiImplicitParams({
@ApiImplicitParam(name = "plateId1", value = "拖拽体"),
@ApiImplicitParam(name = "plateId2", value = "参照体"),
@ApiImplicitParam(name = "type", value = "实现类型1交换位置2挤压位置")
})
@PostMapping("plateSort")
public ServiceResult<Object> plateSort(@RequestParam @NotNull Integer plateId1,
@RequestParam @NotNull Integer plateId2,
@RequestParam @NotNull @Max(2) @Min(1) Integer type) {
//元素原地拖动
if (plateId1.equals(plateId2)) {
return success();
}
LambdaQueryWrapper<TiebaPlate> wrapper = new LambdaQueryWrapper<>();
if (type == 1) {
//两个元素交换位置
wrapper
.eq(TiebaPlate::getId, plateId1)
.or()
.eq(TiebaPlate::getId, plateId2);
List<TiebaPlate> list = tiebaPlateService.list(wrapper);
int x = list.get(0).getOrderNum();
int y = list.get(1).getOrderNum();
list.get(0).setOrderNum(y);
list.get(1).setOrderNum(x);
//将collect更新到数据库
boolean result = tiebaPlateService.updateBatchById(list);
Assert.isTrue(result, "服务器异常");
return success();
} else if (type == 2) {
if (plateId1 < plateId2) {
//将plateId1位置拖到plateId2位置下面
wrapper
.between(TiebaPlate::getId, plateId1, plateId2);
List<TiebaPlate> list = tiebaPlateService.list(wrapper);
List<TiebaPlate> collect = list.stream().sorted(Comparator.comparingInt(TiebaPlate::getOrderNum)).collect(Collectors.toList());
//排序标记
int[] orderNums = collect.stream().mapToInt(TiebaPlate::getOrderNum).toArray();
//将首位元素挪到末尾
TiebaPlate first = collect.get(0);
collect.remove(first);
collect.add(first);
//将集合元素和排序标记绑定
for (int i = 0; i < orderNums.length; i++) {
collect.get(i).setOrderNum(orderNums[i]);
}
//将collect更新到数据库
boolean result = tiebaPlateService.updateBatchById(collect);
Assert.isTrue(result, "服务器异常");
return success();
} else {
//将plateId2位置拖到plateId1前面
wrapper
.between(TiebaPlate::getId, plateId2, plateId1);
List<TiebaPlate> list = tiebaPlateService.list(wrapper);
List<TiebaPlate> collect = list.stream().sorted(Comparator.comparingInt(TiebaPlate::getOrderNum)).collect(Collectors.toList());
//排序标记
int[] orderNums = collect.stream().mapToInt(TiebaPlate::getOrderNum).toArray();
//将末尾元素追加到首位
TiebaPlate last = collect.get(collect.size() - 1);
collect.remove(last);
Collections.reverse(collect);
collect.add(last);
Collections.reverse(collect);
//将集合元素和排序标记绑定
for (int i = 0; i < orderNums.length; i++) {
collect.get(i).setOrderNum(orderNums[i]);
}
//将collect更新到数据库
boolean result = tiebaPlateService.updateBatchById(collect);
Assert.isTrue(result, "服务器异常");
return success();
}
}
return failure();
}
思路:如何优雅的实现数据置顶、置尾、交换、拖动排序? – 简书
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/215507.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...