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

import com.ruoyi.common.utils.collection.CollectionUtils;
import com.ruoyi.ss.deviceSuit.domain.DeviceSuitQuery;
import com.ruoyi.ss.deviceSuit.domain.DeviceSuitVO;
import com.ruoyi.ss.deviceSuit.service.DeviceSuitService;
import com.ruoyi.ss.suit.domain.SuitVO;
import com.ruoyi.ss.suit.service.SuitAssembler;
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.Objects;
import java.util.stream.Collectors;

/**
 * @author wjh
 * 2024/7/31
 */
@Service
public class SuitAssemblerImpl implements SuitAssembler {

    @Autowired
    private DeviceSuitService deviceSuitService;

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

        DeviceSuitQuery query = new DeviceSuitQuery();
        query.setSuitIds(CollectionUtils.map(list, SuitVO::getSuitId));
        Map<Long, List<DeviceSuitVO>> group = deviceSuitService.selectDeviceSuitList(query)
                .stream().collect(Collectors.groupingBy(DeviceSuitVO::getSuitId));

        for (SuitVO suit : list) {
            List<DeviceSuitVO> deviceSuitList = group.get(suit.getSuitId());
            if (deviceSuitList != null) {
                suit.setDeviceList(deviceSuitList.stream().map(DeviceSuitVO::getDevice).filter(Objects::nonNull).collect(Collectors.toList()));
            } else {
                suit.setDeviceList(Collections.emptyList());
            }
        }

    }
}
