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

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.AtuInputApiService;
import net.northking.cctp.scriptcase.dto.atuscriptrecord.*;
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.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 数据源配置 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "输入项")
@RequestMapping("/v1/AtuInputs")
@RestController
public class AtuInputRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AtuInputRestfulCtrl.class);

  @Autowired
  private AtuInputApiService apiService;

  @ApiOperation(value = "下载输入项模板")
  @PostMapping(value = "/excel/template/{templateParam}",
          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void downloadTemplate(HttpServletResponse response, HttpServletRequest request, @PathVariable("templateParam") String templateParam)
  {
    logger.info("下载输入项模板接口开始执行");
    apiService.downloadTemplate(response,request, templateParam);
    logger.info("下载输入项模板接口结束执行");
  }

  @ApiOperation(value = "导入输入项数据")
  @PostMapping(value = "/excel/import/",
          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("type") Integer type,@RequestParam(value = "scriptId",required = false) String scriptId,@RequestParam(value = "path",required = false) String path)//type:0-新增,1-覆盖;
  {
    logger.info("导入输入项数据接口开始执行");
      //字符串转list
    List<TableHeader> tableHeaderList = JSONObject.parseArray(tableHeaders, TableHeader.class);
    ResultWrapper<String> wrapper = apiService.importExcel(file,tableHeaderList,type,scriptId,path);
    logger.info("导入输入项数据接口结束执行");
    return wrapper;
  }

  @ApiOperation(value = "通过输入项地址反显输入项数据")
  @PostMapping(value = "/find/{path}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<JSONObject> find(
          @PathVariable("path") String path)
  {
    logger.info("通过输入项地址反显输入项数据接口开始执行");
    ResultWrapper<JSONObject> wrapper = new ResultWrapper<>();
    JSONObject jsonObject = apiService.find(path);
    logger.info("通过输入项地址反显输入项数据接口结束执行");
    return wrapper.success(jsonObject);
  }

  //初始化的时候
  @ApiOperation(value = "通过脚本id反显输入项数据")
  @PostMapping(value = "/findByScriptId/{scriptId}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<JSONObject> findByScriptId(
          @PathVariable("scriptId") String scriptId)
  {
    logger.info("通过脚本id反显输入项数据接口开始执行");
    ResultWrapper<JSONObject> wrapper = new ResultWrapper<>();
    JSONObject jsonObject = apiService.findByScriptId(scriptId);
    logger.info("通过脚本id反显输入项数据接口结束执行");
    return wrapper.success(jsonObject);
  }
  //通过场景id查输入项数据
  @ApiOperation(value = "通过场景id查输入项数据")
  @PostMapping(value = "/findBySceneId/{sceneId}",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<JSONObject> findBySceneId(
          @PathVariable("sceneId") String sceneId)
  {
    logger.info("通过场景id查输入项数据接口开始执行");
    ResultWrapper<JSONObject> wrapper = new ResultWrapper<>();
    JSONObject jsonObject = apiService.findBySceneId(sceneId);
    logger.info("通过场景id查输入项数据接口结束执行");
    return wrapper.success(jsonObject);
  }
  //保存场景的输入向数据
  @ApiOperation(value = "保存更新场景数据")
  @PostMapping(value = "/saveSceneScriptData",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> saveSceneScriptData(@RequestBody @Validated ScriptInputTableDto dto)
  {
    logger.info("start saveSceneScriptData");
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String url = this.apiService.saveSceneScriptData(dto);
    logger.info("end saveSceneScriptData");
    return wrapper.success(null);
  }

}
