package com.dxhy.core.job.controller;

import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.utils.ValidatorUtils;
import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.service.ScheduleJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 定时任务
 *
 * @author user
 */
@RestController
@RequestMapping("/sys/schedule")
@Slf4j

public class ScheduleJobController{
    @Resource
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务列表
     */
    @RequestMapping("/list")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        PageUtils page = scheduleJobService.queryPage(params);

        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 定时任务信息
     */
    @RequestMapping("/info/{jobId}")
    public ResponseEntity<?> info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.queryById(jobId);

        return ResponseEntity.ok(R.ok().put("schedule", schedule));
    }

    /**
     * 保存定时任务
     */
    @RequestMapping("/save")
    public ResponseEntity<?> save(@RequestBody ScheduleJobEntity scheduleJob) {
        try {
            ValidatorUtils.validateEntity(scheduleJob);

            scheduleJobService.saveJob(scheduleJob);

            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("保存失败"));
        }

    }

    /**
     * 修改定时任务
     */
    @RequestMapping("/update")
    public ResponseEntity<?> update(@RequestBody ScheduleJobEntity scheduleJob) {
        try {
            ValidatorUtils.validateEntity(scheduleJob);
            scheduleJob.setJobStatus("0");
            scheduleJobService.update(scheduleJob);

            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("保存失败"));
        }

    }

    /**
     * 删除定时任务
     */
    @RequestMapping("/delete")
    public ResponseEntity<?> delete(Long jobId) {
        try {
            scheduleJobService.deleteBatch(jobId);
            return ResponseEntity.ok(R.ok("删除成功"));
        } catch (Exception e) {
            return ResponseEntity.ok(R.error("删除失败"));
        }

    }

    /**
     * 立即执行任务
     */
    @RequestMapping("/run")
    public ResponseEntity<?> run(Long jobId) {
        try {
            scheduleJobService.run(jobId);

            return ResponseEntity.ok(R.ok("执行成功"));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error("执行失败"));
        }

    }

    /**
     * 暂停定时任务
     */
    @RequestMapping("/pause")
    public ResponseEntity<?> pause(Long jobId) {
        try {
            scheduleJobService.pause(jobId);

            return ResponseEntity.ok(R.ok("暂停成功"));
        } catch (Exception e) {

            return ResponseEntity.ok(R.error("暂停失败"));
        }

    }

    /**
     * 恢复定时任务
     */
    @RequestMapping("/resume")
    public ResponseEntity<?> resume(Long jobId) {
        try {
            scheduleJobService.resume(jobId);

            return ResponseEntity.ok(R.ok("恢复成功"));
        } catch (Exception e) {
            return ResponseEntity.ok(R.error("恢复失败"));
        }
    }

    /**
    * @Description 通过定时任务类名和方法名执行任务。数组形式，顺序执行，每个任务之间间隔15秒
    * @param jobNames  beanName 名   methodName方法名
    * @Return org.springframework.http.ResponseEntity<?>
    * @Author wangruwei
    * @Date 2024/7/19 11:08
    **/
    @RequestMapping("/doScheJobByBeanNameMethods")
    public ResponseEntity<?> doScheJobByBeanNameMethods(@RequestBody List<Map<String, String>> jobNames) {
        //beanName
        //methodName
        scheduleJobService.doScheJobByBeanNameMethods(jobNames);
        return ResponseEntity.ok(R.ok());
    }

}
