|
|
@@ -1,10 +1,41 @@
|
|
|
package com.hdkj.lt.bf.service.impl;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.extension.service.IService;
|
|
|
+import cn.hutool.core.lang.Snowflake;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.hdkj.hussar.ApiResponse;
|
|
|
+import com.hdkj.lt.base.constants.CommonConstant;
|
|
|
+import com.hdkj.lt.bf.entity.FhzgSeCurrentEvent;
|
|
|
import com.hdkj.lt.bf.entity.FhzgSeEventStateEstimation;
|
|
|
+import com.hdkj.lt.bf.mapper.FhzgSeCurrentEventMapper;
|
|
|
import com.hdkj.lt.bf.mapper.FhzgSeEventStateEstimationMapper;
|
|
|
+import com.hdkj.lt.bf.service.FhzgSeEventStateEstimationService;
|
|
|
+import com.hdkj.lt.core.bizms.modle.dto.StateEstimation;
|
|
|
+import com.hdkj.lt.core.bizms.modle.po.Jxz;
|
|
|
+import com.hdkj.lt.core.bizms.modle.request.StateEstimationRequest;
|
|
|
+import com.hdkj.lt.core.sys.dao.JxzMapper;
|
|
|
+import com.hdkj.lt.feign.IStateEstimationApiClient;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.compress.utils.Lists;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author Annie
|
|
|
@@ -12,9 +43,117 @@ import org.springframework.stereotype.Service;
|
|
|
* @createDate 2026-08-12 11:46:35
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
public class FhzgSeEventStateEstimationServiceImpl extends ServiceImpl<FhzgSeEventStateEstimationMapper, FhzgSeEventStateEstimation>
|
|
|
- implements IService<FhzgSeEventStateEstimation> {
|
|
|
+ implements FhzgSeEventStateEstimationService {
|
|
|
+
|
|
|
+ private final JxzMapper jxzMapper;
|
|
|
+ private final IStateEstimationApiClient stateEstimationApiClient;
|
|
|
+ private final FhzgSeCurrentEventMapper currentEventMapper;
|
|
|
+ private static final int RETRY_COUNT = 2;
|
|
|
+ private static final DateTimeFormatter DT_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ private final ObjectMapper writeMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveStateEstimation(FhzgSeEventStateEstimation estimation) {
|
|
|
+ // 1. 查询接线组线路
|
|
|
+ log.info("========= 触发状估数据保存 =========");
|
|
|
+ Long eventId = estimation.getEventId();
|
|
|
+ String feederId = estimation.getFeederId();
|
|
|
+ List<Jxz> jxzList = jxzMapper.queryJxzXl(feederId);
|
|
|
+ List<String> groupFeederIds = jxzList.stream()
|
|
|
+ .map(Jxz::getFeederId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ //没有接线组只存当前线路数据
|
|
|
+ if (groupFeederIds.isEmpty()) {
|
|
|
+ groupFeederIds = Collections.singletonList(feederId);
|
|
|
+ }
|
|
|
+ log.info("eventId = {} , 接线组线路数量【{}】条,异常触发时间{}", eventId, groupFeederIds.size(),estimation.getTriggerTime());
|
|
|
+ Snowflake snowflake = IdUtil.getSnowflake(1,1);
|
|
|
+ List<FhzgSeEventStateEstimation> stateEstimationList = Lists.newArrayList();
|
|
|
+ for (String feeder : groupFeederIds) {
|
|
|
+ StateEstimationRequest req = buildStateEstimationReq(feeder,estimation.getTriggerTime());
|
|
|
+// StateEstimation stateEstimation = callWithRetry(feeder, req);
|
|
|
+ StateEstimation stateEstimation = stateEstimationApiClient.queryByFeeder(req).getData();
|
|
|
+// try{
|
|
|
+// String str = StreamUtils.copyToString(new ClassPathResource("15DKX-11729.txt").getInputStream(), StandardCharsets.UTF_8);
|
|
|
+// JSON.parseObject(str,StateEstimation.class);
|
|
|
+// JsonNode root = writeMapper.readTree(str);
|
|
|
+// JsonNode data = root.get("data");
|
|
|
+// stateEstimation = writeMapper.treeToValue(data,StateEstimation.class);
|
|
|
+// }catch (IOException e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+ log.info("response = {}", JSON.toJSONString(stateEstimation));
|
|
|
+ if(Objects.nonNull(stateEstimation)){
|
|
|
+ FhzgSeEventStateEstimation entity = new FhzgSeEventStateEstimation();
|
|
|
+ entity.setId(snowflake.nextId()).setEventId(eventId).setFeederId(feeder).setAlarmType(estimation.getAlarmType())
|
|
|
+ .setCreateTime(new Date());
|
|
|
+ try {
|
|
|
+ String jsonStr = writeMapper.writeValueAsString(stateEstimation);
|
|
|
+ entity.setStateEstimationJson(jsonStr);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ log.warn("馈线={} 状估数据 序列化失败: {}", feeder, e.getMessage());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ stateEstimationList.add(entity);
|
|
|
+ try {
|
|
|
+ this.save(entity);
|
|
|
+ log.info("馈线={} ,事件={} ,状估数据 保存成功!", feeder, eventId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("馈线={} ,事件={} ,落库失败: {}", feeder, eventId, e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("事件={} ,保存状估数据共: 【{}】条", eventId, stateEstimationList.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveByList() {
|
|
|
+ LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7);
|
|
|
+ List<FhzgSeCurrentEvent> list = currentEventMapper.selectList(new LambdaQueryWrapper<FhzgSeCurrentEvent>().ge(FhzgSeCurrentEvent::getTriggerTime, sevenDaysAgo));
|
|
|
+ for (FhzgSeCurrentEvent currentEvent : list) {
|
|
|
+ FhzgSeEventStateEstimation estimation = new FhzgSeEventStateEstimation();
|
|
|
+ estimation.setEventId(currentEvent.getId()).setFeederId(currentEvent.getFeederId())
|
|
|
+ .setTriggerTime(currentEvent.getTriggerTime().format(DT_FMT)).setAlarmType(currentEvent.getAlarmType());
|
|
|
+ saveStateEstimation(estimation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建参数
|
|
|
+ * @param pointTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private StateEstimationRequest buildStateEstimationReq(String feederId,String pointTime){
|
|
|
+ StateEstimationRequest req = new StateEstimationRequest();
|
|
|
+ req.setPsrIds(Collections.singletonList(feederId));
|
|
|
+ req.setContainerCode(Collections.singletonList(CommonConstant.SWITCH));
|
|
|
+ req.setStartTime(pointTime);
|
|
|
+ req.setEndTime(pointTime);
|
|
|
+ return req;
|
|
|
+ }
|
|
|
|
|
|
+ private StateEstimation callWithRetry(String feeder, StateEstimationRequest req) {
|
|
|
+ int maxAttempts = RETRY_COUNT + 1;
|
|
|
+ Exception last = null;
|
|
|
+ for (int attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
|
+ try {
|
|
|
+ ApiResponse<StateEstimation> resp = stateEstimationApiClient.queryByFeeder(req);
|
|
|
+ if (resp != null) {
|
|
|
+ return resp.getData();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ last = e;
|
|
|
+ log.warn(" 第{}/{}次: {}, psrIds ={}", attempt, maxAttempts, e.getMessage(), feeder);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("状估调用失败 (已重试 " + RETRY_COUNT+ " 次), psrIds ="
|
|
|
+ + feeder + ", 最后错误: " + (last == null ? "unknown" : last.getMessage()), last);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|