package com.aucma.dms.controller;


import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.core.page.TableDataInfo;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.dms.domain.DmsPlanRepair;
import com.aucma.dms.service.IDmsPlanRepairService;
import com.aucma.quartz.util.CronUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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




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

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

    /**
     * 导出检修计划信息列表
     */
    //@PreAuthorize("@ss.hasPermi('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, "检修计划信息数据");
    }

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

    /**
     * 新增检修计划信息
     */
    //@PreAuthorize("@ss.hasPermi('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;
    }

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

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