package com.docus.server.sdry.collector;

import cn.hutool.core.util.StrUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.server.archive.dto.PatientInfoDto;
import com.docus.server.sdry.converter.ShouMaConverter;
import com.docus.server.sdry.entity.ShouMaView;
import com.docus.server.sdry.mapper.ShouMaViewMapper;

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

/**
 * @author YongBin Wen
 * @date 2024/7/12 14:26
 */
public class ShouMaCollector implements Collector<ShouMaCollectResult> {
    /**
     * 访问数据库视图mapper
     */
    private final ShouMaViewMapper shouMaViewMapper;

    /**
     * 患者基础信息
     */
    private final PatientInfoDto patientInfoDto;

    /**
     * 访问数据库附加的额外条件
     */
    private final String extraCondition;


    public ShouMaCollector(ShouMaViewMapper shouMaViewMapper, PatientInfoDto patientInfoDto, String extraCondition) {
        if (StrUtil.isBlank(patientInfoDto.getInpatientNo())) {
            throw new BaseException("患者基础信息病案号不能为空！");
        }
        if (Func.isEmpty(patientInfoDto.getAdmissTimes())) {
            throw new BaseException("患者基础信息住院次数不能为空！");
        }
        this.shouMaViewMapper = shouMaViewMapper;
        this.patientInfoDto = patientInfoDto;
        this.extraCondition = extraCondition;
    }


    @Override
    public List<ShouMaCollectResult> collect() {
        List<ShouMaView> patientShouMaViewDataList = shouMaViewMapper.getPatientShouMaView(patientInfoDto, extraCondition);
        return patientShouMaViewDataList.stream().map(ShouMaConverter::convertResult).collect(Collectors.toList());
    }
}
