package com.tax.file.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.tax.file.constants.FileConstants;
import com.tax.file.service.async.apply.ApplyContext;
import com.tax.file.service.async.apply.util.ApplyUtil;
import com.tax.file.service.async.strategy.LastMonthFixStrategy;
import com.tax.file.utils.TokenGetUtil;
import com.yxcheng.token.utils.DateUtils;

public class Test {
	
	
	public static void testFixLackApplyService(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str = "2020-11-05";
		Date lastInvDate = null;
		try {
			lastInvDate = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if(ApplyUtil.isInCurrentCycleDate(lastInvDate)){
			System.out.println("当前周期");
		}
		
        Date[] nationDates = ApplyUtil.getCycleDates(FileConstants.applyCycle);
        Date cycleInvDateStart = nationDates[0];

        int monthDiff = DateUtils.monthCompare(lastInvDate,cycleInvDateStart);

        Date invDateStart = DateUtils.addDate(lastInvDate, 1);

        int needFixMonthDiff = DateUtils.monthCompare(invDateStart,cycleInvDateStart);
        
        if(monthDiff ==0 ){

        }else if(needFixMonthDiff ==0){
            if(ApplyUtil.isLastMonthFix()){
                Date firstInvDateEnd = DateUtils.getPreLastDay(invDateStart, 0);
                Date firstInvDateStart = ApplyUtil.getPreApplyDate(invDateStart);
                System.out.println("进行发票下载申请："+firstInvDateStart+","+firstInvDateEnd);
            }
        }else{
            //first 当前时间到月末
            Date firstInvDateEnd = DateUtils.getPreLastDay(invDateStart, 0);
            Date firstInvDateStart = ApplyUtil.getPreApplyDate(invDateStart);
            System.out.println("进行发票下载申请，如果申请失败则return："+firstInvDateStart+","+firstInvDateEnd);

            //second 中间月份
            Date secondInvDateStart = DateUtils.addDate(firstInvDateEnd,1);

            monthDiff = DateUtils.monthCompare(secondInvDateStart,cycleInvDateStart);

            for(int i=0;i<monthDiff;i++){
                Date preDateEnd = DateUtils.getPreLastDay(secondInvDateStart,-i);
                Date preDateStart = DateUtils.getPreFirstDay(secondInvDateStart,-i);
                System.out.println("进行发票下载申请："+preDateStart+","+preDateEnd);
            }
        }
	}
	
    public static Date getContinueDate() {
        Date now = new Date();
        int applyFixDays = DateUtils.getApplyFixDays(now,FileConstants.applyCycle);
        if(applyFixDays<=0){
            return DateUtils.getPreLastDay(now, 1);
        }
        return now;
    }
	
	public static void testContinueBreakPointService(){
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str = "2021-03-05";
		
        Date applyBreakPoint = null;
		try {
			applyBreakPoint = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        Integer preMonth = 60;

        //防止跨月出现
        Date now = getContinueDate();
        if(DateUtils.addMonth(applyBreakPoint,preMonth).before(now)){
        	return;
        };

        boolean flag = true;
        int start = DateUtils.monthCompare(applyBreakPoint,now);
        if(start == -1){
        	return;
        };

        for(int i=start; i<=preMonth;i++){
            final Date preDateEnd = DateUtils.getPreLastDay(now,i);
            final Date preDateStart = DateUtils.getPreFirstDay(now,i);
            
            System.out.println("进行发票下载申请："+sdf.format(preDateStart)+","+sdf.format(preDateEnd));
        }
	}
	
	public static void testCurrentCycleApplyService(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
        Date[] nationDates = DateUtils.getNationDates(FileConstants.applyCycle);
        
        Date dateStart = null;
        Map context = new HashMap();
        
        //context.put(ApplyContext.fixLackApply,"1");

        dateStart = ApplyContext.getFixLackApplyStartDate(context,nationDates[0]);
        
        Date dateEnd = nationDates[1];
        System.out.println("进行发票下载申请："+sdf.format(dateStart)+","+sdf.format(dateEnd));

	}
	
	public static void testFixLastMonthRemainService(){
		
		 Map context = new HashMap();
		 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
        Date now = new Date();
        Date preDateEnd = DateUtils.getPreLastDay(now, 1);
        Date preDateStart = DateUtils.getApplyStartDate(preDateEnd, FileConstants.applyCycle);
        
        context.put(ApplyContext.fixLackApply,"1");
        
        preDateStart = ApplyContext.getFixLackApplyStartDate(context,preDateStart);
        
        preDateStart = DateUtils.getPreFirstDay(now,1);//获取上月第1天
        
        System.out.println("进行发票下载申请："+sdf.format(preDateStart)+","+sdf.format(preDateEnd));
	}

	public static void main(String[] args) {
		//testContinueBreakPointService();
		//testCurrentCycleApplyService();
		//testFixLastMonthRemainService();
		/*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		Date preDateStart = DateUtils.getPreFirstDay(now,1);
		System.out.println("进行发票下载申请："+sdf.format(preDateStart)+","+sdf.format(preDateStart));*/
		
	/*	try {
			String token = TokenGetUtil.getBWToken("91130100695852364Y", "1300");
			System.out.println(token);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date applyDateStart = DateUtils.getNationDateNoMonthLimit("30", new Date());
		System.out.println("进行发票下载申请："+sdf.format(applyDateStart));
    }
		

}
