refactor(ops): 工单中心查询接口参数封装重构

优化 OrderCenterController.queryPage() 方法签名,从 9 个独立参数封装为单个对象,提升代码可维护性。

主要变更:
1. OrderQuery 改用 Lombok @Data 替代手写 getter/setter(减少 ~120 行)
2. OrderQuery.status 从 String 改为 List<String>,支持前端多状态筛选
3. OrderQueryServiceImpl 使用 inIfPresent() 生成 IN 查询(兼容单选和多选)
4. OrderCenterController 接收 OrderQuery 对象替代 9 个 @RequestParam
5. OrderCenterControllerTest 适配新签名,新增多状态测试用例

向后兼容:
- 前端传单个 status=PENDING,Spring 自动转 List.of("PENDING")
- 前端传多个 status=A&status=B,Spring 自动转 List.of("A","B")
- 原有单状态查询不受影响

影响模块:Ops Biz、Ops Server

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
lzh
2026-02-02 22:37:06 +08:00
parent e95080dc8a
commit bdf5b640b0
4 changed files with 83 additions and 172 deletions

View File

@@ -3,7 +3,11 @@ package com.viewsh.module.ops.service;
import com.viewsh.framework.common.pojo.PageParam;
import com.viewsh.framework.common.pojo.PageResult;
import com.viewsh.module.ops.dal.dataobject.workorder.OpsOrderDO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
import java.util.Map;
/**
@@ -51,145 +55,29 @@ public interface OrderQueryService {
/**
* 工单查询条件
*/
@Data
@EqualsAndHashCode(callSuper = true)
class OrderQuery extends PageParam {
/**
* 工单类型(可选)
*/
@Schema(description = "工单类型", example = "CLEAN")
private String orderType;
/**
* 工单状态(可选)
*/
private String status;
@Schema(description = "工单状态,支持多选", example = "PENDING,DISPATCHED")
private List<String> status;
/**
* 优先级(可选)
*/
@Schema(description = "优先级0=P0/1=P1/2=P2", example = "1")
private Integer priority;
/**
* 区域ID可选
*/
@Schema(description = "区域ID", example = "100")
private Long areaId;
/**
* 执行人ID可选
*/
@Schema(description = "执行人ID", example = "30001")
private Long assigneeId;
/**
* 工单编号模糊查询(可选)
*/
@Schema(description = "工单编号(模糊查询)", example = "WO2025")
private String orderCode;
/**
* 标题模糊查询(可选)
*/
@Schema(description = "标题(模糊查询)", example = "A栋")
private String title;
public String getOrderType() {
return orderType;
}
public void setOrderType(String orderType) {
this.orderType = orderType;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Long getAreaId() {
return areaId;
}
public void setAreaId(Long areaId) {
this.areaId = areaId;
}
public Long getAssigneeId() {
return assigneeId;
}
public void setAssigneeId(Long assigneeId) {
this.assigneeId = assigneeId;
}
public String getOrderCode() {
return orderCode;
}
public void setOrderCode(String orderCode) {
this.orderCode = orderCode;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public static OrderQuery builder() {
return new OrderQuery();
}
public OrderQuery withPageNo(Integer pageNo) {
this.setPageNo(pageNo);
return this;
}
public OrderQuery withPageSize(Integer pageSize) {
this.setPageSize(pageSize);
return this;
}
public OrderQuery withOrderType(String orderType) {
this.orderType = orderType;
return this;
}
public OrderQuery withStatus(String status) {
this.status = status;
return this;
}
public OrderQuery withPriority(Integer priority) {
this.priority = priority;
return this;
}
public OrderQuery withAreaId(Long areaId) {
this.areaId = areaId;
return this;
}
public OrderQuery withAssigneeId(Long assigneeId) {
this.assigneeId = assigneeId;
return this;
}
public OrderQuery withOrderCode(String orderCode) {
this.orderCode = orderCode;
return this;
}
public OrderQuery withTitle(String title) {
this.title = title;
return this;
}
}
}

View File

@@ -39,7 +39,7 @@ public class OrderQueryServiceImpl implements OrderQueryService {
// 1. 构建查询条件
LambdaQueryWrapperX<OpsOrderDO> wrapper = new LambdaQueryWrapperX<OpsOrderDO>()
.eqIfPresent(OpsOrderDO::getOrderType, query.getOrderType())
.eqIfPresent(OpsOrderDO::getStatus, query.getStatus())
.inIfPresent(OpsOrderDO::getStatus, query.getStatus())
.eqIfPresent(OpsOrderDO::getPriority, query.getPriority())
.eqIfPresent(OpsOrderDO::getAreaId, query.getAreaId())
.eqIfPresent(OpsOrderDO::getAssigneeId, query.getAssigneeId())
@@ -126,7 +126,9 @@ public class OrderQueryServiceImpl implements OrderQueryService {
.status(order.getStatus())
.areaId(order.getAreaId())
.location(order.getLocation())
.sourceType(order.getSourceType())
.assigneeId(order.getAssigneeId())
.assigneeName(order.getAssigneeName())
.startTime(order.getStartTime())
.endTime(order.getEndTime())
.createTime(order.getCreateTime())

View File

@@ -2,6 +2,8 @@ package com.viewsh.module.ops.controller.admin;
import com.viewsh.framework.common.pojo.CommonResult;
import com.viewsh.framework.common.pojo.PageResult;
import com.viewsh.module.ops.api.clean.QuickStatsRespDTO;
import com.viewsh.module.ops.environment.service.dashboard.CleanDashboardService;
import com.viewsh.module.ops.service.OrderDetailVO;
import com.viewsh.module.ops.service.OrderQueryService;
import com.viewsh.module.ops.service.OrderSummaryVO;
@@ -9,7 +11,9 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@@ -35,39 +39,14 @@ public class OrderCenterController {
@Resource
private OrderQueryService orderQueryService;
@Autowired(required = false)
private CleanDashboardService cleanDashboardService;
@GetMapping("/page")
@Operation(summary = "工单中心分页查询")
@Parameter(name = "page", description = "页码", required = false)
@Parameter(name = "size", description = "每页大小", required = false)
@Parameter(name = "orderType", description = "工单类型", required = false)
@Parameter(name = "status", description = "工单状态", required = false)
@Parameter(name = "priority", description = "优先级", required = false)
@Parameter(name = "areaId", description = "区域ID", required = false)
@Parameter(name = "assigneeId", description = "执行人ID", required = false)
@PreAuthorize("@ss.hasPermission('ops:order-center:query')")
public CommonResult<PageResult<OrderSummaryVO>> queryPage(
@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "size", required = false, defaultValue = "20") Integer size,
@RequestParam(value = "orderType", required = false) String orderType,
@RequestParam(value = "status", required = false) String status,
@RequestParam(value = "priority", required = false) Integer priority,
@RequestParam(value = "areaId", required = false) Long areaId,
@RequestParam(value = "assigneeId", required = false) Long assigneeId,
@RequestParam(value = "orderCode", required = false) String orderCode,
@RequestParam(value = "title", required = false) String title) {
OrderQueryService.OrderQuery query = OrderQueryService.OrderQuery.builder()
.withPageNo(page)
.withPageSize(size)
.withOrderType(orderType)
.withStatus(status)
.withPriority(priority)
.withAreaId(areaId)
.withAssigneeId(assigneeId)
.withOrderCode(orderCode)
.withTitle(title);
PageResult<OrderSummaryVO> result = orderQueryService.queryPage(query);
public CommonResult<PageResult<OrderSummaryVO>> queryPage(@Valid OrderQueryService.OrderQuery reqVO) {
PageResult<OrderSummaryVO> result = orderQueryService.queryPage(reqVO);
return success(result);
}
@@ -90,4 +69,22 @@ public class OrderCenterController {
Map<String, Object> stats = orderQueryService.getStats(groupBy);
return success(stats);
}
@GetMapping("/quick-stats")
@Operation(summary = "快速统计")
@PreAuthorize("@ss.hasPermission('ops:order-center:query')")
public CommonResult<QuickStatsRespDTO> getQuickStats() {
if (cleanDashboardService == null) {
log.warn("[getQuickStats] CleanDashboardService 未注入,返回默认值");
return success(QuickStatsRespDTO.builder()
.pendingCount(0)
.inProgressCount(0)
.completedTodayCount(0)
.onlineBadgeCount(0)
.build());
}
QuickStatsRespDTO stats = cleanDashboardService.getQuickStats();
return success(stats);
}
}

View File

@@ -2,8 +2,6 @@ package com.viewsh.module.ops.controller.admin;
import com.viewsh.framework.common.pojo.CommonResult;
import com.viewsh.framework.common.pojo.PageResult;
import com.viewsh.module.ops.dal.dataobject.workorder.OpsOrderDO;
import com.viewsh.module.ops.dal.mysql.workorder.OpsOrderMapper;
import com.viewsh.module.ops.enums.PriorityEnum;
import com.viewsh.module.ops.enums.WorkOrderStatusEnum;
import com.viewsh.module.ops.service.OrderDetailVO;
@@ -25,9 +23,8 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* 工单中心控制器测试
@@ -66,12 +63,14 @@ class OrderCenterControllerTest {
.build();
PageResult<OrderSummaryVO> pageResult = new PageResult<>(List.of(vo), 1L);
when(orderQueryService.queryPage(any())).thenReturn(pageResult);
// When
CommonResult<PageResult<OrderSummaryVO>> result = controller.queryPage(
1, 20, "CLEAN", null, null, null, null, null, null);
OrderQueryService.OrderQuery reqVO = new OrderQueryService.OrderQuery();
reqVO.setPageNo(1);
reqVO.setPageSize(20);
reqVO.setOrderType("CLEAN");
CommonResult<PageResult<OrderSummaryVO>> result = controller.queryPage(reqVO);
// Then
assertTrue(result.isSuccess());
@@ -89,18 +88,42 @@ class OrderCenterControllerTest {
when(orderQueryService.queryPage(any())).thenReturn(emptyResult);
// When
controller.queryPage(1, 20, "CLEAN", "PENDING",
PriorityEnum.P2.getPriority(), 100L, null, null, null);
OrderQueryService.OrderQuery reqVO = new OrderQueryService.OrderQuery();
reqVO.setPageNo(1);
reqVO.setPageSize(20);
reqVO.setOrderType("CLEAN");
reqVO.setStatus(List.of("PENDING"));
reqVO.setPriority(PriorityEnum.P2.getPriority());
reqVO.setAreaId(100L);
controller.queryPage(reqVO);
// Then
verify(orderQueryService, times(1)).queryPage(argThat(query ->
query.getOrderType().equals("CLEAN") &&
query.getStatus().equals("PENDING") &&
query.getStatus().equals(List.of("PENDING")) &&
query.getPriority().equals(PriorityEnum.P2.getPriority()) &&
query.getAreaId().equals(100L)
));
}
@Test
void testQueryPage_WithMultipleStatus() {
// Given
PageResult<OrderSummaryVO> emptyResult = new PageResult<>(List.of(), 0L);
when(orderQueryService.queryPage(any())).thenReturn(emptyResult);
// When
OrderQueryService.OrderQuery reqVO = new OrderQueryService.OrderQuery();
reqVO.setStatus(List.of("PENDING", "DISPATCHED", "ARRIVED"));
controller.queryPage(reqVO);
// Then
verify(orderQueryService, times(1)).queryPage(argThat(query ->
query.getStatus().size() == 3 &&
query.getStatus().containsAll(List.of("PENDING", "DISPATCHED", "ARRIVED"))
));
}
@Test
void testGetDetail_Success() {
// Given
@@ -190,12 +213,13 @@ class OrderCenterControllerTest {
PageResult<OrderSummaryVO> emptyResult = new PageResult<>(List.of(), 0L);
when(orderQueryService.queryPage(any())).thenReturn(emptyResult);
// When: 不传分页参数
controller.queryPage(null, null, null, null, null, null, null, null, null);
// When: 不传分页参数,使用 PageParam 默认值
OrderQueryService.OrderQuery reqVO = new OrderQueryService.OrderQuery();
controller.queryPage(reqVO);
// Then: 应该使用默认值
// Then: PageParam 默认值 pageNo=1, pageSize=10
verify(orderQueryService, times(1)).queryPage(argThat(query ->
query.getPageNo() == 1 && query.getPageSize() == 20
query.getPageNo() == 1 && query.getPageSize() == 10
));
}
}