package com.reyun.saas.mob.openapi.service.impl;

import com.reyun.saas.common.model.NoSqlTableName;
import com.reyun.saas.mob.openapi.domain.CollaborativeProject;
import com.reyun.saas.mob.openapi.service.OpenApiRtaService;
import com.reyun.saas.mob.user.service.NoSqlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.Set;

/**
 * @author: hanzepeng
 * @Date: 2022/02/14/11:03
 * @Description: RTA配置业务层
 */
@Slf4j
@Service
@Transactional
public class OpenApiRtaServiceImpl implements OpenApiRtaService {

    @Autowired
    private NoSqlService noSqlService;

    //rta白名单表的主键
    private static final String RTA_WHITELIST_KEY = "oid";

    //rta 白名单ip属性名
    private static final String WHITELIST_IPS_FIELD_NAME = "ip_whitelist";

    /**
     * 更新合作项目的RTA ip白名单
     */
    @Override
    public void updateIpWhiteList(CollaborativeProject project, Set<String> ips) {
        noSqlService.putSet(NoSqlTableName.RTA_WHITELIST, RTA_WHITELIST_KEY, project.getOid(),
                WHITELIST_IPS_FIELD_NAME, ips);
    }


    /**
     * 查询合作项目的rta ip白名单
     */
    @Override
    public Set<String> ipWhiteList(CollaborativeProject project) {
        Set<String> result = null;
        Map<String, Object> item2Map = noSqlService.getItem2Map(NoSqlTableName.RTA_WHITELIST, RTA_WHITELIST_KEY, project.getOid());
        if(item2Map != null){
            Set<String> ips = (Set)item2Map.get(WHITELIST_IPS_FIELD_NAME);
            if (!CollectionUtils.isEmpty(ips)){
                result = ips;
            }
        }
        return result;
    }
}
