@@ -1,8 +1,9 @@
package com.viewsh.module.ops.security.service.securityorder ;
import cn.hutool.core.util.StrUtil ;
import com.viewsh.module.ops.core.event.OrderEventPublisher ;
import com.viewsh.module.ops.core.event.OrderCreatedEvent ;
import com.viewsh.module.ops.core.event.OrderEventPublisher ;
import com.viewsh.module.ops.dal.dataobject.area.OpsBusAreaDO ;
import com.viewsh.module.ops.dal.dataobject.workorder.OpsOrderDO ;
import com.viewsh.module.ops.dal.mysql.area.OpsBusAreaMapper ;
import com.viewsh.module.ops.dal.mysql.workorder.OpsOrderMapper ;
@@ -11,17 +12,14 @@ import com.viewsh.module.ops.enums.PriorityEnum;
import com.viewsh.module.ops.enums.SourceTypeEnum ;
import com.viewsh.module.ops.enums.WorkOrderStatusEnum ;
import com.viewsh.module.ops.enums.WorkOrderTypeEnum ;
import com.viewsh.module.ops.infrastructure.area.AreaPathBuilder ;
import com.viewsh.module.ops.infrastructure.code.OrderCodeGenerator ;
import com.viewsh.module.ops.infrastructure.id.OrderIdGenerator ;
import com.viewsh.module.ops.infrastructure.log.enumeration.EventDomain ;
import com.viewsh.module.ops.infrastructure.log.enumeration.LogModule ;
import com.viewsh.module.ops.infrastructure.log.enumeration.LogType ;
import com.viewsh.module.ops.infrastructure.log.recorder.EventLogRecord ;
import com.viewsh.module.ops.infrastructure.log.recorder.EventLogRecorder ;
// 注意: confirm/complete 的业务日志由 SecurityOrderEventListener 统一记录
// 本类仅记录 CREATE 日志(创建不经过状态变更事件)
import static com.viewsh.framework.common.exception.util.ServiceExceptionUtil.exception ;
import static com.viewsh.module.ops.enums.ErrorCodeConstants.* ;
import com.viewsh.module.ops.infrastructure.id.OrderIdGenerator ;
import com.viewsh.module.ops.security.dal.dataobject.workorder.OpsOrderSecurityExtDO ;
import com.viewsh.module.ops.security.dal.mysql.workorder.OpsOrderSecurityExtMapper ;
import com.viewsh.module.ops.service.fsm.OrderStateMachine ;
@@ -30,6 +28,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import static com.viewsh.framework.common.exception.util.ServiceExceptionUtil.exception ;
import static com.viewsh.module.ops.enums.ErrorCodeConstants.* ;
/**
* 安保工单服务实现
*
@@ -57,6 +58,9 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
@Resource
private OpsBusAreaMapper opsBusAreaMapper ;
@Resource
private AreaPathBuilder areaPathBuilder ;
@Resource
private OrderStateMachine orderStateMachine ;
@@ -69,7 +73,8 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
@Transactional ( rollbackFor = Exception . class )
public Long createSecurityOrder ( SecurityOrderCreateReqDTO createReq ) {
// 0. 校验区域是否存在
if ( opsBusAreaMapper . selectById ( createReq . getAreaId ( ) ) = = null ) {
OpsBusAreaDO area = opsBusAreaMapper . selectById ( createReq . getAreaId ( ) ) ;
if ( area = = null ) {
throw exception ( AREA_NOT_FOUND ) ;
}
@@ -82,7 +87,7 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
? createReq . getSourceType ( )
: ( StrUtil . isNotBlank ( createReq . getAlarmId ( ) ) ? SourceTypeEnum . ALARM . getType ( ) : SourceTypeEnum . MANUAL . getType ( ) ) ;
// 3. 构建主表记录
// 3. 构建主表记录( location 由 areaId 自动拼接)
OpsOrderDO order = OpsOrderDO . builder ( )
. id ( orderId )
. orderCode ( orderCode )
@@ -93,7 +98,7 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
. priority ( createReq . getPriority ( ) ! = null ? createReq . getPriority ( ) : PriorityEnum . P2 . getPriority ( ) )
. status ( WorkOrderStatusEnum . PENDING . getStatus ( ) )
. areaId ( createReq . getAreaId ( ) )
. location ( c reateReq . getLocation ( ) )
. location ( a reaPathBuilder . buildPath ( area ) )
. build ( ) ;
opsOrderMapper . insert ( order ) ;
@@ -142,11 +147,14 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
OpsOrderDO order = getOrderOrThrow ( orderId ) ;
validateOrderType ( order ) ;
// 如果 userId 为 null( open-api 调用),取已分配人员
Long effectiveUserId = resolveOperatorId ( orderId , userId ) ;
// 状态转换: DISPATCHED → CONFIRMED( 扩展表时间 + 业务日志由 EventListener 统一记录)
orderStateMachine . transition ( order , WorkOrderStatusEnum . CONFIRMED ,
OperatorTypeEnum . SECURITY_GUARD , u serId, " 安保人员确认接单 " ) ;
OperatorTypeEnum . SECURITY_GUARD , effectiveU serId, " 安保人员确认接单 " ) ;
log . info ( " 安保工单确认: orderId={}, userId={} " , orderId , u serId) ;
log . info ( " 安保工单确认: orderId={}, userId={} " , orderId , effectiveU serId) ;
}
@Override
@@ -169,9 +177,12 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
OpsOrderDO order = getOrderOrThrow ( req . getOrderId ( ) ) ;
validateOrderType ( order ) ;
// 如果 operatorId 为 null( open-api 调用),取已分配人员
Long effectiveOperatorId = resolveOperatorId ( req . getOrderId ( ) , req . getOperatorId ( ) ) ;
// 状态转换 → COMPLETED( 扩展表 completedTime + 业务日志由 EventListener 统一记录,主表 endTime 由状态机统一设置)
orderStateMachine . transition ( order , WorkOrderStatusEnum . COMPLETED ,
OperatorTypeEnum . SECURITY_GUARD , req . get OperatorId( ) , " 安保人员提交处理结果 " ) ;
OperatorTypeEnum . SECURITY_GUARD , effective OperatorId, " 安保人员提交处理结果 " ) ;
// 更新扩展表:结果 + 图片
OpsOrderSecurityExtDO extUpdate = new OpsOrderSecurityExtDO ( ) ;
@@ -185,6 +196,29 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
log . info ( " 安保工单人工完单: orderId={} " , req . getOrderId ( ) ) ;
}
@Override
@Transactional ( rollbackFor = Exception . class )
public void falseAlarmOrder ( Long orderId , Long operatorId ) {
OpsOrderDO order = getOrderOrThrow ( orderId ) ;
validateOrderType ( order ) ;
// 如果 operatorId 为 null( open-api 调用),取已分配人员
Long effectiveOperatorId = resolveOperatorId ( orderId , operatorId ) ;
// 状态转换 → COMPLETED
orderStateMachine . transition ( order , WorkOrderStatusEnum . COMPLETED ,
OperatorTypeEnum . SECURITY_GUARD , effectiveOperatorId , " 误报标记 " ) ;
// 更新扩展表:标记误报 + 结果
OpsOrderSecurityExtDO extUpdate = new OpsOrderSecurityExtDO ( ) ;
extUpdate . setOpsOrderId ( orderId ) ;
extUpdate . setFalseAlarm ( true ) ;
extUpdate . setResult ( " 误报 " ) ;
securityExtMapper . insertOrUpdateSelective ( extUpdate ) ;
log . info ( " 安保工单误报标记: orderId={} " , orderId ) ;
}
@Override
public OpsOrderSecurityExtDO getSecurityExt ( Long opsOrderId ) {
return securityExtMapper . selectByOpsOrderId ( opsOrderId ) ;
@@ -206,4 +240,19 @@ public class SecurityOrderServiceImpl implements SecurityOrderService {
}
}
/**
* 解析操作人ID: 如果传入为 null, 则取扩展表中已分配的安保人员
*/
private Long resolveOperatorId ( Long orderId , Long operatorId ) {
if ( operatorId ! = null ) {
return operatorId ;
}
OpsOrderSecurityExtDO ext = securityExtMapper . selectByOpsOrderId ( orderId ) ;
Long assignedUserId = ext ! = null ? ext . getAssignedUserId ( ) : null ;
if ( assignedUserId = = null ) {
log . warn ( " 工单未分配安保人员,操作人为空: orderId={} " , orderId ) ;
}
return assignedUserId ;
}
}