酸轧OEE后端重构完成
This commit is contained in:
137
klp-da/src/main/java/com/klp/da/service/OeeReportJobService.java
Normal file
137
klp-da/src/main/java/com/klp/da/service/OeeReportJobService.java
Normal file
@@ -0,0 +1,137 @@
|
||||
package com.klp.da.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.klp.pocket.acid.domain.vo.AcidOeeDailySummaryVo;
|
||||
import com.klp.pocket.acid.domain.vo.AcidOeeLoss7Vo;
|
||||
import com.klp.pocket.acid.service.IAcidOeeService;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* OEE 报表异步任务执行 Service(酸轧线)。
|
||||
*
|
||||
* 负责在后台线程中执行任意日期范围的 summary/loss7 计算,
|
||||
* 并将任务状态与结果写入 Redis。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class OeeReportJobService {
|
||||
|
||||
private final IAcidOeeService acidOeeService;
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
||||
|
||||
/**
|
||||
* 异步执行酸轧线报表任务。
|
||||
*
|
||||
* @param jobId 任务 ID
|
||||
* @param metaKey 任务元信息 Redis key
|
||||
* @param resultKey 任务结果 Redis key
|
||||
* @param startDate 开始日期(yyyy-MM-dd)
|
||||
* @param endDate 结束日期(yyyy-MM-dd)
|
||||
* @param includeSummary 是否计算 summary
|
||||
* @param includeLoss7 是否计算 loss7
|
||||
*/
|
||||
@Async
|
||||
public void executeAcidReportJob(
|
||||
String jobId,
|
||||
String metaKey,
|
||||
String resultKey,
|
||||
String startDate,
|
||||
String endDate,
|
||||
boolean includeSummary,
|
||||
boolean includeLoss7
|
||||
) {
|
||||
try {
|
||||
// 更新状态为 RUNNING
|
||||
OeeReportJobMeta meta = readMeta(metaKey);
|
||||
if (meta == null) {
|
||||
meta = new OeeReportJobMeta();
|
||||
meta.setJobId(jobId);
|
||||
meta.setStartDate(startDate);
|
||||
meta.setEndDate(endDate);
|
||||
}
|
||||
meta.setStatus("RUNNING");
|
||||
meta.setStartedAt(LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
||||
writeMeta(metaKey, meta);
|
||||
|
||||
// 实际计算
|
||||
OeeReportJobResult result = new OeeReportJobResult();
|
||||
if (includeSummary) {
|
||||
List<AcidOeeDailySummaryVo> summary =
|
||||
acidOeeService.getDailySummary(startDate, endDate);
|
||||
result.setSummary(summary);
|
||||
}
|
||||
if (includeLoss7) {
|
||||
List<AcidOeeLoss7Vo> loss7 =
|
||||
acidOeeService.getLoss7Summary(startDate, endDate);
|
||||
result.setLoss7(loss7);
|
||||
}
|
||||
|
||||
stringRedisTemplate.opsForValue()
|
||||
.set(resultKey, JSON.toJSONString(result), 1, TimeUnit.DAYS);
|
||||
|
||||
// 标记为 COMPLETED
|
||||
meta.setStatus("COMPLETED");
|
||||
meta.setCompletedAt(LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
||||
writeMeta(metaKey, meta);
|
||||
} catch (Exception e) {
|
||||
log.error("[OeeReportJobService] executeAcidReportJob error, jobId={}", jobId, e);
|
||||
OeeReportJobMeta meta = readMeta(metaKey);
|
||||
if (meta == null) {
|
||||
meta = new OeeReportJobMeta();
|
||||
meta.setJobId(jobId);
|
||||
meta.setStartDate(startDate);
|
||||
meta.setEndDate(endDate);
|
||||
}
|
||||
meta.setStatus("FAILED");
|
||||
meta.setCompletedAt(LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
||||
meta.setErrorMessage(e.getMessage());
|
||||
writeMeta(metaKey, meta);
|
||||
}
|
||||
}
|
||||
|
||||
private OeeReportJobMeta readMeta(String metaKey) {
|
||||
String json = stringRedisTemplate.opsForValue().get(metaKey);
|
||||
if (json == null || json.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parseObject(json, OeeReportJobMeta.class);
|
||||
}
|
||||
|
||||
private void writeMeta(String metaKey, OeeReportJobMeta meta) {
|
||||
stringRedisTemplate.opsForValue()
|
||||
.set(metaKey, JSON.toJSONString(meta), 1, TimeUnit.DAYS);
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class OeeReportJobMeta {
|
||||
private String jobId;
|
||||
private String status;
|
||||
private String submittedAt;
|
||||
private String startedAt;
|
||||
private String completedAt;
|
||||
private String startDate;
|
||||
private String endDate;
|
||||
private String errorMessage;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class OeeReportJobResult {
|
||||
private List<AcidOeeDailySummaryVo> summary;
|
||||
private List<AcidOeeLoss7Vo> loss7;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user