package com.sf.service.gateway.service.impl;

import apijson.JSON;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.SecurityUtils;
import com.sf.service.gateway.domain.GatewayConfig;
import com.sf.service.gateway.enums.EncryptionAlgorithm;
import com.sf.service.gateway.enums.GatewayDataStatus;
import com.sf.service.gateway.mapper.GatewayConfigMapper;
import com.sf.service.gateway.service.IGatewayConfigService;
import com.sf.service.index.util.AppUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 功能描述:
 *
 * @author a_kun
 * @date 2024/4/25 17:24
 */
@Service
public class GatewayConfigServiceImpl implements IGatewayConfigService {

    @Autowired
    private GatewayConfigMapper gatewayConfigMapper;

    @Override
    public GatewayConfig selectGatewayConfigByAppCode(String appCode) {
        if (StringUtils.hasText(appCode)){
            return gatewayConfigMapper.selectByAppCode(appCode);
        }
        return null;
    }

    @Override
    public void updateGatewayConfig(GatewayConfig gatewayConfig) {
        checkInsertOrUpdateGatewayConfig(gatewayConfig);
        String appCode = AppUtils.getAppCodeFromRequestHeader();
        String username = SecurityUtils.getUsername();
        Date nowDate = DateUtils.getNowDate();
        GatewayConfig inDbConfig = gatewayConfigMapper.selectByAppCode(appCode);
        if (inDbConfig == null) {
            gatewayConfig.setAppCode(appCode);
            gatewayConfig.setCreated(username);
            gatewayConfig.setModified(username);
            gatewayConfig.setCreateTime(nowDate);
            gatewayConfig.setUpdateTime(nowDate);
            gatewayConfigMapper.insert(gatewayConfig);
        }else {
            gatewayConfig.setModified(username);
            gatewayConfig.setCreateTime(nowDate);
            gatewayConfig.setUpdateTime(nowDate);
            gatewayConfigMapper.updateByAppCodeSelective(gatewayConfig);
        }
    }

    private void checkInsertOrUpdateGatewayConfig(GatewayConfig gatewayConfig) {
        if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getDataEncryptionStatus())){
            EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithm.getByCode(gatewayConfig.getEncryptionAlgorithm());
            Assert.notNull(encryptionAlgorithm,"不支持的加密算法");
            Assert.hasText(gatewayConfig.getPrivateKey(),"密钥内容不能为空");
            if (EncryptionAlgorithm.RSA.equals(encryptionAlgorithm)){
                Assert.hasText(gatewayConfig.getPublicKey(),"公钥内容不能为空");
            }
        }
        if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getApiCurrentLimitingStatus())){
            Assert.notNull(gatewayConfig.getApiCurrentLimitingThreshold(),"限流值不能为空");
            Assert.isTrue(gatewayConfig.getApiCurrentLimitingThreshold() >= 0,"限流值>=0");
            // 页面没有,这里给个默认值
            if (gatewayConfig.getApiCurrentLimitingTimeWindow() == null) {
                gatewayConfig.setApiCurrentLimitingTimeWindow(1);
            }else {
                Assert.isTrue(gatewayConfig.getApiCurrentLimitingTimeWindow() >= 0,"限流窗口值>=0");
            }
            Assert.isTrue(JSON.isJSONObject(gatewayConfig.getApiCurrentLimitingDefaultResponse()),"请输入正确的JSON格式限流响应信息");
        }
        if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getAppCurrentLimitingStatus())){
            Assert.notNull(gatewayConfig.getAppCurrentLimitingThreshold(),"限流值不能为空");
            Assert.isTrue(gatewayConfig.getAppCurrentLimitingThreshold() >= 0,"限流值>=0");
            // 页面没有,这里给个默认值
            if (gatewayConfig.getAppCurrentLimitingTimeWindow() == null) {
                gatewayConfig.setAppCurrentLimitingTimeWindow(1);
            }else {
                Assert.isTrue(gatewayConfig.getAppCurrentLimitingTimeWindow() >= 0,"限流窗口值>=0");
            }
            Assert.isTrue(JSON.isJSONObject(gatewayConfig.getAppCurrentLimitingDefaultResponse()),"请输入正确的JSON格式限流响应信息");
        }
    }
}
