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

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

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

import com.ruoyi.bst.model.domain.ModelVO;
import com.ruoyi.bst.model.service.ModelAssembler;
import com.ruoyi.bst.suit.domain.SuitQuery;
import com.ruoyi.bst.suit.domain.SuitVO;
import com.ruoyi.bst.suit.service.SuitService;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class ModelAssemblerImpl implements ModelAssembler{

    @Autowired
    private SuitService suitService;

    @Override
    public void assembleSuitList(List<ModelVO> list) {
        if (CollectionUtils.isEmptyElement(list)) {
            return;
        }

        List<Long> suitIds = list.stream().map(ModelVO::getSuitIds)
                .flatMap(Collection::stream)
                .distinct().collect(Collectors.toList());

        SuitQuery query = new SuitQuery();
        query.setIds(suitIds);
        List<SuitVO> suitList = suitService.selectSuitList(query);

        for (ModelVO model : list) {
            if (model.getSuitIds() == null) {
                model.setSuitList(new ArrayList<>());
            }
            List<SuitVO> modelSuitList = suitList.stream()
                .filter(suit -> model.getSuitIds().contains(suit.getId()))
                .collect(Collectors.toList());
            model.setSuitList(modelSuitList);
        }
    }

}
