package com.cmvr.web.controller.inspection;

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

import com.cmvr.inspection.domain.dto.InspectionTaskInstanceQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.cmvr.common.annotation.Log;
import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.common.enums.BusinessType;
import com.cmvr.inspection.domain.InspectionTaskInstance;
import com.cmvr.inspection.domain.vo.InspectionTaskInstanceVo;
import com.cmvr.inspection.service.IInspectionTaskInstanceService;
import com.cmvr.common.utils.poi.ExcelUtil;
import com.cmvr.common.core.page.TableDataInfo;

/**
 * 巡检任务执行实例Controller
 * 
 * @author cmvr-iot
 * @since 2026-06-01
 */
@RestController
@RequestMapping("/inspection/taskInstance")
@Api(tags = "智能巡检--任务执行实例管理")
public class InspectionTaskInstanceController extends BaseController
{
    @Autowired
    private IInspectionTaskInstanceService inspectionTaskInstanceService;

    /**
     * 查询巡检任务执行实例列表
     */
    @ApiOperation("查询巡检任务执行实例列表")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionTaskInstanceQuery inspectionTaskInstanceQuery)
    {
        startPage();
        List<InspectionTaskInstanceVo> list = inspectionTaskInstanceService.selectInspectionTaskInstanceList(inspectionTaskInstanceQuery);
        return getDataTable(list);
    }

    /**
     * 导出巡检任务执行实例列表
     */
    @ApiOperation("导出巡检任务执行实例列表")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:export')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionTaskInstanceQuery inspectionTaskInstanceQuery)
    {
        List<InspectionTaskInstanceVo> list = inspectionTaskInstanceService.selectInspectionTaskInstanceList(inspectionTaskInstanceQuery);
        ExcelUtil<InspectionTaskInstanceVo> util = new ExcelUtil<>(InspectionTaskInstanceVo.class);
        util.exportExcel(response, list, "巡检任务执行实例数据");
    }

    /**
     * 获取巡检任务执行实例详细信息
     */
    @ApiOperation("获取巡检任务执行实例详细信息")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(inspectionTaskInstanceService.selectInspectionTaskInstanceVoById(id));
    }

    /**
     * 新增巡检任务执行实例
     */
    @ApiOperation("新增巡检任务执行实例")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:add')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionTaskInstance inspectionTaskInstance)
    {
        return toAjax(inspectionTaskInstanceService.insertInspectionTaskInstance(inspectionTaskInstance));
    }

    /**
     * 修改巡检任务执行实例
     */
    @ApiOperation("修改巡检任务执行实例")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:edit')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionTaskInstance inspectionTaskInstance)
    {
        return toAjax(inspectionTaskInstanceService.updateInspectionTaskInstance(inspectionTaskInstance));
    }

    /**
     * 删除巡检任务执行实例
     */
    @ApiOperation("删除巡检任务执行实例")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:remove')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(inspectionTaskInstanceService.deleteInspectionTaskInstanceByIds(ids));
    }

    /**
     * 开始执行任务实例
     */
    @ApiOperation("开始执行任务实例")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:edit')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/start/{id}")
    public AjaxResult start(@PathVariable("id") String id)
    {
        return toAjax(inspectionTaskInstanceService.startInstance(id));
    }

    /**
     * 暂停任务实例
     */
    @ApiOperation("暂停任务实例")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:edit')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/pause/{id}")
    public AjaxResult pause(@PathVariable("id") String id)
    {
        return toAjax(inspectionTaskInstanceService.pauseInstance(id));
    }

    /**
     * 终止任务实例
     */
    @ApiOperation("终止任务实例")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:edit')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/stop/{id}")
    public AjaxResult stop(@PathVariable("id") String id)
    {
        return toAjax(inspectionTaskInstanceService.stopInstance(id));
    }

    /**
     * 恢复任务实例
     */
    @ApiOperation("恢复任务实例")
    @PreAuthorize("@ss.hasPermi('inspection:taskInstance:edit')")
    @Log(title = "巡检任务执行实例", businessType = BusinessType.UPDATE)
    @PutMapping("/resume/{id}")
    public AjaxResult resume(@PathVariable("id") String id)
    {
        return toAjax(inspectionTaskInstanceService.resumeInstance(id));
    }
}
