package com.ruoyi.bst.suit.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.modelSuit.domain.ModelSuitQuery;
import com.ruoyi.bst.modelSuit.domain.ModelSuitVO;
import com.ruoyi.bst.modelSuit.service.ModelSuitService;
import com.ruoyi.bst.suit.domain.SuitVO;
import com.ruoyi.bst.suit.service.SuitAssembler;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class SuitAssemblerImpl implements SuitAssembler {
    
    @Autowired
    private ModelSuitService modelSuitService;

    @Override
    public void assembleModelIds(List<SuitVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }
        
        List<Long> suitIds = CollectionUtils.map(list, SuitVO::getId);
        List<ModelSuitVO> modelSuitList = new ArrayList<>();

        if (CollectionUtils.isNotEmptyElement(suitIds)) {
            ModelSuitQuery query = new ModelSuitQuery();
            query.setSuitIds(suitIds);
            modelSuitList = modelSuitService.selectModelSuitList(query);
        }

        for (SuitVO suit : list) {
            // 组装车型ID列表
            List<Long> modelIds = modelSuitList.stream()
                .filter(item -> Objects.equals(item.getSuitId(), suit.getId()))
                .map(ModelSuitVO::getModelId)
                .collect(Collectors.toList());
            suit.setModelIds(modelIds);

            // 组装车型名称列表
            List<String> modelNames = modelSuitList.stream()
                .filter(item -> Objects.equals(item.getSuitId(), suit.getId()))
                .map(ModelSuitVO::getModelName)
                .collect(Collectors.toList());
            suit.setModelNames(modelNames);
        }
    }
    
}
