/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved.
*
*/
package net.northking.cctp.scriptcase.api.casedesign.service;

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.db.annotation.DatabaseReadOnly;
import net.northking.cctp.common.feign.ApiAtsEnviromentFeign;
import net.northking.cctp.scriptcase.dto.casedesign.AtuProjectEnvDetailDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 系统用例环境表服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

@Service
@ConditionalOnMissingBean(name = "AtuProjectEnvApiServiceImpl")
public class AtuProjectEnvApiServiceImpl implements AtuProjectEnvApiService
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AtuProjectEnvApiServiceImpl.class);

  @Autowired
  private ApiAtsEnviromentFeign apiAtsEnviromentFeign;

  @Override
  @DatabaseReadOnly
  public List<AtuProjectEnvDetailDto> findByPK(List<String> projectIds)
  {
    List<Environment> allEnvData = new ArrayList<>();
    for (String projectId : projectIds) {
      Long id = null;
      try {
        id = Long.valueOf(projectId);
      } catch (NumberFormatException e) {
        logger.error("系统Id类型转换异常", e);
        throw new InvalidException("系统Id类型转换异常");
      }
      ResponseVO<List<Environment>> listResponseVO = apiAtsEnviromentFeign.listByProject(id);
      List<Environment> data = listResponseVO.getData();
      allEnvData.addAll(data);
    }
    if (CollectionUtils.isEmpty(allEnvData))
    {
      return null;
    }
    List<AtuProjectEnvDetailDto> atuProjectEnvDetailDtos=new ArrayList<>();
    AtuProjectEnvDetailDto atuProjectEnvDetailDto=null;
    //从缓存中拿环境信息
    List<String> envIds=new ArrayList<>();
    for (Environment environment : allEnvData) {
      atuProjectEnvDetailDto = new AtuProjectEnvDetailDto();
      atuProjectEnvDetailDto.setEnvName(environment.getName());
      atuProjectEnvDetailDto.setMemo(environment.getDescription());
      atuProjectEnvDetailDto.setEnvId(environment.getId().toString());
      atuProjectEnvDetailDtos.add(atuProjectEnvDetailDto);
    }
    return atuProjectEnvDetailDtos;
  }

}
