/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . 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.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.dataPool.api.dataSets.service.DataSetsApiService;
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 javax.servlet.http.HttpServletResponse;

/**
 * 数据集 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "数据集管理")
@RequestMapping("/v1/dataSets")
@RestController
public class DataSetsRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(DataSetsRestfulCtrl.class);

  @Autowired
  private DataSetsApiService apiService;

  @ApiOperation(value = "数据库配置保存数据集")
  @PostMapping(value = "/createDataSet",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> createDataSet(
          @RequestBody @Validated DataSetsAddDto inputDto)
  {
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String uuid = apiService.createDataSet(inputDto);
    return wrapper.success(uuid);
  }

  /**
   * Restful接口-post： 新增数据集
   *
   * @param inputDto         接口数据传输对象
   * @return 主键
   */
  @ApiOperation(value = "新增：数据集")
  @RequiredPermissions("ui-saveDataSets")
  @PostMapping(
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> add(
      @RequestBody @Validated DataSetsAddDto inputDto)
  {
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String uuid = apiService.add(inputDto);
    return wrapper.success(uuid);
  }

  /**
   * Restful接口-put： 更新数据集
   *
   * @param inputDto         接口数据传输对象
   * @return 修改的记录数量
   */
  @ApiOperation(value = "更新：数据集")
  @RequiredPermissions("ui-updateDataSets")
  @PutMapping(
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> update(
      @RequestBody @Validated DataSetsUpdateDto inputDto) throws IllegalAccessException
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.updateByPK(inputDto);
    return wrapper.success(count);
  }

  /**
   * Restful接口-get： 获取数据集
   *
   * @param uuid       主键
   * @return 接口数据传输对象
   */
  @ApiOperation(value = "获取：数据集详情")
  @GetMapping(
      value = "getDataSetDetail/{uuid}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<DataSetsDetailDto> getDataSetDetail(
      @PathVariable("uuid") String uuid)
  {
    ResultWrapper<DataSetsDetailDto> wrapper = new ResultWrapper<>();
    DataSetsDetailDto dto = apiService.findByPK(uuid);
    return wrapper.success(dto);
  }

  /**
   * Restful接口-delete： 删除数据集
   *
   * @param uuid       主键
   * @return 删除的记录数量
   */
  @ApiOperation(value = "删除：数据集")
  @RequiredPermissions("ui-deleteDataSet")
  @DeleteMapping(
      value = "/{uuid}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> delete(
      @PathVariable("uuid") String uuid)
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.deleteByPK(uuid);
    return wrapper.success(count);
  }

  /**
   * Restful接口-post： 批量删除数据集
   *
   * @param dto
   * @return 成功的数量
   */
  @ApiOperation(value = "批量删除：数据集")
  @RequiredPermissions("ui-deleteDataSetList")
  @PostMapping(
          value = "/deleteDataSets",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> deleteBatch(
          @RequestBody @Validated DataSetsDeleteDto dto)
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = this.apiService.deleteBatch(dto);
    return wrapper.success(count);
  }

  /**
   * 分页查询 数据集 列表
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：数据集")
  @RequiredPermissions("staticData")
  @PostMapping(value = "/paging/query/DataSets",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<DataSetsDetailDto>> pagingQuery(
          @RequestBody @Validated QueryByPage<DataSetsQueryDto> queryByPage)
  {
    ResultWrapper<Pagination<DataSetsDetailDto>> wrapper = new ResultWrapper<>();
    Pagination<DataSetsDetailDto> page = apiService.pagingQuery(queryByPage);
    return wrapper.success(page);
  }

  @ApiOperation(value = "保存数据集数据")
  @PostMapping(value = "/save",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Boolean> save(@RequestBody @Validated DataSetsSaveDto dto)
  {
    ResultWrapper<Boolean> wrapper = new ResultWrapper<>();
    Boolean count = this.apiService.save(dto);
    return wrapper.success(count);
  }

  @ApiOperation(value = "新增列校验数据集表头")
  @PostMapping(value = "/checkTableHeader",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Boolean> checkTableHeader(@RequestBody @Validated ColumnCheckDto dto)
  {
    ResultWrapper<Boolean> wrapper = new ResultWrapper<>();
    Boolean count = this.apiService.checkTableHeader(dto);
    return wrapper.success(count);
  }

  @ApiOperation(value = "导出Excel：数据集")
  @GetMapping(value = "/export/{dataSetId}",
          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void export(@PathVariable("dataSetId") String dataSetId, HttpServletResponse response)
  {
    // 定义导出的Excel在客户端浏览器上提示下载的文件名，不写后缀
    logger.debug("start exportDataSet");
    String fileName = "数据集";
    apiService.exportDataSet(response, dataSetId, fileName);
    logger.debug("end exportDataSet");
  }



  @ApiOperation(value = "下载Excel模板：数据集")
  @PostMapping(value = "/downloadTemplate",
          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void downloadTemplate(@RequestBody DataSetsSaveDto dto,
                               HttpServletResponse response)
  {
    logger.debug("start downloadTemplate");
    String fileName = "数据集模版";
    apiService.downloadTemplate(response, fileName, dto);
    logger.debug("end downloadTemplate");
  }

}
