package com.ruoyi.gysl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.gysl.entity.WysmxInformation;
import com.ruoyi.gysl.entity.request.ZwIdPageReq;
import com.ruoyi.gysl.entity.response.WysmxDetail.WysmxDetail;
import com.ruoyi.gysl.entity.response.WysmxResponse;
import com.ruoyi.gysl.mapper.WysmxInformationMapper;
import com.ruoyi.gysl.service.WysmxInformationService;
import org.springframework.stereotype.Service;

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

/**
 * 五要素模型信息(WysmxInformation)表服务实现类
 *
 * @author makejava
 * @since 2025-02-24 14:29:58
 */
@Service("wysmxInformationService")
public class WysmxInformationServiceImpl extends ServiceImpl<WysmxInformationMapper, WysmxInformation> implements WysmxInformationService {

    /**
     * 根据项目id查询该项目的五要素模型信息
     *
     * @param zwIdPageReq 查询实体
     * @return 所有数据
     */
    @Override
    public List<WysmxResponse> list(ZwIdPageReq zwIdPageReq) {
        List<WysmxInformation> allList = baseMapper.list(zwIdPageReq);
        // 按 name 分组，并转换为 GroupedData
        Map<String, WysmxResponse> groupedMap =
                allList.stream().collect(Collectors.groupingBy(WysmxInformation::getYsmc,
                        Collectors.collectingAndThen(Collectors.toList(), list -> {
                            WysmxResponse data = new WysmxResponse();
                            data.setId(list.get(0).getId());
                            data.setXmId(list.get(0).getXmId());
                            data.setYsmc(list.get(0).getYsmc());
                            List<WysmxDetail> itemList = list.stream().map(e -> {
                                WysmxDetail item = new WysmxDetail();
                                item.setZdname(e.getZdname());
                                item.setZdinfor(e.getZdinfor());
                                return item;
                            }).collect(Collectors.toList());
                            data.setList(itemList);
                            return data;
                        })));
        return groupedMap.values().stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}

