package com.cmvr.web.controller.inspection;

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

import cmvr.api.AgvCommand;
import com.cmvr.inspection.domain.vo.InspectionRobotVo;
import io.swagger.annotations.*;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.InspectionRobot;
import com.cmvr.inspection.service.IInspectionRobotService;
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/robot")
@Api(tags = "智能巡检--机器人管理")
public class InspectionRobotController extends BaseController
{
    @Autowired
    private IInspectionRobotService inspectionRobotService;

    /**
     * 查询巡检机器人列表
     */
    @ApiOperation("查询巡检机器人列表")
    @PreAuthorize("@ss.hasPermi('inspection:robot:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionRobot inspectionRobot)
    {
        startPage();
        List<InspectionRobotVo> list = inspectionRobotService.selectInspectionRobotList(inspectionRobot);
        return getDataTable(list);
    }

    /**
     * 导出巡检机器人列表
     */
    @ApiOperation("导出巡检机器人列表")
    @PreAuthorize("@ss.hasPermi('inspection:robot:export')")
    @Log(title = "巡检机器人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionRobot inspectionRobot)
    {
        List<InspectionRobotVo> list = inspectionRobotService.selectInspectionRobotList(inspectionRobot);
        ExcelUtil<InspectionRobotVo> util = new ExcelUtil<>(InspectionRobotVo.class);
        util.exportExcel(response, list, "巡检机器人数据");
    }

    /**
     * 获取巡检机器人详细信息
     */
    @ApiOperation("获取巡检机器人详细信息")
    @PreAuthorize("@ss.hasPermi('inspection:robot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(inspectionRobotService.selectInspectionRobotById(id));
    }

    /**
     * 新增巡检机器人
     */
    @ApiOperation("新增巡检机器人")
    @PreAuthorize("@ss.hasPermi('inspection:robot:add')")
    @Log(title = "巡检机器人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionRobot inspectionRobot)
    {
        return toAjax(inspectionRobotService.insertInspectionRobot(inspectionRobot));
    }

    /**
     * 修改巡检机器人
     */
    @ApiOperation("修改巡检机器人")
    @PreAuthorize("@ss.hasPermi('inspection:robot:edit')")
    @Log(title = "巡检机器人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionRobot inspectionRobot)
    {
        return toAjax(inspectionRobotService.updateInspectionRobot(inspectionRobot));
    }

    /**
     * 删除巡检机器人
     */
    @ApiOperation("删除巡检机器人")
    @PreAuthorize("@ss.hasPermi('inspection:robot:remove')")
    @Log(title = "巡检机器人", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(inspectionRobotService.deleteInspectionRobotByIds(ids));
    }

    /**
     * 获取机器人地图列表（从AGV获取）
     */
    @ApiOperation("获取机器人地图列表")
    @PreAuthorize("@ss.hasPermi('inspection:robot:query')")
    @GetMapping("/{robotId}/maps")
    public AjaxResult getRobotMapList(@PathVariable("robotId") String robotId)
    {
        AgvCommand.AgvMapStatus robotMapList = inspectionRobotService.getRobotMapList(robotId);
        return success(robotMapList.getMapsList());
    }

    /**
     * 绑定机器人地图
     */
    @ApiOperation("绑定机器人地图")
    @PreAuthorize("@ss.hasPermi('inspection:robot:edit')")
    @Log(title = "绑定机器人地图", businessType = BusinessType.UPDATE)
    @PostMapping("/{robotId}/bind-map/{mapId}")
    public AjaxResult bindRobotMap(@PathVariable("robotId") String robotId,
                                   @PathVariable("mapId") String mapId)
    {
        return toAjax(inspectionRobotService.bindRobotMap(robotId, mapId));
    }

    /**
     * 上传地图到机器人
     */
    @ApiOperation("上传地图到机器人")
    @PreAuthorize("@ss.hasPermi('inspection:robot:edit')")
    @Log(title = "上传地图到机器人", businessType = BusinessType.OTHER)
    @PostMapping("/{robotId}/upload-map/{mapId}")
    public AjaxResult uploadMapToRobot(@PathVariable("robotId") String robotId,
                                       @PathVariable("mapId") String mapId)
    {
        String result = inspectionRobotService.uploadMapToRobot(robotId, mapId);
        return success(result);
    }

    /**
     * 从机器人下载地图
     */
    @ApiOperation("从机器人下载地图")
    @PreAuthorize("@ss.hasPermi('inspection:robot:add')")
    @Log(title = "从机器人下载地图", businessType = BusinessType.INSERT)
    @GetMapping("/{robotId}/download-map")
    public AjaxResult downloadMapFromRobot(
            @PathVariable(value = "robotId", required = true) @ApiParam("机器人ID") String robotId,
            @RequestParam(value = "mapName", required = true) @ApiParam("地图名称") String mapName
    )
    {
        Object mapId = inspectionRobotService.downloadMapFromRobot(robotId, mapName);
        return success(mapId);
    }

    /**
     * 同步机器人状态
     */
    @ApiOperation("同步机器人状态")
    @PreAuthorize("@ss.hasPermi('inspection:robot:edit')")
    @Log(title = "同步机器人状态", businessType = BusinessType.UPDATE)
    @PostMapping("/{robotId}/sync-status")
    public AjaxResult syncRobotStatus(@PathVariable("robotId") String robotId)
    {
        return success(inspectionRobotService.syncRobotStatus(robotId));
    }

}

