package com.sf.service.gateway.controller;

import apijson.JSON;
import com.sf.common.annotation.Log;
import com.sf.common.core.controller.BaseController;
import com.sf.common.core.domain.AjaxResult;
import com.sf.common.core.page.TableDataInfo;
import com.sf.common.enums.BusinessType;
import com.sf.common.utils.poi.ExcelUtil;
import com.sf.service.gateway.domain.GatewayConfig;
import com.sf.service.gateway.domain.GatewayStrategy;
import com.sf.service.gateway.domain.dto.QueryGatewayStrategyDTO;
import com.sf.service.gateway.domain.dto.UpdateDataStatusDTO;
import com.sf.service.gateway.domain.vo.GatewayStrategyListVO;
import com.sf.service.gateway.enums.GatewayDataStatus;
import com.sf.service.gateway.service.IGatewayConfigService;
import com.sf.service.gateway.service.IGatewayStrategyService;
import com.sf.service.index.util.AppUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 网关配置管理Controller
 * 
 * @author zoukun
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/gateway/config")
public class GatewayConfigController extends BaseController
{
    @Autowired
    private IGatewayConfigService gatewayConfigService;

    /**
     * 获取网关配置信息
     */
    @PreAuthorize("@ss.hasPermi('gateway:config:query')")
    @GetMapping
    public AjaxResult getInfo()
    {
        String appCode = AppUtils.getAppCodeFromRequestHeader();
        GatewayConfig gatewayConfig = gatewayConfigService.selectGatewayConfigByAppCode(appCode);
        if (gatewayConfig == null) {
            gatewayConfig = new GatewayConfig();
            gatewayConfig.setAppCode(appCode);
            gatewayConfig.setDataEncryptionStatus(GatewayDataStatus.DISABLE.getCode());
            gatewayConfig.setApiCurrentLimitingStatus(GatewayDataStatus.DISABLE.getCode());
            String defaultResponse = JSON.toJSONString(AjaxResult.error("系统繁忙，请稍后重试！"));
            gatewayConfig.setApiCurrentLimitingDefaultResponse(defaultResponse);
            gatewayConfig.setAppCurrentLimitingStatus(GatewayDataStatus.DISABLE.getCode());
            gatewayConfig.setAppCurrentLimitingDefaultResponse(defaultResponse);
        }
        return success(gatewayConfig);
    }

    /**
     * 修改网关配置
     */
    @PreAuthorize("@ss.hasPermi('gateway:config:edit')")
    @Log(title = "网关配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GatewayConfig gatewayConfig)
    {
        gatewayConfigService.updateGatewayConfig(gatewayConfig);
        return success();
    }

}
