package com.diagbot.facade;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.DoctorAverageLevelDTO;
import com.diagbot.dto.DoctorAverageStatisticsDTO;
import com.diagbot.dto.ExportExcelDTO;
import com.diagbot.dto.GetDetailRecordListPageDTO;
import com.diagbot.dto.GetDoctorDetailPageDTO;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.util.DateUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.DoctorAverageLevelVO;
import com.diagbot.vo.DoctorAverageStatisticsVO;
import com.diagbot.vo.GetDetailRecordListPageVO;
import com.diagbot.vo.GetDoctorDetailPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description:控制台（医生）统计相关处理
 * @Author:songxl
 * @date 2022/4/8 13:20
 */
@Component
public class ConsoleByDoctorFacade {
    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;

    @Autowired
    QcresultInfoFacade qcresultInfoFacade;


    /**
     * 医生质控平均分页面统计
     *
     * @param doctorAverageStatisticsVO
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.diagbot.dto.DoctorAverageStatisticsDTO>
     */
    public IPage<DoctorAverageStatisticsDTO> doctorAverageStatistics(DoctorAverageStatisticsVO doctorAverageStatisticsVO) {
        doctorAverageStatisticsVO.setHospitalId(SysUserUtils.getCurrentHospitalID());
        //替换
        transformParam(doctorAverageStatisticsVO);
        return behospitalInfoFacade.getBaseMapper().doctorAverageStatistics(doctorAverageStatisticsVO);
    }

    /**
     * 参数替换
     *
     * @param doctorAverageStatisticsVO
     * @Return void
     */
    private void transformParam(DoctorAverageStatisticsVO doctorAverageStatisticsVO) {

        if ("-".equals(doctorAverageStatisticsVO.getDeptId())) {
            doctorAverageStatisticsVO.setDeptId("-0");
        }
        if ("-".equals(doctorAverageStatisticsVO.getDeptName())) {
            doctorAverageStatisticsVO.setDeptName("-0");
        }
        if ("-".equals(doctorAverageStatisticsVO.getDoctorId())) {
            doctorAverageStatisticsVO.setDoctorId("-0");
        }
        if ("-".equals(doctorAverageStatisticsVO.getDoctorName())) {
            doctorAverageStatisticsVO.setDoctorName("-0");
        }
    }

    /**
     * 医生质控平均分-患者列表（等级）
     *
     * @param doctorAverageLevelVO
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.diagbot.dto.DoctorAverageLevelDTO>
     */
    public IPage<DoctorAverageLevelDTO> doctorAverageLevelPage(DoctorAverageLevelVO doctorAverageLevelVO) {
        doctorAverageLevelPageSet(doctorAverageLevelVO);
        return behospitalInfoFacade.getBaseMapper().doctorAverageLevelPage(doctorAverageLevelVO);
    }


    private void doctorAverageLevelPageSet(DoctorAverageLevelVO doctorAverageLevelVO) {
        //入参验证
        //入院时间
        if (null != doctorAverageLevelVO && null != doctorAverageLevelVO.getBehosDateStart()) {
            doctorAverageLevelVO.setBehosDateStart(DateUtil.getFirstTimeOfDay(doctorAverageLevelVO.getBehosDateStart()));
        }
        if (null != doctorAverageLevelVO && null != doctorAverageLevelVO.getBehosDateEnd()) {
            doctorAverageLevelVO.setBehosDateEnd(DateUtil.getFirstTimeOfDay(DateUtil.addDay(doctorAverageLevelVO.getBehosDateEnd(), 1)));
        }
        if (null != doctorAverageLevelVO && null != doctorAverageLevelVO.getBehosDateStart() && null != doctorAverageLevelVO.getBehosDateEnd()) {
            if (DateUtil.after(doctorAverageLevelVO.getBehosDateStart(), doctorAverageLevelVO.getBehosDateEnd())) {
                throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "入院时间的开始时间必须小于结束时间！");
            }
        }
        //出院时间
        if (null != doctorAverageLevelVO && null != doctorAverageLevelVO.getLeaveHosDateStart()) {
            doctorAverageLevelVO.setLeaveHosDateStart(DateUtil.getFirstTimeOfDay(doctorAverageLevelVO.getLeaveHosDateStart()));
        }
        if (null != doctorAverageLevelVO && null != doctorAverageLevelVO.getLeaveHosDateEnd()) {
            doctorAverageLevelVO.setLeaveHosDateEnd(DateUtil.getFirstTimeOfDay(DateUtil.addDay(doctorAverageLevelVO.getLeaveHosDateEnd(), 1)));
        }
        if (null != doctorAverageLevelVO && null != doctorAverageLevelVO.getLeaveHosDateStart() && null != doctorAverageLevelVO.getLeaveHosDateEnd()) {
            if (DateUtil.after(doctorAverageLevelVO.getLeaveHosDateStart(), doctorAverageLevelVO.getLeaveHosDateEnd())) {
                throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "出院时间的开始时间必须小于结束时间！");
            }
        }
        doctorAverageLevelVO.setHospitalId(Long.parseLong(SysUserUtils.getCurrentHospitalID()));

        //医生、科室为空时参数替换
        if ("-".equals(doctorAverageLevelVO.getDeptId())) {
            doctorAverageLevelVO.setDeptId("-0");
        }
        if ("-".equals(doctorAverageLevelVO.getDeptName())) {
            doctorAverageLevelVO.setDeptName("-0");
        }
        if ("-".equals(doctorAverageLevelVO.getDoctorCode())) {
            doctorAverageLevelVO.setDoctorCode("-0");
        }
        if ("-".equals(doctorAverageLevelVO.getDoctorName())) {
            doctorAverageLevelVO.setDoctorName("-0");
        }
    }

    /**
     * 获取缺陷列表
     *
     * @param getDoctorDetailPageVO
     * @return
     */
    public IPage<GetDoctorDetailPageDTO> getDoctorDetailPage(GetDoctorDetailPageVO getDoctorDetailPageVO) {
        if ("-".equals(getDoctorDetailPageVO.getDeptId())) {
            getDoctorDetailPageVO.setDeptId("-0");
        }
        if ("-".equals(getDoctorDetailPageVO.getDeptName())) {
            getDoctorDetailPageVO.setDeptName("-0");
        }
        if ("-".equals(getDoctorDetailPageVO.getDoctorId())) {
            getDoctorDetailPageVO.setDoctorId("-0");
        }
        if ("-".equals(getDoctorDetailPageVO.getDoctorName())) {
            getDoctorDetailPageVO.setDoctorName("-0");
        }
        getDoctorDetailPageVO.setHospitalId(SysUserUtils.getCurrentHospitalID());
        IPage<GetDoctorDetailPageDTO> detailPageList = behospitalInfoFacade.getBaseMapper().getDoctorDetailPage(getDoctorDetailPageVO);
        return detailPageList;
    }

    /**
     * 获取缺陷数量对应的病历
     *
     * @param getDetailRecordListPageVO
     * @return
     */
    public IPage<GetDetailRecordListPageDTO> getDetailRecordListPage(GetDetailRecordListPageVO getDetailRecordListPageVO) {
        if ("-".equals(getDetailRecordListPageVO.getDeptId())) {
            getDetailRecordListPageVO.setDeptId("-0");
        }
        if ("-".equals(getDetailRecordListPageVO.getDeptName())) {
            getDetailRecordListPageVO.setDeptName("-0");
        }
        if ("-".equals(getDetailRecordListPageVO.getDoctorId())) {
            getDetailRecordListPageVO.setDoctorId("-0");
        }
        if ("-".equals(getDetailRecordListPageVO.getDoctorName())) {
            getDetailRecordListPageVO.setDoctorName("-0");
        }
        getDetailRecordListPageVO.setHospitalId(SysUserUtils.getCurrentHospitalID());
        IPage<GetDetailRecordListPageDTO> detailPageList = behospitalInfoFacade.getBaseMapper().getDetailRecordListPage(getDetailRecordListPageVO);
        return detailPageList;
    }

    /**
     * 导出缺陷数量对应的病历
     *
     * @param getDetailRecordListPageVO
     * @return
     */
    public List<ExportExcelDTO> getDetailRecordListExport(GetDetailRecordListPageVO getDetailRecordListPageVO) {
        if ("-".equals(getDetailRecordListPageVO.getDeptId())) {
            getDetailRecordListPageVO.setDeptId("-0");
        }
        if ("-".equals(getDetailRecordListPageVO.getDeptName())) {
            getDetailRecordListPageVO.setDeptName("-0");
        }
        if ("-".equals(getDetailRecordListPageVO.getDoctorId())) {
            getDetailRecordListPageVO.setDoctorId("-0");
        }
        if ("-".equals(getDetailRecordListPageVO.getDoctorName())) {
            getDetailRecordListPageVO.setDoctorName("-0");
        }
        getDetailRecordListPageVO.setHospitalId(SysUserUtils.getCurrentHospitalID());
        List<ExportExcelDTO> detailPageList = behospitalInfoFacade.getBaseMapper().getDetailRecordListExport(getDetailRecordListPageVO);
        return detailPageList;
    }

}
