package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.GetTopPatientInfoDTO;
import com.diagbot.dto.PatientInfoDTO;
import com.diagbot.entity.DeptInfo;
import com.diagbot.entity.DoctorPageMode;
import com.diagbot.entity.PatientInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.PatientInfoServiceImpl;
import com.diagbot.util.DateUtil;
import com.diagbot.vo.GetTopPatientInfoVO;
import com.diagbot.vo.PatientInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

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

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

    /**
     * 获取患者信息
     *
     * @param patientInfoVO
     * @return
     */
    public PatientInfoDTO getPatientInfo(@RequestBody PatientInfoVO patientInfoVO) {
        PatientInfoDTO patientInfoDTO = this.getPatientInfos(patientInfoVO.getPatientCode(), patientInfoVO.getHospitalCode());
        return patientInfoDTO;
    }

    /**
     * 页面顶部病人医生科室信息查询
     *
     * @param getTopPatientInfoVO
     * @return
     */
    public GetTopPatientInfoDTO getTopPatientInfo(@RequestBody GetTopPatientInfoVO getTopPatientInfoVO) {
        GetTopPatientInfoDTO getTopPatientInfoDTO = baseMapper.getTopPatientInfo(getTopPatientInfoVO);
        getTopPatientInfoDTO.setSystemTime(DateUtil.now());
        getTopPatientInfoDTO.setRecordId(getTopPatientInfoVO.getRecordId());

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

        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;
    }

    /**
     * 通过医院编码、病人编码获取病人信息
     *
     * @param hospitalCode 医院编码
     * @param patientCode  病人编码
     * @return
     */
    public PatientInfo getPatientByCode(String hospitalCode, String patientCode) {
        QueryWrapper<PatientInfo> queryWrapper = new QueryWrapper<PatientInfo>();
        queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        queryWrapper.eq("hospital_code", hospitalCode);
        queryWrapper.eq("code", patientCode);
        return this.getOne(queryWrapper);
    }

}
