package com.emr.util;

import com.emr.entity.Archive_Master_Vo;
import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 报表的时间计算工具
 */
public class DatelUtils {
    /**
     * 比较两个日期的大小
     * @param time1
     * @param time2
     * @return
     * @throws ParseException
     */
    public static boolean compareDate(String time1,String time2) throws ParseException{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date a=sdf.parse(time1);
        Date b=sdf.parse(time2);
        //a早于b返回true
        if(a.before(b)){
            return  true;
        }else{
            return false;
        }
    }


    /**
     * 日期增加年月日
     * @param dayStr 日期str
     * @return 加上几天后的日期
     */
    public static String addDayFun(String dayStr, String dayType, int i) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dt=null;
            if(StringUtils.isBlank(dayStr)){
                //系统时间
                dt=sdf.parse(sdf.format(new Date()));
            }else{
                dt=sdf.parse(dayStr);
            }
            Calendar rightNow=Calendar.getInstance();
            rightNow.setTime(dt);
            if(dayType=="year") {
                rightNow.add(Calendar.YEAR, i);//-1 日期减1年
            }else if(dayType=="month") {
                rightNow.add(Calendar.MONTH, i);//3 日期加三个月
            }else if(dayType=="day") {
                rightNow.add(Calendar.DAY_OF_YEAR, i);//15 日期加15天
            }
            Date dt1=rightNow.getTime();
            String reStr=sdf.format(dt1);
            //System.out.println(reStr);
            return reStr;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Archive_Master_Vo setMaterVoFun(Archive_Master_Vo archiveMasterVo) throws Exception {
        //判断复制时段类型
        String endDateStr="";
        if(StringUtils.isNotBlank(archiveMasterVo.getLockinfo())){
            //day_7 month_day_15 month_1 month_3
            if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) && StringUtils.isBlank(archiveMasterVo.getEndDate())){
                //复印时间的截至日期，不得超过
                endDateStr=getDateFun(archiveMasterVo.getLockinfo(),archiveMasterVo.getStartDate(),"A",0);
                archiveMasterVo.setEndDate(endDateStr);
            }else if(StringUtils.isNotBlank(archiveMasterVo.getStartDate()) &&StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                endDateStr=getDateFun(archiveMasterVo.getLockinfo(),archiveMasterVo.getStartDate(),"A",0);
                if(!compareDate(archiveMasterVo.getStartDate(),endDateStr)){
                    archiveMasterVo.setEndDate(endDateStr);
                }
            }else if(StringUtils.isBlank(archiveMasterVo.getStartDate()) && StringUtils.isNotBlank(archiveMasterVo.getEndDate())){
                //反向扣除s
                endDateStr=getDateFun(archiveMasterVo.getLockinfo(),archiveMasterVo.getEndDate(),"B",0);
                archiveMasterVo.setStartDate(endDateStr);
            }else if(StringUtils.isBlank(archiveMasterVo.getStartDate()) && StringUtils.isBlank(archiveMasterVo.getEndDate())){
                //系统时间
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String nowDate= String.valueOf(sdf.format(new Date()));
                endDateStr=getDateFun(archiveMasterVo.getLockinfo(),nowDate,"B",0);
                archiveMasterVo.setStartDate(endDateStr);
                archiveMasterVo.setEndDate(nowDate);
            }
        }
        archiveMasterVo.setLockinfo(null);
        return  archiveMasterVo;
    }
    public static String getDateFun(String dayType,String currDate,String dayFlag,int numb){
        String endDateStr="";
        if(dayFlag.equals("A")) { //加
            if (dayType.equals("day_7")) {
                endDateStr = addDayFun(currDate, "day", 7);
            } else if (dayType.equals("month_day_15")) {
                endDateStr = addDayFun(currDate, "day", 15);
            } else if (dayType.equals("month_1")) {
                endDateStr = addDayFun(currDate, "month", 1);
            } else if (dayType.equals("month_3")) {
                endDateStr = addDayFun(currDate, "month", 3);
            } else if (dayType.equals("day")) {
                endDateStr = addDayFun(currDate, "day", numb);
            } else if (dayType.equals("month")) {
                endDateStr = addDayFun(currDate, "month", numb);
            }
        }else if(dayFlag.equals("B")) { //减
            if (dayType.equals("day_7")) {
                endDateStr = addDayFun(currDate, "day", -7);
            } else if (dayType.equals("month_day_15")) {
                endDateStr = addDayFun(currDate, "day", -15);
            } else if (dayType.equals("month_1")) {
                endDateStr = addDayFun(currDate, "month", -1);
            } else if (dayType.equals("month_3")) {
                endDateStr = addDayFun(currDate, "month", -3);
            }else if (dayType.equals("day")) {
                endDateStr = addDayFun(currDate, "day", -1*numb);
            } else if (dayType.equals("month")) {
                endDateStr = addDayFun(currDate, "month", -1*numb);
            }
        }
        return endDateStr;
    }


    public static void main(String[] args) throws Exception {

        System.out.println(addDayFun( "2021-12-21",  "day", -15));
    }
}