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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.hzbank.testteam.autotest.dependencies.authDependency.constants.AuthDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.constants.BaseDefineDependencyConstants;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.dto.AdvancedQueryOptions;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.scriptcase.api.elementLibrary.service.AtuElementInfoApiService;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 元素信息管理 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "元素信息管理")
@RequestMapping("/v1/AtuElementInfos")
@RestController
public class AtuElementInfoRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(AtuElementInfoRestfulCtrl.class);

  @Autowired
  private AtuElementInfoApiService apiService;

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

  /**
   * Restful接口-put： 更新元素信息管理
   *
   * @param inputDto         接口数据传输对象
   * @return 修改的记录数量
   */
  @RequiredPermissions("ui-updateElement")
  @ApiOperation(value = "更新：元素信息管理")
  @PutMapping(value = "/updateElement",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> update(
      @RequestBody @Validated AtuElementInfoUpdateDto 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 = "/{uuid}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<AtuElementInfoDetailDto> findByPK(
      @PathVariable("uuid") String uuid)
  {
    ResultWrapper<AtuElementInfoDetailDto> wrapper = new ResultWrapper<>();
    AtuElementInfoDetailDto dto = apiService.findByPK(uuid);
    return wrapper.success(dto);
  }

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

  /**
   * Restful接口-post： 批量删除元素信息管理
   *
   * @return 成功的数量
   */
  @RequiredPermissions("ui-deleteElementBatch")
  @ApiOperation(value = "批量删除：元素信息管理")
  @PostMapping(
          value = "/deleteBatch",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> deleteBatch(
          @RequestBody @Validated AtuElementInfoDelDto dto)
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.deleteBatch(dto);
    return wrapper.success(count);
  }

  /**
   * 分页查询 元素信息管理 列表
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：元素信息管理")
  @RequiredPermissions("elementLibrary")
  @PostMapping(value = "/paging/query/AtuElementInfos",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<AtuElementInfoDetailDto>> pagingQuery(
          @RequestBody @Validated QueryByPage<AtuElementInfoQueryDto> queryByPage)
  {
    ResultWrapper<Pagination<AtuElementInfoDetailDto>> wrapper = new ResultWrapper<>();
    Pagination<AtuElementInfoDetailDto> page = apiService.pagingQuery(queryByPage);

    return wrapper.success(page);
  }

  @ApiOperation(value = "移动元素")
  @PostMapping(value = "/moveElement",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> moveElement(@RequestBody @Validated AtuElementInfoMoveDto inputDto)
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.moveElement(inputDto);
    return wrapper.success(count);
  }

  @ApiOperation(value = "图像识别上传图片")
  @PostMapping(value = "/uploadImage",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<String> uploadImage(@RequestParam("file") MultipartFile file)
  {
    logger.debug("start uploadImage");
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String url = apiService.uploadImage(file);
    logger.debug("end uploadImage");
    return wrapper.success(url);
  }

  @ApiOperation(value = "图像识别展开图片")
  @GetMapping(value = "/downloadImage/{url}")
  public void downloadImage(@PathVariable("url") String url, HttpServletResponse response)
  {
    logger.debug("start downloadImage");
    apiService.downloadImage(response, url);
    logger.debug("end downloadImage");
  }

    @ApiOperation(value = "获取高级查询相关配置数据:创建人，更新人，责任人")
    @GetMapping(value = "/advancedQueryOptions")
    public ResponseVO<AdvancedQueryOptions> advancedQueryOptions(
            @ApiParam(value = "系统ID", required = true, example = "36602691803153920")
            @RequestHeader(AuthDependencyConstants.REQUEST_HEADER_PROJECT_ID) String projectId) {
      logger.debug("start advancedQueryOptions");
      ResponseVO<AdvancedQueryOptions> responseVO = new ResponseVO<>();
      AdvancedQueryOptions result = apiService.advancedQueryOptions(projectId);
      responseVO.setCode(BaseDefineDependencyConstants.SYSTEM_RESPONSE_CODE_SUCCESS);
      responseVO.setData(result);
      responseVO.setMessage("获取成功");
      logger.debug("end advancedQueryOptions");
      return responseVO;
    }
}
