luodangjia
2024-09-27 801164d62ea165e90e77cd7da90829b1615532e3
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/controller/TChargingOrderController.java
@@ -3,6 +3,7 @@
import com.google.common.collect.Lists;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -71,6 +72,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
@@ -583,6 +585,16 @@
    }
    @ResponseBody
    @PostMapping(value = "/work/shop")
    @ApiOperation(value = "购物收入", tags = {"后台-工作台"})
    public R workShop(@RequestBody ChargingStatisticsQueryDto statisticsQueryDto) {
        //count近6个月的数据
        LocalDate sixBefore = PreviousSixMonths.get();
        List<Map<String,Object >> shopData =  shoppingOrderService.getData(statisticsQueryDto);
        return R.ok(shopData);
    }
    @ResponseBody
    @GetMapping(value = "/six/vip")
    @ApiOperation(value = "vip收入", tags = {"后台-数据分析-平台收入分析"})
    public R<List<SixVipDto>> vip() {
@@ -614,7 +626,7 @@
    @ResponseBody
    @PostMapping(value = "/watch/chargingOrder")
    @ApiOperation(value = "监控订单", tags = {"管理后台-订单管理"})
    public AjaxResult<TCharingOrderVO> watchChargingOrder(@RequestBody ChargingOrderQuery dto) {
    public R<List<ChargingOrderAndUploadRealTimeMonitoringDataDto>> watchChargingOrder(@RequestBody ChargingOrderQuery dto) {
        Integer page = dto.getPageCurr();
        Integer pageSize = dto.getPageSize();
        List<Long> data = appUserClient.getUserIdsByPhone(dto.getPhone()).getData();
@@ -641,7 +653,7 @@
        }
        return AjaxResult.success(res);
        return R.ok(dtos);
    }
    
    
@@ -849,18 +861,318 @@
        tCharingUserEvaluateVO.setBlackCount(count);
        //好评标签
       List<Map<String,Object>> goodTop = orderEvaluateService.goodTop(siteIds);
       //差评标签
        List<Map<String,Object>> badTop = orderEvaluateService.badTop(siteIds);
        //标签
        //查出好评ids
//        orderEvaluateService.goodTop();
        //流量分析
        List<Map<String,Object>> sourceMap = chargingOrderService.countBySource(siteIds);
        tCharingUserEvaluateVO.setGoodTop(goodTop);
        tCharingUserEvaluateVO.setBadTop(badTop);
        tCharingUserEvaluateVO.setFlow(sourceMap);
        //流量
        return R.ok(tCharingUserEvaluateVO);
    }
    @ResponseBody
    @PostMapping(value = "/charging/equipment")
    @ApiOperation(value = "电站评价", tags = {"管理后台-数据分析-设备运维分析"})
    public R<TCharingUserEquimentVO> equipment(@RequestBody ChargingStatisticsQueryDto statisticsQueryDto){
        List<Integer> siteIds =new ArrayList<>();
        if (statisticsQueryDto.getSiteId()==null) {
            Long userId = SecurityUtils.getUserId();
            //获取当前登录的siteIds
            List<GetSiteListDTO> data = siteClient.getSiteListByUserId(userId).getData();
            for (GetSiteListDTO datum : data) {
                siteIds.add(datum.getId());
            }
        }else {
            siteIds.add(statisticsQueryDto.getSiteId());
        }
        //直流可用率
        List<Map<String,Object>> equipmentMap1 = chargingOrderService.equipmentUserType1(siteIds,statisticsQueryDto);
        //交流可用率
        List<Map<String,Object>> equipmentMap2= chargingOrderService.equipmentUserType2(siteIds,statisticsQueryDto);
        //直流故障率
        List<Map<String,Object>> equipmentMapbroke1 = chargingOrderService.equipmentMapbroke1(siteIds,statisticsQueryDto);
        //交流故障率
        List<Map<String,Object>> equipmentMapbroke2 = chargingOrderService.equipmentMapbroke2(siteIds,statisticsQueryDto);
        //直流离网率
        List<Map<String,Object>> equipmentMapOut1 = chargingOrderService.equipmentMapOut1(siteIds,statisticsQueryDto);
        //交流离网率
        List<Map<String,Object>> equipmentMapOut2 = chargingOrderService.equipmentMapOut2(siteIds,statisticsQueryDto);
        //需求电流满足率
        List<Map<String,Object>>  needElec =  chargingOrderService.needElec(siteIds,statisticsQueryDto);
        TCharingUserEquimentVO tCharingUserEquimentVO = new TCharingUserEquimentVO();
        tCharingUserEquimentVO.setEquipmentMap1(equipmentMap1);
        tCharingUserEquimentVO.setEquipmentMap2(equipmentMap2);
        tCharingUserEquimentVO.setEquipmentMapbroke1(equipmentMapbroke1);
        tCharingUserEquimentVO.setEquipmentMapbroke2(equipmentMapbroke2);
        tCharingUserEquimentVO.setEquipmentMapOut1(equipmentMapOut1);
        tCharingUserEquimentVO.setEquipmentMapOut2(equipmentMapOut2);
        tCharingUserEquimentVO.setNeedElec(needElec);
        return R.ok(tCharingUserEquimentVO);
    }
    @ResponseBody
    @PostMapping(value = "/work/charge")
    @ApiOperation(value = "上方充电数据统计", tags = {"管理后台-工作台"})
    public R<TCharingWorkVO> workCharge(@RequestBody ChargingStatisticsQueryDto statisticsQueryDto) {
        List<Integer> siteIds = new ArrayList<>();
        if (statisticsQueryDto.getSiteId() == null) {
            Long userId = SecurityUtils.getUserId();
            //获取当前登录的siteIds
            List<GetSiteListDTO> data = siteClient.getSiteListByUserId(userId).getData();
            for (GetSiteListDTO datum : data) {
                siteIds.add(datum.getId());
            }
        } else {
            siteIds.add(statisticsQueryDto.getSiteId());
        }
        List<TChargingOrder> list = chargingOrderService.lambdaQuery().in(!siteIds.isEmpty(), TChargingOrder::getSiteId, siteIds).eq(statisticsQueryDto.getSiteId() != null, TChargingOrder::getSiteId, statisticsQueryDto.getSiteId()).list();
        //当日的订单总数
        int size = list.size();
        //计算list中paymentAmount的总和
        BigDecimal totalPaymentAmount = list.stream().map(TChargingOrder::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        //计算list中electrovalence的总和
        BigDecimal totalElectrovalence = list.stream().map(TChargingOrder::getElectrovalence).reduce(BigDecimal.ZERO, BigDecimal::add);
        //计算list中serviceCharge的总和
        BigDecimal totalServiceCharge = list.stream().map(TChargingOrder::getServiceCharge).reduce(BigDecimal.ZERO, BigDecimal::add);
        //计算list中charging_capacity的总和
        BigDecimal totalChargingCapacity = list.stream().map(TChargingOrder::getChargingCapacity).reduce(BigDecimal.ZERO, BigDecimal::add);
        TCharingWorkVO tCharingWorkVO = new TCharingWorkVO();
        tCharingWorkVO.setCount(size);
        tCharingWorkVO.setTotalPaymentAmount(totalPaymentAmount);
        tCharingWorkVO.setTotalElectrovalence(totalElectrovalence);
        tCharingWorkVO.setTotalServiceCharge(totalServiceCharge);
        tCharingWorkVO.setTotalChargingCapacity(totalChargingCapacity);
        return R.ok(tCharingWorkVO);
    }
    @ResponseBody
    @PostMapping(value = "/work/chargeDetail")
    @ApiOperation(value = "运营情况", tags = {"管理后台-工作台"})
    public R workCharge(@RequestBody ChargingDetailQueryDto statisticsQueryDto) {
        List<Integer> siteIds = new ArrayList<>();
        if (statisticsQueryDto.getSiteId() == null) {
            Long userId = SecurityUtils.getUserId();
            //获取当前登录的siteIds
            List<GetSiteListDTO> data = siteClient.getSiteListByUserId(userId).getData();
            for (GetSiteListDTO datum : data) {
                siteIds.add(datum.getId());
            }
        } else {
            siteIds.add(statisticsQueryDto.getSiteId());
        }
        if (statisticsQueryDto.getDayType()==1) {
          List<Map<String,Object>> charMap1 = chargingOrderService.getHourType(siteIds,statisticsQueryDto);
            List<Map<String, Object>> charMap = new ArrayList<>();
            // 生成从 "00:00" 到 "23:00" 的时间数据
            for (int hour = 0; hour < 24; hour++) {
                String time = String.format("%02d:00", hour);
                 Map<String, Object> mapWithTimeValue = findMapWithTimeValue(charMap1, time);
                if (mapWithTimeValue!=null){
                    charMap.add(mapWithTimeValue);
                }else {
                    Map<String, Object> timeMap = new HashMap<>();
                    timeMap.put("time", time); // 初始化值为 null
                    timeMap.put("electrovalence", 0);
                    timeMap.put("orderCount", 0);
                    timeMap.put("servicecharge", 0);
                    timeMap.put("electricity", 0);
                    charMap.add(timeMap);
                }
            }
          return R.ok(charMap);
        }else if (statisticsQueryDto.getDayType()==2){
            // 假设 chargingOrderService.getDateType() 返回的是按天的数据
            List<Map<String, Object>> charMap1 = chargingOrderService.getDateType(siteIds, statisticsQueryDto);
            // 解析 startTime 和 endTime 为 LocalDate
            LocalDate startDate = statisticsQueryDto.getStartTime();
            LocalDate endDate = statisticsQueryDto.getEndTime();
            List<Map<String, Object>> dateRangeStatistics = new ArrayList<>();
            // 遍历日期范围
            while (!startDate.isAfter(endDate)) {
                String formattedDate = startDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                Map<String, Object> dailyStats = findMapWithDateValue(charMap1, formattedDate);
                if (dailyStats != null) {
                    dateRangeStatistics.add(dailyStats);
                } else {
                    Map<String, Object> dateMap = new HashMap<>();
                    dateMap.put("time", formattedDate);
                    dateMap.put("electrovalence", 0);
                    dateMap.put("orderCount", 0);
                    dateMap.put("servicecharge", 0);
                    dateMap.put("electricity", 0);
                    dateRangeStatistics.add(dateMap);
                }
                // 移动到下一天
                startDate = startDate.plusDays(1);
            }
//            return dateRangeStatistics;
            return R.ok(dateRangeStatistics);
        }else if (statisticsQueryDto.getDayType()==3){
            List<Map<String,Object>> charMap1 =  chargingOrderService.getMonthType(siteIds,statisticsQueryDto);
            // 解析 startTime 和 endTime 为 LocalDate
            LocalDate startDate = statisticsQueryDto.getStartTime();
            LocalDate endDate = statisticsQueryDto.getEndTime();
            List<Map<String, Object>> dateRangeStatistics = new ArrayList<>();
            // 遍历日期范围
            while (!startDate.isAfter(endDate)) {
                String formattedDate = startDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));
                Map<String, Object> dailyStats = findMapWithDateValue(charMap1, formattedDate);
                if (dailyStats != null) {
                    dateRangeStatistics.add(dailyStats);
                } else {
                    Map<String, Object> dateMap = new HashMap<>();
                    dateMap.put("time", formattedDate);
                    dateMap.put("electrovalence", 0);
                    dateMap.put("orderCount", 0);
                    dateMap.put("servicecharge", 0);
                    dateMap.put("electricity", 0);
                    dateRangeStatistics.add(dateMap);
                }
                // 移动到下一天
                startDate = startDate.plusMonths(1);
            }
            return R.ok(dateRangeStatistics);
        }
        return R.ok();
    }
    private static Map<String, Object> findMapWithTimeValue(List<Map<String, Object>> charMap1,String timeValue) {
        for (Map<String, Object> map : charMap1) {
            if (map.containsKey("time") && map.get("time").equals(timeValue)) {
                return map;
            }
        }
        return null; // 如果没有找到,返回 null
    }
    private Map<String, Object> findMapWithDateValue(List<Map<String, Object>> list, String date) {
        for (Map<String, Object> map : list) {
            if (date.equals(map.get("time"))) {
                return map;
            }
        }
        return null;
    }
    @ResponseBody
    @PostMapping(value = "/work/use")
    @ApiOperation(value = "利用率", tags = {"管理后台-工作台"})
    public R workUse(@RequestBody ChargingDetailQueryDto statisticsQueryDto) {
        List<Integer> siteIds = new ArrayList<>();
        if (statisticsQueryDto.getSiteId() == null) {
            Long userId = SecurityUtils.getUserId();
            //获取当前登录的siteIds
            List<GetSiteListDTO> data = siteClient.getSiteListByUserId(userId).getData();
            for (GetSiteListDTO datum : data) {
                siteIds.add(datum.getId());
            }
        } else {
            siteIds.add(statisticsQueryDto.getSiteId());
        }
       List<Map<String,Object>>   capMap  =   chargingOrderService.getchargingCapacity(siteIds,statisticsQueryDto);
        List<TChargingPile> chargingPiles = chargingPileClient.getChargingPileBySiteIds(siteIds).getData();
        //获取chargingPiles的ratedPower的总和再乘以chargingPiles的数量再乘以24
        BigDecimal totalRatedPower = chargingPiles.stream().map(TChargingPile::getRatedPower).reduce(BigDecimal.ZERO, BigDecimal::add).multiply(new BigDecimal(chargingPiles.size())).multiply(new BigDecimal(24));
        //将capMap的chargingCapacity除以totalRatedPower保留两位数
        capMap.forEach(map -> {
            BigDecimal chargingCapacity = (BigDecimal) map.get("chargingCapacity");
            BigDecimal result = chargingCapacity.divide(totalRatedPower, 2, RoundingMode.HALF_UP);
            map.put("chargingCapacity", result);
        });
        //
        // 解析 startTime 和 endTime 为 LocalDate
        LocalDate startDate = statisticsQueryDto.getStartTime();
        LocalDate endDate = statisticsQueryDto.getEndTime();
        List<Map<String, Object>> dateRangeStatistics = new ArrayList<>();
        // 遍历日期范围
        while (!startDate.isAfter(endDate)) {
            String formattedDate = startDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            Map<String, Object> dailyStats = findMapWithDateValue(capMap, formattedDate);
            if (dailyStats != null) {
                dateRangeStatistics.add(dailyStats);
            } else {
                Map<String, Object> dateMap = new HashMap<>();
                dateMap.put("time", formattedDate);
                dateMap.put("chargingCapacity", 0);
                dateRangeStatistics.add(dateMap);
            }
            // 移动到下一天
            startDate = startDate.plusDays(1);
        }
        return R.ok(dateRangeStatistics);
    }
    @Resource
    private TOrderInvoiceService invoiceService;
    @ResponseBody
    @GetMapping(value = "/work/shopOrder")
    @ApiOperation(value = "购物订单统计", tags = {"管理后台-工作台"})
    public R shopOrder() {
        Long count = shoppingOrderService.lambdaQuery().eq(TShoppingOrder::getStatus, 1).count();
        Long count1 = shoppingOrderService.lambdaQuery().eq(TShoppingOrder::getStatus, 2).count();
        List<Long> counts = new ArrayList<>();
        counts.add(count);
        counts.add(count1);
        return R.ok(counts);
    }
    @ResponseBody
    @GetMapping(value = "/work/invoice")
    @ApiOperation(value = "开票统计", tags = {"管理后台-工作台"})
    public R invoice() {
        Long count = invoiceService.lambdaQuery().eq(TOrderInvoice::getStatus, 1).count();
        Long count1 = invoiceService.lambdaQuery().eq(TOrderInvoice::getStatus, 3).count();
        List<Long> counts = new ArrayList<>();
        counts.add(count);
        counts.add(count1);
        return R.ok(counts);
    }
    @ResponseBody
    @GetMapping(value = "/work/users/count")
    @ApiOperation(value = "用户数量", tags = {"管理后台-工作台"})
    public R usersCount() {
       List<Map<String,Object>>  userMap  =    chargingOrderService.countAllUserData();
            return R.ok(userMap);
    }
    public static void main(String[] args) {
        // 示例数据
        List<TChargingOrder> list = getSampleData();
@@ -944,4 +1256,14 @@
        TChargingOrder chargingOrder = chargingOrderService.getChargingOrderByLicensePlate(query);
        return R.ok(chargingOrder);
    }
    /**
     * 修改充电订单
     * @param chargingOrder
     * @return
     */
    @PostMapping("/updateChargingOrder")
    public R<String> updateChargingOrder(@RequestBody TChargingOrder chargingOrder){
        chargingOrderService.updateById(chargingOrder);
        return R.ok();
    }
}