package cn.iocoder.yudao.module.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.project.entity.RepairProject;
import cn.iocoder.yudao.module.project.mapper.RepairProjectMapper;
import cn.iocoder.yudao.module.project.service.RepairProjectService;
import cn.iocoder.yudao.module.project.vo.RepairProjectAppvo;
import cn.iocoder.yudao.module.project.vo.RepairProjectPageReqVO;
import cn.iocoder.yudao.module.project.vo.RepairProjectRespVO;
import cn.iocoder.yudao.module.project.vo.RepairProjectSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 维修项目 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class RepairProjectServiceImpl extends ServiceImpl<RepairProjectMapper, RepairProject> implements RepairProjectService {

    @Resource
    private RepairProjectMapper repairProjectMapper;


    /**
     * 保存维修项目
     *
     * @param saveReqVO RepairProjectSaveReqVO实体
     * @author PQZ
     * @date 14:51 2024/9/12
     **/
    @Override
    public RepairProject saveRepairProject(RepairProjectSaveReqVO saveReqVO) {
        RepairProject repairProject = BeanUtils.toBean(saveReqVO, RepairProject.class);
        saveOrUpdate(repairProject);
        return getById(repairProject.getId());
    }

    /**
     * 删除维修项目
     *
     * @param id 维修项目id
     * @author PQZ
     * @date 15:08 2024/9/12
     **/
    @Override
    public void deleteRepairProject(String id) {
        // 删除
        repairProjectMapper.deleteById(id);
    }

    /**
     * 根据id获取维修项目
     *
     * @param id 维修项目id
     * @return cn.iocoder.yudao.module.project.entity.RepairProject
     * @author PQZ
     * @date 15:08 2024/9/12
     **/
    @Override
    public RepairProject getRepairProject(String id) {
        return repairProjectMapper.selectById(id);
    }

    /**
     * 分页查询维修项目
     *
     * @param pageReqVO RepairProjectPageReqVO实体
     * @param page      分页参数
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.iocoder.yudao.module.project.vo.RepairProjectRespVO>
     * @author PQZ
     * @date 15:08 2024/9/12
     **/
    @Override
    public IPage<RepairProjectRespVO> queryListPage(RepairProjectPageReqVO pageReqVO, Page<RepairProjectRespVO> page) {
        return repairProjectMapper.selectListPage(pageReqVO, page);
    }

    /**
     * 查询服务分类和维修项目列表
     *
     * @return
     */
    @Override
    public List<RepairProjectAppvo> getRepairProjectAndCateGory() {
        List<RepairProjectAppvo> repairProjectAppvos = repairProjectMapper.selectProjectAndCateGory();

//         使用 LinkedHashMap 按 typeId 分组，排除 typeId 为 null 的条目
        Map<String, List<RepairProjectAppvo>> groupedByTypeId = new LinkedHashMap<>();
        for (RepairProjectAppvo appvo : repairProjectAppvos) {
            if (ObjectUtil.isNotEmpty(appvo.getTypeId())) {
                groupedByTypeId.computeIfAbsent(appvo.getTypeId(), k -> new ArrayList<>()).add(appvo);
            }
        }

        // 创建结果列表
        List<RepairProjectAppvo> resultList = new ArrayList<>();
        for (Map.Entry<String, List<RepairProjectAppvo>> entry : groupedByTypeId.entrySet()) {
            RepairProjectAppvo repairProjectAppvo = new RepairProjectAppvo();
            repairProjectAppvo.setTypeId(entry.getKey());
            List<RepairProjectAppvo> filteredList = entry.getValue();
            repairProjectAppvo.setTypeName(filteredList.get(0).getTypeName());

            // 只在最后一次调用 copyToList
            repairProjectAppvo.setGroupList(BeanUtil.copyToList(filteredList, RepairProject.class));
            resultList.add(repairProjectAppvo);
        }

        return resultList;

    }
}