package com.xinelu.manage.service.projectresult.impl;

import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.manage.domain.project.Project;
import com.xinelu.manage.domain.projectgroup.ProjectGroup;
import com.xinelu.manage.domain.projectresult.ProjectResult;
import com.xinelu.manage.dto.projectresult.ProjectResultStatisticDto;
import com.xinelu.manage.mapper.project.ProjectMapper;
import com.xinelu.manage.mapper.projectgroup.ProjectGroupMapper;
import com.xinelu.manage.mapper.projectresult.ProjectResultMapper;
import com.xinelu.manage.service.projectresult.IProjectResultService;
import com.xinelu.manage.vo.projectresult.ProjectResultStatisticVo;
import com.xinelu.manage.vo.projectresult.ProjectResultVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author haown
 * @description 针对表【project_result(检测项目结果表)】的数据库操作Service实现
 * @createDate 2024-03-06 10:40:56
 */
@Service
public class ProjectResultServiceImpl implements IProjectResultService {

    @Resource
    private ProjectGroupMapper projectGroupMapper;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private ProjectResultMapper projectResultMapper;

    /**
     * 查询检测项目结果
     *
     * @param id 检测项目结果主键
     * @return 检测项目结果
     */
    @Override
    public ProjectResult selectProjectResultById(Long id) {
        return projectResultMapper.selectProjectResultById(id);
    }

    /**
     * 查询检测项目结果列表
     *
     * @param projectResult 检测项目结果
     * @return 检测项目结果
     */
    @Override
    public List<ProjectResult> selectProjectResultList(ProjectResult projectResult) {
        return projectResultMapper.selectProjectResultList(projectResult);
    }

    /**
     * 新增检测项目结果
     *
     * @param projectResult 检测项目结果
     * @return 结果
     */
    @Override
    public int insertProjectResult(ProjectResult projectResult) {
        projectResult.setCreateTime(LocalDateTime.now());
        return projectResultMapper.insertProjectResult(projectResult);
    }

    /**
     * 修改检测项目结果
     *
     * @param projectResult 检测项目结果
     * @return 结果
     */
    @Override
    public int updateProjectResult(ProjectResult projectResult) {
        projectResult.setUpdateTime(LocalDateTime.now());
        return projectResultMapper.updateProjectResult(projectResult);
    }

    /**
     * 批量删除检测项目结果
     *
     * @param ids 需要删除的检测项目结果主键
     * @return 结果
     */
    @Override
    public int deleteProjectResultByIds(Long[] ids) {
        return projectResultMapper.deleteProjectResultByIds(ids);
    }

    /**
     * 删除检测项目结果信息
     *
     * @param id 检测项目结果主键
     * @return 结果
     */
    @Override
    public int deleteProjectResultById(Long id) {
        return projectResultMapper.deleteProjectResultById(id);
    }

    @Override
    public ProjectResultStatisticVo curveStatistics(ProjectResultStatisticDto projectResultStatisticDto) {
        // 根据groupCode查询groupId
        if (StringUtils.isNotBlank(projectResultStatisticDto.getGroupCode())) {
            ProjectGroup projectGroup = new ProjectGroup();
            projectGroup.setGroupCode(projectResultStatisticDto.getGroupCode());
            List<ProjectGroup> groupList = projectGroupMapper.selectProjectGroupList(projectGroup);
            if (CollectionUtils.isEmpty(groupList)) {
                throw new ServiceException("未找到该检测项目");
            }
            projectResultStatisticDto.setGroupId(groupList.get(0).getId());
        }
        // 根据projectCode查询projectId
        if (StringUtils.isNotBlank(projectResultStatisticDto.getProjectCode())) {
            Project projectQuery = new Project();
            projectQuery.setProjectCode(projectResultStatisticDto.getProjectCode());
            List<Project> projectList = projectMapper.selectProjectList(projectQuery);
            if (CollectionUtils.isEmpty(projectList)) {
                throw new ServiceException("未找到该检测项目");
            }
            projectResultStatisticDto.setProjectId(projectList.get(0).getId());
        }
        if (projectResultStatisticDto.getPatientId() == null) {
            throw new ServiceException("数据格式错误");
        }
        List<String> measureTimeList = new ArrayList<>();
        Map<String, List<String>> valueMap = new HashMap<>();
        List<ProjectResultVo> resultList = projectResultMapper.selectList(projectResultStatisticDto);
        if (!CollectionUtils.isEmpty(resultList)) {
            // 根据projectId进行分组
            Map<String, List<ProjectResultVo>> groupByProject = resultList.stream().collect(Collectors.groupingBy(ProjectResultVo::getProjectName));
            for (String projectName : groupByProject.keySet()) {
                List<ProjectResultVo> projectResultList = groupByProject.get(projectName);
                List<String> valueList = projectResultList.stream().map(ProjectResultVo::getMeasureResult).collect(Collectors.toList());
                measureTimeList = projectResultList.stream().map(ProjectResultVo::getMeasureTimeStr).collect(Collectors.toList());
                valueMap.put(projectName, valueList);
            }
        }
        return ProjectResultStatisticVo.builder().xList(measureTimeList).yList(valueMap).build();
    }
}
