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);
|
}
|
}
|