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

import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.s3.FileDownloadException;
import net.northking.cctp.common.s3.SimpleStorageService;
import net.northking.cctp.scriptcase.db.entity.MdBusComponentInfo;
import net.northking.cctp.scriptcase.db.entity.MdComponentParam;
import net.northking.cctp.scriptcase.db.service.MdBusComponentInfoService;
import net.northking.cctp.scriptcase.db.service.MdComponentParamService;
import net.northking.cctp.scriptcase.dto.component.HisBusComponentInfoDetailDto;
import net.northking.cctp.scriptcase.dto.component.HisBusComponentInfoQueryDto;
import net.northking.cctp.scriptcase.enums.ScriptCaseAppError;
import net.northking.cctp.scriptcase.tools.MinioPathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

/**
 * 业务组件表_历史服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

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



  @Autowired
  private EntityNameCache entityNameCache;
  @Autowired
  private MdBusComponentInfoService mdBusComponentInfoService;
  @Autowired
  private MdComponentParamService mdComponentParamService;
  @Autowired
  private MdBusComponentInfoApiServiceImpl mdApiService;
  @Autowired
  private MinioPathUtils pathUtil;
  @Autowired
  private SimpleStorageService simpleStorageService;




  @Override
  public HisBusComponentInfoDetailDto queryHisInfoByVersion(HisBusComponentInfoQueryDto dto) {


    HisBusComponentInfoDetailDto result = new HisBusComponentInfoDetailDto();
    MdBusComponentInfo his = mdBusComponentInfoService.findByPrimaryKey(dto.getComId());
      MdComponentParam param = mdComponentParamService.findByPrimaryKey(dto.getComId());
      BeanUtils.copyProperties(his,result);
      result.setComJson(param.getComJson());
      result.setParamJson(param.getParamJson());
    return result;
  }


  private <T> T downloadScriptFile(String url, Class<T> clazz) {
    String[] resultPath = pathUtil.idToPath(url);
    InputStream inputStream = null;
    T dataObject = null;
    try {
      inputStream = simpleStorageService.downloadAsStream(resultPath[0], "/" + resultPath[1]);
      dataObject = pathUtil.getFileContent(inputStream, clazz);
    } catch (FileDownloadException e) {
      logger.error("读取原脚本文件失败，失败原因:" + e);
      throw new PlatformRuntimeException(ScriptCaseAppError.READ_FILE_ERROR);
    } finally {
      if (null != inputStream) {
        try {
          inputStream.close();
        } catch (IOException e) {
          logger.error("读取原脚本文件后关闭输入流失败，失败原因:" + e);
        }
      }
    }
    return dataObject;
  }
}
