package com.njcn.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.system.pojo.param.TimersParam;
import com.njcn.system.pojo.vo.TimersVO;
import com.njcn.system.service.ITimersService;
import com.njcn.web.pojo.annotation.DateTimeStrValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 定时任务 前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2023-12-05
 */
@Slf4j
@RestController
@RequestMapping("/timer")
@Api(tags = "定时任务管理中心")
@AllArgsConstructor
public class TimersController extends BaseController {


    private final ITimersService timersService;

    /**
     * 分页查询定时任务
     *
     * @author hongawen
     */
    @PostMapping("/list")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("查询定时任务")
    @ApiImplicitParam(name = "timersParam", value = "查询参数", required = true)
    public HttpResult<Page<TimersVO>> list(@RequestBody TimersParam timersParam) {
        String methodDescribe = getMethodDescribe("list");
        Page<TimersVO> result = timersService.listTimer(timersParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 添加定时任务
     *
     * @author hongawen
     */
    @PostMapping("/add")
    @ApiOperation("新增定时任务")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @ApiImplicitParam(name = "timersParam", value = "定时任务信息", required = true)
    public HttpResult<Object> add(@RequestBody TimersParam timersParam) {
        String methodDescribe = getMethodDescribe("add");
        boolean flag = timersService.addTimer(timersParam);
        if (flag) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 编辑定时任务
     *
     * @author hongawen
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改定时任务")
    @ApiImplicitParam(name = "timersParam", value = "定时任务信息", required = true)
    public HttpResult<Object> update(@RequestBody @Validated TimersParam timersParam) {
        String methodDescribe = getMethodDescribe("update");
        boolean flag = timersService.editTimer(timersParam);
        if (flag) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 删除定时任务
     *
     * @author hongawen
     */
    @PostMapping("/delete")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("删除定时任务")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, dataTypeClass = List.class)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，任务ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = timersService.deleteTimers(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 获取系统的所有任务列表
     *
     * @author hongawen
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getActionClasses")
    @ApiOperation("获取所有任务列表")
    public HttpResult<List<String>> getActionClasses() {
        String methodDescribe = getMethodDescribe("getActionClasses");
        List<String> actionClasses = timersService.getActionClasses();
        if (CollectionUtil.isNotEmpty(actionClasses)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, actionClasses, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 启动定时任务
     *
     * @author hongawen
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @GetMapping("/start")
    @ApiOperation("启动定时任务")
    public HttpResult<Object> start(String id) {
        String methodDescribe = getMethodDescribe("start");
        timersService.start(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 停止定时任务
     *
     * @author hongawen
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @GetMapping("/stop")
    @ApiOperation("关闭定时任务")
    public HttpResult<Object> stop(String id) {
        String methodDescribe = getMethodDescribe("stop");
        timersService.stop(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 执行一次定时任务
     *
     * @author hongawen
     * @param date 需要执行的日期
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/run")
    @ApiOperation("执行一次定时任务")
    public HttpResult<Object> run(@RequestParam("id") String id,@RequestParam(required = false) @DateTimeStrValid String date) throws InterruptedException {
        String methodDescribe = getMethodDescribe("run");
        timersService.run(id,date);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

}

