Pu Zhibing
2025-05-08 5b64f31f1d35b38b51935b5fa7f4b8cf46fbf4cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.ruoyi.order.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.order.api.model.TChargingOrder;
import com.ruoyi.order.api.model.TChargingOrderAccountingStrategy;
import com.ruoyi.order.mapper.TChargingOrderAccountingStrategyMapper;
import com.ruoyi.order.service.TChargingOrderAccountingStrategyService;
import com.ruoyi.order.service.TChargingOrderService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-07
 */
@Service
public class TChargingOrderAccountingStrategyServiceImpl extends ServiceImpl<TChargingOrderAccountingStrategyMapper, TChargingOrderAccountingStrategy> implements TChargingOrderAccountingStrategyService {
    
    @Resource
    private TChargingOrderService chargingOrderService;
    
    
    
    /**
     * 获取给定天数范围内的充电量统计
     * @param days  天数
     * @return
     */
    @Override
    public List<List<Map<String, Object>>> getTotalElectricQuantity(Integer days, Set<Integer> siteIds) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter sdf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days);
        String startTime = sdf.format(calendar.getTime()) + " 00:00:00";
        List<TChargingOrder> list = chargingOrderService.list(new LambdaQueryWrapper<TChargingOrder>().in(TChargingOrder::getSiteId, siteIds).eq(TChargingOrder::getStatus, 5)
                .eq(TChargingOrder::getDelFlag, 0).last(" and start_time between '" + startTime + "' and DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%s')"));
        List<Long> orderId = list.stream().map(TChargingOrder::getId).collect(Collectors.toList());
        List<TChargingOrderAccountingStrategy> list1 = new ArrayList<>();
        if(orderId.size() > 0){
            list1 = this.list(new LambdaQueryWrapper<TChargingOrderAccountingStrategy>().in(TChargingOrderAccountingStrategy::getChargingOrderId, orderId));
        }
 
        List<List<Map<String, Object>>> list2 = new ArrayList<>();
        for (int i = days; i >= 0; i--) {
            Calendar nowDateTime = Calendar.getInstance();
            nowDateTime.set(Calendar.DAY_OF_YEAR, nowDateTime.get(Calendar.DAY_OF_YEAR) - i);
            List<TChargingOrder> collect = list.stream().filter(s -> s.getStartTime().format(sdf1).equals(sdf.format(nowDateTime.getTime()))).collect(Collectors.toList());
            List<Long> orderIds = collect.stream().map(TChargingOrder::getId).collect(Collectors.toList());
            
            
            List<Map<String, Object>> datas = new ArrayList<>();
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", "尖");
            BigDecimal value1 = list1.stream().filter(s -> orderIds.contains(s.getChargingOrderId()) && s.getType() == 1)
                    .map(TChargingOrderAccountingStrategy::getChargingCapacity).reduce(BigDecimal.ZERO, BigDecimal::add);
            map1.put("value", value1.setScale(2, RoundingMode.HALF_EVEN));
            datas.add(map1);
            
            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", "峰");
            BigDecimal value2 = list1.stream().filter(s -> orderIds.contains(s.getChargingOrderId()) && s.getType() == 2)
                    .map(TChargingOrderAccountingStrategy::getChargingCapacity).reduce(BigDecimal.ZERO, BigDecimal::add);
            map2.put("value", value2.setScale(2, RoundingMode.HALF_EVEN));
            datas.add(map2);
            
            Map<String, Object> map3 = new HashMap<>();
            map3.put("name", "平");
            BigDecimal value3 = list1.stream().filter(s -> orderIds.contains(s.getChargingOrderId()) && s.getType() == 3)
                    .map(TChargingOrderAccountingStrategy::getChargingCapacity).reduce(BigDecimal.ZERO, BigDecimal::add);
            map3.put("value", value3.setScale(2, RoundingMode.HALF_EVEN));
            datas.add(map3);
            
            Map<String, Object> map4 = new HashMap<>();
            map4.put("name", "谷");
            BigDecimal value4 = list1.stream().filter(s -> orderIds.contains(s.getChargingOrderId()) && s.getType() == 4)
                    .map(TChargingOrderAccountingStrategy::getChargingCapacity).reduce(BigDecimal.ZERO, BigDecimal::add);
            map4.put("value", value4.setScale(2, RoundingMode.HALF_EVEN));
            datas.add(map4);
            list2.add(datas);
        }
        return list2;
    }
    
    
    /**
     * 获取给定天数每天的充电度数
     * @param days 天数
     * @return
     */
    @Override
    public List<Double> getDailyChargingDegree(Integer days, Set<Integer> siteIds) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days);
        String startTime = sdf.format(calendar.getTime()) + " 00:00:00";
        List<TChargingOrder> list = chargingOrderService.list(new LambdaQueryWrapper<TChargingOrder>().in(TChargingOrder::getSiteId, siteIds).eq(TChargingOrder::getStatus, 5)
                .eq(TChargingOrder::getDelFlag, 0).last(" and start_time between '" + startTime + "' and DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%s')"));
        
        List<Double> list2 = new ArrayList<>();
        //遍历获取每天的数值
        for (int i = days; i >= 0; i--) {
            Calendar nowDateTime = Calendar.getInstance();
            nowDateTime.set(Calendar.DAY_OF_YEAR, nowDateTime.get(Calendar.DAY_OF_YEAR) - i);
            //充电量
            BigDecimal reduce = list.stream().filter(s -> s.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).equals(sdf.format(nowDateTime.getTime())))
                    .map(TChargingOrder::getChargingCapacity).reduce(BigDecimal.ZERO, BigDecimal::add);
            list2.add(reduce.doubleValue());
        }
        return list2;
    }
}