package com.diagbot.facade;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.client.TranServiceClient;
import com.diagbot.dto.GetTopPatientInfoDTO;
import com.diagbot.dto.PatientInfoDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.DeptInfo;
import com.diagbot.entity.DoctorPageMode;
import com.diagbot.entity.PatientInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.vo.GetTopPatientInfoVO;
import com.diagbot.vo.PatientInfoVO;

/**
 * @Description: 患者业务逻辑
 * @author: wangyu
 * @time: 2018/11/19 13:19
 */
@Component
public class PatientInfoFacade {

    @Autowired
    private DoctorPageModeFacade doctorPageModeFacade;
    @Autowired
    private DeptInfoFacade deptInfoFacade;
    @Autowired
    TranServiceClient tranServiceClient;

    /**
     * 获取患者信息
     *
     * @param patientInfoVO
     * @return
     */
    public PatientInfoDTO getPatientInfo(PatientInfoVO patientInfoVO) {
        RespDTO<PatientInfoDTO> patientInfoDTO = tranServiceClient.getPatientInfo(patientInfoVO);
        if (patientInfoDTO == null
                || !CommonErrorCode.OK.getCode().equals(patientInfoDTO.code)) {
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "获取患者信息失败");
        }
        return patientInfoDTO.data;
    }

    /**
     * 页面顶部病人医生科室信息查询
     *
     * @param getTopPatientInfoVO
     * @return
     */
    public GetTopPatientInfoDTO getTopPatientInfo(@RequestBody GetTopPatientInfoVO getTopPatientInfoVO) {
        RespDTO<GetTopPatientInfoDTO> respDTO = tranServiceClient.getTopPatientInfo(getTopPatientInfoVO);
        if (respDTO == null || !"0".equals(respDTO.code)) {
            throw new CommonException(CommonErrorCode.RPC_ERROR,
                    "页面顶部病人医生科室信息");
        }

        GetTopPatientInfoDTO getTopPatientInfoDTO = respDTO.data;

        QueryWrapper<DeptInfo> deptInfoQ = new QueryWrapper<>();
        deptInfoQ.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("name", getTopPatientInfoDTO.getSelfDeptName());
        DeptInfo deptInfo = deptInfoFacade.getOne(deptInfoQ);
        if (null != deptInfo) {
            getTopPatientInfoDTO.setSelfDeptName(deptInfo.getName());
            getTopPatientInfoDTO.setSelfDeptId(deptInfo.getId());
        }

        QueryWrapper<DoctorPageMode> doctorPageModeQe = new QueryWrapper<>();
        doctorPageModeQe.eq("is_deleted", IsDeleteEnum.N.getKey());
        doctorPageModeQe.eq("doctor_id", getTopPatientInfoDTO.getDoctorId());
        DoctorPageMode doctorPageMode = doctorPageModeFacade.getOne(doctorPageModeQe);
        if (doctorPageMode != null) {
            getTopPatientInfoDTO.setModeClassify(doctorPageMode.getModeClassify());
            getTopPatientInfoDTO.setModeValue(doctorPageMode.getModeValue());
        }
        return getTopPatientInfoDTO;
    }
    
    /**
     * 根据ids获取病人信息
     * @param ids 病人ids
     * @return 病人信息
     */
    public Map<Long, PatientInfo> patientInfoMapByIds(List<Long> ids) {
    	 RespDTO<Map<Long, PatientInfo>> retData = tranServiceClient.patientInfoMapByIds(ids);
         if (retData == null
                 || !CommonErrorCode.OK.getCode().equals(retData.code)) {
             throw new CommonException(CommonErrorCode.RPC_ERROR,
                     "获取患者信息失败");
         }
         return retData.data;
    }

}
