package cn.iocoder.yudao.module.system.controller.admin.robot;

import cn.iocoder.yudao.module.system.controller.admin.robot.mapstop.RobotMapStopPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.mapstop.RobotMapStopRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.mapstop.RobotMapStopSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotMapStopDO;
import cn.iocoder.yudao.module.system.service.robot.mapstop.RobotMapStopService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 车辆(地图)急停记录")
@RestController
@RequestMapping("/system/robot/map-stop")
@Validated
public class RobotMapStopController {

    @Resource
    private RobotMapStopService mapStopService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆(地图)急停记录")
    @PreAuthorize("@ss.hasPermission('robot:map-stop:create')")
    public CommonResult<Long> createMapStop(@Valid @RequestBody RobotMapStopSaveReqVO createReqVO) {
        return success(mapStopService.createMapStop(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆(地图)急停记录")
    @PreAuthorize("@ss.hasPermission('robot:map-stop:update')")
    public CommonResult<Boolean> updateMapStop(@Valid @RequestBody RobotMapStopSaveReqVO updateReqVO) {
        mapStopService.updateMapStop(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆(地图)急停记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:map-stop:delete')")
    public CommonResult<Boolean> deleteMapStop(@RequestParam("id") Long id) {
        mapStopService.deleteMapStop(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆(地图)急停记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:map-stop:query')")
    public CommonResult<RobotMapStopRespVO> getMapStop(@RequestParam("id") Long id) {
        RobotMapStopDO mapStop = mapStopService.getMapStop(id);
        return success(BeanUtils.toBean(mapStop, RobotMapStopRespVO.class));
    }

    @GetMapping("/getByMapId")
    @Operation(summary = "根据地图id获取暂停的地图")
    @Parameter(name = "id", description = "地图id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:map-stop:getByMapId')")
    public CommonResult<List<RobotMapStopDO>> getByMapId(@RequestParam("id") Long id) {
        List<RobotMapStopDO> mapStop = mapStopService.getByMapId(id);
        return success(mapStop);
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆(地图)急停记录分页")
    @PreAuthorize("@ss.hasPermission('robot:map-stop:query')")
    public CommonResult<PageResult<RobotMapStopRespVO>> getMapStopPage(@Valid RobotMapStopPageReqVO pageReqVO) {
        PageResult<RobotMapStopDO> pageResult = mapStopService.getMapStopPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotMapStopRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆(地图)急停记录 Excel")
    @PreAuthorize("@ss.hasPermission('robot:map-stop:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMapStopExcel(@Valid RobotMapStopPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotMapStopDO> list = mapStopService.getMapStopPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆(地图)急停记录.xls", "数据", RobotMapStopRespVO.class,
                        BeanUtils.toBean(list, RobotMapStopRespVO.class));
    }

}