package cn.iocoder.yudao.module.safe.controller.admin.camera;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.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.*;

import cn.iocoder.yudao.module.safe.controller.admin.camera.vo.*;
import cn.iocoder.yudao.module.safe.dal.dataobject.camera.SafeCameraDO;
import cn.iocoder.yudao.module.safe.service.camera.SafeCameraService;

@Tag(name = "用户 APP - 安防前端设备")
@RestController
@RequestMapping("/safe/camera")
@Validated
public class AppSafeCameraController {

    @Resource
    private SafeCameraService cameraService;

    @PostMapping("/create")
    @Operation(summary = "创建安防前端设备")
    public CommonResult<Long> createCamera(@Valid @RequestBody AppSafeCameraSaveReqVO createReqVO) {
        return success(cameraService.createCamera(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新安防前端设备")
    public CommonResult<Boolean> updateCamera(@Valid @RequestBody AppSafeCameraSaveReqVO updateReqVO) {
        cameraService.updateCamera(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除安防前端设备")
    @Parameter(name = "id", description = "编号", required = true)
    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")
    public CommonResult<AppSafeCameraRespVO> getCamera(@RequestParam("id") Long id) {
        SafeCameraDO camera = cameraService.getCamera(id);
        return success(BeanUtils.toBean(camera, AppSafeCameraRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得安防前端设备分页")
    public CommonResult<PageResult<AppSafeCameraRespVO>> getCameraPage(@Valid AppSafeCameraPageReqVO pageReqVO) {
        PageResult<SafeCameraDO> pageResult = cameraService.getCameraPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppSafeCameraRespVO.class));
    }

    @GetMapping("/cameras")
    @Operation(summary = "获得安防后端设备所包括的多路前端设备")
    public CommonResult<List<AppSafeCameraRespVO>> getCameras(@RequestParam("serverId") Long serverId) {
        List<SafeCameraDO> pageResult = cameraService.getCamerasByServerId(serverId);
        return success(BeanUtils.toBean(pageResult, AppSafeCameraRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安防前端设备 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCameraExcel(@Valid AppSafeCameraPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SafeCameraDO> list = cameraService.getCameraPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "安防前端设备.xls", "数据", AppSafeCameraRespVO.class,
                        BeanUtils.toBean(list, AppSafeCameraRespVO.class));
    }

}
