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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;

import net.northking.cctp.scriptcase.api.atuscriptrecord.service.AtuScriptInputSetApiService;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetAddDto;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetAddTmpDto;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetQueryDto;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.AtuScriptInputSetResultDto;
import net.northking.cctp.scriptcase.dto.casedesign.TableHeader;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

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

@Api(tags = "脚本输入项数据集关联草稿表")
@RequestMapping("/v1")
@RestController
public class AtuScriptInputSetCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AtuScriptInputSetCtrl.class);

  @Autowired
  private AtuScriptInputSetApiService apiService;

  /**
   * 单环境数据导出
   * @param atuScriptInputSetAddTmpDto
   * @param response
   */
  @ApiOperation(value = "导出Excel：脚本输入项数据集关联草稿表")
  @RequiredPermissions({"ui-updateScript","ui-saveScript"})
  @PostMapping(value = "/excel/export/AtuScriptInputSets",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void exportExcel(
          @RequestBody @Validated AtuScriptInputSetAddTmpDto atuScriptInputSetAddTmpDto,
          HttpServletResponse response)
  {
    apiService.exportExcel(response, atuScriptInputSetAddTmpDto);
  }

  /**
   * 单环境数据导入
   * @param file
   * @param tableHeaders
   * @return JSONObject
   */
  @ApiOperation(value = "导入Excel：脚本输入项数据集关联草稿表")
  @RequiredPermissions({"ui-updateScript","ui-saveScript"})
  @PostMapping(value = "/excel/import/AtuScriptInputSets",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<JSONObject>  importExcel(
      @RequestParam("file") MultipartFile file, @RequestParam( "tableHeaders") String tableHeaders,@RequestParam( "envId") String envId) {
    long execStartTime = System.currentTimeMillis();
    ResultWrapper<JSONObject> resultWrapper = new ResultWrapper<>();
    List<TableHeader> tableHeaderList = JSONObject.parseArray(tableHeaders, TableHeader.class);
    JSONObject result = apiService.importExcel(file, tableHeaderList, envId);
    long execEndTime = System.currentTimeMillis();
    logger.info("导入脚本输入项总耗时：" + (execEndTime - execStartTime));
    return resultWrapper.success(result);
  }

  /**
   * 所有环境的数据列表查询 脚本输入项数据集关联草稿表 列表
   *
   * @param atuScriptInputSetQueryDto
   * @return JsonObject
   */
  @ApiOperation(value = "数据列表查询")
  @PostMapping(value = "/query/AtuScriptInputSets",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<AtuScriptInputSetResultDto> queryAtuScriptInputSets(
          @RequestBody @Validated AtuScriptInputSetQueryDto atuScriptInputSetQueryDto)
  {
    ResultWrapper<AtuScriptInputSetResultDto> wrapper = new ResultWrapper<>();
    AtuScriptInputSetResultDto resultDto = apiService.queryAtuScriptInputSets(atuScriptInputSetQueryDto);
    return wrapper.success(resultDto);
  }

  /**
   * 所有环境数据列表保存 脚本输入项数据集关联草稿表
   *
   * @param addDto
   * @return 主键集合 数据列表
   */
  @ApiOperation(value = "所有环境数据列表保存 ")
  @RequiredPermissions({"ui-updateScript","ui-saveScript"})
  @PostMapping(value = "/AtuScriptInputSets/saveBatch",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<AtuScriptInputSetAddDto> saveBatch(
          @RequestBody @Validated AtuScriptInputSetAddDto addDto)
  {
    ResultWrapper<AtuScriptInputSetAddDto> wrapper = new ResultWrapper<>();
    logger.info("--------------------->输入参数：{}",JSONObject.toJSONString(addDto));
    AtuScriptInputSetAddDto page = apiService.saveBatch(addDto);

    return wrapper.success(page);
  }

  @ApiOperation(value = "保存场景脚本的输入项数据")
  @PostMapping(value = "/save/sceneInputSet",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<AtuScriptInputSetAddDto> saveSceneInputSet(
          @RequestBody @Validated AtuScriptInputSetAddDto addDto)
  {
    ResultWrapper<AtuScriptInputSetAddDto> wrapper = new ResultWrapper<>();
    AtuScriptInputSetAddDto resultDto = apiService.saveSceneInputSet(addDto);
    return wrapper.success(resultDto);
  }


  @ApiOperation(value = "新增查询脚本输入项数据")
  @PostMapping(value = "/query/script/inputSet")
  public ResultWrapper<AtuScriptInputSetResultDto> querySetDataByPath(
          @RequestBody @Validated AtuScriptInputSetAddDto addDto){
    ResultWrapper<AtuScriptInputSetResultDto> wrapper = new ResultWrapper<>();
    AtuScriptInputSetResultDto result = apiService.querySetDataByPath(addDto);
    return wrapper.success(result);
  }

}


