package com.ywt.outpatient.service.rpc.common;

import com.ywt.biz.common.util.Checker;
import com.ywt.gapi.ResultCode;
import com.ywt.gapi.nfyy.*;
import com.ywt.outpatient.domain.models.enums.DatePeriodEnum;
import com.ywt.outpatient.domain.models.enums.RegisteredTypeEnum;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class NfyyProvider {
    private static Logger logger = LoggerFactory.getLogger(NfyyProvider.class);

    @DubboReference
    private NfyyService nfyyServiceBlockingStub;

    public GetDoctorListNoGroupResponse getDoctorListNoGroup(String doctorName, String deptCode) {
        try {
            GetDoctorListNoGroupRequest req = GetDoctorListNoGroupRequest.newBuilder()
                    .setDoctorName(doctorName)
                    .setDepartmentCode(deptCode)
                    .build();
            return nfyyServiceBlockingStub.getDoctorListNoGroup(req);
        } catch (Exception e) {
            logger.error("getDoctorListNoGroup(): {}", e.getMessage(), e);
            return null;
        }
    }


    public GetPatientInfoResponse getPatientInfo(String patientCard, String patientId, String idNo, String patientName) {
        try {
            GetPatientInfoRequest request = GetPatientInfoRequest.newBuilder()
                    .setPatientCard(patientCard)
                    .setPatientId(patientId)
                    .setIdNo(idNo)
                    .setPatientName(patientName)
                    .build();
            return nfyyServiceBlockingStub.getPatientInfo(request);
        } catch (Exception e) {
            logger.error("getPatientInfo({}, {}, {}, {}): {}", patientCard, patientId, idNo, patientName, e.getMessage());
            return null;
        }
    }

    /**
     * 建索引
     *
     * @param idNo
     * @param patientName
     * @param sex
     * @param birthday
     * @param mobile
     * @return
     */
    public SavePatientCardResponse savePatientCard(String idNo, String patientName, int sex, String birthday, String mobile) {
        SavePatientCardRequest req = SavePatientCardRequest.newBuilder()
                .setIdNo(idNo)
                .setPatientName(patientName)
                .setPatientType("07")
                .setSex(sex)
                .setBirthday(birthday)
                .setIdType("01")
                .setMobile(mobile)
                .build();
        return nfyyServiceBlockingStub.savePatientCard(req);
    }

    /**
     * 获取挂号记录列表
     *
     * @param hisPatientId
     * @param endDate
     * @return
     */
    public GetRegisteredRecordListResponse getRegisteredRecordList(String hisPatientId, String startDate, String endDate) {
        startDate = !Checker.isNone(startDate) ? startDate : "2000-01-01";
        GetRegisteredRecordListRequest req = GetRegisteredRecordListRequest.newBuilder()
                .setStartDate(startDate)
                .setEndDate(endDate)
                .setPatientId(hisPatientId)
                .build();
        return nfyyServiceBlockingStub.getRegisteredRecordList(req);
    }

    /**
     * 获取检查报告列表
     *
     * @param clinicCode
     * @return
     */
    public GetInspectionReportListResponse getInspectionReportList(String clinicCode) {
        GetInspectionReportListRequest req = GetInspectionReportListRequest.newBuilder()
                .setClinicSeq(clinicCode)
                .build();
        return nfyyServiceBlockingStub.getInspectionReportList(req);
    }

    public GetInspectionReportDetailResponse getInspectionReportDetail(String reportId) {
        GetInspectionReportDetailRequest req = GetInspectionReportDetailRequest.newBuilder()
                .setReportId(reportId)
                .build();
        return nfyyServiceBlockingStub.getInspectionReportDetail(req);
    }

    public GetLaboratoryReportListResponse getLaboratoryReportList(String clinicCode) {
        GetLaboratoryReportListRequest req = GetLaboratoryReportListRequest.newBuilder()
                .setClinicSeq(clinicCode)
                .build();
        return nfyyServiceBlockingStub.getLaboratoryReportList(req);
    }

    public GetLaboratoryReportDetailResponse getLaboratoryReportDetail(String inspectionId) {
        GetLaboratoryReportDetailRequest req = GetLaboratoryReportDetailRequest.newBuilder()
                .setInspectionId(inspectionId)
                .build();
        return nfyyServiceBlockingStub.getLaboratoryReportDetail(req);
    }

    /**
     * 判断医生在指定科室、指定日期时间段内是否有排班
     *
     * @param serviceDate
     * @param period
     * @param doctorCode
     * @param deptCode
     * @return
     */
    public boolean hasScheduling(String serviceDate, int period, String doctorCode, String deptCode) {
        String sessionCode = period == DatePeriodEnum.AFTERNOON.getValue() ? "X" : "S";
        GetScheduleTimeInfoRequest req = GetScheduleTimeInfoRequest.newBuilder()
                .setDepartmentCode(deptCode)
                .setDoctorCode(doctorCode)
                .setServiceDate(serviceDate)
                .setRBASSessionCode(sessionCode)
                .setType(RegisteredTypeEnum.GENERAL.getValue())
                .build();
        GetScheduleTimeInfoResponse res = nfyyServiceBlockingStub.getScheduleTimeInfo(req);

        return res.getCode() == ResultCode.SUCCEED_VALUE && res.getScheduleTimeInfosList().size() > 0;

    }

    public List<Schedule> getScheduleList(String deptCode, String doctorCode, String serviceDate, String sessionCode) {
        List<Schedule> scheduleList;
        GetScheduleListRequest req = GetScheduleListRequest.newBuilder()
                .setDepartmentCode(deptCode)
                .setEndDate(serviceDate)
                .setStartDate(serviceDate)
                .setDoctorCode(doctorCode)
                .setRBASSessionCode(sessionCode)
                .build();
        GetScheduleListResponse res = nfyyServiceBlockingStub.getScheduleList(req);

        if (res.getCode() == ResultCode.SUCCEED_VALUE) {
            scheduleList = res.getSchedulesList();
        } else {
            scheduleList = new LinkedList<>();
        }
        return scheduleList;
    }
}
