package com.cmvr.web.controller.inspection;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.InspectionTask;
import com.cmvr.inspection.domain.InspectionWaypoint;
import com.cmvr.inspection.domain.vo.InspectionTaskVo;
import com.cmvr.inspection.service.IInspectionTaskService;
import com.cmvr.common.utils.poi.ExcelUtil;
import com.cmvr.common.core.page.TableDataInfo;

/**
 * 巡检任务Controller
 * 
 * @author cmvr-iot
 * @since 2026-05-29
 */
@RestController
@RequestMapping("/inspection/task")
@Api(tags = "智能巡检--任务管理")
public class InspectionTaskController extends BaseController
{
    @Autowired
    private IInspectionTaskService inspectionTaskService;

    /**
     * 查询巡检任务列表
     */
    @ApiOperation("查询巡检任务列表")
    @PreAuthorize("@ss.hasPermi('inspection:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionTask inspectionTask)
    {
        startPage();
        List<InspectionTaskVo> list = inspectionTaskService.selectInspectionTaskList(inspectionTask);
        return getDataTable(list);
    }

    /**
     * 导出巡检任务列表
     */
    @ApiOperation("导出巡检任务列表")
    @PreAuthorize("@ss.hasPermi('inspection:task:export')")
    @Log(title = "巡检任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionTask inspectionTask)
    {
        List<InspectionTaskVo> list = inspectionTaskService.selectInspectionTaskList(inspectionTask);
        ExcelUtil<InspectionTaskVo> util = new ExcelUtil<>(InspectionTaskVo.class);
        util.exportExcel(response, list, "巡检任务数据");
    }

    /**
     * 获取巡检任务详细信息
     */
    @ApiOperation("获取巡检任务详细信息")
    @PreAuthorize("@ss.hasPermi('inspection:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(inspectionTaskService.selectInspectionTaskById(id));
    }

    /**
     * 新增巡检任务
     */
    @ApiOperation("新增巡检任务")
    @PreAuthorize("@ss.hasPermi('inspection:task:add')")
    @Log(title = "巡检任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionTask inspectionTask)
    {
        return toAjax(inspectionTaskService.insertInspectionTask(inspectionTask));
    }

    /**
     * 修改巡检任务
     */
    @ApiOperation("修改巡检任务")
    @PreAuthorize("@ss.hasPermi('inspection:task:edit')")
    @Log(title = "巡检任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionTask inspectionTask)
    {
        return toAjax(inspectionTaskService.updateInspectionTask(inspectionTask));
    }

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

    /**
     * 查询任务关联的点位列表
     */
    @ApiOperation("查询任务关联的点位列表")
    @PreAuthorize("@ss.hasPermi('inspection:task:query')")
    @GetMapping("/{taskId}/waypoints")
    public AjaxResult getWaypoints(@PathVariable("taskId") String taskId)
    {
        List<InspectionWaypoint> list = inspectionTaskService.selectWaypointsByTaskId(taskId);
        return success(list);
    }

    /**
     * 绑定点位到任务
     */
    @ApiOperation("绑定点位到任务")
    @PreAuthorize("@ss.hasPermi('inspection:task:edit')")
    @Log(title = "巡检任务", businessType = BusinessType.UPDATE)
    @PostMapping("/{taskId}/waypoints")
    public AjaxResult bindWaypoints(@PathVariable("taskId") String taskId, @RequestBody String[] waypointIds)
    {
        return toAjax(inspectionTaskService.bindWaypoints(taskId, waypointIds));
    }

    /**
     * 解绑任务的点位
     */
    @ApiOperation("解绑任务的点位")
    @PreAuthorize("@ss.hasPermi('inspection:task:edit')")
    @Log(title = "巡检任务", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{taskId}/waypoints")
    public AjaxResult unbindWaypoints(@PathVariable("taskId") String taskId)
    {
        return toAjax(inspectionTaskService.unbindWaypoints(taskId));
    }
}
