package cn.ezhizao.framework.task;

import cn.ezhizao.common.constant.Constants;
import cn.ezhizao.common.utils.StringUtils;
import cn.ezhizao.common.utils.http.HttpUtils;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.http.HttpResponse;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    public void initHoliday() {
        getHoliday();
        System.out.println("获取节假日");
    }

    public void initWorkOrder() {
        /// 判断是否是这个月的倒数第二个工作日
        // 先做是否是这个月最后一天。倒数第二个工作日的等再说
        // 获取当前日期
        Calendar today = Calendar.getInstance();
        // 获取本月最后一天
        Calendar lastDayOfMonth = getLastDayOfMonth(today);
        // 判断今天是否是本月倒数第二天
        if (today.equals(lastDayOfMonth)) {
            System.out.println("今天是这个月倒数第二天");
            System.out.println("复制当月委托到下个月");
            System.out.println("将本月委托设为不可更改");
        } else {
            System.out.println("今天不是这个月倒数第二天");
        }
    }

    Calendar getLastDayOfMonth(Calendar calendar) {
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        Calendar lastDay = Calendar.getInstance();
        lastDay.set(year, month + 1, 1);
        lastDay.add(Calendar.DATE, -1);
        return lastDay;
    }

//    public void getHoliday() {
//        requestHoliday();
//        System.out.println("获取节假日");
//    }

    private void getHoliday() {
        String path = "https://jrxxcxsmkj.market.alicloudapi.com/holiday/list";
//        String  = "";
        String appcode = "e173616254524e6e9b043cee70341acf";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        try {
            String result = HttpUtils.sendGet(path, "year=" + DateTime.now().getField(DateField.YEAR), Constants.UTF8, headers);
            System.out.println(result);;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
