package com.diagbot.facade;

import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2024/5/7 13:53
 */
@Component
public class FilterFacade {
    /**
     * 筛选起始时间
     *
     * @param type
     * @return
     */
    public String getStartDateStr(Integer type, Integer optYear) {
        Date date = new Date();
        String startDate = "";
        String year = optYear == null ? DateUtil.getYear(date) : optYear.toString();
        int month = DateUtil.getMonth(date);
        if (type.equals(1)) {
            //本月统计
            startDate = year + "-" + month + "-1";
        } else if (type.equals(2)) {
            //本年统计
            startDate = year + "-1-1";
        }
        return startDate;
    }

    /**
     * 筛选截止时间
     *
     * @param type
     * @return
     */
    public String getEndDateStr(Integer type, Integer optYear) {
        Date date = new Date();
        String endDate = "";
        String year = optYear == null ? DateUtil.getYear(date) : optYear.toString();
        int month = DateUtil.getMonth(date);
        if (type.equals(1)) {
            //本月统计
            if (month == 12) {
                endDate = (Integer.valueOf(year) + 1) + "-1-1";
            } else {
                endDate = year + "-" + (month + 1) + "-1";
            }
        } else if (type.equals(2)) {
            //本年统计
            endDate = (Integer.valueOf(year) + 1) + "-1-1";
        }
        return endDate;
    }

    /**
     * 上一统计周期（上月/去年）的起始时间
     *
     * @param type
     * @return
     */
    public String getLastStartDateStr(Integer type) {
        Date date = new Date();
        String dateStr = "";
        String year = DateUtil.getYear(date);
        int month = DateUtil.getMonth(date);
        if (type.equals(1)) {
            if (month == 1) {
                dateStr = (Integer.valueOf(year) - 1) + "-12-1";
            } else {
                dateStr = year + "-" + (month - 1) + "-1";
            }
        } else if (type.equals(2)) {
            dateStr = (Integer.valueOf(year) - 1) + "-1-1";
        }
        return dateStr;
    }

    /**
     * 获取上一年同期开始结束时间
     *
     * @param date
     * @return
     */
    public String getStaEndDate(String date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateStart = new Date();
        try {
             dateStart = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(dateStart);
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String newDate = formatter.format(y);
        return newDate;
    }

    /**
     * 获取上一年同期开始结束时间
     *
     * @param date
     * @return
     */
    public String getLastStartEndDate(String date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date dateStart = new Date();
        try {
            dateStart = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(dateStart);
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String newDate = formatter.format(y);
        return newDate;
    }

    /**
     * 获取上一统计周期（上月/去年）的起始时间
     *
     * @param type
     * @return
     */
    public String getLastEndDateStr(Integer type) {
        //上一统计周期的截止时间等于本周期的起始时间
        return getStartDateStr(type, null);
    }

    /**
     * 入参拼接-同比
     *
     * @param filterVO
     */
    public void filterVOSetSame(FilterVO filterVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        filterVO.setHospitalId(hospitalId);
        filterVO.setUserId(Long.valueOf(userId));
        if (filterVO.getLimitCount() == null || filterVO.getLimitCount().equals(0)) {
            filterVO.setLimitCount(10);
        }
        if (StringUtil.isBlank(filterVO.getStartDate())) {
            String startDate = getStartDateStr(filterVO.getType(), null);
            filterVO.setStartDate(startDate);
            String lastStartDate = getLastStartEndDate(startDate);
            filterVO.setLastStartDate(lastStartDate);
        }else{
            String lastStartDate = getLastStartEndDate(filterVO.getStartDate());
            filterVO.setLastStartDate(lastStartDate);
        }
        if (StringUtil.isBlank(filterVO.getEndDate())) {
            String endDate = getEndDateStr(filterVO.getType(), null);
            filterVO.setEndDate(endDate);
            String lastEndDate = getLastStartEndDate(endDate);
            filterVO.setLastEndDate(lastEndDate);
        }else{
            String lastEndDate = getLastStartEndDate(filterVO.getEndDate());
            filterVO.setLastEndDate(lastEndDate);
        }
    }

    /**
     * 入参拼接
     *
     * @param filterVO
     */
    public void filterVOSet(FilterVO filterVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        filterVO.setHospitalId(hospitalId);
        filterVO.setUserId(Long.valueOf(userId));
        if (filterVO.getLimitCount() == null || filterVO.getLimitCount().equals(0)) {
            filterVO.setLimitCount(10);
        }
        if (StringUtil.isBlank(filterVO.getStartDate())) {
            String startDate = getStartDateStr(filterVO.getType(), null);
            filterVO.setStartDate(startDate);
        }
        if (StringUtil.isBlank(filterVO.getEndDate())) {
            String endDate = getEndDateStr(filterVO.getType(), null);
            filterVO.setEndDate(endDate);
        } /*else {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = dateFormat.parse(filterVO.getEndDate());
                long interval = date.getTime() + 1000;
                filterVO.setEndDate(dateFormat.format(new Date(Long.valueOf(interval))));
            } catch (ParseException e) {

            }
        }*/
    }

    /**
     * 入参拼接
     *
     * @param filterVO
     */
    public void filterSet(FilterVO filterVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        filterVO.setHospitalId(hospitalId);
        filterVO.setUserId(Long.valueOf(userId));
        if (filterVO.getLimitCount() == null || filterVO.getLimitCount().equals(0)) {
            filterVO.setLimitCount(10);
        }
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            SimpleDateFormat dateFormatSec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dateStart = dateFormat.parse(filterVO.getStartDate());
            Date dateEnd = dateFormat.parse(filterVO.getEndDate());
            filterVO.setStartDate(dateFormatSec.format(dateStart));
            filterVO.setEndDate(dateFormatSec.format(dateEnd));
        } catch (ParseException e) {

        }

    }


    /**
     * 入参拼接
     *
     * @param filterVO
     */
          List<String> getTimeList(FilterVO filterVO) {
              DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              // 返回的日期集合
              List<String> days = new ArrayList<String>();
              Date start =new Date();
              Date end =new Date();
              try {
              if(StringUtils.isEmpty(filterVO.getHospitalId())){
                  String hospitalId = SysUserUtils.getCurrentHospitalID();
                  String userId = SysUserUtils.getCurrentPrincipleID();
                  filterVO.setHospitalId(hospitalId);
                  filterVO.setUserId(Long.valueOf(userId));
                   start = dateFormat.parse(filterVO.getStartDate()+" 00:00:00");
                   end = dateFormat.parse(filterVO.getEndDate()+" 00:00:00");
              }else{
                  //执行上一天的操作
                  Date dNow = new Date(); //当前时间
                  Date dBefore = new Date();
                  Calendar calendar = Calendar.getInstance(); //得到日历
                  calendar.setTime(dNow);//把当前时间赋给日历
                  calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
                  dBefore = calendar.getTime(); //得到前一天的时间
                  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
                  String defaultStartDate = sdf.format(dBefore); //格式化前一天
                  filterVO.setStartDate(defaultStartDate);
                  filterVO.setEndDate(defaultStartDate);
                  defaultStartDate = defaultStartDate+" 00:00:00";
                  String defaultEndDate = defaultStartDate;
                   start = dateFormat.parse(defaultStartDate);
                   end = dateFormat.parse(defaultEndDate);
              }
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            throw new IllegalArgumentException("参数异常");
        }
        return days;
    }

    /**
     * 分页入参拼接
     *
     * @param filterPageVO
     */
    public void filterPageVOSet(FilterPageVO filterPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        filterPageVO.setHospitalId(hospitalId);
        filterPageVO.setUserId(Long.valueOf(userId));
        if (StringUtil.isBlank(filterPageVO.getStartDate())) {
            String startDate = getStartDateStr(filterPageVO.getType(), null);
            filterPageVO.setStartDate(startDate);
        }
        if (StringUtil.isBlank(filterPageVO.getEndDate())) {
            String endDate = getEndDateStr(filterPageVO.getType(), null);
            filterPageVO.setEndDate(endDate);
        }/* else {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = dateFormat.parse(filterPageVO.getEndDate());
                long interval = date.getTime() + 1000;
                filterPageVO.setEndDate(dateFormat.format(new Date(Long.valueOf(interval))));
            } catch (ParseException e) {

            }
        }*/
    }


    /**
     * 过滤条件设置
     *
     * @param filterPageByDeptVO
     */
    public void filterPageByDeptVOSet(FilterPageByDeptVO filterPageByDeptVO) {
        FilterPageVO filterPageVO = new FilterPageVO();
        BeanUtil.copyProperties(filterPageByDeptVO, filterPageVO);
        filterPageVOSet(filterPageVO);
        filterPageByDeptVO.setHospitalId(filterPageVO.getHospitalId());
        filterPageByDeptVO.setUserId(filterPageVO.getUserId());
        filterPageByDeptVO.setStartDate(filterPageVO.getStartDate());
        filterPageByDeptVO.setEndDate(filterPageVO.getEndDate());
    }

    /**
     * 过滤条件设置
     *
     * @param filterOrderVO
     */
    public void filterOrderVOSet(FilterOrderVO filterOrderVO) {
        FilterVO filterVO = new FilterVO();
        BeanUtil.copyProperties(filterOrderVO, filterVO);
        filterVOSet(filterVO);
        filterOrderVO.setHospitalId(filterVO.getHospitalId());
        filterOrderVO.setUserId(filterVO.getUserId());
        filterOrderVO.setStartDate(filterVO.getStartDate());
        filterOrderVO.setEndDate(filterVO.getEndDate());
        filterOrderVO.setLimitCount(filterVO.getLimitCount());
    }

    /**
     * 过滤条件设置
     *
     * @param filterVO
     */
    public void filterVOSame(FilterVO filterVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        filterVO.setHospitalId(hospitalId);
        filterVO.setUserId(Long.valueOf(userId));
        if (filterVO.getLimitCount() == null || filterVO.getLimitCount().equals(0)) {
            filterVO.setLimitCount(10);
        }
        if (StringUtil.isBlank(filterVO.getStartDate())) {
            String startDate = getStartDateStr(filterVO.getType(), null);
            filterVO.setStartDate(startDate);
            String lastStartDate = getStaEndDate(startDate);
            filterVO.setLastStartDate(lastStartDate);
        }else{
            String lastStartDate = getStaEndDate(filterVO.getStartDate());
            filterVO.setLastStartDate(lastStartDate);
        }
        if (StringUtil.isBlank(filterVO.getEndDate())) {
            String endDate = getEndDateStr(filterVO.getType(), null);
            filterVO.setEndDate(endDate);
            String lastEndDate = getStaEndDate(endDate);
            filterVO.setLastEndDate(lastEndDate);
        }else{
            String lastEndDate = getStaEndDate(filterVO.getEndDate());
            filterVO.setLastEndDate(lastEndDate);
        }
    }

    /**
     * 过滤条件设置
     *
     * @param filterOrderVO
     */
    public void filterOrderVOSame(FilterOrderVO filterOrderVO) {
        FilterVO filterVO = new FilterVO();
        BeanUtil.copyProperties(filterOrderVO, filterVO);
        filterVOSetSame(filterVO);
        filterOrderVO.setHospitalId(filterVO.getHospitalId());
        filterOrderVO.setUserId(filterVO.getUserId());
        filterOrderVO.setStartDate(filterVO.getStartDate());
        filterOrderVO.setEndDate(filterVO.getEndDate());
        filterOrderVO.setLastStartDate(filterVO.getLastStartDate());
        filterOrderVO.setLastEndDate(filterVO.getLastEndDate());
        filterOrderVO.setLimitCount(filterVO.getLimitCount());
    }

    /**
     * 过滤条件设置-同比
     *
     * @param filterOrderByDeptVO
     */
    public void filterOrderByDeptVOSet(FilterOrderByDeptVO filterOrderByDeptVO) {
        FilterVO filterVO = new FilterVO();
        BeanUtil.copyProperties(filterOrderByDeptVO, filterVO);
        filterVOSet(filterVO);
        filterOrderByDeptVO.setHospitalId(filterVO.getHospitalId());
        filterOrderByDeptVO.setUserId(filterVO.getUserId());
        filterOrderByDeptVO.setStartDate(filterVO.getStartDate());
        filterOrderByDeptVO.setEndDate(filterVO.getEndDate());
        filterOrderByDeptVO.setLimitCount(filterVO.getLimitCount());
    }

    /**
     * 平均住院天数、平均花费入参设置
     *
     * @param filterPageByAverageVO
     */
    public void filterPageByAverageVOSet(FilterPageByAverageVO filterPageByAverageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        Date date = new Date();
        String year = DateUtil.getYear(date);
        String startDate = getStartDateStr(filterPageByAverageVO.getType(), null);
        String endDate = getEndDateStr(filterPageByAverageVO.getType(), null);
        String lastStartDate = getLastStartDateStr(filterPageByAverageVO.getType());
        String lastEndDate = getLastEndDateStr(filterPageByAverageVO.getType());
        String lastYearStartDate = getStartDateStr(filterPageByAverageVO.getType(), Integer.valueOf(year) - 1);
        String lastYearEndDate = getEndDateStr(filterPageByAverageVO.getType(), Integer.valueOf(year) - 1);
        filterPageByAverageVO.setHospitalId(hospitalId);
        filterPageByAverageVO.setUserId(Long.valueOf(userId));
        filterPageByAverageVO.setStartDate(startDate);
        filterPageByAverageVO.setEndDate(endDate);
        filterPageByAverageVO.setLastStartDate(lastStartDate);
        filterPageByAverageVO.setLastEndDate(lastEndDate);
        filterPageByAverageVO.setLastYearStartDate(lastYearStartDate);
        filterPageByAverageVO.setLastYearEndDate(lastYearEndDate);
    }

    /**
     * 入参拼接
     *
     * @param qcResultShortPageVO
     */
    public void qcResultShortPageVOSet(QcResultShortPageVO qcResultShortPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        qcResultShortPageVO.setHospitalId(hospitalId);
        qcResultShortPageVO.setUserId(Long.valueOf(userId));
        /*long interval = qcResultShortPageVO.getEndDate().getTime() + 1000;
        qcResultShortPageVO.setEndDate(new Date(Long.valueOf(interval)));*/
    }

    /**
     * 不合格数病历号入参拼接
     *
     * @param qcResultPageVO
     */
    public void badLevelPageVOSet(QcResultPageVO qcResultPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        qcResultPageVO.setHospitalId(hospitalId);
        qcResultPageVO.setUserId(Long.valueOf(userId));
        if (qcResultPageVO.getDeptName().equals("全院")|| StringUtils.isEmpty(qcResultPageVO.getDeptName())) {
            qcResultPageVO.setDeptName("");
        }
        /*long interval = qcResultShortPageVO.getEndDate().getTime() + 1000;
        qcResultShortPageVO.setEndDate(new Date(Long.valueOf(interval)));*/
    }
    /**
     * 合格/不合格数病历号入参拼接-科室
     *
     * @param qcResultPageVO
     */
    public void OrGoodLevelPageVOSet(QcResultPageVO qcResultPageVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        qcResultPageVO.setHospitalId(hospitalId);
        qcResultPageVO.setUserId(Long.valueOf(userId));
    }


    /**
     * 关键条目缺陷占比入参拼接
     *
     * @param entryStatisticsVO
     */
    public void entryStatisticsVOSet(EntryStatisticsVO entryStatisticsVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        entryStatisticsVO.setHospitalId(hospitalId);
        entryStatisticsVO.setUserId(Long.valueOf(userId));
        /*long interval = entryStatisticsVO.getEndDate().getTime() + 1000;
        entryStatisticsVO.setEndDate(new Date(Long.valueOf(interval)));*/
    }

    /**
     * 未整改病历统计入参拼接
     *
     * @param filterUnModifyMRVO
     */
    public void filterUnModifyMRVOSet(FilterUnModifyMRVO filterUnModifyMRVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        filterUnModifyMRVO.setHospitalId(hospitalId);
        filterUnModifyMRVO.setUserId(Long.valueOf(userId));
    }

    /**
     * 31天再入院参数拼接
     *
     * @param reBeHosPageVO
     */
    public void reBeHosPageVOSet(ReBeHosPageVO reBeHosPageVO) {
        if(null == reBeHosPageVO.getUserId() && StringUtils.isEmpty(reBeHosPageVO.getHospitalId())){
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        reBeHosPageVO.setHospitalId(hospitalId);
        reBeHosPageVO.setUserId(Long.valueOf(userId));
        }
    }

    /**
     * 病历稽查统计入参拼接
     *
     * @param filterVO
     */
    public void getMedicalCheckVOSet(FilterMedicalCheckVO filterVO) {
        String hospitalId = SysUserUtils.getCurrentHospitalID();
        String userId = SysUserUtils.getCurrentPrincipleID();
        filterVO.setHospitalId(hospitalId);
        filterVO.setUserId(Long.valueOf(userId));
    }
}
