jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.es.service;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.ld.igds.constant.BizType;
import com.ld.igds.data.ChartLine;
import com.ld.igds.data.ChartSeries;
import com.ld.igds.grain.dto.GrainData;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.websocket.WebSocketPacket;
import com.ld.igds.websocket.WebSocketServer;
 
import lombok.extern.slf4j.Slf4j;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.ld.igds.constant.RedisConst;
import com.ld.igds.es.dto.EsData;
import com.ld.igds.es.dto.EsParam;
import com.ld.igds.es.dto.EsSummary;
import com.ld.igds.es.mapper.EsServiceMapper;
import com.ld.igds.util.DateUtil;
import com.ld.igds.util.RedisUtil;
 
@Slf4j
@Component(CoreEsService.BEAN_ID)
public class CoreEsServiceImpl implements CoreEsService {
 
//    @Autowired
//    private WebSocketServer socketServer;
    @Autowired
    private EsServiceMapper esServiceMapper;
    @Autowired
    private RedisUtil redisUtil;
 
    @Override
    public List<EsSummary> getSummaryGroupByDepot(EsParam param) {
 
        if (null == param.getCheckDate()) {
            return null;
        }
        // 说明按照日期来的
        if ("day".equals(param.getTimeType())) {
            param.setStart(DateUtil.getCurZero(param.getCheckDate()));
            param.setEnd(DateUtil.getNextZero(param.getCheckDate()));
        }
        if ("month".equals(param.getTimeType())) {
            param.setStart(DateUtil.getMonthFirst(param.getCheckDate()));
            param.setEnd(DateUtil.getNextMonthFirst(param.getCheckDate()));
        }
 
        return esServiceMapper.getSummaryGroupByDepot(param);
    }
 
    @Override
    public EsSummary getSummaryByParam(EsParam param) {
 
        if (null == param.getStart())
            new EsSummary();
        if (null == param.getEnd())
            new EsSummary();
        EsSummary result = esServiceMapper.getSummaryByParam(param);
        if (null == result)
            result = new EsSummary();
        return result;
    }
 
    @Override
    public List<EsSummary> listSumGroupByDay(EsParam param) {
 
        if (null == param.getStart()) {
            param.setStart(DateUtil.getNewByDay(null, -30));
        }
 
        return esServiceMapper.listSumGroupByDay(param);
    }
 
    @Override
    public List<EsData> listDetailChartData(EsParam param) {
 
        if (null == param.getStart()) {
            param.setStart(DateUtil.getNewByDay(null, -30));
        }
 
        if (null != param.getEnd()) {
            param.setEnd(DateUtil.getNextZero(param.getEnd()));
        }
        return esServiceMapper.listDetailChartData(param);
    }
 
    @Override
    public EsData getCacheEsData(String companyId, String depotId) {
        Map<String, EsData> dataMap = getCacheEsDataMap(companyId);
        if (null == dataMap || dataMap.isEmpty()) {
            return null;
        }
        EsData result = null;
        for (String key : dataMap.keySet()) {
            if(key.startsWith(depotId)){
                if(null == result){
                    result = dataMap.get(key);
                }else {
                    result.setEp(result.getEp() + dataMap.get(key).getEp());
                    result.setEq(result.getEq() + dataMap.get(key).getEq());
                    result.setEs(result.getEs() + dataMap.get(key).getEs());
                }
            }
        }
        return result;
    }
 
    @Override
    public EsData getCacheEsDepotData(String companyId, String depotId, String deviceId) {
        Map<String, EsData> dataMap = getCacheEsDataMap(companyId);
        if (null == dataMap || dataMap.isEmpty()) {
            return null;
        }
        return dataMap.get(depotId + "-" + deviceId);
    }
 
    @Override
    public void updateCacheEsData(EsData esData) {
        Map<String, EsData> dataMap = getCacheEsDataMap(esData.getCompanyId());
        if (null == dataMap) {
            dataMap = new HashMap<String, EsData>();
        } else {
            dataMap.put(esData.getDepotId() + "-" + esData.getDeviceId(), esData);
        }
        String deptId = ContextUtil.subDeptId(null);
        String key = RedisConst.buildKey(esData.getCompanyId(),
                RedisConst.KEY_ES_DATA_MAP, deptId);
        redisUtil.set(key, dataMap);
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, EsData> getCacheEsDataMap(String companyId) {
        String deptId = ContextUtil.subDeptId(null);
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_ES_DATA_MAP, deptId);
        Map<String, EsData> result = (Map<String, EsData>) redisUtil.get(key);
        if (null == result) {
            log.info("系统中没有获取到能耗数据缓存信息!!");
        }
        return result;
    }
 
    @Override
    public void saveAndUpdateInc(EsData data) {
 
        // 获取上次的缓存信息,如果缓存中的数据跟当前数据时间差小于12个小时,则计算增量,否则增量为0,增量不可为负数。
        EsData oldData = getCacheEsDepotData(data.getCompanyId(), data.getDepotId(), data.getDeviceId());
        if (null != oldData && oldData.getEs() > 0) {
            long time = data.getUpdateTime().getTime() - oldData.getUpdateTime().getTime();
            if (time <= 12 * 60 * 60 * 1001) {
                data.setEpInc(data.getEp() - oldData.getEp());
                data.setEpInc(data.getEpInc() > 0 ? data.getEpInc() : 0.0);
 
                data.setEqInc(data.getEq() - oldData.getEq());
                data.setEqInc(data.getEqInc() > 0 ? data.getEqInc() : 0.0);
 
                data.setEsInc(data.getEs() - oldData.getEs());
                data.setEsInc(data.getEsInc() > 0 ? data.getEsInc() : 0.0);
            }
        }
 
        if (null == data.getUpdateTime()) {
            data.setUpdateTime(new Date());
        }
 
        if (StringUtils.isEmpty(data.getId())) {
            data.setId(data.getCompanyId() + "-" + data.getDepotId() + "-" + data.getDeviceId() + "-"
                    + ContextUtil.getCurTimeMillis());
        }
 
        // 更新缓存
        this.updateCacheEsData(data);
 
        // 持久化数据库
        esServiceMapper.insertData(data);
    }
 
    @SuppressWarnings("static-access")
    @Override
    public ChartLine queryScreenEsChart(EsParam esParam, boolean toWeb) {
 
        if (StringUtils.isEmpty(esParam.getCompanyId())) {
            esParam.setCompanyId(ContextUtil.getDefaultCompanyId());
        }
        if (StringUtils.isEmpty(esParam.getDeptId())) {
            esParam.setDeptId(ContextUtil.subDeptId(null));
        }
        List<EsSummary> list = this.listSumGroupByDay(esParam);
 
        if (null == list || list.isEmpty())
            return null;
 
        // 从新排序
        list.sort((a, b) -> a.getGroupTag().compareTo(b.getGroupTag()));
 
        // 调整对象,groupTag = yyyy-MM-dd
        ChartLine line = new ChartLine();
        ChartSeries series = new ChartSeries();
        for (EsSummary sum : list) {
            line.getXaxisData().add(sum.getGroupTag().substring(5));
            series.getData().add(String.valueOf(sum.getEpSum()));
        }
        line.getSeries().add(series);
 
        if (toWeb) {
            // 大屏的数据存放缓存,并推送页面
            WebSocketPacket packet = new WebSocketPacket();
            packet.setCompanyId(ContextUtil.getDefaultCompanyId());
            packet.setBizType(BizType.SCREEN.getCode());
            packet.setDeptId(esParam.getDeptId());
            packet.setBizId(BizType.ES.getCode());
            packet.setData(line);
            WebSocketServer.sendByPocket(packet);
        }
        return line;
    }
 
}