package com.ywt.outpatient.service.rpc;

import com.ywt.biz.common.util.Checker;
import com.ywt.outpatient.domain.entity.center.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * His系统服务类
 */
@Service
public class HisService {
    private static Logger logger = LoggerFactory.getLogger(HisService.class);

    @Autowired
    private InspectionReportRepository inspectionReportRepository;

    @Autowired
    private InspectionDetailRepository inspectionDetailRepository;

    @Autowired
    private LaboratoryReportRepository laboratoryReportRepository;

    @Autowired
    private LaboratoryDetailRepository laboratoryDetailRepository;

    /**
     * 获取检查报告列表
     *
     * @param hisPatientId
     * @return
     */
    public List<InspectionReport> getInspectionReportListByHisPatientId(String hisPatientId) {
        return inspectionReportRepository.getListByHisPatientId(hisPatientId);
    }

    /**
     * 获取检查报告列表
     *
     * @param clinicSeq
     * @return
     */
    public List<InspectionReport> getInspectionReportListByHisClinicSeq(String clinicSeq) {
        return inspectionReportRepository.getListByHisClinicSeq(clinicSeq);
    }

    public List<InspectionReport> getInspectionReportList(String hisPatientId) {
        Date date = getFilterDate();
        if (!Checker.isNone(date)) {
            return inspectionReportRepository.getInspectionReportList(hisPatientId, date);
        }
        return inspectionReportRepository.getInspectionReportList(hisPatientId);
    }

    // compat method
    public List<InspectionReport> getInspectionReportList(String hisPatientId, int hospitalId) {
        Date date = getFilterDate();
        if (!Checker.isNone(date)) {
            if (hospitalId <= 0) return getInspectionReportList(hisPatientId);
            return inspectionReportRepository.getInspectionReportList(hisPatientId, hospitalId, date);
        }
        if (hospitalId <= 0) return getInspectionReportList(hisPatientId);
        return inspectionReportRepository.getInspectionReportList(hisPatientId, hospitalId);
    }

    /**
     * 获取检查报告详情
     *
     * @param reportId
     * @return
     */
    public InspectionDetail getByReportId(String reportId) {
        return inspectionDetailRepository.getByReportId(reportId);
    }

    public InspectionDetail getByReportIdAndHospitalId(String reportId, int hospitalId) {
        return inspectionDetailRepository.getByReportIdAndHospitalId(reportId, hospitalId);
    }

    public LaboratoryReport getLaboratoryReportById(int id) {
        return laboratoryReportRepository.findById(id).orElse(null);
    }

    public List<LaboratoryReport> getLaboratoryReportListByHisPatientId(String hisPatientId) {
        return laboratoryReportRepository.getListByHisPatientId(hisPatientId);
    }

    public List<LaboratoryReport> getLaboratoryReportListByHisClinicSeq(String clinicSeq) {
        Date date = getFilterDate();
        if (!Checker.isNone(date)) {
            return laboratoryReportRepository.getListByHisClinicSeq(clinicSeq, date);
        }
        return laboratoryReportRepository.getListByHisClinicSeq(clinicSeq);
    }

    public List<LaboratoryReport> getLaboratoryReportList(String hisPatientId) {
        Date date = getFilterDate();
        if (!Checker.isNone(date)) {
            return laboratoryReportRepository.getLaboratoryReportList(hisPatientId, date);
        }
        return laboratoryReportRepository.getLaboratoryReportList(hisPatientId);
    }

    public List<LaboratoryReport> getLaboratoryReportList(String hisPatientId, int hospitalId) {
        Date date = getFilterDate();
        if (!Checker.isNone(date)) {
            if (hospitalId <= 0) return getLaboratoryReportList(hisPatientId);
            return laboratoryReportRepository.getLaboratoryReportList(hisPatientId, hospitalId, date);
        }
        if (hospitalId <= 0) return getLaboratoryReportList(hisPatientId);
        return laboratoryReportRepository.getLaboratoryReportList(hisPatientId, hospitalId);
    }

    public List<LaboratoryDetail> getLaboratoryDetailListByInspectionId(String inspectionId) {
        return laboratoryDetailRepository.getListByInspectionId(inspectionId);
    }

    public List<LaboratoryDetail> getLaboratoryDetailListByInspectionIdAndHospitalId(String inspectionId, int hospitalId) {
        return laboratoryDetailRepository.getListByInspectionIdAndHospitalId(inspectionId, hospitalId);
    }

    // 南方医科大学南方医院白云分院v2.1.7（查询优化）版本需要做近180天数据的过滤
    public Date getFilterDate() {
        // Date date = DateUtil.getChangeMonthDate(new Date(), 1, -180);
        // return date; 之后直接采取读取HIS的接口，所以本地不做过滤了
        return null;
    }
}
