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

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.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.scriptcase.api.inputSet.service.AtuInputSetApiService;
import net.northking.cctp.scriptcase.db.entity.AtuInputSet;
import net.northking.cctp.scriptcase.dto.casedesign.TableHeader;
import net.northking.cctp.scriptcase.dto.inputSet.*;
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(tags = "输入项数据集")
@RequestMapping("/v1")
@RestController
public class AtuInputSetQueryCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AtuInputSetQueryCtrl.class);

  @Autowired
  private AtuInputSetApiService apiService;

  /**
   * 分页查询
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：输入项数据集")
  @PostMapping(value = "/paging/query/AtuInputSets",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<AtuInputSetDetailDto>> pagingQuery(
      @RequestBody @Validated QueryByPage<AtuInputSetQueryDto> queryByPage)
  {
    ResultWrapper<Pagination<AtuInputSetDetailDto>> wrapper = new ResultWrapper<>();
    Pagination<AtuInputSetDetailDto> page = apiService.pagingQuery(queryByPage);

    return wrapper.success(page);
  }

  @ApiOperation(value = "导出Excel：输入项数据集")
  @PostMapping(value = "/excel/export/AtuInputSets",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void exportExcel(
      @RequestBody @Validated AtuInputSetDownloadDto dto,
      HttpServletResponse response, HttpServletRequest request)
  {
    // 定义导出的Excel在客户端浏览器上提示下载的文件名，不写后缀
    apiService.downloadInputData(response,request,dto);
  }

  @ApiOperation(value = "下载Excel模板：输入项数据集")
  @PostMapping(value = "/excel/template/AtuInputSets",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void downloadTemplate(HttpServletResponse response, HttpServletRequest request, @RequestBody List<String> firstLine)
  {
    apiService.downloadInputTemplate(response, request,firstLine);
  }

  @ApiOperation(value = "导入Excel：输入项数据集")
  @PostMapping(value = "/excel/import/AtuInputSets",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<String> importExcel(
          @RequestParam("file") MultipartFile file, @RequestParam("tableHeaders") String tableHeaders,
          @RequestParam("envId") String envId,@RequestParam("scriptId")String scriptId)
  {
    List<TableHeader> tableHeaderList = JSONObject.parseArray(tableHeaders, TableHeader.class);
    AtuInputSetFileDto dto = new AtuInputSetFileDto();
    dto.setEnvId(envId);
    dto.setScriptId(scriptId);
    dto.setTableHeaders(tableHeaderList);
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String result = apiService.importSetExcel(file,dto);
    return wrapper.success(result);
  }

  @RequiredPermissions("ui-updateInputSet")
  @ApiOperation(value = "保存输入项数据集")
  @PostMapping(value = "/save/set/data")
  public ResultWrapper<List<InputSet>> saveInputSet(@RequestBody AtuInputSetSaveDto dto){
    ResultWrapper<List<InputSet>> wrapper = new ResultWrapper<>();
    List<InputSet> result = this.apiService.saveInputSet(dto);
    return wrapper.success(result);
  }

  @ApiOperation(value = "查询脚本内的数据集")
  @GetMapping(value = "/queryInputSet/{scriptId}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<InputSetResultDto> queryInputSet(
          @PathVariable("scriptId") String scriptId, @RequestParam(required = false, defaultValue = "true", name = "update")boolean update){
    ResultWrapper<InputSetResultDto> wrapper = new ResultWrapper<>();
    InputSetResultDto resultDto = this.apiService.queryInputData(scriptId, true);
    return wrapper.success(resultDto);
  }

  @ApiOperation(value = "拉去用例集下输入项")
  @PostMapping(value = "/queryInputSet/case",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<InputSetResultDto> queryInputSetByCase(
          @RequestBody AtuInputSetCaseDto dto){
    ResultWrapper<InputSetResultDto> wrapper = new ResultWrapper<>();
    InputSetResultDto result = this.apiService.queryInputSetByCase(dto);
    return wrapper.success(result);
  }

  @ApiOperation(value = "feign调用:根据环境和脚本查询输入项数据")
  @PostMapping(value = "/atuInputSet/query/queryInputByEnvIdAndScriptIdMap")
  public ResultWrapper<List<AtuInputSet>> queryInputByEnvIdAndScriptIdMap(@RequestBody Map<String, List<String>> envScriptMap){
    ResultWrapper<List<AtuInputSet>> wrapper = new ResultWrapper<>();
    List<AtuInputSet> result = this.apiService.queryInputByEnvIdAndScriptIdMap(envScriptMap);
    return wrapper.success(result);
  }

  @ApiOperation(value = "校验静态数据列能否被修改")
  @PostMapping(value = "/check/dataSet/updateOrDelete")
  public ResultWrapper<Boolean> checkDataSetDelete(@RequestBody AtuInnputDataSetQueryDto queryDto){
    ResultWrapper<Boolean> wrapper = new ResultWrapper<>();
    Boolean result = this.apiService.checkDataSetDelete(queryDto);
    return wrapper.success(result);
  }


  @ApiOperation(value = "feign调用:查询静态数据集存在关联")
  @PostMapping(value = "/selectLinkBySetId")
  public List<AtuInputDataSetResultDto> selectLinkBySetId(@RequestBody List<String> setId){
    return this.apiService.selectLinkBySetId(setId);
  }


}


