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

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.dataPool.api.dataSets.service.DataSetTreeApiService;
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 java.util.List;

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

  @Autowired
  private DataSetTreeApiService apiService;

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

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

  /**
   * Restful接口-delete： 删除数据集树目录表
   *
   * @param uuid       主键
   * @return 删除的记录数量
   */
  @ApiOperation(value = "删除：数据集树目录表")
  @RequiredPermissions("ui-deleteDataSetTree")
  @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);
  }

  @ApiOperation(value = "根据层级查询数据集目录节点")
  @PostMapping(
          value = "/queryDataSetTreeByParentId",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<DataSetTreeDetailDto>> queryDataSetTreeByParentId(
          @RequestBody @Validated DataSetTreeQueryDto dto)
  {
    logger.debug("start queryDataSetTreeByParentId, param:{}", JSONObject.toJSONString(dto));
    ResultWrapper<List<DataSetTreeDetailDto>> wrapper = new ResultWrapper<>();
    List<DataSetTreeDetailDto> res = apiService.queryDataSetTreeByParentId(dto);
    logger.debug("end queryDataSetTreeByParentId");
    return wrapper.success(res);
  }

  @ApiOperation(value = "根据名字模糊查询数据集目录节点")
  @PostMapping(
          value = "/queryDataSetTreeByName",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<DataSetTreeDetailDto>> queryDataSetTreeByName(
          @RequestBody @Validated DataSetTreeQueryDto dto)
  {
    logger.debug("start queryDataSetTreeByName, param:{}", JSONObject.toJSONString(dto));
    ResultWrapper<List<DataSetTreeDetailDto>> wrapper = new ResultWrapper<>();
    List<DataSetTreeDetailDto> res = apiService.queryDataSetTreeByName(dto);
    logger.debug("end queryDataSetTreeByName");
    return wrapper.success(res);
  }

  @ApiOperation(value = "拖拽树目录")
  @PostMapping(
          value = "/moveDataSetTree",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> moveDataSetTree(
          @RequestBody @Validated DataSetTreeMoveDto dto)
  {
    logger.debug("start moveDataSetTree, param:{}", JSONObject.toJSONString(dto));
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer num = apiService.moveDataSetTree(dto);
    logger.debug("end moveDataSetTree");
    return wrapper.success(num);
  }
}
