| | |
| | | import com.ruoyi.chargingPile.api.model.TChargingGun; |
| | | import com.ruoyi.chargingPile.api.model.TChargingPile; |
| | | import com.ruoyi.chargingPile.api.model.TParkingRecord; |
| | | import com.ruoyi.chargingPile.api.model.TPartnerAppUser; |
| | | import com.ruoyi.chargingPile.api.vo.GetChargingGunByChargingPileIdsVo; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.dto.ChargingOrderGroup; |
| | |
| | | LocalDateTime[] range = franchiseeRange(params); |
| | | List<FranchiseeDayStat> dayStats = franchiseeDayStats(siteIds, range); |
| | | Map<String, Object> summary = new LinkedHashMap<>(); |
| | | summary.put("orderAmountWan", sum(dayStats, FranchiseeDayStat::getOrderAmount).divide(BigDecimal.valueOf(10000), 4, RoundingMode.HALF_UP)); |
| | | summary.put("orderAmountWan", sum(dayStats, FranchiseeDayStat::getChargingIncome).divide(BigDecimal.valueOf(10000), 4, RoundingMode.HALF_UP)); |
| | | summary.put("serviceFeeWan", sum(dayStats, FranchiseeDayStat::getServiceFee).divide(BigDecimal.valueOf(10000), 4, RoundingMode.HALF_UP)); |
| | | summary.put("chargingPowerWan", sum(dayStats, FranchiseeDayStat::getCapacity).divide(BigDecimal.valueOf(10000), 4, RoundingMode.HALF_UP)); |
| | | summary.put("chargingAmount", sum(dayStats, FranchiseeDayStat::getPaymentAmount)); |
| | | summary.put("chargingAmount", sum(dayStats, FranchiseeDayStat::getChargingIncome)); |
| | | summary.put("chargingOrderCount", dayStats.stream().mapToLong(stat -> numberValue(stat.getOrderCount())).sum()); |
| | | summary.put("powerUtilizationRate", observedPowerUtilization(dayStats)); |
| | | summary.put("demandPowerSatisfactionRate", demandPowerSatisfaction(dayStats)); |
| | | Map<String, Object> result = new LinkedHashMap<>(); |
| | | result.put("summary", summary); |
| | | result.put("keyDataLine", franchiseeDailyTrend(dayStats, range, "订单金额", FranchiseeDayStat::getOrderAmountEffective)); |
| | | result.put("keyDataLine", franchiseeDailyTrend(dayStats, range, "收入合计", FranchiseeDayStat::getChargingIncome)); |
| | | result.put("keyDataRing", franchiseePhaseRing(siteIds, range)); |
| | | result.put("orderStats", franchiseeOrderTrend(dayStats, range)); |
| | | result.put("efficiency", franchiseeEfficiencyTrend(dayStats, range)); |
| | |
| | | @PostMapping("/app/franchisee/income") |
| | | public R<Map<String, Object>> franchiseeIncome(@RequestBody(required = false) Map<String, Object> params) { |
| | | List<Integer> siteIds = franchiseeSiteIds(params); |
| | | LocalDate firstMonth = YearMonth.now().minusMonths(5).atDay(1); |
| | | LocalDate nextMonth = YearMonth.now().plusMonths(1).atDay(1); |
| | | List<FranchiseeDayStat> dayStats = franchiseeDayStats(siteIds, |
| | | new LocalDateTime[]{firstMonth.atStartOfDay(), nextMonth.atStartOfDay()}); |
| | | List<SixChargingDto> sixChargingDtos; |
| | | if (siteIds.isEmpty()) { |
| | | // 无授权站点:返回近6月全零骨架,避免空 IN 导致全站点统计 |
| | | sixChargingDtos = generateLastSixMonths(LocalDate.now()); |
| | | } else { |
| | | // 与后台 数据分析-平台总收入分析 电站收入分析口径一致:历史月读结算账单、当月补原始订单 |
| | | LocalDate sixBefore = PreviousSixMonths.get(); |
| | | List<SixChargingDto> chargingDtos = chargingOrderService.charge(sixBefore, siteIds, false); |
| | | LocalDate statisticsEndMonth = resolveStatisticsEndMonth(LocalDate.now(), chargingDtos); |
| | | sixChargingDtos = generateLastSixMonths(statisticsEndMonth); |
| | | for (SixChargingDto sixChargingDto : sixChargingDtos) { |
| | | for (SixChargingDto chargingDto : chargingDtos) { |
| | | if (sixChargingDto.getMonth().equals(chargingDto.getMonth())) { |
| | | BeanUtils.copyProperties(chargingDto, sixChargingDto); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | BigDecimal electricity = sixChargingDtos.stream() |
| | | .filter(Objects::nonNull) |
| | | .map(SixChargingDto::getElectrovalence) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | BigDecimal service = sixChargingDtos.stream() |
| | | .filter(Objects::nonNull) |
| | | .map(SixChargingDto::getServiceCharge) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | Map<String, Object> result = new LinkedHashMap<>(); |
| | | result.put("totalIncome", sum(dayStats, FranchiseeDayStat::getPaymentAmount)); |
| | | result.put("electricityFee", sum(dayStats, FranchiseeDayStat::getElectrovalence)); |
| | | result.put("serviceFee", sum(dayStats, FranchiseeDayStat::getServiceFee)); |
| | | result.put("incomeColumn", franchiseeIncomeTrend(dayStats, firstMonth, 6)); |
| | | result.put("totalIncome", electricity.add(service)); |
| | | result.put("electricityFee", electricity); |
| | | result.put("serviceFee", service); |
| | | result.put("incomeColumn", franchiseeIncomeTrendFromSix(sixChargingDtos)); |
| | | List<Map<String, Object>> incomePie = new ArrayList<>(); |
| | | Map<String, Object> electricityItem = new LinkedHashMap<>(); |
| | | electricityItem.put("name", "充电费"); |
| | | electricityItem.put("value", sum(dayStats, FranchiseeDayStat::getElectrovalence)); |
| | | electricityItem.put("value", electricity); |
| | | incomePie.add(electricityItem); |
| | | Map<String, Object> serviceItem = new LinkedHashMap<>(); |
| | | serviceItem.put("name", "服务费"); |
| | | serviceItem.put("value", sum(dayStats, FranchiseeDayStat::getServiceFee)); |
| | | serviceItem.put("value", service); |
| | | incomePie.add(serviceItem); |
| | | result.put("incomePie", incomePie); |
| | | return R.ok(result); |
| | |
| | | } |
| | | chargingUsersByDay.computeIfAbsent(agg.getStatDay(), key -> new HashSet<>()).add(agg.getAppUserId()); |
| | | } |
| | | List<TPartnerAppUser> relations = Collections.emptyList(); |
| | | if (partner != null && partner.getId() != null) { |
| | | R<List<TPartnerAppUser>> relationResult = partnerClient.getBoundAppUserRelations(partner.getId()); |
| | | if (relationResult != null && relationResult.getData() != null) { |
| | | relations = relationResult.getData(); |
| | | Map<LocalDate, Long> newUsersByDay = new LinkedHashMap<>(); |
| | | if (range != null && range[0] != null && range[1] != null) { |
| | | List<FranchiseeNewUserStat> newUserStats = chargingOrderMapper.franchiseeNewUserStats(range[0], range[1]); |
| | | if (newUserStats != null) { |
| | | for (FranchiseeNewUserStat stat : newUserStats) { |
| | | if (stat != null && stat.getStatDay() != null && stat.getUserCount() != null) { |
| | | newUsersByDay.put(stat.getStatDay(), stat.getUserCount()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | FranchiseeUserStatistics.Result userStatistics = FranchiseeUserStatistics.calculateByUserDays( |
| | | chargingUsersByDay, relations, range[0], range[1]); |
| | | chargingUsersByDay, newUsersByDay, range[0], range[1]); |
| | | |
| | | Set<Long> users = aggs.stream().map(FranchiseeUserAgg::getAppUserId).filter(Objects::nonNull).collect(Collectors.toSet()); |
| | | List<TAppUser> appUsers = Collections.emptyList(); |
| | |
| | | List<Integer> siteIds = franchiseeSiteIds(params); |
| | | LocalDateTime[] range = franchiseeRange(params); |
| | | ChargingStatisticsQueryDto query = new ChargingStatisticsQueryDto(); |
| | | query.setDayType(5); |
| | | query.setStartTime(range[0].toLocalDate()); |
| | | query.setEndTime(range[1].toLocalDate().minusDays(1)); |
| | | String dateType = params == null ? "today" : String.valueOf(params.getOrDefault("dateType", "today")); |
| | | int dayType = "week".equals(dateType) ? 2 : "month".equals(dateType) ? 3 : "year".equals(dateType) ? 4 : "custom".equals(dateType) ? 5 : 1; |
| | | query.setDayType(dayType); |
| | | if (dayType == 5) { |
| | | query.setStartTime(range[0].toLocalDate()); |
| | | query.setEndTime(range[1].toLocalDate().minusDays(1)); |
| | | } |
| | | |
| | | List<Map<String, Object>> dcAvailable = safeList(chargingOrderService.equipmentUserType1(siteIds, query)); |
| | | List<Map<String, Object>> acAvailable = safeList(chargingOrderService.equipmentUserType2(siteIds, query)); |
| | |
| | | |
| | | private Map<String, Object> operationChart(List<Map<String, Object>> rows, String name) { |
| | | List<String> categories = rows.stream().map(row -> { |
| | | String siteName = row.get("siteName") == null ? "" : String.valueOf(row.get("siteName")); |
| | | String gunName = row.get("name") == null ? "" : String.valueOf(row.get("name")); |
| | | return siteName + gunName; |
| | | return StringUtils.isBlank(gunName) ? "未命名充电枪" : gunName; |
| | | }).collect(Collectors.toList()); |
| | | List<Object> values = rows.stream().map(row -> row.getOrDefault("percent", "0")).collect(Collectors.toList()); |
| | | Map<String, Object> chart = new LinkedHashMap<>(); |
| | |
| | | List<Map<String, Object>> data = new ArrayList<>(); for (int i = 0; i < names.length; i++) { Map<String, Object> item = new LinkedHashMap<>(); item.put("name", names[i]); item.put("value", counts[i]); data.add(item); } |
| | | return Collections.singletonMap("series", Collections.singletonList(Collections.singletonMap("data", data))); |
| | | } |
| | | private Map<String, Object> franchiseeIncomeTrend(List<FranchiseeDayStat> stats, LocalDate firstMonth, int monthCount) { |
| | | /** 收支分析近6月柱状图:与后台 电站收入分析 一致(历史月结算账单 + 当月原始订单)。 */ |
| | | private Map<String, Object> franchiseeIncomeTrendFromSix(List<SixChargingDto> months) { |
| | | List<String> categories = new ArrayList<>(); |
| | | List<BigDecimal> electricity = new ArrayList<>(), service = new ArrayList<>(); |
| | | Map<YearMonth, List<FranchiseeDayStat>> byMonth = stats.stream().filter(stat -> stat != null && stat.getStatDay() != null) |
| | | .collect(Collectors.groupingBy(stat -> YearMonth.from(stat.getStatDay()))); |
| | | for (int i = 0; i < monthCount; i++) { |
| | | YearMonth month = YearMonth.from(firstMonth).plusMonths(i); |
| | | categories.add(String.format("%02d月", month.getMonthValue())); |
| | | List<FranchiseeDayStat> monthStats = byMonth.getOrDefault(month, Collections.emptyList()); |
| | | electricity.add(sum(monthStats, FranchiseeDayStat::getElectrovalence)); |
| | | service.add(sum(monthStats, FranchiseeDayStat::getServiceFee)); |
| | | for (SixChargingDto month : months) { |
| | | if (month == null) { |
| | | continue; |
| | | } |
| | | categories.add(month.getMonth() + "月"); |
| | | electricity.add(month.getElectrovalence() == null ? BigDecimal.ZERO : month.getElectrovalence()); |
| | | service.add(month.getServiceCharge() == null ? BigDecimal.ZERO : month.getServiceCharge()); |
| | | } |
| | | Map<String, Object> chart = new LinkedHashMap<>(); chart.put("categories", categories); |
| | | Map<String, Object> electricitySeries = new LinkedHashMap<>(); electricitySeries.put("name", "充电费"); electricitySeries.put("data", electricity); |
| | | Map<String, Object> serviceSeries = new LinkedHashMap<>(); serviceSeries.put("name", "服务费"); serviceSeries.put("data", service); |
| | | chart.put("series", new ArrayList<>(Arrays.asList(electricitySeries, serviceSeries))); return chart; |
| | | Map<String, Object> chart = new LinkedHashMap<>(); |
| | | chart.put("categories", categories); |
| | | Map<String, Object> electricitySeries = new LinkedHashMap<>(); |
| | | electricitySeries.put("name", "充电费"); |
| | | electricitySeries.put("data", electricity); |
| | | Map<String, Object> serviceSeries = new LinkedHashMap<>(); |
| | | serviceSeries.put("name", "服务费"); |
| | | serviceSeries.put("data", service); |
| | | chart.put("series", new ArrayList<>(Arrays.asList(electricitySeries, serviceSeries))); |
| | | return chart; |
| | | } |
| | | |
| | | |
| | |
| | | public R<List<SixChargingDto>> charge(Integer siteId, Integer partnerId) { |
| | | Long userId = tokenService.getLoginUser().getUserid(); |
| | | SysUser sysUser = sysUserClient.getSysUser(userId).getData(); |
| | | boolean partnerView = isPartnerView(sysUser); |
| | | List<Integer> siteIds = resolvePlatformRevenueSiteIds(userId, sysUser, siteId, partnerId); |
| | | LocalDate sixBefore = PreviousSixMonths.get(); |
| | | //通过siteIds进行sql查询统计 |
| | | List<SixChargingDto> chargingDtos = chargingOrderService.charge(sixBefore, siteIds, false); |
| | | return R.ok(buildLastSixMonths(siteIds, false)); |
| | | } |
| | | |
| | | /** |
| | | * 近6月+当月电站收入:历史月读结算账单、当月补原始订单。 |
| | | * 与「电站收入分析」图表、底部数据分类及加盟商收支同口径。 |
| | | */ |
| | | private List<SixChargingDto> buildLastSixMonths(List<Integer> siteIds, boolean partnerView) { |
| | | LocalDate sixBefore = PreviousSixMonths.get(); |
| | | List<SixChargingDto> chargingDtos = chargingOrderService.charge(sixBefore, siteIds, partnerView); |
| | | LocalDate statisticsEndMonth = resolveStatisticsEndMonth(LocalDate.now(), chargingDtos); |
| | | List<SixChargingDto> sixChargingDtos = generateLastSixMonths(statisticsEndMonth); |
| | | for (SixChargingDto sixChargingDto : sixChargingDtos) { |
| | |
| | | } |
| | | |
| | | } |
| | | |
| | | return R.ok(sixChargingDtos); |
| | | |
| | | return sixChargingDtos; |
| | | } |
| | | |
| | | |
| | |
| | | SysUser sysUser = sysUserClient.getSysUser(userId).getData(); |
| | | boolean partnerView = isPartnerView(sysUser); |
| | | List<Integer> siteIds = resolvePlatformRevenueSiteIds(userId, sysUser, siteId, partnerId); |
| | | Map<String,Object> map = chargingOrderService.countAll(siteIds, false); |
| | | if (map == null) { |
| | | map = new HashMap<String,Object>(); |
| | | } |
| | | LocalDate sixBefore = PreviousSixMonths.get(); |
| | | Map<String,Object> map = new HashMap<String,Object>(); |
| | | // 电费/服务费与上方「电站收入分析」图表同口径:近6月结算账单 + 当月原始订单,保证底部合计=图表6个月相加 |
| | | List<SixChargingDto> sixChargingDtos = buildLastSixMonths(siteIds, false); |
| | | map.put("electrovalence", sixChargingDtos.stream() |
| | | .filter(Objects::nonNull) |
| | | .map(SixChargingDto::getElectrovalence) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | map.put("service_charge", sixChargingDtos.stream() |
| | | .filter(Objects::nonNull) |
| | | .map(SixChargingDto::getServiceCharge) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | // 合作商分成/三方平台分佣按近6月结算口径统计(与图表月份窗口一致) |
| | | Map<String,Object> sub = chargingOrderService.countAll(siteIds, false, sixBefore); |
| | | map.put("commission_amount", sub == null ? BigDecimal.ZERO : sub.get("commission_amount")); |
| | | map.put("sharing_amount", sub == null ? BigDecimal.ZERO : sub.get("sharing_amount")); |
| | | boolean platformVisible = !partnerView; |
| | | if(platformVisible){ |
| | | R<BigDecimal> parkingAmountResult = parkingLotClient.getRecordAmount(siteIds); |
| | | R<BigDecimal> parkingAmountResult = parkingLotClient.getRecordAmount(siteIds, sixBefore.toString()); |
| | | BigDecimal data = parkingAmountResult == null || parkingAmountResult.getData() == null |
| | | ? BigDecimal.ZERO : parkingAmountResult.getData(); |
| | | map.put("parkingAmount",data); |
| | | BigDecimal data1 = shoppingOrderService.getSumAmount(); |
| | | BigDecimal data1 = shoppingOrderService.getSumAmount(sixBefore); |
| | | map.put("shopAmount",data1); |
| | | BigDecimal data2 = vipOrderService.getSumAmout(); |
| | | BigDecimal data2 = vipOrderService.getSumAmout(sixBefore); |
| | | map.put("vipAmount",data2); |
| | | }else{ |
| | | map.put("parkingAmount",0); |