1.先贴结果:
{
"success": true,
"status": 200,
"message": "获取成功",
"data": [
{
"id": null,
"countNum": null,
"countTime": null,
"itemName": "电表1312",
"maintainCountNum": 6,
"reportCountNum": null,
"companyId": null,
"startTime": null,
"endTime": null,
"twocodeType": null,
"maintainOrWarning": null,
"number": 6
},
{
"id": null,
"countNum": null,
"countTime": null,
"itemName": "鼠标",
"maintainCountNum": null,
"reportCountNum": null,
"companyId": null,
"startTime": null,
"endTime": null,
"twocodeType": null,
"maintainOrWarning": null,
"number": 0
},
{
"id": null,
"countNum": null,
"countTime": null,
"itemName": "温度计",
"maintainCountNum": null,
"reportCountNum": null,
"companyId": null,
"startTime": null,
"endTime": null,
"twocodeType": null,
"maintainOrWarning": null,
"number": 0
},
{
"id": null,
"countNum": null,
"countTime": null,
"itemName": "6组出水检修阀\n",
"maintainCountNum": null,
"reportCountNum": null,
"companyId": null,
"startTime": null,
"endTime": null,
"twocodeType": null,
"maintainOrWarning": null,
"number": 0
},
{
"id": null,
"countNum": null,
"countTime": null,
"itemName": "2出水检修阀\n",
"maintainCountNum": null,
"reportCountNum": null,
"companyId": null,
"startTime": null,
"endTime": null,
"twocodeType": null,
"maintainOrWarning": null,
"number": 0
}
]
}
2、使用方法 java8新特性:
备注:把topStatisticsList中的对象StatisticsVO按照对象的属性Number从大到小排序
//按照number从大到小排序
topStatisticsList.sort(Comparator.comparing(StatisticsVO::getNumber).reversed());
如果想从小到大
//按照number从大到小排序
topStatisticsList.sort(Comparator.comparing(StatisticsVO::getNumber));
截取前五个;
topStatisticsList.subList(0,5)
// start,end分别是第几个到第几个,strat和end为索引,从0开始
list = list.subList(start, end);
1.需求:工作中遇到这么一个问题:返回运维、告警记录前五的设备或者场地或者全部的(设备和场地)
2.步骤:
1)获取list ;
2)遍历list,把运维数 和 告警数之后 赋值给对象的number;
3) 把list按照对象的number 从大到小排序;
4)截取list前五个;
5.)业务逻辑:
Impl层
@Service("*****")
@Transactional
public class StatisticsServiceImpl implements StatisticsService {
@Autowired
private StatisticsDao statisticsDao;
public Result topStatistics(StatisticsVO statisticsVO) throws Exception {
List<StatisticsVO> topStatisticsList=statisticsDao.findTopStatistics(statisticsVO);
System.out.println(topStatisticsList);
List<Long> list = new ArrayList<>();
for (int i = 0; i < topStatisticsList.size(); i++) {
if (topStatisticsList.get(i).getMaintainCountNum()!=null){ //运维数据不为null
if (topStatisticsList.get(i).getReportCountNum() !=null){ //告警数据不为null
Long num = topStatisticsList.get(i).getMaintainCountNum() + topStatisticsList.get(i).getReportCountNum(); //运维数据+告警数据
list.add(num);
topStatisticsList.get(i).setNumber(num);
}else {
Long num = topStatisticsList.get(i).getMaintainCountNum() ; //告警数据为null 结果为运维数据
list.add(num);
topStatisticsList.get(i).setNumber(num);
}
}else if(topStatisticsList.get(i).getReportCountNum() !=null){ //运维数据为null 告警数据不为null
Long num = topStatisticsList.get(i).getReportCountNum() ;
list.add(num); //结果为告警数据
topStatisticsList.get(i).setNumber(num);
}else{
Long num = Long.valueOf(0); //都为空 结果为0
list.add(num);
topStatisticsList.get(i).setNumber(num);
}
}
Collections.reverse(topStatisticsList);
topStatisticsList.sort(Comparator.comparing(StatisticsVO::getNumber).reversed()); //按照number从大到小排序
// System.out.println(map);
// map.put("topStatisticsList",topStatisticsList);
return new Result(200,"获取成功",true,topStatisticsList.subList(0,5)); //截取前五个数据
}
}
补充说明:本来想的是直接在mysql中控制从大到小,但是想了好久,sql一直有问题,就换了一个思路,代码质量有点低,各位大佬有好的方法,请给教学一下,谢谢