fix: count franchisee vehicle statistics by order
| | |
| | | result.put("goodTags", evaluation.get("goodTags")); |
| | | result.put("badTags", evaluation.get("badTags")); |
| | | result.put("traffic", franchiseeTraffic(orders)); |
| | | result.put("vehicleClass", franchiseeCarGroup(cars, TAppUserCar::getVehicleUse, "未知用途", 5)); |
| | | result.put("vehicleBrand", franchiseeCarGroup(cars, TAppUserCar::getVehicleBrand, "其他", 5)); |
| | | result.put("vehicleClass", franchiseeOrderCarGroup(orders, cars, TAppUserCar::getVehicleUse, "未知用途", 5)); |
| | | result.put("vehicleBrand", franchiseeOrderCarGroup(orders, cars, TAppUserCar::getVehicleBrand, "其他", 5)); |
| | | return R.ok(result); |
| | | } |
| | | |
| | |
| | | .map(entry -> namedCount(entry.getKey(), entry.getValue())).collect(Collectors.toList()); |
| | | } |
| | | |
| | | private List<Map<String, Object>> franchiseeOrderCarGroup(List<TChargingOrder> orders, |
| | | List<TAppUserCar> cars, |
| | | java.util.function.Function<TAppUserCar, String> getter, |
| | | String emptyName, |
| | | int limit) { |
| | | Map<Long, TAppUserCar> carsById = cars.stream() |
| | | .filter(car -> car.getId() != null) |
| | | .collect(Collectors.toMap(TAppUserCar::getId, car -> car, (left, right) -> left)); |
| | | Map<String, Long> counts = orders.stream().collect(Collectors.groupingBy(order -> { |
| | | TAppUserCar car = carsById.get(order.getAppUserCarId()); |
| | | String value = car == null ? null : getter.apply(car); |
| | | return value == null || value.trim().isEmpty() ? emptyName : value; |
| | | }, Collectors.counting())); |
| | | return counts.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByValue().reversed()).limit(limit) |
| | | .map(entry -> namedCount(entry.getKey(), entry.getValue())).collect(Collectors.toList()); |
| | | } |
| | | |
| | | private List<Map<String, Object>> namedCounts(String[] names, long[] values) { |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | for (int i = 0; i < names.length; i++) if (values[i] > 0) result.add(namedCount(names[i], values[i])); |
| | |
| | | import com.ruoyi.account.api.feignClient.AppUserClient; |
| | | import com.ruoyi.account.api.feignClient.AppUserTagClient; |
| | | import com.ruoyi.account.api.model.TAppUserTag; |
| | | import com.ruoyi.account.api.model.TAppUserCar; |
| | | import com.ruoyi.chargingPile.api.model.Site; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.order.api.model.TChargingOrder; |
| | |
| | | assertEquals("老用户", result.get(0).get("name")); |
| | | assertEquals(1L, result.get(0).get("value")); |
| | | } |
| | | |
| | | @Test |
| | | void franchiseeVehicleClassCountsOrdersInsteadOfDistinctVehicles() { |
| | | TAppUserCar privateCar = new TAppUserCar(); |
| | | privateCar.setId(1L); |
| | | privateCar.setVehicleUse("私家车"); |
| | | TAppUserCar companyCar = new TAppUserCar(); |
| | | companyCar.setId(2L); |
| | | companyCar.setVehicleUse("企业车"); |
| | | List<TChargingOrder> orders = new java.util.ArrayList<>(); |
| | | for (int i = 0; i < 10; i++) { |
| | | TChargingOrder order = new TChargingOrder(); |
| | | order.setAppUserCarId(1L); |
| | | orders.add(order); |
| | | } |
| | | for (int i = 0; i < 3; i++) { |
| | | TChargingOrder order = new TChargingOrder(); |
| | | order.setAppUserCarId(2L); |
| | | orders.add(order); |
| | | } |
| | | |
| | | @SuppressWarnings("unchecked") |
| | | List<java.util.Map<String, Object>> result = ReflectionTestUtils.invokeMethod( |
| | | new TChargingOrderController(), "franchiseeOrderCarGroup", orders, |
| | | Arrays.asList(privateCar, companyCar), |
| | | (java.util.function.Function<TAppUserCar, String>) TAppUserCar::getVehicleUse, |
| | | "未知用途", 5); |
| | | |
| | | assertEquals("私家车", result.get(0).get("name")); |
| | | assertEquals(10L, result.get(0).get("value")); |
| | | assertEquals("企业车", result.get(1).get("name")); |
| | | assertEquals(3L, result.get(1).get("value")); |
| | | } |
| | | } |