refactor(ops): 提取巡检结果和归属判定枚举,替换硬编码常量

新增 InspectionResultEnum(合格/不合格)和 InspectionAttributionEnum
(个人责任/突发状况/正常),替换 InspectionRecordServiceImpl 和
InspectionAttributionServiceImpl 中的 private static final int 硬编码常量。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
lzh
2026-03-19 09:43:37 +08:00
parent a4ab24b29c
commit 4a105da46e
5 changed files with 81 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
@Schema(description = "管理后台 - 巡检记录 Response VO")
@Data
@@ -21,18 +22,21 @@ public class InspectionRecordRespVO {
@Schema(description = "位置是否异常0正常 1异常", example = "0")
private Integer isLocationException;
@Schema(description = "巡检结果0不合格 1合格", example = "1")
@Schema(description = "巡检结果,参见 InspectionResultEnum0不合格 1合格", example = "1")
private Integer resultStatus;
@Schema(description = "备注", example = "检查完成")
private String remark;
@Schema(description = "归属判定结果1个人责任 2突发状况 3正常", example = "3")
@Schema(description = "归属判定结果,参见 InspectionAttributionEnum1个人责任 2突发状况 3正常", example = "3")
private Integer attributionResult;
@Schema(description = "整改工单ID", example = "2048")
private Long generatedOrderId;
@Schema(description = "快捷标签", example = "[\"地面污渍\",\"垃圾未清理\"]")
private List<String> tags;
@Schema(description = "创建时间")
private LocalDateTime createTime;

View File

@@ -1,6 +1,7 @@
package com.viewsh.module.ops.environment.service.inspection;
import com.viewsh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.viewsh.module.ops.enums.InspectionAttributionEnum;
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;
@@ -22,13 +23,6 @@ import org.springframework.validation.annotation.Validated;
@Slf4j
public class InspectionAttributionServiceImpl implements InspectionAttributionService {
/** 归属判定:个人责任(停留时长不足) */
private static final int ATTRIBUTION_PERSONAL = 1;
/** 归属判定:突发状况(停留时长达标) */
private static final int ATTRIBUTION_EMERGENCY = 2;
/** 归属判定:正常(合格巡检,无需判定) */
private static final int ATTRIBUTION_NORMAL = 3;
@Resource
private OpsInspectionRecordMapper inspectionRecordMapper;
@@ -50,7 +44,7 @@ public class InspectionAttributionServiceImpl implements InspectionAttributionSe
if (lastOrder == null) {
log.warn("[determineAttribution] 区域 {} 无已完成工单,标记为正常: recordId={}", areaId, recordId);
updateAttributionResult(recordId, null, null, ATTRIBUTION_NORMAL);
updateAttributionResult(recordId, null, null, InspectionAttributionEnum.NORMAL.getResult());
return;
}
@@ -65,7 +59,7 @@ public class InspectionAttributionServiceImpl implements InspectionAttributionSe
OpsBusAreaDO area = opsBusAreaMapper.selectById(areaId);
if (area == null || area.getStandardDuration() == null) {
log.warn("[determineAttribution] 区域 {} 无标准时长配置,标记为正常: recordId={}", areaId, recordId);
updateAttributionResult(recordId, lastOrder.getId(), stayDurationSeconds, ATTRIBUTION_NORMAL);
updateAttributionResult(recordId, lastOrder.getId(), stayDurationSeconds, InspectionAttributionEnum.NORMAL.getResult());
return;
}
int standardDurationSeconds = area.getStandardDuration() * 60; // 分钟 → 秒
@@ -74,8 +68,8 @@ public class InspectionAttributionServiceImpl implements InspectionAttributionSe
// T_stay >= clean_threshold → 突发状况(保洁员已做到位,不扣分)
// T_stay < clean_threshold → 个人责任(保洁时长不足,扣信用分)
int attributionResult = stayDurationSeconds >= standardDurationSeconds
? ATTRIBUTION_EMERGENCY
: ATTRIBUTION_PERSONAL;
? InspectionAttributionEnum.EMERGENCY.getResult()
: InspectionAttributionEnum.PERSONAL.getResult();
log.info("[determineAttribution] 归属判定完成: recordId={}, areaId={}, lastOrderId={}, " +
"stayDuration={}s, standardDuration={}s({}min), result={}",

View File

@@ -3,6 +3,7 @@ package com.viewsh.module.ops.environment.service.inspection;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.viewsh.framework.common.pojo.PageResult;
import com.viewsh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.viewsh.module.ops.enums.InspectionResultEnum;
import com.viewsh.module.ops.environment.controller.admin.inspection.vo.InspectionRecordPageReqVO;
import com.viewsh.module.ops.environment.controller.admin.inspection.vo.InspectionStatsReqVO;
import com.viewsh.module.ops.environment.controller.admin.inspection.vo.InspectionStatsRespVO;
@@ -33,11 +34,6 @@ import java.util.Map;
@Slf4j
public class InspectionRecordServiceImpl implements InspectionRecordService {
/** 巡检结果:合格 */
private static final int RESULT_STATUS_PASSED = 1;
/** 巡检结果:不合格 */
private static final int RESULT_STATUS_FAILED = 0;
@Resource
private OpsInspectionRecordMapper inspectionRecordMapper;
@@ -53,7 +49,7 @@ public class InspectionRecordServiceImpl implements InspectionRecordService {
// 1. 判定巡检结果:任一项不合格 → 整体不合格
boolean allPassed = submitReqVO.getItems().stream()
.allMatch(InspectionSubmitItemVO::getIsPassed);
int resultStatus = allPassed ? RESULT_STATUS_PASSED : RESULT_STATUS_FAILED;
int resultStatus = allPassed ? InspectionResultEnum.PASSED.getResult() : InspectionResultEnum.FAILED.getResult();
// 2. 保存巡检主记录
OpsInspectionRecordDO record = OpsInspectionRecordDO.builder()
@@ -62,6 +58,7 @@ public class InspectionRecordServiceImpl implements InspectionRecordService {
.isLocationException(submitReqVO.getIsLocationException())
.resultStatus(resultStatus)
.remark(submitReqVO.getRemark())
.tags(submitReqVO.getTags())
.build();
inspectionRecordMapper.insert(record);
@@ -72,12 +69,13 @@ public class InspectionRecordServiceImpl implements InspectionRecordService {
.templateId(itemVO.getTemplateId())
.isPassed(itemVO.getIsPassed())
.remark(itemVO.getRemark())
.tags(itemVO.getTags())
.build())
.toList();
inspectionRecordItemMapper.insertBatch(items);
// 4. 不合格时,在事务提交后异步触发归属判定 + 整改工单
if (resultStatus == RESULT_STATUS_FAILED) {
if (resultStatus == InspectionResultEnum.FAILED.getResult()) {
Long recordId = record.getId();
Long areaId = submitReqVO.getAreaId();
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@@ -113,7 +111,7 @@ public class InspectionRecordServiceImpl implements InspectionRecordService {
long passedCount = inspectionRecordMapper.selectCount(new LambdaQueryWrapperX<OpsInspectionRecordDO>()
.eqIfPresent(OpsInspectionRecordDO::getAreaId, reqVO.getAreaId())
.betweenIfPresent(OpsInspectionRecordDO::getCreateTime, reqVO.getCreateTime())
.eq(OpsInspectionRecordDO::getResultStatus, RESULT_STATUS_PASSED));
.eq(OpsInspectionRecordDO::getResultStatus, InspectionResultEnum.PASSED.getResult()));
long failedCount = totalCount - passedCount;
// 3. 合格率
@@ -124,7 +122,7 @@ public class InspectionRecordServiceImpl implements InspectionRecordService {
// 4. 不合格热点区域按不合格次数降序取前10
QueryWrapper<OpsInspectionRecordDO> groupWrapper = new QueryWrapper<>();
groupWrapper.select("area_id AS areaId", "COUNT(*) AS failedCount");
groupWrapper.eq("result_status", RESULT_STATUS_FAILED);
groupWrapper.eq("result_status", InspectionResultEnum.FAILED.getResult());
groupWrapper.eq("deleted", false);
if (reqVO.getAreaId() != null) {
groupWrapper.eq("area_id", reqVO.getAreaId());

View File

@@ -0,0 +1,32 @@
package com.viewsh.module.ops.enums;
import com.viewsh.framework.common.core.ArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* 巡检归属判定枚举
*
* @author lzh
*/
@AllArgsConstructor
@Getter
public enum InspectionAttributionEnum implements ArrayValuable<Integer> {
PERSONAL(1, "个人责任"),
EMERGENCY(2, "突发状况"),
NORMAL(3, "正常");
public static final Integer[] ARRAYS = Arrays.stream(values()).map(InspectionAttributionEnum::getResult).toArray(Integer[]::new);
private final int result;
private final String description;
@Override
public Integer[] array() {
return ARRAYS;
}
}

View File

@@ -0,0 +1,31 @@
package com.viewsh.module.ops.enums;
import com.viewsh.framework.common.core.ArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
/**
* 巡检结果枚举
*
* @author lzh
*/
@AllArgsConstructor
@Getter
public enum InspectionResultEnum implements ArrayValuable<Integer> {
FAILED(0, "不合格"),
PASSED(1, "合格");
public static final Integer[] ARRAYS = Arrays.stream(values()).map(InspectionResultEnum::getResult).toArray(Integer[]::new);
private final int result;
private final String description;
@Override
public Integer[] array() {
return ARRAYS;
}
}