OptimizedTvTaskService.java 5.01 KB
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;
    }
}