package com.ywt.mg.web.common;

import com.ywt.gapi.user.DoctorInfo;
import com.ywt.gapi.user.DoctorServiceGrpc;
import com.ywt.gapi.user.GetDoctorAllRequest;
import com.ywt.gapi.user.GetDoctorAllResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author daiyihua
 * @create 2022-08-23 下午3:52
 * @desc 医生相关的公共方法
 **/
@Service
public class DoctorSrv {

    private Logger logger = LoggerFactory.getLogger(DoctorSrv.class);

    @Autowired
    private DoctorServiceGrpc.DoctorServiceBlockingStub doctorServiceBlockingStub;

    /**
     * 请求rpc方法，得到所有医生（认证的和未认证的）
     *
     * @return
     */
    public List<DoctorInfo> getDoctorAll() {
        try {
            // 获取医生数据
            GetDoctorAllRequest rest = GetDoctorAllRequest.newBuilder().build();
            GetDoctorAllResponse resp = doctorServiceBlockingStub.getDoctorAll(rest);
            return resp.getDoctorInfosList();
        } catch (Exception e) {
            logger.error("调用doctorServiceBlockingStub.getDoctorAll()方法出错", e.getMessage(), e);
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    /**
     * 从缓存中根据医生ID获取医生相关信息
     *
     * @param doctorId 医生ID
     * @return {@link DoctorInfo}
     */
    public DoctorInfo getDoctorInfoById(int doctorId) {
        List<DoctorInfo> doctorInfoList = getDoctorAll();
        return getDoctorInfoById(doctorInfoList, doctorId);
    }

    /**
     * 从缓存中根据医生ID获取医生相关信息
     *
     * @param doctorId 医生ID
     * @return {@link DoctorInfo}
     */
    public DoctorInfo getDoctorInfoById(List<DoctorInfo> doctorInfoList, int doctorId) {
        return doctorInfoList.stream().filter(f -> f.getDoctorid() == doctorId).findFirst().orElse(null);
    }


}
