Commit 7a6dd2db3c30eac906d04482e55e8628974be855
1 parent
5f0ac20f
手动分页功能代码重构;
Showing
4 changed files
with
67 additions
and
79 deletions
src/main/java/com/objecteye/service/impl/HumanVehicleAssociationServiceImpl.java
... | ... | @@ -303,66 +303,41 @@ public class HumanVehicleAssociationServiceImpl implements HumanVehicleAssociati |
303 | 303 | } |
304 | 304 | |
305 | 305 | double currentFea = compareDistance.getDistance(currentFeature); |
306 | - | |
307 | -// //遍历mongodb所有的车辆信息 | |
306 | + //遍历mongodb所有的车辆信息 | |
308 | 307 | List<PicVehicleCompare> pvc = new ArrayList<PicVehicleCompare>(); |
309 | - | |
310 | - int numberCount = 0; | |
311 | - for (int i = 0; i < allRabbitMQVehicleList.size(); i++) { | |
312 | - numberCount++; | |
313 | - RabbitMQVehicle rmqv = allRabbitMQVehicleList.get(i); | |
314 | - int vehicle_special_type = rmqv.getVehicle_special_type(); | |
315 | - | |
316 | - double[] dbFeature = rmqv.getVehicle_fea_feature(); | |
308 | + for (RabbitMQVehicle rabbitMQVehicle : allRabbitMQVehicleList) { | |
309 | + int vehicle_special_type = rabbitMQVehicle.getVehicle_special_type(); | |
310 | + double[] dbFeature = rabbitMQVehicle.getVehicle_fea_feature(); | |
317 | 311 | //进行比对 |
318 | 312 | double dbFea = compareDistance.getDistance(dbFeature); |
319 | 313 | double similar = compareDistance.simDistance(currentFeature, currentFea, dbFeature, dbFea); |
320 | 314 | if (threshold <= similar) { |
321 | 315 | similar = (double) Math.round(similar * 100) / 100; |
322 | 316 | similar = similar >= 1 ? 1 : similar; |
323 | - | |
324 | 317 | PicVehicleCompare picVehicleCompare = new PicVehicleCompare(); |
325 | 318 | picVehicleCompare.setSimilar(similar); |
326 | - picVehicleCompare.setRabbitMQVehicle(rmqv); | |
319 | + picVehicleCompare.setRabbitMQVehicle(rabbitMQVehicle); | |
327 | 320 | pvc.add(picVehicleCompare); |
328 | 321 | } |
329 | - | |
330 | 322 | } |
331 | 323 | |
332 | 324 | //对row进行排序 |
333 | - pvc.sort(new Comparator<PicVehicleCompare>() { | |
334 | - @Override | |
335 | - public int compare(PicVehicleCompare o1, PicVehicleCompare o2) { | |
336 | - if (o2.getSimilar() == o1.getSimilar()) { | |
337 | - return 0; | |
338 | - } else { | |
339 | - return (o2.getSimilar() > o1.getSimilar()) ? 1 : -1; | |
340 | - } | |
325 | + pvc.sort((o1, o2) -> { | |
326 | + if (o2.getSimilar() == o1.getSimilar()) { | |
327 | + return 0; | |
328 | + } else { | |
329 | + return (o2.getSimilar() > o1.getSimilar()) ? 1 : -1; | |
341 | 330 | } |
342 | 331 | }); |
343 | 332 | |
344 | 333 | //对row进行分页查询 |
345 | - ArrayList<PicVehicleCompare> picVehicleComparesNew = new ArrayList<>(); | |
346 | - int startNumber = (currentpage - 1) * pagevolume; | |
347 | - int endNumber = startNumber + pagevolume; | |
348 | - endNumber = pagevolume > (pvc.size() - startNumber) ? pvc.size() : endNumber; | |
349 | - for (int m = startNumber; m < endNumber; m++) { | |
350 | - PicVehicleCompare picVehicleCompare = pvc.get(m); | |
351 | - picVehicleComparesNew.add(picVehicleCompare); | |
352 | - } | |
353 | - | |
354 | - //获取total(总条数) | |
355 | - int total = pvc.size(); | |
334 | + List<PicVehicleCompare> picVehicleComparesNew = UserTools.getPagedResultByBaseList(pvc, currentpage, pagevolume); | |
356 | 335 | //根据总条数获取总页数 |
357 | - int totalPage = (int) Math.ceil(((double) total) / pagevolume); | |
358 | - | |
359 | - data = new JSONObject(); | |
336 | + int totalPage = UserTools.getPageTotalByBaseList(pvc, pagevolume); | |
360 | 337 | |
361 | 338 | //获取row数据 |
362 | 339 | JSONArray row = new JSONArray(); |
363 | - Iterator<PicVehicleCompare> it = picVehicleComparesNew.iterator(); | |
364 | - while (it.hasNext()) { | |
365 | - PicVehicleCompare next = it.next(); | |
340 | + for (PicVehicleCompare next : picVehicleComparesNew) { | |
366 | 341 | JSONObject rowData = new JSONObject(); |
367 | 342 | //获取id |
368 | 343 | RabbitMQVehicle rabbitMQVehicle1 = next.getRabbitMQVehicle(); |
... | ... | @@ -381,7 +356,8 @@ public class HumanVehicleAssociationServiceImpl implements HumanVehicleAssociati |
381 | 356 | rowData.put("picurl", rabbitMQVehicle1.getPicurl()); |
382 | 357 | row.add(rowData); |
383 | 358 | } |
384 | - if (total > 0 && row != null && row.size() > 0) { | |
359 | + data = new JSONObject(); | |
360 | + if (pvc.size() > 0 && row.size() > 0) { | |
385 | 361 | data.put("total", totalPage); |
386 | 362 | data.put("row", row); |
387 | 363 | } | ... | ... |
src/main/java/com/objecteye/service/impl/SpecialtyServicesImpl.java
... | ... | @@ -129,15 +129,9 @@ public class SpecialtyServicesImpl implements SpecialtyServices { |
129 | 129 | }); |
130 | 130 | |
131 | 131 | //对row进行分页查询 |
132 | - int startNumber = (currentpage - 1) * pagevolume; | |
133 | - int endNumber = startNumber + pagevolume; | |
134 | - endNumber = Math.min(endNumber, pvc.size()); | |
135 | - List<PicVehicleCompare> picVehicleComparesNew = pvc.subList(startNumber, endNumber); | |
136 | - | |
137 | - //获取total(总条数) | |
138 | - int total = pvc.size(); | |
139 | - //根据总条数获取总页数 | |
140 | - int totalPage = (int) Math.ceil(((double) total) / pagevolume); | |
132 | + List<PicVehicleCompare> picVehicleComparesNew = UserTools.getPagedResultByBaseList(pvc, currentpage, pagevolume); | |
133 | + //总页数 | |
134 | + int totalPage = UserTools.getPageTotalByBaseList(pvc, pagevolume); | |
141 | 135 | |
142 | 136 | JSONObject data = new JSONObject(); |
143 | 137 | |
... | ... | @@ -163,14 +157,14 @@ public class SpecialtyServicesImpl implements SpecialtyServices { |
163 | 157 | |
164 | 158 | data.put("total", totalPage); |
165 | 159 | data.put("row", row); |
166 | - if (total > 0 && row.size() > 0) { | |
160 | + if (pvc.size() > 0 && row.size() > 0) { | |
167 | 161 | result.put("code", 200); |
168 | 162 | result.put("message", "操作成功"); |
169 | 163 | result.put("data", data); |
170 | 164 | } else { |
171 | 165 | result.put("code", 201); |
172 | 166 | result.put("message", "没有符合要求的数据"); |
173 | - data.put("total", total); | |
167 | + data.put("total", pvc.size()); | |
174 | 168 | data.put("row", row); |
175 | 169 | result.put("data", data); |
176 | 170 | } | ... | ... |
src/main/java/com/objecteye/service/impl/VehicleCurrencyServiceImpl.java
... | ... | @@ -273,19 +273,9 @@ public class VehicleCurrencyServiceImpl implements VehicleCurrencyService { |
273 | 273 | }); |
274 | 274 | |
275 | 275 | //对row进行分页查询 |
276 | - ArrayList<PicVehicleCompare> picVehicleComparesNew = new ArrayList<>(); | |
277 | - int startNumber = (currentpage - 1) * pagevolume; | |
278 | - int endNumber = startNumber + pagevolume; | |
279 | - endNumber = pagevolume > (pvc.size() - startNumber) ? pvc.size() : endNumber; | |
280 | - for (int m = startNumber; m < endNumber; m++) { | |
281 | - PicVehicleCompare picVehicleCompare = pvc.get(m); | |
282 | - picVehicleComparesNew.add(picVehicleCompare); | |
283 | - } | |
284 | - | |
285 | - //获取total(总条数) | |
286 | - int total = pvc.size(); | |
287 | - //根据总条数获取总页数 | |
288 | - int totalPage = (int) Math.ceil(((double) total) / pagevolume); | |
276 | + List<PicVehicleCompare> picVehicleComparesNew = UserTools.getPagedResultByBaseList(pvc, currentpage, pagevolume); | |
277 | + //总页数 | |
278 | + int totalPage = UserTools.getPageTotalByBaseList(pvc, pagevolume); | |
289 | 279 | |
290 | 280 | ArrayList<PicVehicleRow> picVehicleRows = new ArrayList<>(); |
291 | 281 | for (PicVehicleCompare next : picVehicleComparesNew) { |
... | ... | @@ -294,12 +284,9 @@ public class VehicleCurrencyServiceImpl implements VehicleCurrencyService { |
294 | 284 | // 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN |
295 | 285 | double similar = next.getSimilar(); |
296 | 286 | String picurl = rabbitMqVehicle1.getPicurl(); |
297 | - | |
298 | 287 | //获取到快照路径 |
299 | 288 | String snapshoturl = rabbitMqVehicle1.getSnapshoturl(); |
300 | - | |
301 | 289 | String bg = String.format("%.3f", similar); |
302 | - | |
303 | 290 | //因为需要将快照图的值给picurl所以需要下面的返回方式 |
304 | 291 | PicVehicleRow picVehicleRow = new PicVehicleRow(rabbitMqVehicle1.getId(), rabbitMqVehicle1.getRecordid(), rabbitMqVehicle1.getVehicle_plate_hphm(), |
305 | 292 | snapshoturl, picurl, bg, |
... | ... | @@ -309,7 +296,6 @@ public class VehicleCurrencyServiceImpl implements VehicleCurrencyService { |
309 | 296 | } |
310 | 297 | |
311 | 298 | PicVehicleDataResult picVehicleDataResult = new PicVehicleDataResult(totalPage, picVehicleRows); |
312 | - | |
313 | 299 | if (picVehicleDataResult.getRow().size() <= 0) { |
314 | 300 | return CommonResult.success(201, "没有查询到任何数据", null); |
315 | 301 | } else { | ... | ... |
src/main/java/com/objecteye/utils/UserTools.java
... | ... | @@ -28,30 +28,62 @@ public class UserTools { |
28 | 28 | * 统一手动分页 |
29 | 29 | * |
30 | 30 | * @param baseList 原始数据集合 |
31 | - * @param currentpage 页码 | |
32 | - * @param pagevolume 页面容量 | |
31 | + * @param currentPage 页码 | |
32 | + * @param pageVolume 页面容量 | |
33 | 33 | * @return 结果集 |
34 | 34 | */ |
35 | - public static PageResult makePageResultByBaseList(List<?> baseList, Integer currentpage, Integer pagevolume) { | |
36 | - // 总页数 | |
37 | - int pageTotal = (baseList.size() / pagevolume) + ((baseList.size() % pagevolume > 0) ? 1 : 0); | |
35 | + public static PageResult makePageResultByBaseList(List baseList, int currentPage, int pageVolume) { | |
36 | + return new PageResult<>(getPageTotalByBaseList(baseList, pageVolume), | |
37 | + getPagedResultByBaseList(baseList, currentPage, pageVolume)); | |
38 | + } | |
39 | + | |
40 | + /** | |
41 | + * 手动分页获取页码数量 | |
42 | + * | |
43 | + * @param baseList 原始数据集合 | |
44 | + * @param pageVolume 页面容量 | |
45 | + * @return 页数 | |
46 | + */ | |
47 | + public static int getPageTotalByBaseList(List baseList, int pageVolume) { | |
48 | + return getPageTotalByBaseList(baseList.size(), pageVolume); | |
49 | + } | |
50 | + | |
51 | + /** | |
52 | + * 手动分页获取页码数量 | |
53 | + * | |
54 | + * @param size 集合数量 | |
55 | + * @param pageVolume 页面容量 | |
56 | + * @return 页数 | |
57 | + */ | |
58 | + public static int getPageTotalByBaseList(int size, int pageVolume) { | |
59 | + return (size / pageVolume) + ((size % pageVolume > 0) ? 1 : 0); | |
60 | + } | |
38 | 61 | |
39 | - int fromIndex = (currentpage - 1) * pagevolume; | |
62 | + /** | |
63 | + * 手动获取分页对应页面的数据 | |
64 | + * | |
65 | + * @param baseList 原始数据集合 | |
66 | + * @param currentPage 页码 | |
67 | + * @param pageVolume 页面容量 | |
68 | + * @return 数据集合 | |
69 | + */ | |
70 | + public static List getPagedResultByBaseList(List baseList, int currentPage, int pageVolume) { | |
71 | + int pageTotal = getPageTotalByBaseList(baseList, pageVolume); | |
72 | + int fromIndex = (currentPage - 1) * pageVolume; | |
40 | 73 | int toIndex; |
41 | - if (currentpage == pageTotal) { | |
74 | + if (currentPage == pageTotal) { | |
42 | 75 | toIndex = baseList.size(); |
43 | 76 | } else if (pageTotal == 0) { |
44 | - return new PageResult<>(pageTotal, new ArrayList<>()); | |
77 | + return new ArrayList<>(); | |
45 | 78 | } else { |
46 | - toIndex = currentpage * pagevolume; | |
79 | + toIndex = currentPage * pageVolume; | |
47 | 80 | if (toIndex > baseList.size()) { |
48 | 81 | toIndex = baseList.size(); |
49 | 82 | } |
50 | 83 | if (fromIndex >= toIndex) { |
51 | - return new PageResult<>(pageTotal, new ArrayList<>()); | |
84 | + return new ArrayList<>(); | |
52 | 85 | } |
53 | 86 | } |
54 | - | |
55 | - return new PageResult<>(pageTotal, baseList.subList(fromIndex, toIndex)); | |
87 | + return baseList.subList(fromIndex, toIndex); | |
56 | 88 | } |
57 | 89 | } | ... | ... |