package jnpf.cultivate.v2.controller.web.timing;

import jnpf.base.ActionResult;
import jnpf.cultivate.utils.CultivateLearnUtils;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.cultivate.v2.service.V2CultivateExamService;
import jnpf.cultivate.v2.service.V2CultivateTaskService;
import jnpf.exam.V2CultivateTimingApi;
import jnpf.util.NoDataSourceBind;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * 培训定时任务
 */

@Slf4j
@RestController
@RequestMapping("/v2/cultivate/timing")
public class V2TimingController implements V2CultivateTimingApi {


    @Autowired
    private V2CultivateTaskService learnTaskListService;

    @Autowired
    private CultivateLearnUtils cultivateLearnUtils;

    @Resource
    private ThreadPoolExecutor cultivateThreadPool;

    @Autowired
    private UserApiV2Util userApiV2Util;


    @Autowired
    private V2CultivateExamService v2CultivateExamService;




    /**
     * 每分钟执行
     *
     * @param tenantId 租户ID
     * @return 响应
     */
    @Override
    @NoDataSourceBind
    @GetMapping("/per-minute")
    public ActionResult<Integer> perMinute(@RequestParam("tenantId") String tenantId) {

        CompletableFuture.runAsync(() -> {
            userApiV2Util.checkOutTenant(tenantId);
            //修改学习任务的状态
            try {
                learnTaskListService.checkAndUpdateTaskStatus(tenantId);
            } catch (Exception e) {
                log.error("任务学习定时任务执行异常: {}", e.getMessage());
            }

            try {
                //1检测和修改用户的过期考试
                v2CultivateExamService.checkAndUpdateExpireUserExamStatus();
                //2检测和修改常规考试过期
                v2CultivateExamService.checkAndUpdateExamStatus();
            } catch (Exception e) {
                log.error("考试定时任务执行异常: {}", e.getMessage());
            }
        }, cultivateThreadPool);

        return ActionResult.success("");
    }

    /**
     * 每个小时执行
     *
     * @param tenantId 租户ID
     * @return 响应
     */
    @Override
    @NoDataSourceBind
    @GetMapping("/per-hour")
    public ActionResult<Integer> perHour(@RequestParam("tenantId") String tenantId) {
        log.error("start perHour..");
        CompletableFuture.runAsync(() -> {
            userApiV2Util.checkOutTenant(tenantId);
            //检查任务开始并发消息提醒
            try {
                cultivateLearnUtils.timingTaskLearningAlert(tenantId);
            } catch (Exception e) {
                log.error("定时任务执行异常: {}", e.getMessage());
            }
        }, cultivateThreadPool);


        return ActionResult.success("");
    }

    /**
     * 每半个小时执行
     *
     * @param tenantId 租户ID
     * @return 响应
     */
    @Override
    @NoDataSourceBind
    @GetMapping("/per-half-hour")
    public ActionResult<Integer> perHalfHour(@RequestParam("tenantId") String tenantId) {
        return ActionResult.success("");
    }
}
