package com.ruoyi.ss.transfer.service.impl;

import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.transfer.domain.TransferVO;
import com.ruoyi.ss.transfer.service.TransferAssembler;
import com.ruoyi.ss.transferDetail.domain.TransferDetailQuery;
import com.ruoyi.ss.transferDetail.domain.TransferDetailVO;
import com.ruoyi.ss.transferDetail.service.ITransferDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wjh
 * 2024/8/12
 */
@Service
public class TransferAssemblerImpl implements TransferAssembler {

    @Autowired
    private ITransferDetailService transferDetailService;

    /**
     * 拼接明细
     */
    @Override
    public void assembleDetail(List<TransferVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }
        TransferDetailQuery query = new TransferDetailQuery();
        query.setTransferIds(CollectionUtils.map(list, TransferVO::getBatchId));
        Map<Long, List<TransferDetailVO>> group = transferDetailService.selectTransferDetailList(query).stream()
                .collect(Collectors.groupingBy(TransferDetailVO::getTransferId));

        for (TransferVO transfer : list) {
            List<TransferDetailVO> detailList = group.get(transfer.getBatchId());
            if (CollectionUtils.isNotEmptyElement(detailList)) {
                transfer.setDetailList(detailList);
            } else {
                transfer.setDetailList(Collections.emptyList());
            }
        }

    }
}
