package com.jiayue.insu.incloud.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiayue.insu.common.core.util.R;
import com.jiayue.insu.incloud.entity.IntegrationCompany;
import com.jiayue.insu.incloud.entity.Quartz;
import com.jiayue.insu.incloud.quartz.ScheduledTask;
import com.jiayue.insu.incloud.service.IntegrationCompanyService;
import com.jiayue.insu.incloud.service.QuartzService;
import lombok.RequiredArgsConstructor;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;


/**
 * QuartzController
 *
 * @author yh
 * @date 2022-03-18 15:48:48
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/quartz")
public class QuartzController {

    private final QuartzService quartzService;
    private final ScheduledTask scheduledTask;

    /**
     * 分页查询
     *
     * @param page   分页对象
     * @param quartz quartz
     * @return
     */

    @GetMapping("/page")
    public R getPage(Page page, Quartz quartz) {
        Page data = quartzService.page(page, Wrappers.query(quartz));
        List<Quartz> records = data.getRecords();
        try {
            for (Quartz q:records){
                q.setJobState(scheduledTask.getJobStatus(q.getJobName()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return R.ok(data);
    }

    @GetMapping("/all")
    public R getAll() {
        return R.ok(quartzService.list());
    }

    /**
     * 通过code查询定时任务
     *
     * @param jobName jobName
     * @return R
     */
    @GetMapping("/jobName/{jobName}")
    public R getByCode(@PathVariable("jobName") String  jobName) {
        return R.ok(quartzService.findByJobName(jobName));
    }


    /**
     * 新增quartz
     *
     * @param quartz quartz
     * @return R
     */

    @PostMapping
    public R save(@RequestBody  Quartz quartz) throws SchedulerException, ParseException, ClassNotFoundException {

        return R.ok(quartzService.saveQuartz(quartz));
    }

    /**
     * 修改quartz
     *
     * @param quartz quartz
     * @return R
     */

    @PutMapping
    public R updateById(@RequestBody Quartz quartz) throws SchedulerException, ParseException, ClassNotFoundException {
        return R.ok(quartzService.updateQuartz(quartz));
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable Integer id) throws SchedulerException {
        return R.ok(quartzService.deleteQuartz(id));
    }


    /**
     * 通过code查询定时任务
     *
     * @param jobName jobName
     * @return R
     */
    @GetMapping("/jobTime/{jobName}")
    public R getJobTime(@PathVariable("jobName") String  jobName) throws SchedulerException {
        Map<String,String> map = new HashMap<>();
        try {
            Date jobNextFireTime = scheduledTask.getJobNextFireTime(jobName);

            if(jobNextFireTime !=null){
                map.put("nextFireTime", DateUtil.format(jobNextFireTime, "yyyy/MM/dd HH:mm:ss"));
            }

        }catch (Exception e){
            e.printStackTrace();
            map.put("nextFireTime", "不存在");
        }

        try {
            Date previousFireTime = scheduledTask.getJobPreviousFireTime(jobName);
            if(previousFireTime !=null){
                map.put("previousFireTime", DateUtil.format(previousFireTime, "yyyy/MM/dd HH:mm:ss"));
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("previousFireTime", "不存在");
        }


        return R.ok(map);

    }


}
