whyclxw
2025-06-13 37b198e2ed9a87ae52987e0069d7c7d7e01cc7fb
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.whyc.service;
 
import com.whyc.dto.NameValueDto;
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.db_batt_testdata.BatttestdataInf;
import com.whyc.pojo.db_station.BattInf;
import com.whyc.pojo.db_station.PowerInf;
import com.whyc.pojo.db_station.StationInf;
import com.whyc.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
 
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.stream.Collectors;
 
import static com.whyc.util.DateUtil.convertToLocalDateTime;
 
 
@Service
public class LeaderHomeService {
 
    @Autowired
    private PowerInfService  powerInfService;
 
    @Autowired
    private BattInfService battInfService;
 
    @Autowired
    private StationInfService stationInfService;
 
    @Autowired
    private BatttestdataInfService battTestDataInfService;
 
    public Response getAll(Integer userId) {
        Response response = new Response();
        Map<String,Object> map = new HashMap<>();
 
        CountDownLatch latch = new CountDownLatch(9);
        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
 
        //站点信息统计,
        poolExecutor.execute(() -> {
            stationInfoStatistics(userId, map);
            latch.countDown();
        });
 
        //电源信息(品牌及电压)统计
        poolExecutor.execute(() -> {
            powerInfoStatistics(userId, map);
            latch.countDown();
        });
        //蓄电池信息(品牌及电压)统计
        poolExecutor.execute(() -> {
            batteryInfoStatistics(userId, map);
            latch.countDown();
        });
        //测试数据信息统计
        poolExecutor.execute(() -> {
            testDataInfoStatistics(userId, map);
            latch.countDown();
        });
        //TODO 设备数量信息统计
        poolExecutor.execute(() -> {
            //deviceInfoStatistics(userId, map);
            latch.countDown();
        });
        //TODO 实时告警信息统计
        poolExecutor.execute(() -> {
            //alarmRealtimeStatistics(userId, map);
            latch.countDown();
        });
        //TODO 不同时间同一品牌蓄电池性能统计
        poolExecutor.execute(() -> {
            //performanceInfoStatistics(userId, map);
            latch.countDown();
        });
        //TODO 同一时间不同品牌蓄电池性能统计
        poolExecutor.execute(() -> {
            //performanceInfoStatistics2(userId, map);
            latch.countDown();
        });
        //TODO 同一时间同一品牌蓄电池性能统计
        poolExecutor.execute(() -> {
            //performanceInfoStatistics3(userId, map);
            latch.countDown();
        });
 
        return response;
    }
 
    private void testDataInfoStatistics(Integer userId, Map<String, Object> map) {
        List<BatttestdataInf> testInfList = battTestDataInfService.getListByUserId(userId);
        //过滤出本月的
        LocalDateTime startOfMonth = DateUtil.getStartOfMonth();
        List<BatttestdataInf> testInfListOfMonth = testInfList.stream()
                .filter(testData -> {
                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
                    return !startTime.isBefore(startOfMonth);
                })
                .collect(Collectors.toList());
 
        //统计本月各种充放电类型及数量
        List<NameValueDto> testInfOfMonth = getChargeAndDischargeStatistics(testInfListOfMonth);
        map.put("testInf_month",testInfOfMonth);
 
        //过滤出本季度的
        LocalDateTime startOfQuarter = DateUtil.getStartOfQuarter();
        List<BatttestdataInf> testInfListOfQuarter = testInfList.stream()
                .filter(testData -> {
                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
                    return !startTime.isBefore(startOfQuarter);
                })
                .collect(Collectors.toList());
 
        //统计本季度各种充放电类型及数量
        List<NameValueDto> testInfOfQuarter = getChargeAndDischargeStatistics(testInfListOfQuarter);
        map.put("testInf_quarter",testInfOfQuarter);
 
        //过滤出本年的
        LocalDateTime startOfYear = DateUtil.getStartOfYear();
        List<BatttestdataInf> testInfListOfYear = testInfList.stream()
                .filter(testData -> {
                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
                    return !startTime.isBefore(startOfYear);
                })
                .collect(Collectors.toList());
 
        //统计本年各种充放电类型及数量
        List<NameValueDto> testInfOfYear = getChargeAndDischargeStatistics(testInfListOfYear);
        map.put("testInf_year",testInfOfYear);
    }
 
    /**
     * 统计各种充放电类型及数量
     * @param testInfListOfMonth 被统计的列表
     * @return
     */
    private List<NameValueDto> getChargeAndDischargeStatistics(List<BatttestdataInf> testInfListOfMonth) {
        List<NameValueDto> testInfOfMonth = new ArrayList<>();
        int chargeHr = 0;
        int chargeJc = 0;
        int dischargeHr = 0;
        int dischargeJc = 0;
        for (int i = 0; i < testInfListOfMonth.size(); i++) {
            BatttestdataInf testData = testInfListOfMonth.get(i);
            if (testData.getTestType() == 3) {  // 测试类型为放电
                if (testData.getTestStarttype() == 3) {  //核容放电
                    dischargeHr++;
                } else if(testData.getTestStarttype() != 4){  //监测放电
                    dischargeJc++;
                }
            } else if (testData.getTestType() == 2) {  // 测试类型为充电
                if (testData.getTestStarttype() == 3) {  //核容充电
                    chargeHr++;
                } else {  //监测充电
                    chargeJc++;
                }
            }
        }
        //核容放电,核容充电,监测放电,检测充电
        testInfOfMonth.add(new NameValueDto("核容放电",String.valueOf(dischargeHr)));
        testInfOfMonth.add(new NameValueDto("核容充电",String.valueOf(chargeHr)));
        testInfOfMonth.add(new NameValueDto("监测放电",String.valueOf(dischargeJc)));
        testInfOfMonth.add(new NameValueDto("监测充电",String.valueOf(chargeJc)));
        return testInfOfMonth;
    }
 
    private void batteryInfoStatistics(Integer userId, Map<String, Object> map) {
        List<BattInf> battInfList = battInfService.getListByUserId(userId);
        Map<String, List<BattInf>> brandMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getProduct));
        Map<Float, List<BattInf>> volMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getMonvolstd));
 
        List<NameValueDto> brandList = new ArrayList<>();
        brandMap.forEach((k,v)->{
            NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size()));
            brandList.add(brandDto);
        });
        map.put("battGroupInfo_brand",brandList);
 
        List<NameValueDto> volList = new ArrayList<>();
        volMap.forEach((k,v)->{
            NameValueDto volDto = new NameValueDto(String.valueOf(k), String.valueOf(v.size()));
            volList.add(volDto);
        });
        map.put("battGroupInfo_vol",volList);
    }
 
    private void powerInfoStatistics(Integer userId, Map<String, Object> map) {
        List<PowerInf> powerInfList = powerInfService.getListByUserId(userId);
        Map<String, List<PowerInf>> brandMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getCompany));
        Map<Integer, List<PowerInf>> typeMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getPowerType));
 
        List<NameValueDto> brandList = new ArrayList<>();
        brandMap.forEach((k,v)->{
            NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size()));
            brandList.add(brandDto);
        });
        map.put("powerInfo_brand",brandList);
 
        List<NameValueDto> typeList = new ArrayList<>();
        //电源类型:1:直流,2:通讯,3:配网
        typeMap.forEach((k,v)->{
            NameValueDto typeDto = new NameValueDto();
            switch (k){
                case 1:
                    typeDto.setName("直流");
                    break;
                case 2:
                    typeDto.setName("通讯");
                    break;
                case 3:
                    typeDto.setName("配网");
                    break;
            }
            typeDto.setValue(String.valueOf(v.size()));
            typeList.add(typeDto);
        });
        map.put("powerInfo_type",typeList);
 
    }
 
    /**
     * 按站点类型和站点电压
     * 站点类型:节点/非节点
     */
    private void stationInfoStatistics(Integer userId, Map<String, Object> map) {
        List<StationInf> stationInfList =stationInfService.getListByUserId(userId);
        Map<Integer, List<StationInf>> nodeMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getNodeStation));
        Map<String, List<StationInf>> volMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getStationType));
 
        List<NameValueDto> stationTypeList = new ArrayList<>();
        List<NameValueDto> stationVolList = new ArrayList<>();
        nodeMap.forEach((k,v)->{
            NameValueDto stationTypeDto = new NameValueDto(k == 0 ? "非节点" : "节点", String.valueOf(v.size()));
            stationTypeList.add(stationTypeDto);
        });
        volMap.forEach((k,v)->{
            NameValueDto stationVolDto = new NameValueDto(k, String.valueOf(v.size()));
            stationVolList.add(stationVolDto);
        });
 
        map.put("stationInfo_stationType",stationTypeList);
        map.put("stationInfo_stationVol",stationVolList);
    }
 
}