package com.hw.dms.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.hw.common.core.constant.SecurityConstants;
import com.hw.common.core.domain.R;
import com.hw.job.api.RemoteJobService;
import com.hw.job.api.domain.SysJob;
import com.hw.job.api.util.CronUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.CronExpression;
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.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.dms.domain.DmsPlanRepair;
import com.hw.dms.service.IDmsPlanRepairService;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.page.TableDataInfo;

/**
 * 检修计划信息Controller
 *
 * @author Open Platform
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/dmsRepair")
public class DmsPlanRepairController extends BaseController
{
    @Autowired
    private IDmsPlanRepairService dmsPlanRepairService;
    @Autowired
    private RemoteJobService remoteJobService;

    /**
     * 查询检修计划信息列表
     */
    @RequiresPermissions("dms:dmsRepair:list")
    @GetMapping("/list")
    public TableDataInfo list(DmsPlanRepair dmsPlanRepair)
    {
        startPage();
        List<DmsPlanRepair> list = dmsPlanRepairService.selectDmsPlanRepairList(dmsPlanRepair);
        return getDataTable(list);
    }

    /**
     * 导出检修计划信息列表
     */
    @RequiresPermissions("dms:dmsRepair:export")
    @Log(title = "检修计划信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsPlanRepair dmsPlanRepair)
    {
        List<DmsPlanRepair> list = dmsPlanRepairService.selectDmsPlanRepairList(dmsPlanRepair);
        ExcelUtil<DmsPlanRepair> util = new ExcelUtil<DmsPlanRepair>(DmsPlanRepair.class);
        util.exportExcel(response, list, "检修计划信息数据");
    }

    /**
     * 获取检修计划信息详细信息
     */
    @RequiresPermissions("dms:dmsRepair:query")
    @GetMapping(value = "/{planRepairId}")
    public AjaxResult getInfo(@PathVariable("planRepairId") Long planRepairId)
    {
        return success(dmsPlanRepairService.selectDmsPlanRepairByPlanRepairId(planRepairId));
    }

    /**
     * 新增检修计划信息
     */
    @RequiresPermissions("dms:dmsRepair:add")
    @Log(title = "检修计划信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsPlanRepair dmsPlanRepair)
    {
        String time = dmsPlanRepair.getCronExpression();
        //通过cron表达式获取下一次执行时间
        Date nextExecution = CronUtils.getNextExecution(time);
        dmsPlanRepair.setRepairTime(nextExecution);
        AjaxResult ajaxResult = toAjax(dmsPlanRepairService.insertDmsPlanRepair(dmsPlanRepair));


        SysJob job = new SysJob();
        job.setJobName("创建计划编号为"+dmsPlanRepair.getPlanRepairCode()+"工单");
        job.setJobGroup("DEFAULT");
        job.setInvokeTarget("ryTask.getDmsRepairInstance(\""+dmsPlanRepair.getPlanRepairId()+"\")");
        job.setCronExpression(dmsPlanRepair.getCronExpression());
        job.setMisfirePolicy("1");
        job.setConcurrent("1");
        job.setStatus("1");
        remoteJobService.add(SecurityConstants.INNER,job);
        SysJob job1 = remoteJobService.selectJobList(dmsPlanRepair.getPlanRepairId());
        dmsPlanRepair.setJobId(job1.getJobId());
        dmsPlanRepairService.updateDmsPlanRepair(dmsPlanRepair);
        return ajaxResult;
    }

    /**
     * 修改检修计划信息
     */
    @RequiresPermissions("dms:dmsRepair:edit")
    @Log(title = "检修计划信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsPlanRepair dmsPlanRepair)
    {
        return toAjax(dmsPlanRepairService.updateDmsPlanRepair(dmsPlanRepair));
    }

    /**
     * 删除检修计划信息
     */
    @RequiresPermissions("dms:dmsRepair:remove")
    @Log(title = "检修计划信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planRepairIds}")
    public AjaxResult remove(@PathVariable Long[] planRepairIds)
    {
        return toAjax(dmsPlanRepairService.deleteDmsPlanRepairByPlanRepairIds(planRepairIds));
    }
}
