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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.robot.camera.RobotCameraPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.camera.RobotCameraRespVO;
import cn.iocoder.yudao.module.system.controller.admin.robot.camera.RobotCameraSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.robot.RobotCameraDO;
import cn.iocoder.yudao.module.system.service.robot.camera.RobotCameraService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 车辆摄像头信息")
@RestController
@RequestMapping("/system/robot/camera")
@Validated
public class RobotCameraController {

    @Resource
    private RobotCameraService cameraService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆摄像头信息")
    @PreAuthorize("@ss.hasPermission('robot:camera:create')")
    public CommonResult<Long> createCamera(@Valid @RequestBody RobotCameraSaveReqVO createReqVO) {
        return success(cameraService.createCamera(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆摄像头信息")
    @PreAuthorize("@ss.hasPermission('robot:camera:update')")
    public CommonResult<Boolean> updateCamera(@Valid @RequestBody RobotCameraSaveReqVO updateReqVO) {
        cameraService.updateCamera(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆摄像头信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('robot:camera:delete')")
    public CommonResult<Boolean> deleteCamera(@RequestParam("id") Long id) {
        cameraService.deleteCamera(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆摄像头信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('robot:camera:query')")
    public CommonResult<RobotCameraRespVO> getCamera(@RequestParam("id") Long id) {
        RobotCameraDO camera = cameraService.getCamera(id);
        return success(BeanUtils.toBean(camera, RobotCameraRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆摄像头信息分页")
    @PreAuthorize("@ss.hasPermission('robot:camera:query')")
    public CommonResult<PageResult<RobotCameraRespVO>> getCameraPage(@Valid RobotCameraPageReqVO pageReqVO) {
        PageResult<RobotCameraDO> pageResult = cameraService.getCameraPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotCameraRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆摄像头信息 Excel")
    @PreAuthorize("@ss.hasPermission('robot:camera:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCameraExcel(@Valid RobotCameraPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotCameraDO> list = cameraService.getCameraPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆摄像头信息.xls", "数据", RobotCameraRespVO.class,
                        BeanUtils.toBean(list, RobotCameraRespVO.class));
    }

}