feat(ops): 补充工单状态变更业务日志
- pauseOrder: 记录工单暂停日志 - resumeOrder: 记录工单恢复日志 - interruptOrder: 记录P0插队打断日志 - cancelOrder: 记录工单取消日志 - completeOrder: 记录手动完成日志 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,9 @@ import com.viewsh.module.ops.dal.mysql.workorder.OpsOrderMapper;
|
||||
import com.viewsh.module.ops.enums.OperatorTypeEnum;
|
||||
import com.viewsh.module.ops.enums.PriorityEnum;
|
||||
import com.viewsh.module.ops.enums.WorkOrderStatusEnum;
|
||||
import com.viewsh.module.ops.infrastructure.log.enumeration.EventDomain;
|
||||
import com.viewsh.module.ops.infrastructure.log.recorder.EventLogRecord;
|
||||
import com.viewsh.module.ops.infrastructure.log.recorder.EventLogRecorder;
|
||||
import com.viewsh.module.ops.service.fsm.OrderStateMachine;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
@@ -23,6 +26,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -66,6 +71,9 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
@Resource
|
||||
private EventPublishHandler eventPublishHandler;
|
||||
|
||||
@Resource
|
||||
private EventLogRecorder eventLogRecorder;
|
||||
|
||||
/**
|
||||
* 责任链处理器
|
||||
*/
|
||||
@@ -169,6 +177,9 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
if (!result.isSuccess()) {
|
||||
throw new IllegalStateException("暂停工单失败: " + result.getMessage());
|
||||
}
|
||||
|
||||
// 记录业务日志
|
||||
recordStatusChangeLog(orderId, result, "ORDER_PAUSED", "工单暂停");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -191,6 +202,9 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
if (!result.isSuccess()) {
|
||||
throw new IllegalStateException("恢复工单失败: " + result.getMessage());
|
||||
}
|
||||
|
||||
// 记录业务日志
|
||||
recordStatusChangeLog(orderId, result, "ORDER_RESUMED", "工单恢复");
|
||||
}
|
||||
|
||||
// ==================== 打断/恢复 ====================
|
||||
@@ -217,6 +231,24 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
if (!result.isSuccess()) {
|
||||
throw new IllegalStateException("打断工单失败: " + result.getMessage());
|
||||
}
|
||||
|
||||
// 记录业务日志
|
||||
Map<String, Object> extra = new HashMap<>();
|
||||
extra.put("urgentOrderId", urgentOrderId);
|
||||
extra.put("interruptReason", "P0任务插队");
|
||||
|
||||
OpsOrderDO order = opsOrderMapper.selectById(orderId);
|
||||
eventLogRecorder.record(EventLogRecord.builder()
|
||||
.module("clean")
|
||||
.domain(EventDomain.DISPATCH)
|
||||
.eventType("ORDER_INTERRUPTED")
|
||||
.message("工单被P0紧急任务打断")
|
||||
.targetId(orderId)
|
||||
.targetType("order")
|
||||
.deviceId(order != null ? order.getAssigneeDeviceId() : null)
|
||||
.personId(order != null ? order.getAssigneeId() : null)
|
||||
.payload(extra)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -252,6 +284,12 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
if (!result.isSuccess()) {
|
||||
throw new IllegalStateException("完成工单失败: " + result.getMessage());
|
||||
}
|
||||
|
||||
// 注意:IoT 触发的自动完成在 CleanOrderCompleteEventHandler 中记录日志
|
||||
// 这里只记录手动完成的日志(operatorId 不为空的情况)
|
||||
if (operatorId != null) {
|
||||
recordStatusChangeLog(orderId, result, "ORDER_COMPLETED_MANUAL", "工单手动完成");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -260,6 +298,9 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
log.info("开始取消工单: orderId={}, operatorId={}, operatorType={}, reason={}",
|
||||
orderId, operatorId, operatorType, reason);
|
||||
|
||||
// 查询工单信息(日志记录用)
|
||||
OpsOrderDO order = opsOrderMapper.selectById(orderId);
|
||||
|
||||
// 构建请求
|
||||
OrderTransitionRequest request = OrderTransitionRequest.builder()
|
||||
.orderId(orderId)
|
||||
@@ -275,6 +316,23 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
if (!result.isSuccess()) {
|
||||
throw new IllegalStateException("取消工单失败: " + result.getMessage());
|
||||
}
|
||||
|
||||
// 记录业务日志
|
||||
Map<String, Object> extra = new HashMap<>();
|
||||
extra.put("cancelReason", reason);
|
||||
extra.put("operatorType", operatorType != null ? operatorType.getCode() : "SYSTEM");
|
||||
|
||||
eventLogRecorder.record(EventLogRecord.builder()
|
||||
.module("clean")
|
||||
.domain(EventDomain.SYSTEM)
|
||||
.eventType("ORDER_CANCELLED")
|
||||
.message("工单已取消: " + reason)
|
||||
.targetId(orderId)
|
||||
.targetType("order")
|
||||
.deviceId(order != null ? order.getAssigneeDeviceId() : null)
|
||||
.personId(order != null ? order.getAssigneeId() : null)
|
||||
.payload(extra)
|
||||
.build());
|
||||
}
|
||||
|
||||
// ==================== 查询方法 ====================
|
||||
@@ -337,4 +395,42 @@ public class OrderLifecycleManagerImpl implements OrderLifecycleManager {
|
||||
|| WorkOrderStatusEnum.CONFIRMED == status
|
||||
|| WorkOrderStatusEnum.ARRIVED == status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录状态变更业务日志
|
||||
*
|
||||
* @param orderId 工单ID
|
||||
* @param result 状态转换结果
|
||||
* @param eventType 事件类型
|
||||
* @param message 日志消息
|
||||
*/
|
||||
private void recordStatusChangeLog(Long orderId, OrderTransitionResult result,
|
||||
String eventType, String message) {
|
||||
try {
|
||||
OpsOrderDO order = opsOrderMapper.selectById(orderId);
|
||||
if (order == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, Object> extra = new HashMap<>();
|
||||
extra.put("oldStatus", result.getOldStatus() != null ? result.getOldStatus().getStatus() : null);
|
||||
extra.put("newStatus", result.getNewStatus() != null ? result.getNewStatus().getStatus() : null);
|
||||
|
||||
eventLogRecorder.record(EventLogRecord.builder()
|
||||
.module("clean")
|
||||
.domain(EventDomain.DISPATCH)
|
||||
.eventType(eventType)
|
||||
.message(message)
|
||||
.targetId(orderId)
|
||||
.targetType("order")
|
||||
.deviceId(order.getAssigneeDeviceId())
|
||||
.personId(order.getAssigneeId())
|
||||
.payload(extra)
|
||||
.build());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("[OrderLifecycleManager] 记录状态变更日志失败: orderId={}, eventType={}",
|
||||
orderId, eventType, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user