package com.chushang.inspection.utils;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.extra.spring.SpringUtil;
import com.chushang.inspection.project.po.PollingTask;
import com.chushang.inspection.project.service.PollingTaskService;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TaskConfigUtils {

    private static final PollingTaskService TASK_SERVICE = SpringUtil.getBean(PollingTaskService.class);


    /**
     * 获取巡检任务
     *
     * @param taskId 任务id
     * @return 任务信息
     */
    public static PollingTask getConfig(Long taskId) {
        PollingTask config = TASK_SERVICE.getTopTask(taskId);
        Assert.isTrue(config.getEnabled(), "此任务未开通");
        if (config.getEndTime() != null && config.getStartTime() != null) {
            Assert.isTrue(LocalDateTimeUtil.isIn(LocalDateTime.now(), config.getStartTime(), config.getEndTime()),
                    "此任务不在执行日期内");
        }
        return config;
    }

    /**
     * 是否复审
     *
     * @param taskId 任务id
     */
    public static Integer reviewMethod(Long taskId) {
        Integer isRecheck = getConfig(taskId).getReviewMethod();
        Assert.notNull(isRecheck, "未指定审核方式");
        return isRecheck;
    }

    /**
     * 是否重复派单
     *
     * @param taskId 任务id
     */
    public static boolean isRepeat(Long taskId) {
        Integer isRepeat = getConfig(taskId).getIsRepeat();
        return isRepeat != null && isRepeat == 1;
    }

    /**
     * 是否有巡检单
     *
     * @param taskId 任务id
     */
    public static boolean isisInspection(Long taskId) {
        Integer isRepeat = getConfig(taskId).getIsInspection();
        return isRepeat != null && isRepeat == 1;
    }


    /**
     * 巡检方式
     *
     * @param taskId 任务id
     */
    public static Integer dispatchMethod(Long taskId) {
        Integer method = getConfig(taskId).getDispatchMethod();
        return method == null ? 0 : method;
    }
}
