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

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.frpc.vo.FrpcProxyServerPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.frpc.vo.FrpcProxyServerRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.frpc.vo.FrpcProxyServerSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.frpc.FrpcProxyServerDO;
import cn.iocoder.yudao.module.iot.service.frpc.FrpcProxyServerService;
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/frpc-proxy-server")
@Validated
public class FrpcProxyServerController {

    @Resource
    private FrpcProxyServerService frpcProxyServerService;

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

    @PutMapping("/update")
    @Operation(summary = "更新代理服务")
    @PreAuthorize("@ss.hasPermission('iot:frpc-proxy-server:update')")
    public CommonResult<Boolean> updateFrpcProxyServer(@Valid @RequestBody FrpcProxyServerSaveReqVO updateReqVO) {
        frpcProxyServerService.updateFrpcProxyServer(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> deleteFrpcProxyServer(@RequestParam("id") Long id) {
        frpcProxyServerService.deleteFrpcProxyServer(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<FrpcProxyServerRespVO> getFrpcProxyServer(@RequestParam("id") Long id) {
        FrpcProxyServerDO frpcProxyServer = frpcProxyServerService.getFrpcProxyServer(id);
        return success(BeanUtils.toBean(frpcProxyServer, FrpcProxyServerRespVO.class));
    }

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

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

}