tjx
昨天 a53e6872c70ba7c6c870627007ece21df9bcedbc
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
package com.hk.NumericalCollection.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hk.NumericalCollection.config.DataAcquisitionConfiguration;
import com.hk.NumericalCollection.dto.*;
import com.hk.NumericalCollection.entity.*;
import com.hk.NumericalCollection.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
 
 
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class NumericalNoOrderServiceImpl implements INumericalNoOrderService {
 
 
    private final ApiService apiService;
 
    private final DeviceService deviceService;
 
    private final DevMachineService devMachineService;
 
    private final DeviceStatusService deviceStatusService;
 
    private final MesOrderSelectService orderSelectService;
 
    private final MesNumericalService mesNumericalServicen;
 
    private final DeviceErrorService deviceErrorService;
 
    @Override
    public boolean SoDeviceList() throws Exception {
        ApiResponse<?> sig = apiService.getSig(DataAcquisitionConfiguration.PUBLIC_KEY, DataAcquisitionConfiguration.APP_ID);
 
        ApiResponseCode apiResponseCode = ApiResponseCode.fromCode(sig.getCode());
        if (!apiResponseCode.getFlag().booleanValue())
            return false;
        ApiRequestBody requestBody = new ApiRequestBody();
        requestBody.setExt("");
        requestBody.setVistApi("soDeviceList");
        requestBody.setSig(sig.getData());
        requestBody.setAppId(DataAcquisitionConfiguration.APP_ID);
        requestBody.setTime("1671418916");
        Params params = new Params();
        params.setPageNo(1);
        params.setPageSize(999);
        params.setStatus(-99);
        requestBody.setParams(params);
        ApiResponse<Device> response = this.apiService.sendListRequest(requestBody, Device.class);
        ApiResponseCode isTrue = ApiResponseCode.fromCode(response.getCode());
 
        if (!isTrue.getFlag()) {
            throw new Exception("在请求数采接口时出现:" + isTrue.getDescription());
        }
 
        List<Device> deviceList = response.getList();
 
        List<String> uidList = deviceList.stream().map(Device::getDevNo).collect(Collectors.toList());
 
        LambdaUpdateWrapper<Device> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Device::getDevNo, uidList);
        deviceService.remove(wrapper);
        return deviceService.saveOrUpdateBatch(deviceList);
    }
 
    @Override
    public void getDeviceRealTimeData(String uid) throws Exception {
        ApiDataResult<DeviceRealTimeData> response = getDeviceRealTimeDataApiDataResult(uid);
        if (response == null)
            return;
        DeviceRealTimeData data = (DeviceRealTimeData) response.getData();
        setMesOrderSta(data);
    }
 
    @Override
    public void getDeviceDayCount(String uid, String date) throws IOException {
        ApiResponse<?> sig = apiService.getSig(DataAcquisitionConfiguration.PUBLIC_KEY, DataAcquisitionConfiguration.APP_ID);
 
        ApiResponseCode apiResponseCode = ApiResponseCode.fromCode(sig.getCode());
        if (apiResponseCode != null && !apiResponseCode.getFlag()) {
            return; // 如果响应不成功则退出
        }
 
        ApiRequestBody requestBody = new ApiRequestBody();
        requestBody.setExt("");
        requestBody.setVistApi("getDeviceDayCount");
        requestBody.setSig(sig.getData());
        requestBody.setAppId(DataAcquisitionConfiguration.APP_ID);
        requestBody.setTime("1671418916");
 
        Params params = new Params();
        params.setUid(uid);
        params.setDate(date);
 
        requestBody.setParams(params);
 
        ApiDataResult<DeviceDayCount> response = this.apiService.sendDataRequest(requestBody, DeviceDayCount.class);
 
        apiResponseCode = ApiResponseCode.fromCode(response.getCode());
 
        if (apiResponseCode == null || !apiResponseCode.getFlag()) {
            return;
        }
 
        DeviceDayCount data = response.getData();
 
        if (data.getFaultTime() == null && data.getWaitTime() == null) {
            return;
        }
 
        LambdaQueryWrapper<DevMachine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DevMachine::getDevNo, uid);
 
        DevMachine devMachine = devMachineService.getOne(wrapper);
        if (devMachine == null) {
            return;
        }
 
        double v = (data.getFaultTime() + data.getWaitTime()) * 60;
 
        if (v <= 0) {
            return;
        }
 
        LambdaQueryWrapper<MesOrderSelect> lambdaUpdateWrapper1 = new LambdaQueryWrapper<>();
 
        lambdaUpdateWrapper1.eq(MesOrderSelect::getEditDate, date)
                .eq(MesOrderSelect::getMachineNo, devMachine.getMachineNo());
 
        long count = orderSelectService.count(lambdaUpdateWrapper1);
 
        if (count <= 0L)
            return;
 
        LambdaUpdateWrapper<MesOrderSelect> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
 
        v = Math.round(v / count * 100.0D) / 100.0D;
 
        lambdaUpdateWrapper
                .set(MesOrderSelect::getTjCount, 1)
                .set(MesOrderSelect::getTjTime, v)
                .eq(MesOrderSelect::getEditDate, date)
                .eq(MesOrderSelect::getMachineNo, devMachine.getMachineNo());
 
        orderSelectService.update(lambdaUpdateWrapper);
    }
 
    @Override
    public ApiDataResult<DeviceRealTimeData> getDeviceRealTimeDataApiDataResult(String uid) throws Exception {
        // 获取sig
        ApiResponse<?> sig = apiService.getSig(DataAcquisitionConfiguration.PUBLIC_KEY, DataAcquisitionConfiguration.APP_ID);
 
        ApiResponseCode apiResponseCode = ApiResponseCode.fromCode(sig.getCode());
        if (apiResponseCode == null || !apiResponseCode.getFlag().booleanValue())
            return null;
 
        ApiRequestBody requestBody = new ApiRequestBody();
        requestBody.setExt("");
        requestBody.setVistApi("getDeviceRealTimeData");
        requestBody.setSig(sig.getData());
        requestBody.setAppId(DataAcquisitionConfiguration.APP_ID);
        requestBody.setTime("1671418916");
 
        Params params = new Params();
        params.setUid(uid);
 
        requestBody.setParams(params);
 
        ApiDataResult<DeviceRealTimeData> response = this.apiService.sendDataRequest(requestBody, DeviceRealTimeData.class);
 
        ApiResponseCode isTrue = ApiResponseCode.fromCode(response.getCode());
        if (isTrue != null && !isTrue.getFlag().booleanValue())
            throw new Exception("在请求数采接口时出现:" + isTrue.getDescription());
 
        return response;
    }
 
    @Override
    public boolean RefreshDev(NumbericalDto barcode) {
        if (StrUtil.isNullOrUndefined(barcode.getMachineNo()))
            return false;
        try {
 
            LambdaQueryWrapper<DevMachine> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DevMachine::getMachineNo, barcode.getMachineNo());
 
            DevMachine one = devMachineService.getOne(queryWrapper, false);
 
            if (one == null)
                return false;
 
            getDeviceRealTimeData(one.getDevNo());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
    private void setMesOrderSta(DeviceRealTimeData data) {
        LambdaQueryWrapper<DevMachine> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DevMachine::getDevNo, data.getDevNo());
 
        DevMachine one = devMachineService.getOne(wrapper, false);
 
        if (one == null) {
            System.out.println("异常1:当前机器没有绑定机台");
            return;
        }
 
        String formattedDate = data.getLastEditDate().substring(0, 10);
 
        setOrder(data, formattedDate, one);
    }
 
    @Override
    public void getDeviceErrorList(String uid, String startDate, String endDate) throws Exception {
        ApiResponse<?> sig = apiService.getSig(DataAcquisitionConfiguration.PUBLIC_KEY, DataAcquisitionConfiguration.APP_ID);
 
        ApiResponseCode apiResponseCode = ApiResponseCode.fromCode(sig.getCode());
        if (apiResponseCode == null || !apiResponseCode.getFlag()) {
            return;
        }
 
        ApiRequestBody requestBody = new ApiRequestBody();
        requestBody.setExt("");
        requestBody.setVistApi("getDeviceErrorList");
        requestBody.setSig(sig.getData());
        requestBody.setAppId(DataAcquisitionConfiguration.APP_ID);
        requestBody.setTime("1671418916");
 
        Params params = new Params();
        params.setUid(uid);
        params.setStartDate(startDate);
        params.setEndDate(endDate);
 
        requestBody.setParams(params);
 
        ApiResponse<DeviceError> response = this.apiService.sendListRequest(requestBody, DeviceError.class);
 
        apiResponseCode = ApiResponseCode.fromCode(response.getCode());
        if (apiResponseCode == null || !apiResponseCode.getFlag()) {
            throw new Exception("在请求数采接口时出现:" + apiResponseCode.getDescription());
        }
 
        List<DeviceError> errorList = response.getList();
        if (errorList != null && !errorList.isEmpty()) {
            List<Integer> ids = errorList.stream().map(DeviceError::getId).collect(Collectors.toList());
            deviceErrorService.removeByIds(ids);
            deviceErrorService.saveBatch(errorList);
        }
    }
 
    private void setOrder(DeviceRealTimeData data, String formattedDate, DevMachine one) {
        int outItemNum = 1;
        DeviceStatus status = new DeviceStatus();
        status.setDevNo(data.getDevNo());
        status.setStatus(data.getStatus());
        status.setOutput(data.getOutput());
        status.setOnlineTime(data.getOnlineTime());
        status.setTodayOnlineTime(data.getTodayOnlineTime());
        status.setPower(data.getTodayPower());
        status.setTodayPower(data.getTodayPower());
        status.setFaultNum(data.getFaultNum());
        status.setTodayFaultNum(data.getTodayFaultNum());
        status.setEditDate(formattedDate);
        String d15 = data.getD15();
        if (StrUtil.isEmpty(d15))
            d15 = "0";
        status.setTodayOutput(Double.parseDouble(d15) * outItemNum);
        status.setWorkStartDate(data.getWorkstartDate());
        status.setWorkEndDate(data.getWorkendDate());
        status.setDeviceTypeClassItemId(data.getDeviceTypeClassItemId());
        status.setWorkOutput(Double.parseDouble(data.getWorkOutPut()));
        status.setHourOutput(Double.parseDouble(data.getHourOutPut()));
        status.setBadoutput(Double.parseDouble(data.getBadoutput()));
        status.setTodayBadoutput(Double.parseDouble(data.getTodayBadoutput()));
        status.setWorkBadoutput(Double.parseDouble(data.getWorkBadoutput()));
        status.setHourBadoutput(Double.parseDouble(data.getHourBadoutput()));
        boolean nullOrUndefined = StrUtil.isNullOrUndefined(data.getTodayRuntime());
        if (!nullOrUndefined)
            status.setTodayRunTime(Double.parseDouble(data.getTodayRuntime()));
        deviceStatusService.save(status);
        MesNumerical numerical = new MesNumerical();
        numerical.setEditDate(formattedDate);
        numerical.setMachineNo(one.getMachineNo());
        numerical.setCjNum(Integer.valueOf(Integer.parseInt(d15) * outItemNum));
        numerical.setCjTiem(data.getLastEditDate());
        mesNumericalServicen.save(numerical);
    }
}