package thyyxxk.webserver.service.zhuyuanyisheng;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.zhuyuanyisheng.EmrPatientDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.zhuyuanyisheng.EmrPatientData;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Slf4j
public class EmrServer {

    private final EmrPatientDao dao;

    public EmrServer(EmrPatientDao dao) {
        this.dao = dao;
    }

    /**
     * 获取这个患者这个住院次数的全部电子病历
     *
     * @param patNo 住院号
     * @param times 住院次数
     * @return 数据
     */
    public ResultVo<List<EmrPatientData>> getPatientData(String patNo, Integer times) {

        List<EmrPatientData> data = new ArrayList<>();

        Map<String, List<EmrPatientData>> map = dao.getPatientData(patNo, times).stream().collect(
                Collectors.groupingBy(EmrPatientData::getEmrCategoryCode)
        );

        for (Map.Entry<String, List<EmrPatientData>> key : map.entrySet()) {
            if (key.getValue().size() == 1) {
                data.add(key.getValue().get(0));
            } else {
                EmrPatientData item = new EmrPatientData();

                List<EmrPatientData> children = new ArrayList<>();

                for (int i = 0, len = key.getValue().size(); i < len; i++) {
                    EmrPatientData childNode = key.getValue().get(i);
                    children.add(childNode);
                }

                item.setName(key.getValue().get(0).getEmrName());
                item.setChildren(new ArrayList<>()).getChildren().addAll(children);
                data.add(item);
            }
        }

        return ResultVoUtil.success(data);
    }

    public ResultVo<Boolean> existCourseRecord(String patNo, Integer times) {
        return ResultVoUtil.success(dao.isThereARecordOfTheCourseOfTheDisease(patNo, times) > 0);
    }

    /**
     * 保存患者电子病历信息
     *
     * @param param 参数
     * @return 提示
     */
    public ResultVo<String> insertEmrData(EmrPatientData param) {
        param.setCreateId(TokenUtil.getTokenUserId());

        try {
            dao.emrInsertForTheFirstTime(param);
        } catch (Exception e) {
            dao.updateCreatedTemplate(param);
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "保存成功，已覆盖原数据。");
        }
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "保存到本院成功");
    }

    /**
     * 根据患者的电子病历 id 来删除
     * 这里用的是逻辑删除
     *
     * @param documentId 电子病历id
     * @return 提示
     */
    public ResultVo<String> deletePatientEmrByDocumentId(String documentId) {
        dao.deletePatientEmrByDocumentId(documentId, TokenUtil.getTokenUserId());
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "删除成功.");
    }


    public ResultVo<Boolean> queryWhetherThePatientHasASpecifiedMedicalRecord(EmrPatientData param) {
        return ResultVoUtil.success(dao.queryWhetherThePatientHasASpecifiedMedicalRecord(param.getPatNo(), param.getTimes(), param.getEmrCategoryCode()) == 1);
    }


}
