From b7dd69a4f6c63e25d7d9ff567992c9095b48919c Mon Sep 17 00:00:00 2001 From: Lius <Lius2225@163.com> Date: 星期二, 17 六月 2025 17:56:53 +0800 Subject: [PATCH] 算法调整 --- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java | 136 +++++++++++++++++++++++---------------------- 1 files changed, 69 insertions(+), 67 deletions(-) diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java index 189a144..3a35ec8 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java +++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentFaultInfoServiceImpl.java @@ -10,6 +10,7 @@ import org.jeecg.modules.mdc.service.IMdcSystemParametersService; import org.jeecg.modules.mdc.util.DateUtils; import org.jeecg.modules.mdc.vo.EquFaultRecord; +import org.jeecg.modules.mdc.vo.TimeInterval; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -85,18 +86,18 @@ if (equFaultRecordList != null && !equFaultRecordList.isEmpty()) { // 淇暣鏃堕棿 Map<String, List<EquFaultRecord>> equFaultRecordMap = equFaultRecordList.stream().collect(Collectors.groupingBy(EquFaultRecord::getEquipmentId)); - equFaultRecordMap.forEach((key, value) -> { - if (map.containsKey(key)) { - MdcEquipmentFaultInfo equFaultRecords = map.get(key); + map.forEach((key, value1) -> { + MdcEquipmentFaultInfo equFaultRecords = value1; + if (equFaultRecordMap.containsKey(key)) { + List<EquFaultRecord> value = equFaultRecordMap.get(key); long faultLong = calculateTotalFaultDuration(value, start, end); equFaultRecords.setFaultLong((int) faultLong); if (faultLong != 0) { - equFaultRecords.setFaultRate(new BigDecimal(faultLong).divide(new BigDecimal("86400"), 2, RoundingMode.HALF_UP)); + equFaultRecords.setFaultRate(new BigDecimal(faultLong).divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP)); } - // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫姞宸ユ椂闂� // step.1 鏌ヨ鍔犲伐鏃堕棿 - List<MdcEquipmentRunningSection> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.selectRunningData(key, start, end); + List<MdcEquipmentRunningSection> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.listEquipmentRunningSectionRun(key, start.getTime(), end.getTime()); if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) { // 鏃堕棿淇 if (mdcEquipmentRunningSections.get(0).getStartTime().before(start)) { @@ -111,17 +112,49 @@ mdcEquipmentRunningSections.get(0).setEndTime(end); } } - // step.2 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫姞宸ユ椂闂� - long processingTime = calculateProcessingTimeWithoutFaults(mdcEquipmentRunningSections, value, start, end); - equFaultRecords.setRemoveFaultRunLong((int) processingTime); - if (faultLong != 0) { + List<MdcEquipmentRunningSection> collect = mdcEquipmentRunningSections.stream().filter(mdcEquipmentRunningSection -> mdcEquipmentRunningSection.getStatus() == 3).collect(Collectors.toList()); + if (!collect.isEmpty()) { + // step.2 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫姞宸ユ椂闂� + long processingTime = calculateProcessingTimeWithoutFaults(collect, value, start, end); + equFaultRecords.setRemoveFaultRunLong((int) processingTime); + if (faultLong != 0 && faultLong != 86400) { + // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫埄鐢ㄧ巼 + BigDecimal removeFaultRate = new BigDecimal(processingTime).divide(new BigDecimal("864").subtract(new BigDecimal(faultLong)), 2, RoundingMode.HALF_UP); + equFaultRecords.setRemoveFaultRate(removeFaultRate); + } + } + } + } else { + List<MdcEquipmentRunningSection> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.listEquipmentRunningSectionRun(key, start.getTime(), end.getTime()); + if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) { + // 鏃堕棿淇 + if (mdcEquipmentRunningSections.get(0).getStartTime().before(start)) { + mdcEquipmentRunningSections.get(0).setStartTime(start); + } + if (mdcEquipmentRunningSections.size() > 1) { + if (mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(mdcEquipmentRunningSections.size() - 1).setEndTime(end); + } + } else { + if (mdcEquipmentRunningSections.get(0).getEndTime().after(end)) { + mdcEquipmentRunningSections.get(0).setEndTime(end); + } + } + List<MdcEquipmentRunningSection> collect = mdcEquipmentRunningSections.stream().filter(mdcEquipmentRunningSection -> mdcEquipmentRunningSection.getStatus() == 3).collect(Collectors.toList()); + + if (!collect.isEmpty()) { + long totalProcessingTime = 0; + for (MdcEquipmentRunningSection mdcEquipmentRunningSection : collect) { + totalProcessingTime += ChronoUnit.SECONDS.between(DateUtils.convertToLocalDateTime(mdcEquipmentRunningSection.getStartTime()), DateUtils.convertToLocalDateTime(mdcEquipmentRunningSection.getEndTime())); + } + equFaultRecords.setRemoveFaultRunLong((int) totalProcessingTime); // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鐨勫埄鐢ㄧ巼 - BigDecimal removeFaultRate = new BigDecimal(processingTime).divide(new BigDecimal("86400").subtract(new BigDecimal(faultLong)), 2, RoundingMode.HALF_UP); + BigDecimal removeFaultRate = new BigDecimal(totalProcessingTime).divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP); equFaultRecords.setRemoveFaultRate(removeFaultRate); } } - map.put(key, equFaultRecords); } + map.put(key, equFaultRecords); }); } if (!map.isEmpty()) { @@ -130,8 +163,8 @@ } public static long calculateTotalFaultDuration(List<EquFaultRecord> records, Date startTime, Date endTime) { - LocalDateTime start = convertToLocalDateTime(startTime); - LocalDateTime end = convertToLocalDateTime(endTime); + LocalDateTime start = DateUtils.convertToLocalDateTime(startTime); + LocalDateTime end = DateUtils.convertToLocalDateTime(endTime); // 淇璁板綍鏃堕棿 List<EquFaultRecord> correctedRecords = correctRecordTimes(records, start, end); @@ -144,16 +177,16 @@ // 璁$畻鎬绘椂闀匡紙绉掞級 return mergedIntervals.stream() - .mapToLong(interval -> ChronoUnit.SECONDS.between(interval.start, interval.end)) + .mapToLong(interval -> ChronoUnit.SECONDS.between(interval.getStart(), interval.getEnd())) .sum(); } private static List<EquFaultRecord> correctRecordTimes(List<EquFaultRecord> records, LocalDateTime startTime, LocalDateTime endTime) { return records.stream() .map(record -> { - LocalDateTime recordStart = convertToLocalDateTime(record.getStartTime()); + LocalDateTime recordStart = DateUtils.convertToLocalDateTime(record.getStartTime()); LocalDateTime recordEnd = record.getEndTime() != null ? - convertToLocalDateTime(record.getEndTime()) : null; + DateUtils.convertToLocalDateTime(record.getEndTime()) : null; // 淇寮�濮嬫椂闂� LocalDateTime correctedStart = recordStart.isBefore(startTime) ? @@ -166,8 +199,8 @@ // 鍒涘缓淇鍚庣殑璁板綍 return new EquFaultRecord( record.getEquipmentId(), - convertToDate(correctedStart), - convertToDate(correctedEnd) + DateUtils.convertToDate(correctedStart), + DateUtils.convertToDate(correctedEnd) ); }) .collect(Collectors.toList()); @@ -176,8 +209,8 @@ private static List<TimeInterval> mergeIntervals(List<EquFaultRecord> records) { List<TimeInterval> intervals = records.stream() .map(record -> new TimeInterval( - convertToLocalDateTime(record.getStartTime()), - convertToLocalDateTime(record.getEndTime()))) + DateUtils.convertToLocalDateTime(record.getStartTime()), + DateUtils.convertToLocalDateTime(record.getEndTime()))) .collect(Collectors.toList()); if (intervals.isEmpty()) { @@ -189,9 +222,9 @@ for (int i = 1; i < intervals.size(); i++) { TimeInterval next = intervals.get(i); - if (next.start.isBefore(current.end) || next.start.equals(current.end)) { + if (next.getStart().isBefore(current.getEnd()) || next.getStart().equals(current.getEnd())) { // 鏈夐噸鍙狅紝鍚堝苟鍖洪棿 - current.end = current.end.isAfter(next.end) ? current.end : next.end; + current.setEnd(current.getEnd().isAfter(next.getEnd()) ? current.getEnd() : next.getEnd()); } else { // 鏃犻噸鍙狅紝娣诲姞褰撳墠鍖洪棿骞舵洿鏂板綋鍓嶅尯闂� merged.add(current); @@ -203,33 +236,6 @@ return merged; } - // Date涓嶭ocalDateTime浜掔浉杞崲鐨勫伐鍏锋柟娉� - private static LocalDateTime convertToLocalDateTime(Date date) { - if (date == null) { - return null; - } - return Instant.ofEpochMilli(date.getTime()) - .atZone(ZoneId.systemDefault()) - .toLocalDateTime(); - } - - private static Date convertToDate(LocalDateTime localDateTime) { - if (localDateTime == null) { - return null; - } - return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); - } - - static class TimeInterval { - LocalDateTime start; - LocalDateTime end; - - public TimeInterval(LocalDateTime start, LocalDateTime end) { - this.start = start; - this.end = end; - } - } - // 璁$畻鍘婚櫎鏁呴殰鏃堕暱鍚庣殑鍔犲伐鏃堕棿 private long calculateProcessingTimeWithoutFaults( List<MdcEquipmentRunningSection> runningSections, @@ -238,23 +244,19 @@ Date endTime) { // 杞崲涓篖ocalDateTime杩涜澶勭悊 - LocalDateTime start = convertToLocalDateTime(startTime); - LocalDateTime end = convertToLocalDateTime(endTime); + LocalDateTime start = DateUtils.convertToLocalDateTime(startTime); + LocalDateTime end = DateUtils.convertToLocalDateTime(endTime); - // 灏嗘晠闅滆褰曡浆鎹负鏃堕棿鍖洪棿骞跺悎骞堕噸鍙犻儴鍒� -// List<TimeInterval> faultIntervals = faultRecords.stream() -// .map(record -> new TimeInterval( -// convertToLocalDateTime(record.getStartTime()), -// convertToLocalDateTime(record.getEndTime()))) -// .collect(Collectors.toList()); - List<TimeInterval> mergedFaultIntervals = mergeIntervals(faultRecords); + // 淇璁板綍鏃堕棿 + List<EquFaultRecord> correctedRecords = correctRecordTimes(faultRecords, start, end); + List<TimeInterval> mergedFaultIntervals = mergeIntervals(correctedRecords); long totalProcessingTime = 0; // 閬嶅巻姣忎釜鍔犲伐鍖洪棿锛屾帓闄ゆ晠闅滄椂闂� for (MdcEquipmentRunningSection section : runningSections) { - LocalDateTime sectionStart = convertToLocalDateTime(section.getStartTime()); - LocalDateTime sectionEnd = convertToLocalDateTime(section.getEndTime()); + LocalDateTime sectionStart = DateUtils.convertToLocalDateTime(section.getStartTime()); + LocalDateTime sectionEnd = DateUtils.convertToLocalDateTime(section.getEndTime()); // 鎺掗櫎鏁呴殰鏃堕棿鍚庣殑鏈夋晥鍔犲伐鏃堕棿 List<TimeInterval> validIntervals = excludeFaultsFromSection( @@ -263,7 +265,7 @@ // 绱姞鏈夋晥鍔犲伐鏃堕棿锛堢锛� for (TimeInterval interval : validIntervals) { - totalProcessingTime += ChronoUnit.SECONDS.between(interval.start, interval.end); + totalProcessingTime += ChronoUnit.SECONDS.between(interval.getStart(), interval.getEnd()); } } @@ -301,7 +303,7 @@ // 鍒ゆ柇涓や釜鏃堕棿鍖洪棿鏄惁閲嶅彔 private boolean isOverlapping(TimeInterval a, TimeInterval b) { - return a.start.isBefore(b.end) && b.start.isBefore(a.end); + return a.getStart().isBefore(b.getEnd()) && b.getStart().isBefore(a.getEnd()); } // 鍒嗗壊鍖洪棿锛堟墸闄ら噸鍙犻儴鍒嗭級 @@ -311,13 +313,13 @@ List<TimeInterval> result) { // 閲嶅彔鍓嶇殑閮ㄥ垎 - if (valid.start.isBefore(fault.start)) { - result.add(new TimeInterval(valid.start, fault.start)); + if (valid.getStart().isBefore(fault.getStart())) { + result.add(new TimeInterval(valid.getStart(), fault.getStart())); } // 閲嶅彔鍚庣殑閮ㄥ垎 - if (valid.end.isAfter(fault.end)) { - result.add(new TimeInterval(fault.end, valid.end)); + if (valid.getEnd().isAfter(fault.getEnd())) { + result.add(new TimeInterval(fault.getEnd(), valid.getEnd())); } } -- Gitblit v1.9.3