package com.diagbot.facade;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.diagbot.dto.AverageStatisticsDTO;
import com.diagbot.dto.AverageStatisticsFeeDTO;
import com.diagbot.dto.AverageStatisticsFeeMonthDTO;
import com.diagbot.dto.AverageStatisticsMonthDTO;
import com.diagbot.dto.EntryNumDTO;
import com.diagbot.dto.EntryNumGroupDTO;
import com.diagbot.dto.EntryStatisticsDTO;
import com.diagbot.dto.ExportExcelBehospitalDTO;
import com.diagbot.dto.ExportExcelDTO;
import com.diagbot.dto.ExportExcelWideBehospitalDTO;
import com.diagbot.dto.ExportWideExcelDTO;
import com.diagbot.dto.HomePageImproveDTO;
import com.diagbot.dto.HomePageNumDTO;
import com.diagbot.dto.LevelStatisticsDTO;
import com.diagbot.dto.LevelStatisticsTZDTO;
import com.diagbot.dto.MedicalCheckExportDTO;
import com.diagbot.dto.NumDTO;
import com.diagbot.dto.QcResultPercentDTO;
import com.diagbot.dto.QualityControlDTO;
import com.diagbot.dto.ReBeHosDTO;
import com.diagbot.dto.SjcfStatisticsDTO;
import com.diagbot.dto.UnModifyMRDTO;
import com.diagbot.entity.SysHospitalSet;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.ExcelUtils;
import com.diagbot.util.ListUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.BehospitalPageVO;
import com.diagbot.vo.EntryStatisticsVO;
import com.diagbot.vo.FilterMedicalCheckVO;
import com.diagbot.vo.FilterOrderVO;
import com.diagbot.vo.FilterPageByAverageVO;
import com.diagbot.vo.FilterPageVO;
import com.diagbot.vo.FilterUnModifyMRVO;
import com.diagbot.vo.QcResultShortPageVO;
import com.diagbot.vo.ReBeHosPageVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;

/**
 * @Description: 报表导出装饰层
 * @author: gaodm
 * @time: 2024/6/18 10:41
 */
@Component
public class ConsoleExportFacade {

    @Autowired
    private ConsoleFacade consoleFacade;
    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private SysHospitalSetFacade sysHospitalSetFacade;
    @Autowired
    private FilterFacade filterFacade;

    /**
     * 病案首页合格率占比
     *
     * @param filterOrderVO
     * @return
     */
    public void homePageLevelExport(HttpServletResponse response, FilterOrderVO filterOrderVO) {
        List<HomePageNumDTO> res = consoleFacade.homePageLevelStatistics(filterOrderVO);
        String fileName = "病案首页合格率占比.xls";
        ExcelUtils.exportExcel(res, null, "sheet1", HomePageNumDTO.class, fileName, response, 12.8f);
    }

    /**
     * 条目缺陷占比（内页）
     *
     * @param filterPageVO
     * @return
     */
    public void entryGroupByEntryExport(HttpServletResponse response, FilterPageVO filterPageVO) {
        filterPageVO.setCurrent(1L);
        filterPageVO.setSize(Long.MAX_VALUE);
        filterPageVO.setSearchCount(false);
        IPage<EntryNumDTO> page = consoleFacade.entryGroupByEntryInnerPage(filterPageVO);
        String fileName = "条目缺陷占比.xls";
        ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", EntryNumDTO.class, fileName, response, 12.8f);
    }

    /**
     * 各科室缺陷占比（组合）
     *
     * @param filterOrderVO
     * @return
     */
    public void levelExport(HttpServletResponse response, FilterOrderVO filterOrderVO) {
        List<LevelStatisticsDTO> records = consoleFacade.levelStatisticsStr(filterOrderVO);
        String fileName = "各科室缺陷占比.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", LevelStatisticsDTO.class, fileName, response, 12.8f);
    }

    /**
     * 各科室缺陷占比（组合）-台州-导出
     *
     * @param filterOrderVO
     * @return
     */
    public void levelExport_TZ(HttpServletResponse response, FilterOrderVO filterOrderVO) {
        List<LevelStatisticsTZDTO> records = consoleFacade.levelStatisticsByDeptStr_TZ(filterOrderVO);
        String fileName = "各科室缺陷占比-台州.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", LevelStatisticsTZDTO.class, fileName, response, 12.8f);
    }

    /**
     * 按科室统计平均住院天数导出
     *
     * @param filterPageByAverageVO
     * @return
     */
    public void getAverageDayNumExport(HttpServletResponse response, FilterPageByAverageVO filterPageByAverageVO) {
        filterPageByAverageVO.setCurrent(1L);
        filterPageByAverageVO.setSize(Long.MAX_VALUE);
        filterPageByAverageVO.setSearchCount(false);
        IPage<AverageStatisticsDTO> page = consoleFacade.getAverageDayNumPage(filterPageByAverageVO);
        String fileName = " 按科室统计平均住院天数.xls";

        if (filterPageByAverageVO.getType() == 2) {
            ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", AverageStatisticsDTO.class, fileName, response, 12.8f);
        }
        if (filterPageByAverageVO.getType() == 1) {
            List<AverageStatisticsMonthDTO> list = BeanUtil.listCopyTo(page.getRecords(), AverageStatisticsMonthDTO.class);
            ExcelUtils.exportExcel(list, null, "sheet1", AverageStatisticsMonthDTO.class, fileName, response, 12.8f);
        }

    }

    /**
     * 按科室统计平均住院费用导出
     *
     * @param filterPageByAverageVO
     * @return
     */
    public void getAverageFeeExport(HttpServletResponse response, FilterPageByAverageVO filterPageByAverageVO) {
        filterPageByAverageVO.setCurrent(1L);
        filterPageByAverageVO.setSize(Long.MAX_VALUE);
        filterPageByAverageVO.setSearchCount(false);
        IPage<AverageStatisticsFeeDTO> page = consoleFacade.getAverageFeePage(filterPageByAverageVO);
        String fileName = " 按科室统计平均住院费用.xls";

        if (filterPageByAverageVO.getType() == 2) {
            ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", AverageStatisticsFeeDTO.class, fileName, response, 12.8f);
        }
        if (filterPageByAverageVO.getType() == 1) {
            List<AverageStatisticsFeeMonthDTO> list = BeanUtil.listCopyTo(page.getRecords(), AverageStatisticsFeeMonthDTO.class);
            ExcelUtils.exportExcel(list, null, "sheet1", AverageStatisticsFeeMonthDTO.class, fileName, response, 12.8f);
        }
    }

    /**
     * 各科室甲级病历占比排行导出
     *
     * @param filterPageVO
     * @return
     */
    public void levelPercentGroupByDeptExport(HttpServletResponse response, FilterPageVO filterPageVO) {
        filterPageVO.setCurrent(1L);
        filterPageVO.setSize(Long.MAX_VALUE);
        filterPageVO.setSearchCount(false);
        IPage<QcResultPercentDTO> page = consoleFacade.levelPercentGroupByDeptPage(filterPageVO);
        String fileName = " 各科室甲级病历占比排行.xls";
        ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", QcResultPercentDTO.class, fileName, response, 12.8f);
    }

    /**
     * 缺陷详情导出
     *
     * @param filterPageVO
     * @return
     */
    public void entryCountGroupByEntryExport(HttpServletResponse response, FilterPageVO filterPageVO) {
        filterPageVO.setCurrent(1L);
        filterPageVO.setSize(Long.MAX_VALUE);
        filterPageVO.setSearchCount(false);
        IPage<EntryNumGroupDTO> page = consoleFacade.entryCountGroupByEntryPage(filterPageVO);
        String fileName = "缺陷详情.xls";
        ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", EntryNumGroupDTO.class, fileName, response, 12.8f);
    }


    /**
     * 各模块缺陷占比导出
     *
     * @param filterPageVO
     * @return
     */
    public void entryCountGroupByCaseExport(HttpServletResponse response, FilterPageVO filterPageVO) {
        filterPageVO.setCurrent(1L);
        filterPageVO.setSize(Long.MAX_VALUE);
        filterPageVO.setSearchCount(false);
        IPage<NumDTO> page = consoleFacade.entryCountGroupByCasePage(filterPageVO);
        String fileName = "缺陷详情.xls";
        ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", NumDTO.class, fileName, response, 12.8f);
    }

    /**
     * 关键条目缺陷占比统计
     *
     * @param entryStatisticsVO
     * @return
     */
    public void entryStatisticsExport(HttpServletResponse response, EntryStatisticsVO entryStatisticsVO) {
        List<EntryStatisticsDTO> records = consoleFacade.entryStatistics(entryStatisticsVO);
        String fileName = "关键条目缺陷占比统计.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", EntryStatisticsDTO.class, fileName, response, 12.8f);
    }
    
    public void sjcfStatisticsExport(HttpServletResponse response, @Valid EntryStatisticsVO entryStatisticsVO) {
    	 List<SjcfStatisticsDTO> records = consoleFacade.sjcfStatistics(entryStatisticsVO);
         String fileName = "三级查房制度相关制度.xls";
         ExcelUtils.exportExcel(records, null, "sheet1", SjcfStatisticsDTO.class, fileName, response, 12.8f);
    }

    /**
     * 缺陷详情质控评分页导出
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void qcResultShortPageExport(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        qcResultShortPageVO.setHospitalId(hospitalId);
        if (StringUtil.isNotBlank(qcResultShortPageVO.getDeptName())
                && qcResultShortPageVO.getDeptName().equals("全部")) {
            qcResultShortPageVO.setDeptName("");
        }
        Date startDate = qcResultShortPageVO.getStartDate();
        Date endDate = qcResultShortPageVO.getEndDate();
        //时间间隔90天
        long interval = 90 * 24l * 60l * 60l * 1000l;
        if (endDate.getTime() < startDate.getTime()) {
            throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
        }
        if (endDate.getTime() - startDate.getTime() > interval) {
            throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于90天");
        }

        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelDTO> record = behospitalInfoFacade.qcResultShortPageExport(qcResultShortPageVO);
        String fileName = "缺陷详情质控评分页.xls";
        ExcelUtils.exportExcelUser(record, null, "sheet1", ExportExcelDTO.class, fileName, response);
    }

    /**
     * 出院人数统计导出
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void leaveHosMrPageExport(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        qcResultShortPageVO.setHospitalId(hospitalId);
        if (StringUtil.isNotBlank(qcResultShortPageVO.getDeptName())
                && qcResultShortPageVO.getDeptName().equals("全部")) {
            qcResultShortPageVO.setDeptName("");
        }
        Date startDate = qcResultShortPageVO.getStartDate();
        Date endDate = qcResultShortPageVO.getEndDate();
        //时间间隔7天
        long interval_7 = 7 * 24 * 60 * 60 * 1000;
        long interval_90 = 90 * 24 * 60 * 60 * 1000l;

        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            //时间间隔7天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_7) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于7天");
            }
        }else{
            //时间间隔90天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_90) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于90天");
            }
        }

        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelDTO> records = behospitalInfoFacade.leaveHosMRPageExport(qcResultShortPageVO);
        List<ExportWideExcelDTO> resWide = new ArrayList<ExportWideExcelDTO>();
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            resWide = transList(records, resWide);
        }
        String fileName = "出院人数统计.xls";
          if("1".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(records, null, "sheet1", ExportExcelDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportWideExcelDTO.class, fileName, response);
        }
    }

    public List<ExportWideExcelDTO> transList(List<ExportExcelDTO>res, List<ExportWideExcelDTO>resWide){
        if(ListUtil.isNotEmpty(res)){
            res.forEach(exportExcelDTO->{
                ExportWideExcelDTO exportWideExcelDTO = new ExportWideExcelDTO();
                exportWideExcelDTO.setAvgScore(exportExcelDTO.getAvgScore());
                exportWideExcelDTO.setBehDeptName(exportExcelDTO.getBehDeptName());
                List<ExportExcelBehospitalDTO> excelBehospitalDTOS = exportExcelDTO.getExcelBehospitalDTOS();
                List<ExportExcelWideBehospitalDTO> exportExcelWideBehospitalDTOs = new ArrayList<>();
                excelBehospitalDTOS.forEach(exportExcelBehospitalDTO->{
                    ExportExcelWideBehospitalDTO exportExcelWideBehospitalDTO = new ExportExcelWideBehospitalDTO();
                    exportExcelWideBehospitalDTO.setDoctorName(exportExcelBehospitalDTO.getDoctorName());
                    exportExcelWideBehospitalDTO.setBehospitalCode(exportExcelBehospitalDTO.getBehospitalCode());
                    exportExcelWideBehospitalDTO.setBehospitalDate(exportExcelBehospitalDTO.getBehospitalDate());
                    exportExcelWideBehospitalDTO.setLeaveHospitalDate(exportExcelBehospitalDTO.getLeaveHospitalDate());
                    exportExcelWideBehospitalDTO.setPatName(exportExcelBehospitalDTO.getPatName());
                    exportExcelWideBehospitalDTO.setScore(exportExcelBehospitalDTO.getScore());
                    exportExcelWideBehospitalDTO.setScoreBn(exportExcelBehospitalDTO.getScoreBn());
                    exportExcelWideBehospitalDTOs.add(exportExcelWideBehospitalDTO);
                });
                exportWideExcelDTO.setExcelBehospitalDTOS(exportExcelWideBehospitalDTOs);
                resWide.add(exportWideExcelDTO);
            });
        }
        return resWide;
    }

    /**
     * 质控核查统计导出
     *
     * @param filterOrderVO
     * @return
     */
    public void qcCheckStatisticsExport(HttpServletResponse response, FilterOrderVO filterOrderVO) {
        List<HomePageImproveDTO> records = consoleFacade.qcCheckStatistics(filterOrderVO);
        String fileName = "质控核查统计.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", HomePageImproveDTO.class, fileName, response, 12.8f);
    }

    /**
     * 未整改病历详情页导出
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void unModifyMRPageExport(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        qcResultShortPageVO.setHospitalId(hospitalId);
        if (StringUtil.isNotBlank(qcResultShortPageVO.getDeptName())
                && qcResultShortPageVO.getDeptName().equals("全部")) {
            qcResultShortPageVO.setDeptName("");
        }
        Date startDate = qcResultShortPageVO.getStartDate();
        Date endDate = qcResultShortPageVO.getEndDate();
        //时间间隔7天
        long interval = 90 * 24l * 60l * 60l * 1000l;
        if (endDate.getTime() < startDate.getTime()) {
            throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
        }
        if (endDate.getTime() - startDate.getTime() > interval) {
            throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于90天");
        }

        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelDTO> records = behospitalInfoFacade.unModifyMRPageExport(qcResultShortPageVO);
        String fileName = "未整改病历详情页.xls";
        ExcelUtils.exportExcelUser(records, null, "sheet1", ExportExcelDTO.class, fileName, response);
    }

    /**
     * 病案首页不合格/合格数
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void badLevelPagePageExport(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        qcResultShortPageVO.setHospitalId(hospitalId);
        if (StringUtil.isNotBlank(qcResultShortPageVO.getDeptName())
                && qcResultShortPageVO.getDeptName().equals("全部")) {
            qcResultShortPageVO.setDeptName("");
        }
        Date startDate = qcResultShortPageVO.getStartDate();
        Date endDate = qcResultShortPageVO.getEndDate();
        //时间间隔7天
        long interval_7 = 7 * 24 * 60 * 60 * 1000;
        long interval_90 = 90 * 24 * 60 * 60 * 1000l;

        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            //时间间隔7天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_7) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于7天");
            }
        }else{
            //时间间隔90天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_90) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于90天");
            }
        }

        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelDTO> records = behospitalInfoFacade.badLevelPagePageExport(qcResultShortPageVO);
        List<ExportWideExcelDTO> resWide = new ArrayList<ExportWideExcelDTO>();
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            resWide = transList(records, resWide);
        }
        String fileName = null;
        if("不合格数".equals(qcResultShortPageVO.getTitleName())){
            fileName = "病案首页不合格数病历详情页.xls";
        }else{
            fileName = "病案首页合格数病历详情页.xls";
        }
        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(records, null, "sheet1", ExportExcelDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportWideExcelDTO.class, fileName, response);
        }
   }

    /**
     * 31天再入院统计导出
     *
     * @param reBeHosPageVO
     * @return
     */
    public void reHos31DaysPageExport(HttpServletResponse response, ReBeHosPageVO reBeHosPageVO) {
        reBeHosPageVO.setSearchCount(false);
        reBeHosPageVO.setFlag(1);
        IPage<ReBeHosDTO> page = consoleFacade.reHos31DaysPage(reBeHosPageVO);
        List<ReBeHosDTO> records = page.getRecords();
        String fileName = "31天再入院统计详情.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", ReBeHosDTO.class, fileName, response);
    }

    /**
     * 病案首页改善率质控评分页导出
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void hmImproveMRPageExport(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        qcResultShortPageVO.setHospitalId(hospitalId);
    if (StringUtil.isNotBlank(qcResultShortPageVO.getDeptName())
                && qcResultShortPageVO.getDeptName().equals("全部")) {
            qcResultShortPageVO.setDeptName("");
        }
        Date startDate = qcResultShortPageVO.getStartDate();
        Date endDate = qcResultShortPageVO.getEndDate();
        //时间间隔7天
        long interval_7 = 7 * 24 * 60 * 60 * 1000;
        long interval_90 = 90 * 24 * 60 * 60 * 1000l;

        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            //时间间隔7天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_7) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于7天");
            }
        }else{
            //时间间隔90天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_90) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于90天");
            }
        }

        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelDTO> records = behospitalInfoFacade.hmImproveMRPageExport(qcResultShortPageVO);
        List<ExportWideExcelDTO> resWide = new ArrayList<ExportWideExcelDTO>();
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            resWide = transList(records, resWide);
        }
        String fileName = "病案首页改善率评分病历导出.xls";
        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(records, null, "sheet1", ExportExcelDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportWideExcelDTO.class, fileName, response);
        }
    }

    /**
     * 质控核查质控评分页导出
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void qcCheckMRPageExport(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        qcResultShortPageVO.setHospitalId(hospitalId);
        if (StringUtil.isNotBlank(qcResultShortPageVO.getDeptName())
                && qcResultShortPageVO.getDeptName().equals("全部")) {
            qcResultShortPageVO.setDeptName("");
        }
        Date startDate = qcResultShortPageVO.getStartDate();
        Date endDate = qcResultShortPageVO.getEndDate();

        //时间间隔7天
        long interval_7 = 7 * 24 * 60 * 60 * 1000;
        long interval_90 = 90 * 24 * 60 * 60 * 1000l;

        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            //时间间隔7天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_7) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于7天");
            }
        }else{
            //时间间隔90天
            if (endDate.getTime() < startDate.getTime()) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "截止时间不能小于起始时间");
            }
            if (endDate.getTime() - startDate.getTime() > interval_90) {
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "统计区间不能大于90天");
            }
        }

        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelDTO> records = behospitalInfoFacade.qcCheckMRPageExport(qcResultShortPageVO);
        List<ExportWideExcelDTO> resWide = new ArrayList<ExportWideExcelDTO>();
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            resWide = transList(records, resWide);
        }
        String fileName = "质控核查病历导出.xls";
        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(records, null, "sheet1", ExportExcelDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportWideExcelDTO.class, fileName, response);
        }
    }

   static void orderMethod(BehospitalPageVO behospitalPageVO){
       List<?> orders = behospitalPageVO.getOrders();
       Boolean flag= false;
       List<OrderItem> itemList = new ArrayList<>();
       for (Object order : orders) {
           ObjectMapper objectMapper = new ObjectMapper();
           OrderItem orderName = objectMapper.convertValue(order, OrderItem.class);
           itemList.add(orderName);
           if(!orderName.getColumn().equals("deptName")){
               flag = true;
           }
       }
       //科室作为第一升序排列，传入排序随后 配合excel注解属性mergeVertical处理科室合并
       if(flag){
           OrderItem orderItem = new OrderItem();
           orderItem.setAsc(true);
           orderItem.setColumn("deptName");
           itemList.add(orderItem);
           Collections.reverse(itemList);
           behospitalPageVO.setOrders(itemList);
       }

   }


}
