package com.ruoyi.gysl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.gysl.entity.ProjectLegend;
import com.ruoyi.gysl.entity.request.ZwIdPageReq;
import com.ruoyi.gysl.entity.response.ImgResponse.ImgResponse;
import com.ruoyi.gysl.entity.response.ProjectLegendResponse;
import com.ruoyi.gysl.mapper.ProjectLegendMapper;
import com.ruoyi.gysl.service.ProjectLegendService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目图例(ProjectLegend)表服务实现类
 *
 * @author makejava
 * @since 2025-03-19 10:12:35
 */
@Service("projectLegendService")
public class ProjectLegendServiceImpl extends ServiceImpl<ProjectLegendMapper, ProjectLegend> implements ProjectLegendService {

    /**
     * 分页查询所有数据
     *
     * @param projectLegend 查询实体
     * @return 所有数据
     */
    @Override
    public List<ProjectLegendResponse> page(ZwIdPageReq projectLegend) {
        List<ProjectLegendResponse> a1 = new ArrayList<>();
        List<ProjectLegend> page = baseMapper.page(projectLegend);
        // 按 id 分组
        Map<Long, List<ProjectLegend>> grouped = page.stream()
                .collect(Collectors.groupingBy(ProjectLegend::getXmId));
        for (Long id : grouped.keySet()) {
            List<ProjectLegend> legends = grouped.get(id);
            ProjectLegendResponse ps = new ProjectLegendResponse();

            legends.forEach(x -> {
                ImgResponse imgResponse = new ImgResponse();
                imgResponse.setImages(x.getImg().split(","));
                if(x.getImgType() == 1){
                    ps.setType1(imgResponse);
                }else {
                    ps.setType2(imgResponse);
                }
            });
            ps.setId(id);
            a1.add(ps);
        }
        return a1;
    }
}

