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.domain.DmsRepairInstance;
import com.aucma.dms.service.IDmsPlanRepairService;
import com.aucma.dms.service.IDmsRepairInstanceService;
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.List;




/**
 * 检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单Controller
 *
 * @author Open Platform
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/dms/dmsRepairInstance")
public class DmsRepairInstanceController extends BaseController
{
    @Autowired
    private IDmsRepairInstanceService dmsRepairInstanceService;
    @Autowired
    private IDmsPlanRepairService dmsPlanRepairService;

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

    /**
     * 导出检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单列表
     */
    //@PreAuthorize("@ss.hasPermi('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, "检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单数据");
    }

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

    /**
     * 新增检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单
     */
    //@PreAuthorize("@ss.hasPermi('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));
    }



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

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