package com.hw.dms.controller;

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

import com.hw.common.core.utils.DateUtils;
import com.hw.dms.domain.DmsPlanRepair;
import com.hw.dms.service.IDmsPlanRepairService;
import com.hw.job.api.util.CronUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.DmsRepairInstance;
import com.hw.dms.service.IDmsRepairInstanceService;
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-24
 */
@RestController
@RequestMapping("/dmsRepairInstance")
public class DmsRepairInstanceController extends BaseController
{
    @Autowired
    private IDmsRepairInstanceService dmsRepairInstanceService;
    @Autowired
    private IDmsPlanRepairService dmsPlanRepairService;

    /**
     * 查询检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单列表
     */
    @RequiresPermissions("dms:dmsRepairInstance:list")
    @GetMapping("/list")
    public TableDataInfo list(DmsRepairInstance dmsRepairInstance)
    {
        startPage();
        List<DmsRepairInstance> list = dmsRepairInstanceService.selectDmsRepairInstanceList(dmsRepairInstance);
        return getDataTable(list);
    }

    /**
     * 导出检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单列表
     */
    @RequiresPermissions("dms:dmsRepairInstance:export")
    @Log(title = "检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsRepairInstance dmsRepairInstance)
    {
        List<DmsRepairInstance> list = dmsRepairInstanceService.selectDmsRepairInstanceList(dmsRepairInstance);
        ExcelUtil<DmsRepairInstance> util = new ExcelUtil<DmsRepairInstance>(DmsRepairInstance.class);
        util.exportExcel(response, list, "检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单数据");
    }

    /**
     * 获取检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单详细信息
     */
    @RequiresPermissions("dms:dmsRepairInstance:query")
    @GetMapping(value = "/{repairInstanceId}")
    public AjaxResult getInfo(@PathVariable("repairInstanceId") Long repairInstanceId)
    {
        return success(dmsRepairInstanceService.selectDmsRepairInstanceByRepairInstanceId(repairInstanceId));
    }

    /**
     * 新增检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     */
    @RequiresPermissions("dms:dmsRepairInstance:add")
    @Log(title = "检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsRepairInstance dmsRepairInstance)
    {

        return toAjax(dmsRepairInstanceService.insertDmsRepairInstance(dmsRepairInstance));
    }
    //自动添加
    @PostMapping("/autoAdd")
    public AjaxResult autoAdd(@RequestBody String planRepairId)
    {
        String s = planRepairId.replaceAll("\"", "");

        DmsPlanRepair dmsPlanRepair = dmsPlanRepairService.selectDmsPlanRepairByPlanRepairId(Long.valueOf(s));
//        Date time = CronUtils.getNextExecution()

        //        DmsRepairInstance dmsRepairInstance = dmsRepairInstanceService.selectDmsRepairInstanceByRepairInstanceId(2l);
        dmsPlanRepair.setCreateMethod("2");
        DmsRepairInstance dmsRepairInstance = new DmsRepairInstance();
        dmsRepairInstance.setWfProcessId(101l);
        dmsRepairInstance.setPlanRepairId(dmsPlanRepair.getPlanRepairId());
        dmsRepairInstance.setPlanBeginTime(dmsPlanRepair.getRepairTime());
        dmsRepairInstance.setInstanceType("2");
        dmsRepairInstance.setRepairType(dmsPlanRepair.getRepairType());
        dmsRepairInstance.setRepairGroup(dmsPlanRepair.getRepairGroup());
        dmsRepairInstance.setRepairSupervisor(dmsPlanRepair.getRepairSupervisor());

        return toAjax(dmsRepairInstanceService.insertDmsRepairInstance(dmsRepairInstance));
    }



    /**
     * 修改检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     */
    @RequiresPermissions("dms:dmsRepairInstance:edit")
    @Log(title = "检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsRepairInstance dmsRepairInstance)
    {
        return toAjax(dmsRepairInstanceService.updateDmsRepairInstance(dmsRepairInstance));
    }

    /**
     * 删除检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     */
    @RequiresPermissions("dms:dmsRepairInstance:remove")
    @Log(title = "检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{repairInstanceIds}")
    public AjaxResult remove(@PathVariable Long[] repairInstanceIds)
    {
        return toAjax(dmsRepairInstanceService.deleteDmsRepairInstanceByRepairInstanceIds(repairInstanceIds));
    }
}
