OptimizedTvTaskService.java
5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.huaheng.api.tv.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetailTv;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class OptimizedTvTaskService {
@Autowired
private TaskHeaderService taskHeaderService;
@Autowired
private TaskDetailService taskDetailService;
@Autowired
private ShipmentHeaderService shipmentHeaderService;
/**
* 优化查询 - 使用JOIN一次性获取数据
*/
public List<ShipmentDetailTv> queryShipmentDataOptimized(List<String> portList) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime startTime = now.minusMinutes(2);
LocalDateTime endTime = now.plusMinutes(2);
// 1. 查询任务头信息
List<TaskHeader> taskHeaderList = taskHeaderService.list(
new LambdaQueryWrapper<TaskHeader>()
.in(TaskHeader::getPort, portList)
.and(wrapper -> wrapper
.and(w -> w.eq(TaskHeader::getTaskType, 400).eq(TaskHeader::getStatus, 50))
.or(w -> w.eq(TaskHeader::getTaskType, 300).eq(TaskHeader::getStatus, 100)
.between(TaskHeader::getLastUpdated, startTime, endTime))
)
.orderByDesc(TaskHeader::getLastUpdated)
.last("LIMIT 20")
);
if (taskHeaderList.isEmpty()) {
return Collections.emptyList();
}
// 2. 批量查询任务详情
List<Integer> taskIds = taskHeaderList.stream().map(TaskHeader::getId).collect(Collectors.toList());
List<TaskDetail> taskDetailList = taskDetailService.list(new LambdaQueryWrapper<TaskDetail>().in(TaskDetail::getTaskId, taskIds));
// 3. 批量查询发货单信息
List<String> billCodes = taskDetailList.stream().map(TaskDetail::getBillCode).distinct().collect(Collectors.toList());
Map<String, String> noticeNoMap = batchGetNoticeNos(billCodes);
// 4. 组装结果
return buildShipmentDetailTvList(taskDetailList, noticeNoMap, taskHeaderList);
}
/**
* 批量获取发货单通知号
*/
private Map<String, String> batchGetNoticeNos(List<String> billCodes) {
if (billCodes.isEmpty()) {
return Collections.emptyMap();
}
List<ShipmentHeader> headers = shipmentHeaderService.list(
new LambdaQueryWrapper<ShipmentHeader>()
.in(ShipmentHeader::getCode, billCodes)
.select(ShipmentHeader::getCode, ShipmentHeader::getNoticeNo)
);
return headers.stream()
.collect(Collectors.toMap(
ShipmentHeader::getCode,
ShipmentHeader::getNoticeNo,
(v1, v2) -> v1
));
}
/**
* 构建TV显示数据
*/
private List<ShipmentDetailTv> buildShipmentDetailTvList(List<TaskDetail> taskDetailList,
Map<String, String> noticeNoMap,
List<TaskHeader> taskHeaderList) {
// 构建任务类型映射
Map<Integer, Integer> taskTypeMap = taskHeaderList.stream().collect(Collectors.toMap(TaskHeader::getId, TaskHeader::getTaskType));
List<ShipmentDetailTv> result = new ArrayList<>();
for (TaskDetail taskDetail : taskDetailList) {
ShipmentDetailTv tv = new ShipmentDetailTv();
tv.setMaterialcode(taskDetail.getMaterialCode());
tv.setMaterialname(taskDetail.getMaterialName());
tv.setQty(String.valueOf(taskDetail.getQty()));
tv.setInvoicenumber(taskDetail.getBillCode());
tv.setTraynumber(taskDetail.getContainerCode());
// 设置通知号
String noticeNo = noticeNoMap.get(taskDetail.getBillCode());
tv.setNoticeNo(noticeNo != null ? noticeNo : "");
// 设置类型
Integer taskType = taskTypeMap.get(taskDetail.getTaskId());
if (taskType != null && taskType.equals(300)) {
tv.setType("整出");
} else {
tv.setType("分拣");
}
result.add(tv);
}
return result;
}
}