调用第三方数据校验接口

This commit is contained in:
caozehui
2026-06-22 09:47:06 +08:00
parent 1ff2d373f5
commit d746e68d63
13 changed files with 340 additions and 4 deletions

View File

@@ -29,6 +29,7 @@ import com.njcn.gather.device.service.IPqDevSubService;
import com.njcn.gather.plan.pojo.po.AdPlan;
import com.njcn.gather.plan.service.IAdPlanService;
import com.njcn.gather.result.pojo.enums.ResultUnitEnum;
import com.njcn.gather.result.service.IResultService;
import com.njcn.gather.script.pojo.param.PqScriptCheckDataParam;
import com.njcn.gather.script.pojo.param.PqScriptIssueParam;
import com.njcn.gather.script.pojo.po.SourceIssue;
@@ -46,6 +47,7 @@ import com.njcn.gather.system.dictionary.pojo.po.DictData;
import com.njcn.gather.system.dictionary.service.IDictDataService;
import com.njcn.gather.system.pojo.enums.DicDataEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
@@ -80,6 +82,10 @@ public class SocketDevResponseService {
private final IAdPlanService adPlanService;
private final IDictDataService dictDataService;
private final IPqSourceService pqSourceService;
private final IResultService resultService;
@Value("${dataCheck.enable}")
private Boolean dataCheck;
/**
* 存储的装置相序数据
@@ -1380,6 +1386,9 @@ public class SocketDevResponseService {
List<String> valueType = iPqScriptCheckDataService.getValueType(checkDataParam);
iPqDevService.updateResult(param.getDevIds(), valueType, param.getCode(), param.getUserId(), param.getTemperature(), param.getHumidity(), true);
if (dataCheck) {
resultService.tryNotifyThirdPartyAfterFormalTest(param);
}
CnSocketUtil.quitSend(param);
// 数模式检测全部小项完成 → 释放锁,避免用户必须点"停止"才能让出
DetectionLockManager.getInstance()
@@ -1806,6 +1815,7 @@ public class SocketDevResponseService {
FormalTestManager.overload = getOverloadResult(param);
FormalTestManager.checkStartTime = LocalDateTime.now();
FormalTestManager.reCheckType = param.getReCheckType();
}

View File

@@ -219,4 +219,9 @@ public class FormalTestManager {
* 检测开始时间
*/
public static LocalDateTime checkStartTime;
/**
* 数模式 检测类型"1"-"全部检测" , "2"-"不合格项复检"
*/
public static String reCheckType;
}

View File

@@ -3,10 +3,14 @@ package com.njcn.gather.device.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.njcn.gather.device.pojo.po.PqDevSub;
import java.time.LocalDateTime;
import java.util.List;
/**
* @author caozehui
* @date 2025-07-04
*/
public interface IPqDevSubService extends IService<PqDevSub> {
LocalDateTime resolveBatchMaxCheckEndTime(List<String> devIds);
}

View File

@@ -26,6 +26,7 @@ import com.njcn.common.pojo.poi.PullDown;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.EncryptionUtil;
import com.njcn.db.mybatisplus.constant.DbConstant;
import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
import com.njcn.gather.detection.util.socket.FormalTestManager;
import com.njcn.gather.device.mapper.PqDevMapper;
import com.njcn.gather.device.pojo.dto.DataCheckResDTO;
@@ -509,8 +510,8 @@ public class PqDevServiceImpl extends ServiceImpl<PqDevMapper, PqDev> implements
LambdaUpdateWrapper<PqDevSub> wrapper = new LambdaUpdateWrapper<PqDevSub>()
.set(PqDevSub::getCheckResult, result.get(pqDevVo.getId()))
.set(StrUtil.isNotBlank(userId), PqDevSub::getCheckBy, userId)
.set(updateCheckNum, PqDevSub::getCheckEndTime, LocalDateTime.now())
.set(updateCheckNum && ObjectUtil.isNotNull(FormalTestManager.checkStartTime), PqDevSub::getCheckStartTime, FormalTestManager.checkStartTime)
.set(updateCheckNum && SourceOperateCodeEnum.ALL_TEST.getValue().equals(FormalTestManager.reCheckType), PqDevSub::getCheckEndTime, LocalDateTime.now())
.set(updateCheckNum && SourceOperateCodeEnum.ALL_TEST.getValue().equals(FormalTestManager.reCheckType) && ObjectUtil.isNotNull(FormalTestManager.checkStartTime), PqDevSub::getCheckStartTime, FormalTestManager.checkStartTime)
.eq(PqDevSub::getDevId, pqDevVo.getId());
String currrentScene = sysTestConfigService.getCurrrentScene();
if (SceneEnum.PROVINCE_PLATFORM.getValue().equals(currrentScene)) {

View File

@@ -1,13 +1,20 @@
package com.njcn.gather.device.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.gather.device.pojo.po.PqDevSub;
import com.njcn.gather.device.mapper.PqDevSubMapper;
import com.njcn.gather.device.pojo.po.PqDevSub;
import com.njcn.gather.device.service.IPqDevSubService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
/**
* @author caozehui
* @date 2025-07-04
@@ -16,5 +23,19 @@ import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class PqDevSubServiceImpl extends ServiceImpl<PqDevSubMapper, PqDevSub> implements IPqDevSubService {
@Override
public LocalDateTime resolveBatchMaxCheckEndTime(List<String> devIds) {
if (CollUtil.isEmpty(devIds)) {
return null;
}
return this.list(new LambdaQueryWrapper<PqDevSub>()
.in(PqDevSub::getDevId, devIds)
.isNotNull(PqDevSub::getCheckEndTime))
.stream()
.map(PqDevSub::getCheckEndTime)
.filter(Objects::nonNull)
.max(Comparator.naturalOrder())
.orElse(null);
}
}

View File

@@ -163,6 +163,18 @@ public class ResultController extends BaseController {
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
}
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@PostMapping("/createChecksquareTask")
@ApiOperation("调用第三方数模数据检测接口")
@ApiImplicitParam(name = "devId", value = "设备id", required = true)
public HttpResult<String> createChecksquareTask(@RequestParam("devId") String devId) {
String methodDescribe = getMethodDescribe("createChecksquareTask");
LogUtil.njcnDebug(log, "{}调用第三方数模数据检测接口设备id为{}", methodDescribe, devId);
String result = resultService.createChecksquareTaskByDevId(devId);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
}
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
@GetMapping("/getMonitorDataSourceResult")

View File

@@ -0,0 +1,13 @@
package com.njcn.gather.result.pojo.param;
import lombok.Data;
import java.util.List;
@Data
public class DataCheckRequest {
private List<String> lineIds;
private List<String> indicatorCodes;
private String timeStart;
private String timeEnd;
}

View File

@@ -0,0 +1,22 @@
package com.njcn.gather.result.pojo.vo;
import lombok.Builder;
import lombok.Value;
import java.time.LocalDateTime;
import java.util.List;
/**
* 数据校验通知 上下文
*/
@Value
@Builder
public class DataCheckNotifyContext {
String notifyKey;
String planId;
List<String> sortedDevIds;
List<String> lineIds;
LocalDateTime timeStart;
LocalDateTime timeEnd;
String reCheckType;
}

View File

@@ -0,0 +1,75 @@
package com.njcn.gather.result.service;
import com.njcn.gather.result.pojo.param.DataCheckRequest;
import com.njcn.gather.result.pojo.vo.DataCheckNotifyContext;
import com.njcn.gather.result.service.impl.ResultServiceImpl;
import com.njcn.http.util.RestTemplateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Map;
@Slf4j
@Component
@RequiredArgsConstructor
public class DataCheckAsyncNotifier {
private final static String url="http://172.17.100.111:18091/steady/checksquare/create";
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final RestTemplateUtil restTemplateUtil;
@Async
public void notifyAsync(DataCheckNotifyContext context, Map<String, ResultServiceImpl.NotifyState> notifyStates) {
notifyDirect(context, notifyStates);
}
void notifyDirect(DataCheckNotifyContext context, Map<String, ResultServiceImpl.NotifyState> notifyStates) {
int maxRetries = 3;
for (int attempt = 1; attempt <= maxRetries; attempt++) {
try {
DataCheckRequest request = new DataCheckRequest();
request.setLineIds(context.getLineIds());
request.setIndicatorCodes(Collections.emptyList());
request.setTimeStart(FORMATTER.format(context.getTimeStart()));
request.setTimeEnd(FORMATTER.format(context.getTimeEnd()));
restTemplateUtil.postJson(url, request, String.class);
notifyStates.put(context.getNotifyKey(),
new ResultServiceImpl.NotifyState(ResultServiceImpl.NotifyStatus.SUCCESS, attempt - 1, null, LocalDateTime.now()));
log.info("checksquare notify success, key={}, planId={}, lineCount={}", context.getNotifyKey(), context.getPlanId(), context.getLineIds().size());
return;
} catch (Exception ex) {
int failCount = attempt;
if (failCount > maxRetries) {
notifyStates.put(context.getNotifyKey(),
new ResultServiceImpl.NotifyState(ResultServiceImpl.NotifyStatus.FAIL, maxRetries, ex.getMessage(), LocalDateTime.now()));
log.error("checksquare notify failed, key={}, failCount={}", context.getNotifyKey(), maxRetries, ex);
return;
}
if (!sleepBeforeRetry(context, failCount)) {
notifyStates.put(context.getNotifyKey(),
new ResultServiceImpl.NotifyState(ResultServiceImpl.NotifyStatus.FAIL, failCount, "retry interrupted", LocalDateTime.now()));
return;
}
}
}
}
private boolean sleepBeforeRetry(DataCheckNotifyContext context, int failCount) {
long delayMillis = (long) Math.pow(2, failCount) * 1000L;
log.warn("checksquare notify retry scheduled, key={}, failCount={}, delayMillis={}", context.getNotifyKey(), failCount, delayMillis);
try {
Thread.sleep(delayMillis);
return true;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return false;
}
}
}

View File

@@ -1,5 +1,6 @@
package com.njcn.gather.result.service;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.device.pojo.vo.PqDevVO;
import com.njcn.gather.report.pojo.DevReportParam;
import com.njcn.gather.report.pojo.result.ContrastTestResult;
@@ -17,6 +18,10 @@ import java.util.Map;
* @data 2024-12-30
*/
public interface IResultService {
void tryNotifyThirdPartyAfterFormalTest(PreDetectionParam param);
String createChecksquareTaskByDevId(String devId);
/**
* 检测详情表单头
*

View File

@@ -23,6 +23,7 @@ import com.njcn.common.utils.PubUtils;
import com.njcn.gather.detection.handler.SocketContrastResponseService;
import com.njcn.gather.detection.pojo.enums.DetectionCodeEnum;
import com.njcn.gather.detection.pojo.enums.ResultEnum;
import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.detection.pojo.po.AdPair;
import com.njcn.gather.detection.pojo.po.DevData;
@@ -31,12 +32,15 @@ import com.njcn.gather.detection.pojo.vo.DetectionData;
import com.njcn.gather.detection.service.IAdPariService;
import com.njcn.gather.detection.service.impl.DetectionServiceImpl;
import com.njcn.gather.detection.util.socket.CnSocketUtil;
import com.njcn.gather.detection.util.socket.FormalTestManager;
import com.njcn.gather.device.pojo.enums.CommonEnum;
import com.njcn.gather.device.pojo.enums.PatternEnum;
import com.njcn.gather.device.pojo.po.PqDev;
import com.njcn.gather.device.pojo.po.PqDevSub;
import com.njcn.gather.device.pojo.po.PqStandardDev;
import com.njcn.gather.device.pojo.vo.PqDevVO;
import com.njcn.gather.device.service.IPqDevService;
import com.njcn.gather.device.service.IPqDevSubService;
import com.njcn.gather.device.service.IPqStandardDevService;
import com.njcn.gather.err.service.IPqErrSysService;
import com.njcn.gather.monitor.pojo.po.PqMonitor;
@@ -55,8 +59,10 @@ import com.njcn.gather.report.pojo.enums.PowerIndexEnum;
import com.njcn.gather.report.pojo.result.ContrastTestResult;
import com.njcn.gather.report.pojo.result.SingleTestResult;
import com.njcn.gather.result.pojo.enums.ResultUnitEnum;
import com.njcn.gather.result.pojo.param.DataCheckRequest;
import com.njcn.gather.result.pojo.param.ResultParam;
import com.njcn.gather.result.pojo.vo.*;
import com.njcn.gather.result.service.DataCheckAsyncNotifier;
import com.njcn.gather.result.service.IResultService;
import com.njcn.gather.script.mapper.PqScriptMapper;
import com.njcn.gather.script.pojo.param.PqScriptCheckDataParam;
@@ -85,8 +91,10 @@ import com.njcn.gather.system.dictionary.service.IDictDataService;
import com.njcn.gather.system.dictionary.service.IDictTreeService;
import com.njcn.gather.system.pojo.enums.DicDataEnum;
import com.njcn.gather.util.StorageUtil;
import com.njcn.http.util.RestTemplateUtil;
import com.njcn.web.utils.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -96,6 +104,7 @@ import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -132,6 +141,13 @@ public class ResultServiceImpl implements IResultService {
private final IPqMonitorService pqMonitorService;
private final IPqErrSysService pqErrSysService;
private final IPqStandardDevService standardDevService;
private final IPqDevSubService pqDevSubService;
private final DataCheckAsyncNotifier dataCheckAsyncNotifier;
private final RestTemplateUtil restTemplateUtil;
public static final String CHECKSQUARE_CREATE_URL = "http://172.17.100.111:18091/steady/checksquare/create";
private static final DateTimeFormatter CHECKSQUARE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final Map<String, NotifyState> notifyStates = new ConcurrentHashMap<>();
/**
* 谐波类code取树形字典表中的code
@@ -139,6 +155,89 @@ public class ResultServiceImpl implements IResultService {
private final List<String> HARMONIC_TYPE_CODE = Arrays.asList("HV", "HI", "HP", "HSV", "HSI");
@Override
public void tryNotifyThirdPartyAfterFormalTest(PreDetectionParam param) {
if (param == null || !SourceOperateCodeEnum.ALL_TEST.getValue().equals(FormalTestManager.reCheckType)) {
return;
}
if (ObjectUtil.isNull(FormalTestManager.checkStartTime)
|| StrUtil.isBlank(param.getPlanId())
|| CollUtil.isEmpty(param.getDevIds())
|| CollUtil.isEmpty(FormalTestManager.monitorIdListComm)) {
return;
}
LocalDateTime batchEndTime = pqDevSubService.resolveBatchMaxCheckEndTime(param.getDevIds());
if (ObjectUtil.isNull(batchEndTime)) {
log.warn("checksquare notify skipped, batch end time is empty, planId={}, devIds={}", param.getPlanId(), param.getDevIds());
return;
}
List<String> sortedDevIds = param.getDevIds().stream().sorted().collect(Collectors.toList());
String notifyKey = param.getPlanId() + "|" + String.join(",", sortedDevIds) + "|"
+ CHECKSQUARE_TIME_FORMATTER.format(FormalTestManager.checkStartTime) + "|" + FormalTestManager.reCheckType;
if (notifyStates.get(notifyKey) != null) {
return;
}
if (notifyStates.putIfAbsent(notifyKey, NotifyState.running()) != null) {
return;
}
DataCheckNotifyContext context = DataCheckNotifyContext.builder()
.notifyKey(notifyKey)
.planId(param.getPlanId())
.sortedDevIds(sortedDevIds)
.lineIds(new ArrayList<>(FormalTestManager.monitorIdListComm))
.timeStart(FormalTestManager.checkStartTime)
.timeEnd(batchEndTime)
.reCheckType(FormalTestManager.reCheckType)
.build();
dataCheckAsyncNotifier.notifyAsync(context, notifyStates);
}
@Override
public String createChecksquareTaskByDevId(String devId) {
if (StrUtil.isBlank(devId)) {
throw new BusinessException(CommonResponseEnum.FAIL, "设备id不能为空");
}
List<PqMonitor> monitorList = pqMonitorService.listPqMonitorByDevIds(Collections.singletonList(devId));
if (CollUtil.isEmpty(monitorList)) {
throw new BusinessException(CommonResponseEnum.FAIL, "该设备监测点不存在");
}
PqDevSub devSub = pqDevSubService.getOne(new LambdaQueryWrapper<PqDevSub>()
.eq(PqDevSub::getDevId, devId), false);
if (ObjectUtil.isNull(devSub)
|| ObjectUtil.isNull(devSub.getCheckStartTime())
|| ObjectUtil.isNull(devSub.getCheckEndTime())) {
throw new BusinessException(CommonResponseEnum.FAIL, "该设备检测开始时间或结束时间为空");
}
DataCheckRequest request = new DataCheckRequest();
request.setLineIds(monitorList.stream().map(PqMonitor::getId).collect(Collectors.toList()));
request.setIndicatorCodes(Collections.emptyList());
request.setTimeStart(CHECKSQUARE_TIME_FORMATTER.format(devSub.getCheckStartTime()));
request.setTimeEnd(CHECKSQUARE_TIME_FORMATTER.format(devSub.getCheckEndTime()));
return restTemplateUtil.postJson(CHECKSQUARE_CREATE_URL, request, String.class);
}
public enum NotifyStatus {
RUNNING, SUCCESS, FAIL
}
@Value
public static class NotifyState {
NotifyStatus status;
int failCount;
String lastError;
LocalDateTime triggerTime;
public static NotifyState running() {
return new NotifyState(NotifyStatus.RUNNING, 0, null, LocalDateTime.now());
}
}
@Override
public FormContentVO getFormContent(ResultParam.QueryParam queryParam) {
FormContentVO formContentVO = new FormContentVO();