package cn.iocoder.yudao.module.device.controller.admin.cameradevice;

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.constraints.*;
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.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.device.controller.admin.cameradevice.vo.*;
import cn.iocoder.yudao.module.device.dal.dataobject.cameradevice.CameraDeviceDO;
import cn.iocoder.yudao.module.device.service.cameradevice.CameraDeviceService;

@Tag(name = "管理后台 - 设备-监控摄像头")
@RestController
@RequestMapping("/device/camera-device")
@Validated
public class CameraDeviceController {

    @Resource
    private CameraDeviceService cameraDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建设备-监控摄像头")
    @PreAuthorize("@ss.hasPermission('device:camera-device:create')")
    public CommonResult<Long> createCameraDevice(@Valid @RequestBody CameraDeviceSaveReqVO createReqVO) {
        return success(cameraDeviceService.createCameraDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备-监控摄像头")
    @PreAuthorize("@ss.hasPermission('device:camera-device:update')")
    public CommonResult<Boolean> updateCameraDevice(@Valid @RequestBody CameraDeviceSaveReqVO updateReqVO) {
        cameraDeviceService.updateCameraDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备-监控摄像头")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:camera-device:delete')")
    public CommonResult<Boolean> deleteCameraDevice(@RequestParam("id") Long id) {
        cameraDeviceService.deleteCameraDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备-监控摄像头")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:camera-device:query')")
    public CommonResult<CameraDeviceRespVO> getCameraDevice(@RequestParam("id") Long id) {
        CameraDeviceDO cameraDevice = cameraDeviceService.getCameraDevice(id);
        return success(BeanUtils.toBean(cameraDevice, CameraDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备-监控摄像头分页")
    @PreAuthorize("@ss.hasPermission('device:camera-device:query')")
    public CommonResult<PageResult<CameraDeviceRespVO>> getCameraDevicePage(@Valid CameraDevicePageReqVO pageReqVO) {
        PageResult<CameraDeviceDO> pageResult = cameraDeviceService.getCameraDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CameraDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备-监控摄像头 Excel")
    @PreAuthorize("@ss.hasPermission('device:camera-device:export')")
    @OperateLog(type = EXPORT)
    public void exportCameraDeviceExcel(@Valid CameraDevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CameraDeviceDO> list = cameraDeviceService.getCameraDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备-监控摄像头.xls", "数据", CameraDeviceRespVO.class,
                        BeanUtils.toBean(list, CameraDeviceRespVO.class));
    }

}