package com.ruoyi.gysl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.gysl.entity.Mx;
import com.ruoyi.gysl.entity.response.MxPageDetailedInfo.MxPageDetailedInfo;
import com.ruoyi.gysl.entity.response.MxPageResponse;
import com.ruoyi.gysl.mapper.MxMapper;
import com.ruoyi.gysl.service.MxService;
import org.springframework.stereotype.Service;

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

/**
 * 模型管理(Mx)表服务实现类
 *
 * @author makejava
 * @since 2025-02-24 13:40:40
 */
@Service("mxService")
public class MxServiceImpl extends ServiceImpl<MxMapper, Mx> implements MxService {

    /**
     * 查询所有模型数据
     *
     * @return 所有数据
     */
    @Override
    public List<MxPageResponse> selectAll() {
        List<Mx> allList = list();
        // 按 name 分组，并转换为 GroupedData
        Map<String, MxPageResponse> groupedMap =
                allList.stream().collect(Collectors.groupingBy(Mx::getName,
                        Collectors.collectingAndThen(Collectors.toList(), list -> {
                            MxPageResponse data = new MxPageResponse();
                            data.setName(list.get(0).getName());
                            data.setType(list.get(0).getType());// 分组 name
                            // 提取 a1 和 a2 到 AItem 列表
                            List<MxPageDetailedInfo> itemList = list.stream().map(e -> {
                                MxPageDetailedInfo item = new MxPageDetailedInfo();
                                item.setJtzb(e.getJtzb());
                                item.setXxyq(e.getXxyq());
                                return item;
                            }).collect(Collectors.toList());
                            data.setList(itemList);
                            return data;
                        })));
        return groupedMap.values().stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}

