package com.diagbot.facade;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.diagbot.client.TranServiceClient;
import com.diagbot.dto.HospitalInfoDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.vo.HospitalInfoVO;

/**
 * @Description:
 * @author: wangyu
 * @time: 2018/11/22 13:43
 */
@Component
public class HospitalInfoFacade {

    @Autowired
    TranServiceClient tranServiceClient;

    /**
     * 获取医院信息
     *
     * @param hospitalInfoVO
     * @return
     */
    public List<HospitalInfoDTO> getHospitalInfo(HospitalInfoVO hospitalInfoVO) {
        RespDTO<List<HospitalInfoDTO>> hospitalInfoDTOList = tranServiceClient.getHospitalInfo(hospitalInfoVO);
        if (hospitalInfoDTOList == null
                || !CommonErrorCode.OK.getCode().equals(hospitalInfoDTOList.code)) {
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "获取医院信息失败");
        }
        return hospitalInfoDTOList.data;
    }

    /**
     * 通过医院编码获取医院信息
     *
     * @param code 医院编码
     * @return
     */
    public HospitalInfoDTO getHospitalByCode(String code) {
    	HospitalInfoVO hospitalInfoVO = new HospitalInfoVO();
    	hospitalInfoVO.setCode(code);
    	RespDTO<List<HospitalInfoDTO>> hospitalInfoDTOList = tranServiceClient.getHospitalInfo(hospitalInfoVO);
        if (hospitalInfoDTOList == null
                || !CommonErrorCode.OK.getCode().equals(hospitalInfoDTOList.code)
                ||hospitalInfoDTOList.data.size()!=1) {
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "获取医院信息失败");
        }
        return hospitalInfoDTOList.data.get(0);
    }

}
