package com.eitc.patient.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eitc.common.utils.bean.BeanUtils;
import com.eitc.patient.domain.MedicalHistory;
import com.eitc.patient.domain.MedicalRecordDetails;
import com.eitc.patient.enums.MedicalHistoryDiseaseSourceEnum;
import com.eitc.patient.enums.MedicalHistoryDiseaseTypeEnum;
import com.eitc.patient.mapper.MedicalRecordDetailsMapper;
import com.eitc.patient.service.IMedicalHistoryService;
import com.eitc.patient.service.IMedicalRecordDetailsService;
import com.eitc.patient.vo.MedicalRecordDetailsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * MedicalRecordDetailsServiceImpl
 *
 * @author mtx
 * @date: 2024/05/17 15:39
 */
@Service
public class MedicalRecordDetailsServiceImpl extends ServiceImpl<MedicalRecordDetailsMapper, MedicalRecordDetails> implements IMedicalRecordDetailsService {

    @Resource
    private IMedicalHistoryService medicalHistoryService;

    /**
     * 通过病历id获取病历详情vo
     * @param medicalRecordId 病历id
     * @return 病历详情vo
     * @author mtx
     * @date: 2024/6/5 9:10
     */
    @Override
    public List<MedicalRecordDetailsVO> findMedicalRecordDetailsVOByMedicalRecordId(String medicalRecordId) {
        List<MedicalRecordDetails> list = this.list(Wrappers.<MedicalRecordDetails>lambdaQuery()
                .eq(MedicalRecordDetails::getMedicalRecordId, medicalRecordId)
                .orderByAsc(MedicalRecordDetails::getOrderNum));
        if (CollectionUtils.isNotEmpty(list)) {
            // 封装病例详情数据
            return list.stream().map(item -> {
                MedicalRecordDetailsVO detailsVO = new MedicalRecordDetailsVO();
                BeanUtils.copyProperties(item, detailsVO);
                // 根据病历ID和疾病类型，查询病史信息，并将查询结果封装到MedicalRecordDetailsVO对象中。
                if (MedicalHistoryDiseaseTypeEnum.ALLERGY_HISTORY.getInfo().equals(detailsVO.getTemplateName())) {
                    findMedicalHistory(medicalRecordId, MedicalHistoryDiseaseTypeEnum.ALLERGY_HISTORY.getCode(), detailsVO);
                }
                if (MedicalHistoryDiseaseTypeEnum.PAST_HISTORY.getInfo().equals(detailsVO.getTemplateName())) {
                    findMedicalHistory(medicalRecordId, MedicalHistoryDiseaseTypeEnum.PAST_HISTORY.getCode(), detailsVO);
                }
                if (MedicalHistoryDiseaseTypeEnum.FAMILY_HISTORY.getInfo().equals(detailsVO.getTemplateName())) {
                    findMedicalHistory(medicalRecordId, MedicalHistoryDiseaseTypeEnum.FAMILY_HISTORY.getCode(), detailsVO);
                }
                return detailsVO;
            }).collect(Collectors.toList());
        } else {
            return Collections.emptyList();
        }
    }

    /**
     * 统计诊断分类
     *
     * @return 统计结果
     * @author mtx
     * @date: 2024/6/5 9:10
     */
    @Override
    public List<Map<String, String>> countByDiagnosisTypeName() {
        return this.baseMapper.countByDiagnosisTypeName();
    }

    /**
     * 根据病历ID和疾病类型，查询病史信息，并将查询结果封装到MedicalRecordDetailsVO对象中。
     *
     * @param medicalRecordId 病历ID，用于查询特定病历的病史。
     * @param diseaseType 疾病类型，用于筛选特定类型的病史记录。
     * @param detailsVO 一个MedicalRecordDetailsVO对象，用于存储查询结果。
     *                  该对象将包含患者自述病史和医生记录的病史。
     */
    private void findMedicalHistory(String medicalRecordId, Integer diseaseType, MedicalRecordDetailsVO detailsVO) {
        // 根据病历ID和疾病类型查询所有的病史记录
        List<MedicalHistory> histories = medicalHistoryService.list(Wrappers.<MedicalHistory>lambdaQuery()
                .eq(MedicalHistory::getDiseaseType, diseaseType)
                .eq(MedicalHistory::getMedicalRecordId, medicalRecordId));

        // 分别筛选出患者自述和医生记录的病史内容，并将它们加入到detailsVO对象中
        detailsVO.setPatientMedicalRecord(histories.stream()
                .filter(li -> MedicalHistoryDiseaseSourceEnum.PATIENT.getCode().equals(li.getDiseaseSource()))
                .map(MedicalHistory::getDiseaseContent).collect(Collectors.joining(",")));
        detailsVO.setDoctorMedicalRecord(histories.stream()
                .filter(li -> MedicalHistoryDiseaseSourceEnum.DOCTOR.getCode().equals(li.getDiseaseSource()))
                .map(MedicalHistory::getDiseaseContent).collect(Collectors.joining(",")));

        // 根据患者自述和医生记录的病史内容，设置最终的内容信息
        String patientMedicalRecord = detailsVO.getPatientMedicalRecord();
        String doctorMedicalRecord = detailsVO.getDoctorMedicalRecord();
        if (StringUtils.isNotBlank(patientMedicalRecord) && StringUtils.isNotBlank(doctorMedicalRecord)) {
            detailsVO.setContentInfo(patientMedicalRecord + "，" + doctorMedicalRecord);
        } else if (StringUtils.isNotBlank(patientMedicalRecord)) {
            detailsVO.setContentInfo(patientMedicalRecord);
        } else if (StringUtils.isNotBlank(doctorMedicalRecord)) {
            detailsVO.setContentInfo(doctorMedicalRecord);
        }
    }

}
