Selaa lähdekoodia

定时在去更新送样卡记录表

lingpeng.li 5 päivää sitten
vanhempi
sitoutus
2e0db50899

+ 14 - 11
zgztBus/jeecg-module-sbm/src/main/java/org/jeecg/modules/billet/billetOriginalProductRecord/service/impl/BilletOriginalProductRecordServiceImpl.java

@@ -391,19 +391,22 @@ public class BilletOriginalProductRecordServiceImpl extends ServiceImpl<BilletOr
                                 .eq(QualityInspectionStatistics::getChangeShiftId, shift.getId())
                 );
 
-                // 如果是补全任务(changeShiftId为空),且该班次已交班,则跳过
+                // 如果交班时间存在,且从交班时间到当前时间超过45分钟,才跳过;否则仍然重新统计、更新
                 if (StringUtils.isBlank(changeShiftId) && shift.getChangeShiftTime() != null) {
                     if (existing != null) {
-                        int dayEndCount = dayStartCount + existing.getClassTotalCount();
-                        BigDecimal dayEndWeight = dayStartWeight.add(existing.getClassTotalWeight());
-                        dayStartCount = dayEndCount;
-                        dayStartWeight = dayEndWeight;
-
-                        int monthEndCount = monthStartCount + existing.getClassTotalCount();
-                        BigDecimal monthEndWeight = monthStartWeight.add(existing.getClassTotalWeight());
-                        monthStartCount = monthEndCount;
-                        monthStartWeight = monthEndWeight;
-                        continue;
+                        long minutesSinceChange = (System.currentTimeMillis() - shift.getChangeShiftTime().getTime()) / (60 * 1000);
+                        if (minutesSinceChange >= 45) {
+                            int dayEndCount = dayStartCount + existing.getClassTotalCount();
+                            BigDecimal dayEndWeight = dayStartWeight.add(existing.getClassTotalWeight());
+                            dayStartCount = dayEndCount;
+                            dayStartWeight = dayEndWeight;
+
+                            int monthEndCount = monthStartCount + existing.getClassTotalCount();
+                            BigDecimal monthEndWeight = monthStartWeight.add(existing.getClassTotalWeight());
+                            monthStartCount = monthEndCount;
+                            monthStartWeight = monthEndWeight;
+                            continue;
+                        }
                     }
                 }
 

+ 139 - 0
zgztBus/jeecg-module-sbm/src/main/java/org/jeecg/modules/billet/sampleCardDeliveryRecord/task/SampleCardSyncTask.java

@@ -8,6 +8,8 @@ import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.util.DateUtils;
+import org.jeecg.modules.billet.billetHotsendChangeShift.entity.BilletHotsendChangeShift;
+import org.jeecg.modules.billet.billetHotsendChangeShift.service.IBilletHotsendChangeShiftService;
 import org.jeecg.modules.billet.sampleCardDeliveryRecord.entity.SampleCardDeliveryRecord;
 import org.jeecg.modules.billet.sampleCardDeliveryRecord.service.ISampleCardDeliveryRecordService;
 import org.springframework.dao.DuplicateKeyException;
@@ -27,6 +29,7 @@ public class SampleCardSyncTask {
 
     private final RestTemplate restTemplate;
     private final ISampleCardDeliveryRecordService sampleCardDeliveryRecordService;
+    private final IBilletHotsendChangeShiftService billetHotsendChangeShiftService;
 
 
     @Scheduled(fixedRate = 10000) // 每10秒执行一次
@@ -172,6 +175,142 @@ public class SampleCardSyncTask {
         }
     }
 
+
+    @Scheduled(cron = "0 30 * * * ?") // 每小时30分执行一次
+    public void syncLastSampleCardRecords() {
+        for (String ccmNo : Arrays.asList("5", "6")) {
+            final String URL = "http://localhost:7005/billet/billetOriginalProductRecord/queryBilletRecordByCcmNo?queryType=2&ccmNo=%s&changeShiftId=%s";
+
+
+            // 根据铸机号、交班结束时间倒序并不为空,查询交班记录,
+            LambdaQueryWrapper<BilletHotsendChangeShift> queryWrapper = new LambdaQueryWrapper<>();
+            queryWrapper.eq(BilletHotsendChangeShift::getCcmNo, ccmNo)
+                    .isNotNull(BilletHotsendChangeShift::getChangeShiftTime)
+                    .orderByDesc(BilletHotsendChangeShift::getChangeShiftTime)
+                    .last("limit 1");
+
+            BilletHotsendChangeShift billetHotsendChangeShift = billetHotsendChangeShiftService.getOne(queryWrapper);
+            if (billetHotsendChangeShift == null) {
+                log.info("交班记录为空,跳过 ccmNo = {}", ccmNo);
+                continue;
+            }
+
+            Date changeShiftTime = billetHotsendChangeShift.getChangeShiftTime();
+            if (changeShiftTime == null) {
+                log.info("交班记录缺少交班时间,跳过 ccmNo = {}", ccmNo);
+                continue;
+            }
+
+            long now = System.currentTimeMillis();
+            long shiftTime = changeShiftTime.getTime();
+            long fortyMinutes = 40 * 60 * 1000L;
+
+            if (shiftTime < now - fortyMinutes || shiftTime > now) {
+                log.info("交班时间不在当前时间 - 40分钟范围内,跳过 ccmNo = {}, 交班时间 = {}", ccmNo, changeShiftTime);
+                continue;
+            }
+
+
+            String requestUrl = String.format(URL, ccmNo, billetHotsendChangeShift.getId());
+
+            try {
+                Result<?> result = restTemplate.getForObject(requestUrl, Result.class);
+                if (result != null && result.isSuccess()) {
+                    @SuppressWarnings("unchecked")
+                    JSONObject jsonObject = new JSONObject((Map<String, Object>) result.getResult());
+
+                    Integer confirmStatus = jsonObject.getInteger("confirmStatus");
+                    if (confirmStatus != null && confirmStatus == 3) {
+//						log.info("ccmNo={} 的数据已确认,跳过新增与更新。", ccmNo);
+
+                    }
+
+                    JSONArray jsonArray = jsonObject.getJSONArray("billetOriginalProductRecordList");
+
+                    Set<String> remoteHeatNos = new HashSet<>();
+                    List<SampleCardDeliveryRecord> recordsToSave = new ArrayList<>();
+                    List<SampleCardDeliveryRecord> recordsToUpdate = new ArrayList<>();
+
+                    LocalDate referenceDate = null;
+
+                    for (int i = 0; i < jsonArray.size(); i++) {
+                        JSONObject json = jsonArray.getJSONObject(i);
+                        SampleCardDeliveryRecord record = json.toJavaObject(SampleCardDeliveryRecord.class);
+
+                        Date createDate = DateUtils.str2Date(json.getString("createTime"), DateUtils.datetimeFormat.get());
+                        LocalDate createLocalDate = createDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
+                        if (referenceDate == null || createLocalDate.isAfter(referenceDate)) {
+                            referenceDate = createLocalDate;
+                        }
+
+                        remoteHeatNos.add(record.getHeatNo());
+
+                        SampleCardDeliveryRecord existRecord = sampleCardDeliveryRecordService.findExistingRecord(record);
+
+                        if (existRecord == null) {
+                            record.setId(String.valueOf(IdWorker.getId()));
+                            recordsToSave.add(record);
+                        } else {
+                            record.setId(existRecord.getId());
+                            recordsToUpdate.add(record);
+                        }
+                    }
+
+                    // 查询本地同一天、同班次、同班组的数据,用于计算需要删除的记录
+                    List<SampleCardDeliveryRecord> localRecords = sampleCardDeliveryRecordService.list(
+                            new LambdaQueryWrapper<SampleCardDeliveryRecord>()
+                                    .eq(SampleCardDeliveryRecord::getCcmNo, ccmNo)
+                                    .eq(SampleCardDeliveryRecord::getShift, billetHotsendChangeShift.getShift())
+                                    .eq(SampleCardDeliveryRecord::getShiftGroup, billetHotsendChangeShift.getShiftGroup())
+                                    .between(SampleCardDeliveryRecord::getCreateTime,
+                                            Date.from(referenceDate.atStartOfDay(ZoneId.systemDefault()).toInstant()),
+                                            Date.from(referenceDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).minusNanos(1).toInstant()))
+                    );
+
+                    Set<String> localHeatNos = localRecords.stream()
+                            .map(SampleCardDeliveryRecord::getHeatNo)
+                            .collect(Collectors.toSet());
+
+                    Set<String> heatNosToDelete = new HashSet<>(localHeatNos);
+                    heatNosToDelete.removeAll(remoteHeatNos); // 本地有但远程没有
+
+                    int successCount = 0;
+                    for (SampleCardDeliveryRecord record : recordsToSave) {
+                        try {
+                            sampleCardDeliveryRecordService.saveOrUpdate(record, buildUniqueKeyWrapper(record));
+                            successCount++;
+                        } catch (DuplicateKeyException e) {
+                            log.warn("唯一键冲突跳过保存:heatNo={}, ccmNo={}", record.getHeatNo(), record.getCcmNo());
+                        } catch (Exception e) {
+                            log.error("保存记录出错:{}", record, e);
+                        }
+                    }
+
+                    if (!recordsToUpdate.isEmpty()) {
+                        sampleCardDeliveryRecordService.updateBatchById(recordsToUpdate);
+                    }
+                    if (!heatNosToDelete.isEmpty()) {
+                        sampleCardDeliveryRecordService.remove(
+                                new LambdaQueryWrapper<SampleCardDeliveryRecord>()
+                                        .eq(SampleCardDeliveryRecord::getCcmNo, ccmNo)
+                                        .eq(SampleCardDeliveryRecord::getShift, billetHotsendChangeShift.getShift())
+                                        .eq(SampleCardDeliveryRecord::getShiftGroup, billetHotsendChangeShift.getShiftGroup())
+                                        .in(SampleCardDeliveryRecord::getHeatNo, heatNosToDelete)
+                                        .between(SampleCardDeliveryRecord::getCreateTime,
+                                                Date.from(referenceDate.atStartOfDay(ZoneId.systemDefault()).toInstant()),
+                                                Date.from(referenceDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).minusNanos(1).toInstant()))
+                        );
+                    }
+
+                } else {
+                    log.warn("接口请求失败:{}", result != null ? result.getMessage() : "null response");
+                }
+            } catch (Exception e) {
+                log.error("同步 SampleCardDeliveryRecord 出错,ccmNo={}, id={}", ccmNo, billetHotsendChangeShift.getId(), e);
+            }
+        }
+    }
+
     private LambdaQueryWrapper<SampleCardDeliveryRecord> buildUniqueKeyWrapper(SampleCardDeliveryRecord record) {
         LocalDate createDate = record.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();