package com.ruoyi.bst.model.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.model.domain.ModelVO;
import com.ruoyi.bst.model.service.ModelAssembler;
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.service.SuitService;
import com.ruoyi.common.utils.collection.CollectionUtils;

@Service
public class ModelAssemblerImpl implements ModelAssembler{

    @Autowired
    private SuitService suitService;

    @Autowired
    private ModelSuitService modelSuitService;

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

        List<Long> modelIds = CollectionUtils.map(list, ModelVO::getId);
        List<ModelSuitVO> modelSuitList = new ArrayList<>();
        if (CollectionUtils.isNotEmptyElement(modelIds)) {
            ModelSuitQuery query = new ModelSuitQuery();
            query.setModelIds(modelIds);
            modelSuitList = modelSuitService.selectModelSuitList(query);

        }
        for (ModelVO model : list) {
            // 组装套餐ID列表
            List<Long> suitIds = modelSuitList.stream()
                .filter(item -> Objects.equals(item.getModelId(), model.getId()))
                .map(ModelSuitVO::getSuitId)
                .collect(Collectors.toList());
            model.setSuitIds(suitIds);

            // 组装套餐名称列表
            List<String> suitNames = modelSuitList.stream()
                .filter(item -> Objects.equals(item.getModelId(), model.getId()))
                .map(ModelSuitVO::getSuitName)
                .collect(Collectors.toList());
            model.setSuitNames(suitNames);
        }
    }

}
