package com.reyun.saas.report.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.reyun.saas.report.vo.ReportParamsVO;
import org.springframework.util.StringUtils;

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

/**
 * @desc
 * @date
 */
public class ReportParamsUril {

    /**
     * 功能描述:处理报表参数接收对象的开始结束时间
     */
    public static ReportParamsVO handDate(ReportParamsVO reportParamsVO){
        String startDateStr = reportParamsVO.getStartDate();
        String endDateStr = reportParamsVO.getEndDate();
        Date startDate = DateUtil.parse(startDateStr, DatePattern.NORM_DATE_PATTERN).toJdkDate();
        Date endDate = DateUtil.parse(endDateStr, DatePattern.NORM_DATE_PATTERN).toJdkDate();
        Long betweenDay = DateUtil.betweenDay(startDate, endDate, true);
        reportParamsVO.setBetweenDay(betweenDay.intValue());

        String today = DateUtil.today();
        Date todayDate = DateUtil.parse(today, DatePattern.NORM_DATE_PATTERN).toJdkDate();
        DateTime yesterday = DateUtil.yesterday();

        Date yesterdayDate = DateUtil.parse(yesterday.toString(DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN).toJdkDate();

        Boolean todayDateBoolean = checkBetweenDate(startDate, endDate, todayDate);
        Boolean yesterdayDateBoolean = checkBetweenDate(startDate, endDate, yesterdayDate);

        if(todayDateBoolean&&yesterdayDateBoolean){//今天昨天都包含
            //判断天数是否是两天betweenDay=1
            if(betweenDay.intValue()==1){//实时
                reportParamsVO.setStartDateStream(yesterday.toString(DatePattern.NORM_DATE_PATTERN));
                reportParamsVO.setEndDateStream(today);
                reportParamsVO.setDateType("1");
            }else{//实时+离线
                reportParamsVO.setStartDateStream(yesterday.toString(DatePattern.NORM_DATE_PATTERN));
                reportParamsVO.setEndDateStream(today);
                DateTime dateTime = DateUtil.offsetDay(yesterday, -1);
                reportParamsVO.setStartDateOffLine(reportParamsVO.getStartDate());
                reportParamsVO.setEndDateOffLine(dateTime.toString(DatePattern.NORM_DATE_PATTERN));
                reportParamsVO.setDateType("3");
            }

        }else if(todayDateBoolean){//只包含今天
            //如果含有离线那么必定会包含昨天，所以这里不用判断
            reportParamsVO.setStartDateStream(reportParamsVO.getStartDate());
            reportParamsVO.setEndDateStream(reportParamsVO.getEndDate());
            reportParamsVO.setDateType("1");
        }else if(yesterdayDateBoolean){//只包含昨天
            //判断天数是否是一天betweenDay=0
            if(betweenDay.intValue()==0){//实时
                reportParamsVO.setStartDateStream(yesterday.toString(DatePattern.NORM_DATE_PATTERN));
                reportParamsVO.setEndDateStream(yesterday.toString(DatePattern.NORM_DATE_PATTERN));
                reportParamsVO.setDateType("1");
            }else{//实时+离线
                reportParamsVO.setStartDateStream(yesterday.toString(DatePattern.NORM_DATE_PATTERN));
                reportParamsVO.setEndDateStream(yesterday.toString(DatePattern.NORM_DATE_PATTERN));
                DateTime dateTime = DateUtil.offsetDay(yesterday, -1);
                reportParamsVO.setStartDateOffLine(reportParamsVO.getStartDate());
                reportParamsVO.setEndDateOffLine(dateTime.toString(DatePattern.NORM_DATE_PATTERN));
                reportParamsVO.setDateType("3");
            }
        }else{//离线
            reportParamsVO.setStartDateOffLine(reportParamsVO.getStartDate());
            reportParamsVO.setEndDateOffLine(reportParamsVO.getEndDate());
            reportParamsVO.setDateType("2");
        }
        return reportParamsVO;
    }

    public static ReportParamsVO handAppkeys(ReportParamsVO reportParamsVO){
        reportParamsVO.setAppkeys(handParamStr(reportParamsVO.getAppkeys()));
        return reportParamsVO;
    }

    public static String handParamStr(String param){
        if(!StringUtils.isEmpty(param)){
            String[] split = param.split(",");
            final StringBuilder sb = new StringBuilder();
            List<String> paramList = new ArrayList<>();
            for (String cid : split) {
                paramList.add(new StringBuilder("'").append(cid).append("'").toString());
            }
            return StrUtil.join(",",paramList);
        }else{
            return null;
        }
    }

    /**
     * 功能描述:betweenDate在startDate和endDate区间内返回true
     */
    public static Boolean checkBetweenDate(Date startDate,Date endDate,Date betweenDate){
        if(DateUtil.compare(startDate,betweenDate)<=0 && DateUtil.compare(endDate,betweenDate)>=0 ){
            return true;
        }else{
            return false;
        }
    }
}
