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.request.MxRequest;
import com.ruoyi.gysl.entity.request.XmzskPageReq;
import com.ruoyi.gysl.entity.request.YsUpdateReq;
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.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(MxRequest mxRequest) {
        List<Mx> allList = baseMapper.selectAll(mxRequest);
//        // 按 name 分组，并转换为 GroupedData
        Map<String, MxPageResponse> groupedMap = allList.stream().collect(
                Collectors.groupingBy(
                        Mx::getName, Collectors.collectingAndThen(Collectors.toList(), list -> {
            if (list.isEmpty()) return null;

            Mx firstItem = list.get(0);
            String name = firstItem.getName();
            String type = firstItem.getType();


            if (name == null && type == null) return null;

            MxPageResponse data = new MxPageResponse();
            data.setName(name);
            data.setType(type);

            // 关键修改：过滤 null 和无效的 MxPageDetailedInfo
            List<MxPageDetailedInfo> itemList = list.stream().map(e -> {
                        // 如果 jtzb 和 xxyq 均为 null，则返回 null
                        if (e.getJtzb() == null && e.getXxyq() == null) {
                            return null;
                        }
                        MxPageDetailedInfo item = new MxPageDetailedInfo();
                        item.setJtzb(e.getJtzb());
                        item.setXxyq(e.getXxyq());
                        item.setId(e.getId());
                        return item;
                    }).filter(Objects::nonNull) // 过滤掉 null
                    .collect(Collectors.toList());
            data.setList(itemList);
            return data;
        }))).entrySet().stream().filter(entry -> entry.getValue() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        return groupedMap.values().stream().filter(Objects::nonNull).collect(Collectors.toList());
    }

    /**
     * 新增指标数据
     *
     * @param mx 实体对象
     * @return 新增结果
     */
    @Override
    public int add(Mx mx) {
        //查找有没有type和name有的，指标和要求为空的
        Mx one = lambdaQuery().eq(Mx::getType, mx.getType())
                .eq(Mx::getName, mx.getName())
                .isNull(Mx::getJtzb)
                .isNull(Mx::getXxyq).one();
        if(one!=null){
            mx.setId(one.getId());
            updateById(mx);
        }else {
            save(mx);
        }
        return 0;
    }


    /**
     * 修改要素
     * @param mx
     * @return
     */
    @Override
    public Boolean updateYs(YsUpdateReq mx) {
        return baseMapper.updateYs(mx);
    }

    /**
     * 删除要素
     */
    @Override
    public Boolean deleteYs(XmzskPageReq req) {
        return baseMapper.deleteYs(req);
    }
}

