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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.dataPool.api.dataSets.service.DataSetEnvRelApiService;
import net.northking.cctp.dataPool.dto.dataSets.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(tags = "数据集环境关联表")
@RequestMapping("/v1")
@RestController
public class DataSetEnvRelCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(DataSetEnvRelCtrl.class);

  @Autowired
  private DataSetEnvRelApiService apiService;

  /**
   * 导出单环境下的数据集数据
   * @param dataSetEnvRelQueryDto
   * @param response
   */
  @ApiOperation(value = "导出Excel：数据集环境关联表")
  @RequiredPermissions("ui-exportDataSetEnvRels")
  @PostMapping(value = "/excel/export/DataSetEnvRels",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void exportExcel(
          @RequestBody @Validated DataSetEnvRelQueryDto dataSetEnvRelQueryDto,
          HttpServletResponse response)
  {
    apiService.exportExcel(response, dataSetEnvRelQueryDto);
  }

  /**
   *  单环境数据导入
   * @param file
   * @param dataSetId
   * @param envId
   * @return
   */
  @ApiOperation(value = "导入Excel：单环境数据导入")
  @PostMapping(value = "/excel/import/DataSetEnvRels",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<DataSetsSaveDto> importExcel(
      @RequestParam("file") MultipartFile file,@RequestParam("dataSetId") String dataSetId,@RequestParam("envId") String envId,@RequestParam("tableHeaders") String tableHeaders)
  {
    long startTime = System.currentTimeMillis();
    ResultWrapper<DataSetsSaveDto> resultWrapper = new ResultWrapper<>();
    DataSetsSaveDto result = apiService.importExcel(file, dataSetId, envId, tableHeaders);
    long endTime = System.currentTimeMillis();
    logger.info("单环境数据导入静态数据集耗时："+(endTime - startTime));
    return resultWrapper.success(result);
  }


  /**
   * 获取数据集环境关联表数据列表查询
   *
   * @param    queryDto    查询条件对象
   * @return  DataSetsDetailDto
   */
  @ApiOperation(value = "获取：数据集环境关联表")
  @PostMapping(
          value = "/query/list",
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<DataSetsSaveDto>> queryList(
          @RequestBody DataSetEnvRelQueryDto queryDto)
  {
    ResultWrapper<List<DataSetsSaveDto>> wrapper = new ResultWrapper<>();
    List<DataSetsSaveDto> dto = apiService.queryList(queryDto);
    return wrapper.success(dto);
  }

  /**
   * 数据集内多环境数据同时保存
   *
   * @param    addDto    新增数据对象信息
   * @return
   */
  @ApiOperation(value = "保持：数据集内多环境数据同时保存")
  @RequiredPermissions("ui-saveDataSetBatch")
  @PostMapping(
          value = "/save/batch",
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<String>> saveBatch(
          @RequestBody DataSetEnvRelAddDto addDto)
  {
    return apiService.saveBatch(addDto);
  }

  @ApiOperation(value = "查询Interface")
  @PostMapping(value = "/query/selectDataUrlAndTreeNameInterface",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<AtuScriptInputSetQuoteFieldDto> selectDataUrlAndTreeNameInterface(@RequestBody AtuScriptInputSetQuoteFieldDto atuScriptInputSetQuoteFieldDto){
    ResultWrapper<AtuScriptInputSetQuoteFieldDto> wrapper = new ResultWrapper<>();
    return wrapper.success(apiService.selectDataUrlAndTreeName(atuScriptInputSetQuoteFieldDto));
  }

  @ApiOperation(value = "查询静态数据集对应的文件")
  @PostMapping(value = "/query/getDataSetFile",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public Map<String,AtuScriptInputSetQuoteFeignDto> queryDataSetFile(@RequestBody AtuDataSetFeignDto dto){
    return apiService.getDataSetFile(dto);
  }

}


