package cn.iocoder.yudao.module.iot.controller.admin.frps;

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.iot.controller.admin.frps.vo.FrpsServerPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.frps.vo.FrpsServerRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.frps.vo.FrpsServerSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.frps.FrpsServerDO;
import cn.iocoder.yudao.module.iot.service.frps.FrpsServerService;
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("/iot/frps-server")
@Validated
public class FrpsServerController {

    @Resource
    private FrpsServerService frpsServerService;

    @PostMapping("/create")
    @Operation(summary = "创建代理服务器")
    @PreAuthorize("@ss.hasPermission('iot:frpc-proxy-server:create')")
    public CommonResult<Long> createFrpsServer(@Valid @RequestBody FrpsServerSaveReqVO createReqVO) {
        return success(frpsServerService.createFrpsServer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理服务器")
    @PreAuthorize("@ss.hasPermission('iot:frpc-proxy-server:update')")
    public CommonResult<Boolean> updateFrpsServer(@Valid @RequestBody FrpsServerSaveReqVO updateReqVO) {
        frpsServerService.updateFrpsServer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理服务器")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:frpc-proxy-server:delete')")
    public CommonResult<Boolean> deleteFrpsServer(@RequestParam("id") Long id) {
        frpsServerService.deleteFrpsServer(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理服务器")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:frpc-proxy-server:query')")
    public CommonResult<FrpsServerRespVO> getFrpsServer(@RequestParam("id") Long id) {
        FrpsServerDO frpsServer = frpsServerService.getFrpsServer(id);
        return success(BeanUtils.toBean(frpsServer, FrpsServerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理服务器分页")
    @PreAuthorize("@ss.hasPermission('iot:frpc-proxy-server:query')")
    public CommonResult<PageResult<FrpsServerRespVO>> getFrpsServerPage(@Valid FrpsServerPageReqVO pageReqVO) {
        PageResult<FrpsServerDO> pageResult = frpsServerService.getFrpsServerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FrpsServerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理服务器 Excel")
    @PreAuthorize("@ss.hasPermission('iot:frpc-proxy-server:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFrpsServerExcel(@Valid FrpsServerPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FrpsServerDO> list = frpsServerService.getFrpsServerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "代理服务器.xls", "数据", FrpsServerRespVO.class,
                BeanUtils.toBean(list, FrpsServerRespVO.class));
    }

}