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

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.server.vo.*;
import cn.iocoder.yudao.module.safe.dal.dataobject.server.SafeServerDO;
import cn.iocoder.yudao.module.safe.service.server.SafeServerService;

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

    @Resource
    private SafeServerService serverService;

    @PostMapping("/create")
    @Operation(summary = "创建安防后端设备")
    public CommonResult<Long> createServer(@Valid @RequestBody AppSafeServerSaveReqVO createReqVO) {
        return success(serverService.createServer(createReqVO));
    }

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

    @DeleteMapping("/delete")
    @Operation(summary = "删除安防后端设备")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteServer(@RequestParam("id") Long id) {
        serverService.deleteServer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得安防后端设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppSafeServerRespVO> getServer(@RequestParam("id") Long id) {
        SafeServerDO server = serverService.getServer(id);
        return success(BeanUtils.toBean(server, AppSafeServerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得安防后端设备分页")
    public CommonResult<PageResult<AppSafeServerRespVO>> getServerPage(@Valid AppSafeServerPageReqVO pageReqVO) {
        PageResult<SafeServerDO> pageResult = serverService.getServerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppSafeServerRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得所有安防后端设备")
    public CommonResult<List<AppSafeServerRespVO>> getAllServer() {
        List<SafeServerDO> list = serverService.getAllServer();
        return success(BeanUtils.toBean(list, AppSafeServerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安防后端设备 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServerExcel(@Valid AppSafeServerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SafeServerDO> list = serverService.getServerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "安防后端设备.xls", "数据", AppSafeServerRespVO.class,
                        BeanUtils.toBean(list, AppSafeServerRespVO.class));
    }

}
