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

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.elementLibrary.service.AtuElementGroupApiService;
import net.northking.cctp.scriptcase.dto.elementLibrary.*;
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/AtuElementGroups")
@RestController
public class AtuElementGroupRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AtuElementGroupRestfulCtrl.class);

  @Autowired
  private AtuElementGroupApiService apiService;

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

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

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

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

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

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