package net.northking.cctp.scriptcase.tools;

import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.constants.BaseDefineDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.entity.Environment;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.exception.InvalidException;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ResponseVO;
import net.northking.cctp.common.feign.ApiAtsEnviromentFeign;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class EnvNameUtils {

    private static final Logger logger = LoggerFactory.getLogger(EnvNameUtils.class);

    @Autowired
    private ApiAtsEnviromentFeign apiAtsEnviromentFeign;

    public String getEnvName(String envId) {
        if (StringUtils.isNotBlank(envId)) {
            Long id = null;
            try {
                id = Long.valueOf(envId);
            } catch (NumberFormatException e) {
                logger.error("环境Id类型转化异常");
                throw new InvalidException("环境Id类型转化异常");
            }
            List<Long> envIds = new ArrayList<>();
            envIds.add(id);
            ResponseVO<List<Environment>> responseVo = apiAtsEnviromentFeign.getEnvInfoById(envIds);
            if (responseVo == null || !responseVo.getCode().equals(BaseDefineDependencyConstants.SYSTEM_RESPONSE_CODE_SUCCESS)) {
                throw new InvalidException("服务间调用获取环境信息失败");
            }

            if (responseVo.getData() != null) {
                return responseVo.getData().get(0).getName();
            }
        }
        return null;
    }
}
