peng
2026-03-18 e59a0201057ba67cad425fed804c82ff4ba0c6f1
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.tievd.jyz.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.entity.OilRecord;
import com.tievd.jyz.entity.OilStatis;
import com.tievd.jyz.entity.TrafficFlow;
import com.tievd.jyz.entity.vo.DataStatisReqVo;
import com.tievd.jyz.entity.vo.OilVolumeVo;
import com.tievd.jyz.entity.vo.StatDataTableVo;
import com.tievd.jyz.mapper.OilRecordMapper;
import com.tievd.jyz.mapper.OilStaticMapper;
import com.tievd.jyz.mapper.TrafficFlowMapper;
import com.tievd.jyz.service.IOilRecordService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * OilRecord
 * @author      cube
 * @since       2023-02-27
 * @version     V2.0.0
 */
@Service
@Log4j2
public class OilRecordServiceImpl extends ServiceImpl<OilRecordMapper, OilRecord> implements IOilRecordService {
    
    @Autowired
    OilRecordMapper oilRecordMapper;
    
    @Autowired
    TrafficFlowMapper trafficFlowMapper;
    
    @Autowired
    OilStaticMapper oilStaticMapper;
    
    @Override
    public Map statisOilPosition(String licenseNum, String orgCode) {
        Map res = new HashMap();
        List<Map> positionList = oilRecordMapper.statisOilPosition(licenseNum, orgCode);
        List<Integer> countList = new ArrayList<>();
        try {
            //加油位多边形统计数据
            Object max = positionList.stream().max(Comparator.comparingInt(m -> Integer.valueOf(m.get("count").toString()))).get().get("count");
            for (Map map : positionList) {
                countList.add(Integer.valueOf(map.get("count").toString()));
                map.put("max", max);
            }
        } catch (Exception e) {
            log.error("加油位统计失败", e);
        }
        res.put("count", countList);
        res.put("oilPosition", positionList);
        return res;
    }
    
    @Override
    public Map statisByMonth(String licenseNum, String orgCode) {
        Map res = new HashMap();
        try {
            List<Map> monthCountList = oilRecordMapper.statisByMonth(licenseNum, orgCode);
            res = convertToStatisList(monthCountList);
        } catch (Exception e) {
            log.error("车辆月统计失败", e);
        }
        return res;
    }
    
    private Map convertToStatisList(List<? extends Map> dataList) {
        Set keys = dataList.get(0).keySet();
        Map res = new HashMap();
        for (Object key : keys) {
            List convertList = new ArrayList();
            for (Map map : dataList) {
                convertList.add(map.get(key));
            }
            res.put(key, convertList);
        }
        return res;
    }
    
    @Override
    public Map statisByHour(String licenseNum, String orgCode) {
        Map res = new HashMap();
        try {
            List<Map> hourCountList = oilRecordMapper.statisByHour(licenseNum, orgCode);
            res = convertToStatisList(hourCountList);
        } catch (Exception e) {
            log.error("车辆时统计失败", e);
        }
        return res;
    }
    
    @Override
    public Map statisByStayTime(String licenseNum, String orgCode) {
        Map res = new HashMap();
        try {
            List<Map> stayCountList = oilRecordMapper.statisByStayTime(licenseNum, orgCode);
            res = convertToStatisList(stayCountList);
        } catch (Exception e) {
            log.error("车辆停留时长统计失败", e);
        }
        return res;
    }
    
    /**
     * 加油量分页
     */
    @Override
    public IPage<Map> getStatisOilVolume(IPage page, String orgCode, String dateMonth) {
        return oilRecordMapper.getStatisOilVolume(page, orgCode, dateMonth);
    }
    
    @Override
    public Map getOilVolumeTotal(String orgCode, String dateMonth) {
        return oilRecordMapper.getOilVolumeTotal(orgCode, dateMonth);
    }
    
    /**
     * 加油量详情
     */
    @Override
    public IPage<OilVolumeVo> descOilVolumeList(IPage page, Map map) {
        return oilRecordMapper.descOilVolumeList(page, map);
    }
    
    @Override
    public StatDataTableVo statisTotal(DataStatisReqVo param) {
        return oilRecordMapper.statisTotal(param);
    }
    
    @Override
    public List<Map> statisOrgTopTraffic(DataStatisReqVo param) {
        return oilRecordMapper.statisOrgTopTraffic(param);
    }
    
    @Override
    public List<Map> statisOrgTopOil(DataStatisReqVo param) {
        return oilRecordMapper.statisOrgTopOil(param);
    }
    
    @Override
    public List<Map> statisOrgTopVolume(DataStatisReqVo param) {
        return oilRecordMapper.statisOrgTopVolume(param);
    }
    
    @Override
    public JSONObject statFan(DataStatisReqVo param) {
        JSONObject res;
        List<StatDataTableVo> dataList = new ArrayList<>();
        Function<StatDataTableVo, ?> xfunc = null;
        Function<StatDataTableVo, Integer> barfunc = null;
        switch(param.getType()) {
            case 1 :
                dataList = oilRecordMapper.statFanByModel(param);
                xfunc = StatDataTableVo::getModelName;
                barfunc = StatDataTableVo::getOilCount;
                break;
            case 2 :
                dataList = oilRecordMapper.statFanByPosition(param);
                xfunc = StatDataTableVo::getOilPosition;
                barfunc = StatDataTableVo::getOilCount;
                break;
            case 3 :
                dataList = oilRecordMapper.statFanByModel(param);
                xfunc = StatDataTableVo::getModelName;
                barfunc = StatDataTableVo::getOilVolume;
                break;
            case 4 :
                dataList = oilRecordMapper.statFanByPosition(param);
                xfunc = StatDataTableVo::getOilPosition;
                barfunc = StatDataTableVo::getOilVolume;
                break;
        }
        res = dataTransLists(dataList, xfunc, barfunc, null, "", "");
        return res;
    }
    
    
    @Override
    public JSONObject statBar(DataStatisReqVo param) {
        JSONObject res;
        List<StatDataTableVo> dataList;
        Function<StatDataTableVo, ?> xfunc;
        Function<StatDataTableVo, Integer> barfunc;
        Function<StatDataTableVo, Integer> linefunc;
        String barName = "";
        String lineName = "";
        if (param.getType() == 1) {
            dataList = oilRecordMapper.statBarByModel(param);
            xfunc = StatDataTableVo::getModelName;
            barfunc = StatDataTableVo::getOilCount;
            linefunc = StatDataTableVo::getCarCount;
            barName = "加油数";
            lineName = "车流量";
        } else {
            dataList = oilRecordMapper.statFanByPosition(param);
            xfunc = StatDataTableVo::getOilPosition;
            barfunc = StatDataTableVo::getOilCount;
            linefunc = StatDataTableVo::getOilVolume;
            barName = "加油数";
            lineName = "油品销量";
        }
        res = dataTransLists(dataList, xfunc, barfunc, linefunc, barName, lineName);
        return res;
    }
    
    /**
     * 加油相关图表
     * @param param
     * @return
     */
    @Override
    public JSONObject statTrend(DataStatisReqVo param) {
        DataStatisReqVo.StatUnit timeUnit = param.getTimeUnit();
        DataStatisReqVo.TrendType trendType = param.getTrendType();
    
        //时间区间
        LocalDateTime timeLimit = LocalDateTime.now()
                .minus(timeUnit.value(), ChronoUnit.valueOf(timeUnit.name()))
                .with(timeUnit.initField(), 1);
        LocalDateTime timeLimitRight = LocalDateTime.now().with(timeUnit.initField(), 1);
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<OilRecord>()
                .ge(OilRecord::getStartTime, timeLimit.toString())
                .le(OilRecord::getStartTime, timeLimitRight.toString())
                .likeRight(OilRecord::getOrgCode, param.getOrgCode());
        List<OilRecord> records = this.list(wrapper);
        
        
        //x轴
        Map<String, StatDataTableVo> statVoMap = new HashMap<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timeUnit.statFormat());
        for (int i = 0; i < timeUnit.value(); i++) {
            LocalDateTime time = timeLimit.plus(i, ChronoUnit.valueOf(timeUnit.name()));
            String key = time.format(formatter);
            String preKey = time.minus(1, ChronoUnit.valueOf(timeUnit.name())).format(formatter);
            StatDataTableVo statVo = new StatDataTableVo().setStatTime(time);
            if (statVo.getPreStatVo() == null) statVo.setPreStatVo(statVoMap.getOrDefault(preKey, new StatDataTableVo()));
            statVoMap.put(key, statVo);
        }
        for (OilRecord record : records) {
            String key = formatter.format(record.getStartTime().toLocalDateTime());
            if (!statVoMap.containsKey(key)) {
                continue;
            }
            StatDataTableVo statVo = statVoMap.get(key);
            statVo.addAppearCount(1);
            if (record.getBehavior().intValue() == SystemConstant.BEHAVIOR_TYPE_OIL) statVo.addOilCount(1);
            if (record.getBehavior().intValue() == SystemConstant.BEHAVIOR_TYPE_OIL) statVo.addOilVolume(record.getOilVolume());
            statVo.addSumSpand(record.getSpandTime());
        }
        
        //车流量
        switch(trendType) {
            case TRAFFIC:
                wrapper = new LambdaQueryWrapper<TrafficFlow>()
                        .select(TrafficFlow::getCarCount, TrafficFlow::getCaptureTime, TrafficFlow::getOrgCode)
                        .ge(TrafficFlow::getCaptureTime, timeLimit.toString())
                        .le(TrafficFlow::getCaptureTime, timeLimitRight.toString())
                        .likeRight(TrafficFlow::getOrgCode, param.getOrgCode());
                List<TrafficFlow> trafficList = trafficFlowMapper.selectList(wrapper);
                for (TrafficFlow traffic : trafficList) {
                    String key = formatter.format(traffic.getCaptureTime().toLocalDateTime());
                    if (!statVoMap.containsKey(key)) continue;
                    StatDataTableVo statVo = statVoMap.get(key);
                    statVo.addCarCount(traffic.getCarCount());
                }
                break;
        }
    
        Function<StatDataTableVo,?> xFunc = t -> t.getStatTime().format(formatter);
        Function barFunc = trendType.getBarFunc();
        Function lineFunc = trendType.getLineFunc();
        String barName = trendType.getBarName();
        String lineName = trendType.getLineName();
        List<StatDataTableVo> dataList = new ArrayList<>(statVoMap.values());
        dataList.sort((t1, t2) -> t1.getStatTime().compareTo(t2.getStatTime()));
        JSONObject res = dataTransLists(dataList, xFunc, barFunc, lineFunc, barName, lineName);
        return res;
    }
    
    /**
     * 客户图表
     * @param param
     * @return
     */
    @Override
    public JSONObject statTrendClient(DataStatisReqVo param) {
        DataStatisReqVo.StatUnit timeUnit = param.getTimeUnit();
        DataStatisReqVo.TrendType trendType = param.getTrendType();
    
        //时间区间
        LocalDateTime timeLimit = LocalDateTime.now()
                .minus(timeUnit.value(), ChronoUnit.valueOf(timeUnit.name()))
                .with(timeUnit.initField(), 1);
        LocalDateTime timeLimitRight = LocalDateTime.now().with(timeUnit.initField(), 1);
        
        //x轴
        Map<String, Integer> timeValMap = new TreeMap<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timeUnit.statFormat());
        for (int i = 0; i < timeUnit.value(); i++) {
            LocalDateTime time = timeLimit.plus(i, ChronoUnit.valueOf(timeUnit.name()));
            String key = time.format(formatter);
            timeValMap.put(key, 0);
        }
    
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<OilStatis>()
                .ge(OilStatis::getUpdateTimeSelf, timeLimit.toString())
                .le(OilStatis::getUpdateTimeSelf, timeLimitRight.toString())
                .likeRight(OilStatis::getOrgCode, param.getOrgCode());
        List<OilStatis> oilStatis = oilStaticMapper.selectList(wrapper);
        List barDatas = new ArrayList();
        List lineDatas = new ArrayList();
        switch(trendType) {
            case CLIENT:
                Map<String, Map<String, Integer>> dataMap = new HashMap<>();
                for (OilStatis stat : oilStatis) {
                    dataMap.putIfAbsent(stat.getClientName(), new TreeMap<String, Integer>(){{putAll(timeValMap);}});
                    Map<String, Integer> client = dataMap.get(stat.getClientName());
                    String key = formatter.format(stat.getUpdateTimeSelf().toLocalDateTime());
                    if (!client.containsKey(key)) continue;
                    client.put(key, client.get(key) + 1);
                }
                barDatas = dataMap.entrySet().stream().map(e -> {
                    JSONObject barData = new JSONObject();
                    barData.put("name", e.getKey());
                    barData.put("value", e.getValue().values());
                    return barData;
                }).collect(Collectors.toList());
                break;
            case LOSE_CLIENT:
                Map<String, Integer> sumMap = new HashMap<String, Integer>(){{putAll(timeValMap);}};
                for (OilStatis stat : oilStatis) {
                    String key = formatter.format(stat.getUpdateTimeSelf().toLocalDateTime());
                    if (!timeValMap.containsKey(key)) continue;
                    sumMap.put(key, sumMap.get(key) + 1);
                    if (stat.getClientId() == SystemConstant.LOSE_CLIENT_ID) {
                        timeValMap.put(key, timeValMap.get(key) + 1);
                    }
                }
                lineDatas = timeValMap.entrySet().stream().map(e ->
                    sumMap.get(e.getKey()) == 0 ? 0 : e.getValue() * 100 / sumMap.get(e.getKey())
                ).collect(Collectors.toList());
                break;
        }
        
    
        List<String> xDatas = new ArrayList<>(timeValMap.keySet());
        JSONObject res = new JSONObject();
        res.put("xData", xDatas);
        res.put("barData", barDatas);
        res.put("lineData", lineDatas);
        return res;
    }
    
    private <E> JSONObject  dataTransLists(
            List<E> dataList,
            Function<E, ?> xDataMapper,
            Function<E, ?> barDataMapper,
            Function<E, ?> lineDataMapper,
            String barName,
            String lineName) {
        List xDatas = new ArrayList();
        List<Integer> barDatas = new ArrayList<>();
        List lineDatas = new ArrayList<>();
        int sum = 0;
        for (E statVo : dataList) {
            Object xData = "";
            int barData = 0;
            Object lineData = 0;
            try {
                xData = xDataMapper.apply(statVo);
                barData = Integer.valueOf(barDataMapper.apply(statVo).toString());
                if (lineDataMapper != null) lineData = lineDataMapper.apply(statVo);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("统计出现异常", e);
            }
    
            xDatas.add(xData);
            barDatas.add(barData);
            lineDatas.add(lineData);
            sum += barData;
        }
        if (lineDataMapper == null) {
            int finalSum = sum;
            lineDatas = barDatas.stream().map(n -> n*100/ finalSum).collect(Collectors.toList());
        }
        JSONObject res = new JSONObject();
        res.put("xData", xDatas);
        res.put("barData", barDatas);
        res.put("lineData", lineDatas);
        res.put("barName", barName);
        res.put("lineName", lineName);
        return res;
    }
    
}