package com.cpxt.web.controller.biz;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cpxt.biz.domain.BizOrderTask;
import com.cpxt.biz.service.IBizOrderTaskService;
import com.cpxt.quartz.domain.SysJob;
import com.cpxt.quartz.service.ISysJobService;
import org.quartz.SchedulerException;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
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 com.cpxt.common.annotation.Log;
import com.cpxt.common.core.controller.BaseController;
import com.cpxt.common.core.domain.AjaxResult;
import com.cpxt.common.enums.BusinessType;
import com.cpxt.common.utils.poi.ExcelUtil;
import com.cpxt.common.core.page.TableDataInfo;

/**
 * 订单任务Controller
 * 
 * @author ruoyi
 * @date 2024-12-20
 */
@RestController
@RequestMapping("/system/task")
public class BizOrderTaskController extends BaseController
{
    @Autowired
    private IBizOrderTaskService bizOrderTaskService;

    @Autowired
    private ISysJobService jobService;

    /**
     * 查询订单任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizOrderTask bizOrderTask)
    {
        startPage();
        List<BizOrderTask> list = bizOrderTaskService.selectBizOrderTaskList(bizOrderTask);
        return getDataTable(list);
    }

    /**
     * 导出订单任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "订单任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizOrderTask bizOrderTask)
    {
        List<BizOrderTask> list = bizOrderTaskService.selectBizOrderTaskList(bizOrderTask);
        ExcelUtil<BizOrderTask> util = new ExcelUtil<BizOrderTask>(BizOrderTask.class);
        util.exportExcel(response, list, "订单任务数据");
    }

    /**
     * 获取订单任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(bizOrderTaskService.selectBizOrderTaskById(id));
    }

    /**
     * 新增订单任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "订单任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizOrderTask bizOrderTask)
    {
        return toAjax(bizOrderTaskService.insertBizOrderTask(bizOrderTask));
    }

    /**
     * 修改订单任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "订单任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizOrderTask bizOrderTask)
    {
        return toAjax(bizOrderTaskService.updateBizOrderTask(bizOrderTask));
    }

    /**
     * 删除订单任务
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "订单任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(bizOrderTaskService.deleteBizOrderTaskByIds(ids));
    }

    /**
     * 定时任务状态修改
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody BizOrderTask orderTask) throws SchedulerException
    {
        // 同步修改task表中的状态
        bizOrderTaskService.updateBizOrderTaskStatus(orderTask.getJobId(),orderTask.getStatus());
        SysJob newJob = jobService.selectJobById(orderTask.getJobId());
        newJob.setStatus(orderTask.getStatus());
        return toAjax(jobService.changeStatus(newJob));
    }

    /**
     * 定时任务立即执行一次
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PutMapping("/run")
    public AjaxResult run(@RequestBody BizOrderTask orderTask) throws SchedulerException
    {
        SysJob newJob = jobService.selectJobById(orderTask.getJobId());
        boolean result = jobService.run(newJob);
        return result ? success() : error("任务不存在或已过期！");
    }
}
