| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | 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); |
| | |
| | | |
| | | // 计算总时长(秒) |
| | | 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) ? |
| | |
| | | // 创建修正后的记录 |
| | | return new EquFaultRecord( |
| | | record.getEquipmentId(), |
| | | convertToDate(correctedStart), |
| | | convertToDate(correctedEnd) |
| | | DateUtils.convertToDate(correctedStart), |
| | | DateUtils.convertToDate(correctedEnd) |
| | | ); |
| | | }) |
| | | .collect(Collectors.toList()); |
| | |
| | | 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()) { |
| | |
| | | |
| | | 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); |
| | |
| | | return merged; |
| | | } |
| | | |
| | | // Date与LocalDateTime互相转换的工具方法 |
| | | 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, |
| | |
| | | Date endTime) { |
| | | |
| | | // 转换为LocalDateTime进行处理 |
| | | LocalDateTime start = convertToLocalDateTime(startTime); |
| | | LocalDateTime end = convertToLocalDateTime(endTime); |
| | | LocalDateTime start = DateUtils.convertToLocalDateTime(startTime); |
| | | LocalDateTime end = DateUtils.convertToLocalDateTime(endTime); |
| | | |
| | | // 将故障记录转换为时间区间并合并重叠部分 |
| | | // List<TimeInterval> faultIntervals = faultRecords.stream() |
| | |
| | | |
| | | // 遍历每个加工区间,排除故障时间 |
| | | 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( |
| | |
| | | |
| | | // 累加有效加工时间(秒) |
| | | for (TimeInterval interval : validIntervals) { |
| | | totalProcessingTime += ChronoUnit.SECONDS.between(interval.start, interval.end); |
| | | totalProcessingTime += ChronoUnit.SECONDS.between(interval.getStart(), interval.getEnd()); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | // 判断两个时间区间是否重叠 |
| | | 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()); |
| | | } |
| | | |
| | | // 分割区间(扣除重叠部分) |
| | |
| | | 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())); |
| | | } |
| | | } |
| | | |