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

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.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.server.controller.admin.reverseproxy.vo.*;
import cn.iocoder.yudao.module.server.dal.dataobject.reverseproxy.ReverseProxyDO;
import cn.iocoder.yudao.module.server.service.reverseproxy.ReverseProxyService;

@Tag(name = "管理后台 - 反向代理配置")
@RestController
@RequestMapping("/server/reverse-proxy")
@Validated
public class ReverseProxyController {

    @Resource
    private ReverseProxyService reverseProxyService;

    @PostMapping("/create")
    @Operation(summary = "创建反向代理配置")
    @PreAuthorize("@ss.hasPermission('server:reverse-proxy:create')")
    public CommonResult<Long> createReverseProxy(@Valid @RequestBody ReverseProxySaveReqVO createReqVO) {
        return success(reverseProxyService.createReverseProxy(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新反向代理配置")
    @PreAuthorize("@ss.hasPermission('server:reverse-proxy:update')")
    public CommonResult<Boolean> updateReverseProxy(@Valid @RequestBody ReverseProxySaveReqVO updateReqVO) {
        reverseProxyService.updateReverseProxy(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除反向代理配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:reverse-proxy:delete')")
    public CommonResult<Boolean> deleteReverseProxy(@RequestParam("id") Long id) {
        reverseProxyService.deleteReverseProxy(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得反向代理配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:reverse-proxy:query')")
    public CommonResult<ReverseProxyRespVO> getReverseProxy(@RequestParam("id") Long id) {
        ReverseProxyDO reverseProxy = reverseProxyService.getReverseProxy(id);
        return success(BeanUtils.toBean(reverseProxy, ReverseProxyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得反向代理配置分页")
    @PreAuthorize("@ss.hasPermission('server:reverse-proxy:query')")
    public CommonResult<PageResult<ReverseProxyRespVO>> getReverseProxyPage(@Valid ReverseProxyPageReqVO pageReqVO) {
        PageResult<ReverseProxyDO> pageResult = reverseProxyService.getReverseProxyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReverseProxyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出反向代理配置 Excel")
    @PreAuthorize("@ss.hasPermission('server:reverse-proxy:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReverseProxyExcel(@Valid ReverseProxyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReverseProxyDO> list = reverseProxyService.getReverseProxyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "反向代理配置.xls", "数据", ReverseProxyRespVO.class,
                        BeanUtils.toBean(list, ReverseProxyRespVO.class));
    }

}