zxl
2026-03-25 0b39edb68acc67ed01fbfe5d31bfa776a1b17de1
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
package com.tievd.jyz.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.tievd.jyz.dto.CarModelProportionDTO;
import com.tievd.jyz.dto.SalesStatDTO;
import com.tievd.jyz.dto.TrafficFlowStatDTO;
import com.tievd.jyz.entity.vo.*;
import com.tievd.jyz.mapper.OilRecordMapper;
import com.tievd.jyz.mapper.OilStaticMapper;
import com.tievd.jyz.mapper.TrafficFlowMapper;
import com.tievd.jyz.service.IOperationDataService;
import com.tievd.jyz.util.PercentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
import static cn.dev33.satoken.SaManager.log;
 
/**
 * @author yang'zhi'shui
 */
@Service
public class OperationDataServiceImpl implements IOperationDataService {
 
    @Autowired
    private TrafficFlowMapper trafficFlowMapper;
 
    @Autowired
    private OilRecordMapper oilRecordMapper;
 
    @Autowired
    private OilStaticMapper oilStaticMapper;
 
    @Override
    public OperationOverviewVO overview(String today, String orgCode) {
        Long trafficFlow = trafficFlowMapper.getTrafficFlow(today, orgCode);
        OperationOverviewVO overviewVO = oilRecordMapper.getOilRecordOverview(today, orgCode);
        overviewVO.setTrafficFlow(trafficFlow);
        if (trafficFlow == 0 || overviewVO.getInboundCount() == 0) {
            overviewVO.setTurnInRate(0d);
            overviewVO.setAddOilRate(0d);
        } else {
            overviewVO.setTurnInRate(BigDecimal.valueOf((float) overviewVO.getInboundCount() / trafficFlow).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            overviewVO.setAddOilRate(BigDecimal.valueOf((float) overviewVO.getAddOilCount() / overviewVO.getInboundCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
        return overviewVO;
    }
 
    @Override
    public Map<String, List> trafficFlowStat(Integer intervalDay, String endDay, String orgCode) {
        List<TrafficFlowStatDTO> trafficFlowStatList = trafficFlowMapper.trafficFlowStat(intervalDay, endDay, orgCode);
        List<TrafficFlowStatDTO> inboundCountStatList = oilRecordMapper.inboundCountStat(intervalDay, endDay, orgCode);
        Map<String, Integer> inboundMap = inboundCountStatList.stream().collect(Collectors.toMap(TrafficFlowStatDTO::getDate, TrafficFlowStatDTO::getInboundCount));
        List<String> dateList = new ArrayList();
        List<Integer> trafficFlowList = new ArrayList();
        List<Integer> inboundCountList = new ArrayList();
        for (int i = 0; i < trafficFlowStatList.size(); i++) {
            TrafficFlowStatDTO dto = trafficFlowStatList.get(i);
            dateList.add(i, dto.getDate());
            trafficFlowList.add(i, dto.getTrafficFlow());
            inboundCountList.add(i, inboundMap.get(dto.getDate()));
        }
        Map<String, List> result = new HashMap<>();
        result.put("dateArr", dateList);
        result.put("trafficFlowArr", trafficFlowList);
        result.put("inboundCountArr", inboundCountList);
        return result;
    }
 
    @Override
    public List<RegionTrafficFlowStatVO> regionTrafficFlowStat(String today, String orgCode, Integer limit) {
        List<RegionTrafficFlowStatVO> trafficFlowStatList = trafficFlowMapper.regionTrafficFlowStat(today, orgCode, limit);
        List<RegionTrafficFlowStatVO> inboundCountStatList = oilRecordMapper.regionTrafficFlowStat(today, orgCode, limit);
        Map<String, Integer> inboundMap = inboundCountStatList.stream().collect(Collectors.toMap(RegionTrafficFlowStatVO::getDepartCode, RegionTrafficFlowStatVO::getInboundCount));
        trafficFlowStatList.forEach(vo -> vo.setInboundCount(inboundMap.get(vo.getDepartCode())));
        return trafficFlowStatList;
    }
 
    @Override
    public CustomerStatVO customerStat(String orgCode) {
        return oilStaticMapper.custommerStat(orgCode);
    }
 
    @Override
    public Map<String, List> oilRecordTimeStat(String date, String orgCode) {
        List<OilRecordTimeStatVO> list = oilRecordMapper.oilRecordTimeStat(date, orgCode);
        Map<String, OilRecordTimeStatVO> oilRecordTimeStatVOMap = list.stream().collect(Collectors.toMap(OilRecordTimeStatVO::getTime, Function.identity()));
        List<String> timeArr = new ArrayList<>();
        List<Integer> stopCountArr = new ArrayList<>();
        List<Integer> addOilCountArr = new ArrayList<>();
        List<Double> addOilRateArr = new ArrayList<>();
        Integer tempInboundCount = 0;
        Integer tempStopCount = 0;
        Integer tempAddOilCount = 0;
        Integer index = 0;
        for (int i = 0; i < 24; i++) {
            OilRecordTimeStatVO vo = oilRecordTimeStatVOMap.get(String.valueOf(i));
            if (vo == null) {
                vo = new OilRecordTimeStatVO();
                vo.setAddOilCount(0);
                vo.setInboundCount(0);
                vo.setStopCount(0);
            }
            tempInboundCount += vo.getInboundCount();
            tempStopCount += vo.getInboundCount() - vo.getAddOilCount();
            tempAddOilCount += vo.getAddOilCount();
            if (i % 2 == 1) {
                stopCountArr.add(index, tempStopCount);
                addOilCountArr.add(index, tempAddOilCount);
                if (tempInboundCount == 0) {
                    addOilRateArr.add(index, 0d);
                } else {
                    addOilRateArr.add(index, BigDecimal.valueOf((float) tempAddOilCount / tempInboundCount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                }
                timeArr.add(index, (i + 1) + "点");
                index++;
                tempInboundCount = 0;
                tempStopCount = 0;
                tempAddOilCount = 0;
            }
        }
        Map<String, List> result = new HashMap<>();
        result.put("timeArr", timeArr);
        result.put("stopCountArr", stopCountArr);
        result.put("addOilCountArr", addOilCountArr);
        result.put("addOilRateArr", addOilRateArr);
        return result;
    }
 
    @Override
    public List<OilPositionHeatMapVO> oilPositionHeatMap(String today, String orgCode) {
        List<OilPositionHeatMapVO> heatList = oilRecordMapper.getOilPositionHeatMap(today, orgCode);
        OilPositionHeatMapVO maxVO = heatList.stream().max(Comparator.comparing(OilPositionHeatMapVO::getStopCount)).get();
        if (maxVO.getStopCount() == 0) {
            heatList.forEach(vo -> vo.setHeatIndex(0));
        } else {
            heatList.forEach(vo -> vo.setHeatIndex(vo.getStopCount() * 100 / maxVO.getStopCount()));
        }
        return heatList;
    }
 
    @Override
    public Map<String, List> carModelStat(String today, String orgCode) {
        List<CarModelProportionDTO> list = oilRecordMapper.carModelStat(today, orgCode);
        long addOilSum = list.stream().mapToLong(CarModelProportionDTO::getAddOilCount).sum();
        if (addOilSum == 0) {
            list.forEach(vo -> vo.setProportion(0.00d));
        } else {
            list.forEach(vo -> vo.setProportion(BigDecimal.valueOf((float) vo.getAddOilCount() / addOilSum).doubleValue()));
        }
        List<String> carModelArr = new ArrayList<>();
        List<Integer> addOilCountArr = new ArrayList<>();
        List<Double> proportionArr = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CarModelProportionDTO vo = list.get(i);
            carModelArr.add(i, vo.getCarModel());
            addOilCountArr.add(i, vo.getAddOilCount());
            proportionArr.add(i, vo.getProportion());
        }
        Map<String, List> result = new HashMap<>();
        result.put("carModelArr", carModelArr);
        result.put("addOilCountArr", addOilCountArr);
        if (addOilSum == 0) {
            result.put("proportionArr", proportionArr);
        } else {
            // 校准百分比之和为1
            result.put("proportionArr", PercentUtil.getPercentValue(proportionArr, 1, 0));
        }
        return result;
    }
 
    @Override
    public Map<String, List> salesStat(Integer intervalDay, String today, String orgCode) {
        List<SalesStatDTO> list = oilRecordMapper.salesStat(intervalDay, today, orgCode);
        Integer pre = 0;
        for (SalesStatDTO dto : list) {
            Integer curr = dto.getSalesVolume();
            if (pre == 0) {
                dto.setGrowthRate(0.00d);
            } else {
                dto.setGrowthRate(BigDecimal.valueOf((float) (curr - pre) / pre).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }
            pre = curr;
        }
        List<String> dateArr = new ArrayList<>();
        List<Integer> salesVolumeArr = new ArrayList<>();
        List<Double> growthRateArr = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            SalesStatDTO dto = list.get(i);
            dateArr.add(i, dto.getDate());
            salesVolumeArr.add(i, dto.getSalesVolume());
            growthRateArr.add(i, dto.getGrowthRate());
        }
        Map<String, List> result = new HashMap<>();
        result.put("dateArr", dateArr);
        result.put("salesVolumeArr", salesVolumeArr);
        result.put("growthRateArr", growthRateArr);
        return result;
    }
 
    @Override
    public List<OilRecordStatVO> regionOilRecordStat(String orgCode, Integer freq, Integer limit) {
        List<OilRecordStatVO> result;
        if (freq == 0) { //当天
            result = oilRecordMapper.getRegionOilRecordStatByDay(orgCode, DateUtil.today(), limit);
        } else { //当月
            result = oilRecordMapper.getRegionOilRecordStatByMonth(orgCode, DateUtil.format(new Date(), "yyyy-MM"), limit);
        }
        for (OilRecordStatVO vo : result) {
            Integer inboundCount = vo.getInboundCount();
            if (inboundCount == 0) {
                vo.setAddOilRate(0.00d);
            } else {
                vo.setAddOilRate(BigDecimal.valueOf((float) vo.getAddOilCount() / inboundCount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            }
        }
        return result;
    }
 
    @Override
    public List<SalesStatVO> salesRanking(String orgCode, Integer freq, Integer limit) {
        List<SalesStatVO> result;
        if (freq == 0) { //当天
            result = oilRecordMapper.getSalesRankingByDay(orgCode, DateUtil.today(), limit);
        } else { //当月
            result = oilRecordMapper.getSalesRankingByMonth(orgCode, DateUtil.format(new Date(), "yyyy-MM"), limit);
        }
        return result;
    }
 
}