package com.diagbot.facade;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
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.*;
import com.diagbot.entity.SysHospitalSet;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.util.*;
import com.diagbot.vo.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

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

    @Autowired
    private ConsoleByDeptFacade consoleByDeptFacade;
    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private SysHospitalSetFacade sysHospitalSetFacade;
    @Autowired
    private FilterFacade filterFacade;
    @Autowired
    private QcresultInfoFacade qcresultInfoFacade;


    /**
     * 病案首页合格率占比
     *
     * @param filterOrderByDeptVO
     * @return
     */
    public void homePageLevelExportByDept(HttpServletResponse response, FilterOrderByDeptVO filterOrderByDeptVO) {
        List<HomePageByDeptDTO> res = consoleByDeptFacade.homePageLevelStatisticsByDept(filterOrderByDeptVO);
        String fileName = "病案首页合格率占比.xls";
        ExcelUtils.exportExcel(res, null, "sheet1", HomePageByDeptDTO.class, fileName, response, 12.8f);
    }

    /**
     * 病案首页不合格/合格数
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void homePageOrLevelExportByDept(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        solveParam(qcResultShortPageVO);
        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelByDeptDTO> records = behospitalInfoFacade.homePageOrLevelExportByDept(qcResultShortPageVO);
        List<ExportSevenByDeptDTO> resDTO = new ArrayList<ExportSevenByDeptDTO>();
        if(ListUtil.isNotEmpty(records)){
                ExportSevenByDeptDTO exportByDeptDTO = new ExportSevenByDeptDTO();
                exportByDeptDTO.setBehDeptName(records.get(0).getBehDeptName());
                exportByDeptDTO.setExportExcelByDeptDTOS(records);
            resDTO.add(exportByDeptDTO);
        }

        List<ExportNinetyByDeptDTO> resWide = new ArrayList<ExportNinetyByDeptDTO>();
        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(resDTO, null, "sheet1", ExportSevenByDeptDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportNinetyByDeptDTO.class, fileName, response);
        }
   }

    /**
     * 缺陷详情-科室（分页）导出
     *
     * @param filterPageByDeptVO
     * @return
     */
    public void entryGroupExportByDeptPage(HttpServletResponse response,FilterPageByDeptVO filterPageByDeptVO) {
        filterFacade.filterPageByDeptVOSet(filterPageByDeptVO);
        filterPageByDeptVO.setCurrent(1L);
        filterPageByDeptVO.setSize(Long.MAX_VALUE);
        filterPageByDeptVO.setSearchCount(false);
        IPage<EntryNumGroupDTO> page = qcresultInfoFacade.entryCountGroupByEntryAndDeptPage(filterPageByDeptVO);
        String fileName = "缺陷详情.xls";
        ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", EntryNumGroupDTO.class, fileName, response, 12.8f);
    }


    /**
     *  各模块缺陷占比导出-科室
     *
     * @param filterPageByDeptVO
     * @return
     */
    public void entryGroupExportByDeptCase(HttpServletResponse response,FilterPageByDeptVO filterPageByDeptVO) {
        filterFacade.filterPageByDeptVOSet(filterPageByDeptVO);
        filterPageByDeptVO.setCurrent(1L);
        filterPageByDeptVO.setSize(Long.MAX_VALUE);
        filterPageByDeptVO.setSearchCount(false);
        IPage<NumDTO> page = qcresultInfoFacade.entryCountGroupByCaseAndDeptPage(filterPageByDeptVO);
        String fileName = "各模块缺陷详情.xls";
        ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", NumDTO.class, fileName, response, 12.8f);
    }

    /**
     *  条目缺陷占比（内页）-科室
     *
     * @param filterPageByDeptVO
     * @return
     */
    public void entryGroupByEntryDeptPage(HttpServletResponse response,FilterPageByDeptVO filterPageByDeptVO) {
        filterFacade.filterPageByDeptVOSet(filterPageByDeptVO);
        filterPageByDeptVO.setCurrent(1L);
        filterPageByDeptVO.setSize(Long.MAX_VALUE);
        filterPageByDeptVO.setSearchCount(false);
        IPage<DeptEntryNumDTO> page = qcresultInfoFacade.entryGroupByEntryAndDeptInnerPage(filterPageByDeptVO);
        String fileName = "条目缺陷占比详情.xls";
        ExcelUtils.exportExcel(page.getRecords(), null, "sheet1", DeptEntryNumDTO.class, fileName, response, 12.8f);
    }

    /**
     * 缺陷详情质控评分页导出-科室
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void qcResultShortByDeptPageExport(HttpServletResponse response,QcResultShortPageVO qcResultShortPageVO) {
        filterFacade.qcResultShortPageVOSet(qcResultShortPageVO);
        nineTytime(qcResultShortPageVO);
        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelByDeptDTO> records = behospitalInfoFacade.qcResultShortByDeptPageExport(qcResultShortPageVO);
        //带缺陷
        List<ExportSevenByDeptDTO> resDTO = new ArrayList<ExportSevenByDeptDTO>();
        if(ListUtil.isNotEmpty(records)){
            ExportSevenByDeptDTO exportByDeptDTO = new ExportSevenByDeptDTO();
            exportByDeptDTO.setBehDeptName(records.get(0).getBehDeptName());
            exportByDeptDTO.setExportExcelByDeptDTOS(records);
            resDTO.add(exportByDeptDTO);
        }
        String fileName = "缺陷详情质控评分.xls";
        ExcelUtils.exportExcelUser(resDTO, null, "sheet1", ExportSevenByDeptDTO.class, fileName, response);

    }
         public void nineTytime(QcResultShortPageVO qcResultShortPageVO){
            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天");
            }
        };

    /**
     * 各科室缺陷占比-科室（组合）
     *
     * @param  filterOrderVO
     * @return
     */
    public void levelByDeptExport(HttpServletResponse response,FilterOrderVO filterOrderVO) {
        filterFacade.filterOrderVOSame(filterOrderVO);
        List<LevelStsByDeptDTO> records = behospitalInfoFacade.levelStatisticsByDept(filterOrderVO);
        if(ListUtil.isNotEmpty(records)){
            for (LevelStsByDeptDTO record : records) {
                BigDecimalStrExport(record);
            }
        }
        String fileName = "各科室缺陷占比-科室.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", LevelStsByDeptDTO.class, fileName, response, 12.8f);
    }

    /**
     * 关键条目缺陷占比统计导出-科室
     *
     * @param  entryStatisticsVO
     * @return
     */
    public void entryStatisticsByDeptExport(HttpServletResponse response,EntryStatisticsVO entryStatisticsVO) {
        List<EntryStasByDeptDTO> records = behospitalInfoFacade.entryStatisticsByDept(entryStatisticsVO);
        String fileName = "关键条目缺陷占比统计导出.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", EntryStasByDeptDTO.class, fileName, response, 12.8f);
    }
    /**
     * 出院人数统计导出-科室
     *
     * @param  qcResultShortPageVO
     * @return
     */
    public void leaveHosMrPageExportByDept(HttpServletResponse response,QcResultShortPageVO qcResultShortPageVO) {
        solveParam(qcResultShortPageVO);
        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelByDeptDTO> records = behospitalInfoFacade.leaveHosMRPageExportByDept(qcResultShortPageVO);
        List<ExportSevenByDeptDTO> resDTO = new ArrayList<ExportSevenByDeptDTO>();
        if(ListUtil.isNotEmpty(records)){
            ExportSevenByDeptDTO exportByDeptDTO = new ExportSevenByDeptDTO();
            exportByDeptDTO.setBehDeptName(records.get(0).getBehDeptName());
            exportByDeptDTO.setExportExcelByDeptDTOS(records);
            resDTO.add(exportByDeptDTO);
        }

        List<ExportNinetyByDeptDTO> resWide = new ArrayList<ExportNinetyByDeptDTO>();
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            resWide = transList(records, resWide);
        }
        String fileName = "出院人数统计.xls";
        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resDTO, null, "sheet1", ExportSevenByDeptDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportNinetyByDeptDTO.class, fileName, response);
        }
    }
    /**
     * 质控核查统计导出-科室
     *
     * @param filterOrderVO
     * @return
     */
    public void qcCheckStaExportByDept(HttpServletResponse response, FilterOrderVO filterOrderVO) {
        List<ImproveByDeptDTO> record =consoleByDeptFacade.qcCheckStatisticsByDept(filterOrderVO);
        ArrayList<ImproveExportByDeptDTO> records = new ArrayList<>();
        ImproveExportByDeptDTO improveExportByDeptDTO = new ImproveExportByDeptDTO();
        if(ListUtil.isNotEmpty(record)){
            improveExportByDeptDTO.setDeptName(filterOrderVO.getDeptName());
            improveExportByDeptDTO.setImproveByDeptDTOS(record);
            records.add(improveExportByDeptDTO);
        }

        String fileName = "质控核查统计.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", ImproveExportByDeptDTO.class, fileName, response, 12.8f);
    }

    /**
     * 质控核查质控评分页-科室 导出
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void qcCheckMRPageExportByDept(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        solveParam(qcResultShortPageVO);
        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelByDeptDTO> records = behospitalInfoFacade.qcCheckMRPageExportByDept(qcResultShortPageVO);
        List<ExportSevenByDeptDTO> resDTO = new ArrayList<ExportSevenByDeptDTO>();
        if(ListUtil.isNotEmpty(records)){
            ExportSevenByDeptDTO exportByDeptDTO = new ExportSevenByDeptDTO();
            exportByDeptDTO.setBehDeptName(records.get(0).getBehDeptName());
            exportByDeptDTO.setExportExcelByDeptDTOS(records);
            resDTO.add(exportByDeptDTO);
        }

        List<ExportNinetyByDeptDTO> resWide = new ArrayList<ExportNinetyByDeptDTO>();
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            resWide = transList(records, resWide);
        }
        String fileName = "质控核查质控评分详情.xls";
        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resDTO, null, "sheet1", ExportSevenByDeptDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportNinetyByDeptDTO.class, fileName, response);
        }
    }


    /**
     * 病案首页改善率质控评分页导出-科室
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void improveMRExportByDept(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        solveParam(qcResultShortPageVO);
        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelByDeptDTO> records = behospitalInfoFacade.improveMRExportByDept(qcResultShortPageVO);
        List<ExportSevenByDeptDTO> resDTO = new ArrayList<ExportSevenByDeptDTO>();
        if(ListUtil.isNotEmpty(records)){
            ExportSevenByDeptDTO exportByDeptDTO = new ExportSevenByDeptDTO();
            exportByDeptDTO.setBehDeptName(records.get(0).getBehDeptName());
            exportByDeptDTO.setExportExcelByDeptDTOS(records);
            resDTO.add(exportByDeptDTO);
        }

        List<ExportNinetyByDeptDTO> resWide = new ArrayList<ExportNinetyByDeptDTO>();
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            resWide = transList(records, resWide);
        }
        String fileName = "病案首页改善率质控评分详情.xls";
        if("1".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resDTO, null, "sheet1", ExportSevenByDeptDTO.class, fileName, response);
        }
        if("2".equals(qcResultShortPageVO.getRadioCheck())){
            ExcelUtils.exportExcelUser(resWide, null, "sheet1", ExportNinetyByDeptDTO.class, fileName, response);
        }
    }

    /**
     * 31天再入院统计导出-科室
     *
     * @param reBeHosPageVO
     * @return
     */
    public void reHos31DaysPageExportByDept(HttpServletResponse response, ReBeHosPageVO reBeHosPageVO) {
        reBeHosPageVO.setSearchCount(false);
        reBeHosPageVO.setFlag(1);
        IPage<ReBeHosByDeptDTO> page = consoleByDeptFacade.reHos31DaysPageByDept(reBeHosPageVO);
        List<ReBeHosByDeptDTO> records = page.getRecords();
        String fileName = "31天再入院统计详情.xls";
        ExcelUtils.exportExcel(records, null, "sheet1", ReBeHosByDeptDTO.class, fileName, response);
    }
    /**
     * 未整改病历详情页导出-科室
     *
     * @param qcResultShortPageVO
     * @return
     */
    public void unModifyExportByDept(HttpServletResponse response, QcResultShortPageVO qcResultShortPageVO) {
        solveParam(qcResultShortPageVO);
        qcResultShortPageVO.setCurrent(1L);
        qcResultShortPageVO.setSize(Long.MAX_VALUE);
        qcResultShortPageVO.setSearchCount(false);
        List<ExportExcelByDeptDTO> records = behospitalInfoFacade.unModifyExportByDept(qcResultShortPageVO);
        String fileName = "未整改病历详情页.xls";
        ExcelUtils.exportExcelUser(records, null, "sheet1", ExportExcelByDeptDTO.class, fileName, response);
    }

    /**
     * 稽查统计导出-科室
     *
     * @param filterVO
     * @return
     */
    public void medicalCheckExportByDept(HttpServletResponse response, FilterMedicalCheckVO filterVO) {
        filterVO.setCurrent(1L);
        filterVO.setSize(Long.MAX_VALUE);
        filterVO.setSearchCount(false);
        String fileName = null;
        if ( "1".equals(filterVO.getIsPlacefile())) {
            fileName = "终末病历稽查表.xls";
        } else {
            fileName = "运行病历稽查表.xls";
        }
        List<MedicalDeptDTO> records = consoleByDeptFacade.medicalCheckExportByDept(filterVO);
        ExcelUtils.exportExcel(records, null, "sheet1", MedicalDeptDTO.class, fileName, response, 15.8f);
    }

    /**
     * 病历质控报表-科室-导出
     *
     * @param behospitalPageVO
     * @return
     */
    public void qualityControlExportByDept(HttpServletResponse response, BehospitalPageVO behospitalPageVO) {
        behospitalPageVO.setCurrent(1L);
        behospitalPageVO.setSize(Long.MAX_VALUE);
        behospitalPageVO.setSearchCount(false);
        Date startDate = behospitalPageVO.getLeaveHosDateStart();
        Date endDate = behospitalPageVO.getLeaveHosDateEnd();
      //  orderMethod(behospitalPageVO);
        //时间间隔30天
        long interval = 30 * 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, "统计区间不能大于30天");
        }
        String  fileName = "病历质控报表.xls";
        IPage<QualityControlDeptDTO> records = consoleByDeptFacade.qualityControlByDept(behospitalPageVO);
        ExcelUtils.exportExcel(records.getRecords(), "病历质控报表", "sheet1", QualityControlDeptDTO.class, fileName, response, 26.8f);
    }

    static void orderMethod(BehospitalPageVO behospitalPageVO){
        List orders = behospitalPageVO.getOrders();
        Boolean flag = false;
        List<OrderItem> itemList = new ArrayList<>();
        List<OrderItem> targetItemList = 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")||orderName.getColumn().equals("doctorName"))){
                flag = true;
            }
        }
        //科室、医生作为第一升序排列，传入排序随后 配合excel注解属性mergeVertical处理科室合并
            OrderItem orderDept = new OrderItem();
            orderDept.setAsc(true);
            orderDept.setColumn("deptName");
            targetItemList.add(orderDept);
            OrderItem orderDoctor = new OrderItem();
            orderDoctor.setAsc(true);
            orderDoctor.setColumn("doctorName");
            targetItemList.add(orderDoctor);
        if(flag){
            if(itemList.size()==1){
                targetItemList.add(itemList.get(0));
            }
        }
        behospitalPageVO.setOrders(targetItemList);

    };



    /**
     * 未整改病历统计导出-科室
     *
     * @param response
     * @param filterUnModifyMRVO
     */
    public void unModifyMRSExportByDept(HttpServletResponse response, FilterUnModifyMRVO filterUnModifyMRVO) {
        filterFacade.filterUnModifyMRVOSet(filterUnModifyMRVO);
        List<ExcelExportEntity> colList = Lists.newLinkedList();
        ExcelExportEntity deptNameCol = new ExcelExportEntity("主管医生", "doctorName");
        deptNameCol.setWidth(50);
        colList.add(deptNameCol);

        QueryWrapper<SysHospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
        hospitalSetQueryWrapper.eq("is_deleted", 'N')
                .eq("hospital_id", filterUnModifyMRVO.getHospitalId())
                .eq("code", "unmodify_mr_entry");
        SysHospitalSet hospitalSet = sysHospitalSetFacade.getOne(hospitalSetQueryWrapper);
        //缺陷条目未维护
        if (hospitalSet == null || StringUtil.isBlank(hospitalSet.getValue())) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "未整改病历条目未设置");
        }

        List<String> columnSet = Arrays.asList(hospitalSet.getValue().split(","));
        for (String valueStr : columnSet) {
            if (StringUtil.isBlank(valueStr)) {
                continue;
            }
            String[] keyValue = valueStr.split("--");
            if (keyValue != null || keyValue.length > 1) {
                ExcelExportEntity entryCol = new ExcelExportEntity(keyValue[1], "entry_" + keyValue[0] + "_num");
                entryCol.setWidth(20);
                colList.add(entryCol);
            }
        }
        UnModifyMRDTO unModifyMRDTO = consoleByDeptFacade.unModifyMRSByDept(filterUnModifyMRVO);
        List<Object> data = Lists.newLinkedList();
        if (unModifyMRDTO != null && ListUtil.isNotEmpty(unModifyMRDTO.getData())) {
            data = unModifyMRDTO.getData();
        }
        String fileName = "未整改病历统计.xls";
        ExcelUtils.exportExcelDynamicCol(colList, data, null, "sheet1", fileName, response);
    }



    public void solveParam(QcResultShortPageVO qcResultShortPageVO){
       String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
       qcResultShortPageVO.setHospitalId(hospitalId);
        qcResultShortPageVO.setUserId(Long.valueOf(userId));
       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天");
           }
       }
   }

    public List<ExportNinetyByDeptDTO> transList(List<ExportExcelByDeptDTO>res, List<ExportNinetyByDeptDTO>resWide){
        if(ListUtil.isNotEmpty(res)){
            ExportNinetyByDeptDTO exportNinetyByDeptDTO = new ExportNinetyByDeptDTO();
            List<ExportWideExcelByDeptDTO>exportWideExcelByDeptDTOs  =  new ArrayList<ExportWideExcelByDeptDTO>();
            res.forEach(exportExcelByDeptDTO->{
                ExportWideExcelByDeptDTO exportWideExcelByDeptDTO = new ExportWideExcelByDeptDTO();
                exportWideExcelByDeptDTO.setAvgScore(exportExcelByDeptDTO.getAvgScore());
                exportNinetyByDeptDTO.setBehDeptName( exportExcelByDeptDTO.getBehDeptName());
                exportWideExcelByDeptDTO.setDoctorName(exportExcelByDeptDTO.getDoctorName());
                List<ExportExcelBehByDeptDTO> exportExcelBehByDeptDTOS = exportExcelByDeptDTO.getExcelBehospitalDTOS();
                List<ExportExcelWideBehByDeptDTO> exportExcelWideBehByDepDTOs = new ArrayList<>();

                exportExcelBehByDeptDTOS.forEach(exportExcelBehByDeptDTO->{
                    ExportExcelWideBehByDeptDTO exportExcelWideBehByDeptDTO = new ExportExcelWideBehByDeptDTO();
                    exportExcelWideBehByDeptDTO.setBehospitalCode(exportExcelBehByDeptDTO.getBehospitalCode());
                    exportExcelWideBehByDeptDTO.setBehospitalDate(exportExcelBehByDeptDTO.getBehospitalDate());
                    exportExcelWideBehByDeptDTO.setLeaveHospitalDate(exportExcelBehByDeptDTO.getLeaveHospitalDate());
                    exportExcelWideBehByDeptDTO.setPatName(exportExcelBehByDeptDTO.getPatName());
                    exportExcelWideBehByDeptDTO.setScore(exportExcelBehByDeptDTO.getScore());
                    exportExcelWideBehByDeptDTO.setScoreBn(exportExcelBehByDeptDTO.getScoreBn());
                    exportExcelWideBehByDepDTOs.add(exportExcelWideBehByDeptDTO);
                });
                exportWideExcelByDeptDTO.setExcelBehospitalDTOS(exportExcelWideBehByDepDTOs);
                exportWideExcelByDeptDTOs.add(exportWideExcelByDeptDTO);
            });
            exportNinetyByDeptDTO.setExportExcelByDeptDTOS(exportWideExcelByDeptDTOs);
            resWide.add(exportNinetyByDeptDTO);
        }
        return resWide;
    }

    public void BigDecimalStrExport(LevelStsByDeptDTO record){

        BigDecimal entrySameBigDecimal = getSubtract(new BigDecimal(record.getEntryNum()), new BigDecimal(record.getSameNum()  == null? 0:record.getSameNum()));

        if(entrySameBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setEntryStrNum(record.getEntryNum()+"↑");
        }else if(entrySameBigDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setEntryStrNum(record.getEntryNum()+"↓");
        }

        BigDecimal sameMrNumBigDecimal = getSubtract(new BigDecimal(record.getMrNum()), new BigDecimal(record.getSameMrNum()  == null? 0:record.getSameMrNum()));
        if(sameMrNumBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setMrStrNum(record.getMrNum()+"↑");
        }else if(sameMrNumBigDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setMrStrNum(record.getMrNum()+"↓");
        }

        BigDecimal sameAvgValueDecimal = getSubtract(new BigDecimal(record.getAverageValue()), new BigDecimal(record.getSameAvgValue() == null? 0:record.getSameAvgValue()));
        if(sameAvgValueDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setAverageStrValue(record.getAverageValue()+"↑");
        }else if(sameAvgValueDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setAverageStrValue(record.getAverageValue()+"↓");
        }

        BigDecimal SameFirstLevelBigDecimal = getSubtract(new BigDecimal(record.getFirstLevelNum()), new BigDecimal(record.getSameFirstLevelNum() == null? 0:record.getSameFirstLevelNum()));
        if(SameFirstLevelBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setFirstLevelStrNum(record.getFirstLevelNum()+"↑");
        }else if(SameFirstLevelBigDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setFirstLevelStrNum(record.getFirstLevelNum()+"↓");
        }

        BigDecimal SameSecondLevelBigDecimal = getSubtract(new BigDecimal(record.getSecondLevelNum()), new BigDecimal(record.getSameSecondLevelNum() == null? 0:record.getSameSecondLevelNum()));
        if(SameSecondLevelBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setSecondLevelStrNum(record.getSecondLevelNum()+"↑");
        }else if(SameSecondLevelBigDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setSecondLevelStrNum(record.getSecondLevelNum()+"↓");
        }

        BigDecimal SameThirdLevelBigDecimal = getSubtract(new BigDecimal(record.getThirdLevelNum()), new BigDecimal(record.getSameThirdLevelNum() == null? 0:record.getSameThirdLevelNum()));
        if(SameThirdLevelBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setThirdLevelStrNum(record.getThirdLevelNum()+"↑");
        }else if(SameThirdLevelBigDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setThirdLevelStrNum(record.getThirdLevelNum()+"↓");
        }

        BigDecimal SameFirstLevelPeDecimal = getSubtract(new BigDecimal(record.getFirstLevelPercent()), new BigDecimal(record.getSameFirstLevelPercent() == null? 0:record.getSameFirstLevelPercent()));
        if(SameFirstLevelPeDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setFirstLevelPercentStr(record.getFirstLevelPercentStr()+"↑");
        }else if(SameFirstLevelPeDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setFirstLevelPercentStr(record.getFirstLevelPercentStr()+"↓");
        }

        BigDecimal SameSecondLevelPeDecimal = getSubtract(new BigDecimal(record.getSecondLevelPercent()), new BigDecimal(record.getSameSecondLevelPercent() == null? 0:record.getSameSecondLevelPercent()));
        if(SameSecondLevelPeDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setSecondLevelPercentStr(record.getSecondLevelPercentStr()+"↑");
        }else if(SameSecondLevelPeDecimal.compareTo(BigDecimal.ZERO)<0){
            record.setSecondLevelPercentStr(record.getSecondLevelPercentStr()+"↓");
        }

        BigDecimal SameThirdLevelPeDecimal = getSubtract(new BigDecimal(record.getThirdLevelPercent()), new BigDecimal(record.getSameThirdLevelPercent() == null? 0:record.getSameThirdLevelPercent()));
        if(SameThirdLevelPeDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setThirdLevelPercentStr(record.getThirdLevelPercentStr()+"↑");
        }else if(SameThirdLevelPeDecimal.compareTo(BigDecimal.ZERO) <0){
            record.setThirdLevelPercentStr(record.getThirdLevelPercentStr()+"↓");
        }
    };

    public void BigDecimalMethod(LevelStatisticsDTO record){
        BigDecimal entrySameBigDecimal = getSubtract(new BigDecimal(record.getEntryNum()), new BigDecimal(record.getSameNum() == null? 0:record.getSameNum()));

        if(entrySameBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setEntrySameNum(1);
        }else if(entrySameBigDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setEntrySameNum(0);
        }else{
            record.setEntrySameNum(-1);
        }

        BigDecimal sameMrNumBigDecimal = getSubtract(new BigDecimal(record.getMrNum()), new BigDecimal(record.getSameMrNum()  == null? 0:record.getSameMrNum()));
        if(sameMrNumBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setMrSameNum(1);
        }else if(sameMrNumBigDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setMrSameNum(0);
        }else{
            record.setMrSameNum(-1);
        }

        BigDecimal sameAvgValueDecimal = getSubtract(new BigDecimal(record.getAverageValue()), new BigDecimal(record.getSameAvgValue() == null? 0:record.getSameAvgValue()));
        if(sameAvgValueDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setAvSameValue(1);
        }else if(sameAvgValueDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setAvSameValue(0);
        }else{
            record.setAvSameValue(-1);
        }

        BigDecimal SameFirstLevelBigDecimal = getSubtract(new BigDecimal(record.getFirstLevelNum()), new BigDecimal(record.getSameFirstLevelNum() == null? 0:record.getSameFirstLevelNum()));
        if(SameFirstLevelBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setFirSameLevelNum(1);
        }else if(SameFirstLevelBigDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setFirSameLevelNum(0);
        }else{
            record.setFirSameLevelNum(-1);
        }

        BigDecimal SameSecondLevelBigDecimal = getSubtract(new BigDecimal(record.getSecondLevelNum()), new BigDecimal(record.getSameSecondLevelNum() == null? 0:record.getSameSecondLevelNum()));
        if(SameSecondLevelBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setSecSameLevelNum(1);
        }else if(SameSecondLevelBigDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setSecSameLevelNum(0);
        }else{
            record.setSecSameLevelNum(-1);
        }

        BigDecimal SameThirdLevelBigDecimal = getSubtract(new BigDecimal(record.getThirdLevelNum()), new BigDecimal(record.getSameThirdLevelNum() == null? 0:record.getSameThirdLevelNum()));
        if(SameThirdLevelBigDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setThiSameLevelNum(1);
        }else if(SameThirdLevelBigDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setThiSameLevelNum(0);
        }else{
            record.setThiSameLevelNum(-1);
        }

        BigDecimal SameFirstLevelPeDecimal = getSubtract(new BigDecimal(record.getFirstLevelPercent()), new BigDecimal(record.getSameFirstLevelPercent() == null? 0:record.getSameFirstLevelPercent()));
        if(SameFirstLevelPeDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setFirSameLevelPercent(1);
        }else if(SameFirstLevelPeDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setFirSameLevelPercent(0);
        }else{
            record.setFirSameLevelPercent(-1);
        }

        BigDecimal SameSecondLevelPeDecimal = getSubtract(new BigDecimal(record.getSecondLevelPercent()), new BigDecimal(record.getSameSecondLevelPercent() == null? 0:record.getSameSecondLevelPercent()));
        if(SameSecondLevelPeDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setSecSameLevelPercent(1);
        }else if(SameSecondLevelPeDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setSecSameLevelPercent(0);
        }else{
            record.setSecSameLevelPercent(-1);
        }

        BigDecimal SameThirdLevelPeDecimal = getSubtract(new BigDecimal(record.getThirdLevelPercent()), new BigDecimal(record.getSameThirdLevelPercent() == null? 0:record.getSameThirdLevelPercent()));
        if(SameThirdLevelPeDecimal.compareTo(BigDecimal.ZERO)>0){
            record.setThiSameLevelPercent(1);
        }else if(SameThirdLevelPeDecimal.compareTo(BigDecimal.ZERO) == 0){
            record.setThiSameLevelPercent(0);
        }else{
            record.setThiSameLevelPercent(-1);
        }
    };

    /**
     * 差值计算
     *
     * @param bigDecimalOne
     * @param bigDecimalTwo
     * @return
     */
    private BigDecimal getSubtract(BigDecimal bigDecimalOne, BigDecimal bigDecimalTwo) {
        BigDecimal newBigDecimal = new BigDecimal(0);
        if (null == bigDecimalOne && null != bigDecimalTwo) {
            newBigDecimal = newBigDecimal.subtract(bigDecimalTwo);
        } else if (null != bigDecimalOne && null == bigDecimalTwo) {
            newBigDecimal = bigDecimalOne.subtract(newBigDecimal);
        } else if (null != bigDecimalOne && null != bigDecimalTwo) {
            newBigDecimal = bigDecimalOne.subtract(bigDecimalTwo);
        }
        return newBigDecimal;
    }


}
