package org.jeecg.modules.task.util;

import cn.hutool.core.date.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.task.entity.CsPtTask;

import java.util.Date;

/**
 * 任务工具类
 */
public class TaskWorkUtils {

    /***
     * 创建任务信息
     * @param source 任务来源
     * @param name 任务名称
     * @param rwlx 任务类型
     * @param note  任务描述
     * @param state 任务状态
     * @param params 请求参数
     * @return
     */
    public static CsPtTask getTask(String source, String name,String rwlx, String note, String state, String params){
        CsPtTask task=new CsPtTask();
        //获取当前用户信息
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        task.setId(UUIDGenerator.generate());
        task.setCreateTime(new Date());
        task.setCreateBy(user.getUsername());
        task.setSysOrgCode(user.getOrgCode());
        task.setSource(source);
        task.setName(name);
        task.setTenantId(user.getLoginTenantId());
        task.setCompanyId(user.getCompanyId());
        task.setNote(note);
        task.setType(rwlx);
        task.setState(state);
        task.setParams(params);
        return task;
    }

    /**
     * 根据重试次数计算下次执行时间
     * @param failCount
     * @return
     */
    public static Date getNextExecTime(int failCount) {
        Date nextTime = null;
        switch (failCount) {
            case 1:
                nextTime = DateUtil.offsetMinute(new Date(), 5);
                break;
            case 2:
                nextTime = DateUtil.offsetMinute(new Date(), 10);
                break;
            case 3:
                nextTime = DateUtil.offsetMinute(new Date(), 30);
                break;
            case 4:
                nextTime = DateUtil.offsetDay(new Date(), 1);
                break;
            case 5:
                nextTime = DateUtil.offsetDay(new Date(), 1);
                break;
            default:
                nextTime = DateUtil.offsetDay(new Date(), 1);
                break;
        }
        return nextTime;
    }
}
