今天遇到一个需求,通过前端页面动态设置后台数据的排序,本来以为很容易,谁知花了一上午才实现,低估了,作为一个老程序,耻辱呀。
首先需要排序的数据库表中有一个字段,是int类型的。
先看效果图
主要是从三个地方入手来完成排序。
添加数据
第一添加数据的时候,把当前添加的数据的优先级设置为最大,逻辑是从数据库中取出最大的优先级,然后加一,赋值给当前数据
@RequestMapping("/addItem")
@ResponseBody
public ResponseData addItem(HistoryInfoParam historyInfoParam) {
//查询最大优先级别
Integer priority=0;
List<HistoryInfoResult> historyInfoResults=this.historyInfoService.findListBySpec(null);
if (historyInfoResults!=null&&historyInfoResults.size()>0)
{
priority=historyInfoResults.stream().filter(o -> o.getPriority() != null).map(HistoryInfoResult::getPriority).distinct().max((e1, e2) -> e1.compareTo(e2)).get();
priority=priority+1;
}
historyInfoParam.setPriority(priority);
historyInfoParam.setTownCode(LoginContextHolder.getContext().getUser().getTownCode());
historyInfoParam.setTownName(LoginContextHolder.getContext().getUser().getTownName());
historyInfoParam.setCreateUserName(LoginContextHolder.getContext().getUser().getUsername());
historyInfoParam.setCreateTime(new Date());
this.historyInfoService.add(historyInfoParam);
return ResponseData.success();
}
2.向下移动
@RequestMapping("/updateDown")
@ResponseBody
public ResponseData updateDown(HistoryInfoParam historyInfoParam) {
List<HistoryInfoResult> historyInfoResults=this.historyInfoService.findListBySpec(null);
List<HistoryInfoResult> historyInfoResults1=historyInfoResults.stream().filter(p->p.getPriority()<historyInfoParam.getPriority()).sorted(Comparator.comparing(HistoryInfoResult::getPriority).reversed()).collect(Collectors.toList());
if (historyInfoResults1==null||historyInfoResults1.size()==0)
{
historyInfoParam.setPriority(historyInfoParam.getPriority()-1);
}
else
{
historyInfoParam.setPriority(historyInfoResults1.get(0).getPriority()-1);
HistoryInfoParam historyInfoParam1=new HistoryInfoParam();
historyInfoParam1.setId(historyInfoResults1.get(0).getId());
historyInfoParam1.setPriority(historyInfoResults1.get(0).getPriority()+1);
this.historyInfoService.updatePriority(historyInfoParam1);
for (HistoryInfoResult historyInfoResult:historyInfoResults1)
{
if (!historyInfoResult.getId().equals(historyInfoParam1.getId()))
{
HistoryInfoParam historyInfoParam2=new HistoryInfoParam();
historyInfoParam2.setId(historyInfoResult.getId());
historyInfoParam2.setPriority(historyInfoResult.getPriority()-1);
this.historyInfoService.updatePriority(historyInfoParam2);
}
}
}
this.historyInfoService.updatePriority(historyInfoParam);
return ResponseData.success();
}
3.向上移动
@RequestMapping("/updateUp")
@ResponseBody
public ResponseData updateUp(HistoryInfoParam historyInfoParam) {
List<HistoryInfoResult> historyInfoResults=this.historyInfoService.findListBySpec(null);
List<HistoryInfoResult> historyInfoResults1=historyInfoResults.stream().filter(p->p.getPriority()>historyInfoParam.getPriority()).sorted(Comparator.comparing(HistoryInfoResult::getPriority)).collect(Collectors.toList());
if (historyInfoResults1==null||historyInfoResults1.size()==0)
{
historyInfoParam.setPriority(historyInfoParam.getPriority()+1);
}
else
{
historyInfoParam.setPriority(historyInfoResults1.get(0).getPriority()+1);
HistoryInfoParam historyInfoParam1=new HistoryInfoParam();
historyInfoParam1.setId(historyInfoResults1.get(0).getId());
historyInfoParam1.setPriority(historyInfoResults1.get(0).getPriority()-1);
this.historyInfoService.updatePriority(historyInfoParam1);
for (HistoryInfoResult historyInfoResult:historyInfoResults1)
{
if (!historyInfoResult.getId().equals(historyInfoParam1.getId()))
{
HistoryInfoParam historyInfoParam2=new HistoryInfoParam();
historyInfoParam2.setId(historyInfoResult.getId());
historyInfoParam2.setPriority(historyInfoResult.getPriority()+1);
this.historyInfoService.updatePriority(historyInfoParam2);
}
}
}
this.historyInfoService.updatePriority(historyInfoParam);
return ResponseData.success();
}
其实这个功能,看似简单,实则有点复杂,真是活到老,学到老呀。