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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;

import net.northking.cctp.project.dto.env.*;
import net.northking.cctp.project.api.env.service.CpProjectEnvSysApiService;
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/CpProjectEnvSyss")
@RestController
public class CpProjectEnvSysRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(CpProjectEnvSysRestfulCtrl.class);

  @Autowired
  private CpProjectEnvSysApiService apiService;

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

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

  /**
   * Restful接口-delete： 删除系统环境被测系统信息
   *
   * @param uuid       主键
   * @return 删除的记录数量
   */
  @ApiOperation(value = "删除：系统环境被测系统信息")
  @RequiredPermissions("ui-deleteEnvSyss")
  @DeleteMapping(
      value = "/{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);
  }

  /**
   * 系统环境数据源信息下拉选项
   * @param projectId 系统id
   * @param envId 环境id
   * @return 选项集
   */
  @ApiOperation(value = "获取：系统环境数据源信息下拉选项")
  @GetMapping("/select")
  public ResultWrapper<List<CpProjectEnvSysSelectDto>> findSelect(@RequestParam String projectId,
                                                                  @RequestParam String envId){

    logger.info("start CpProjectEnvSys findSelect  projectId => {}，envId => {}", projectId, envId);

    ResultWrapper<List<CpProjectEnvSysSelectDto>> wrapper = new ResultWrapper<>();
    List<CpProjectEnvSysSelectDto> resultList = apiService.findSelect(projectId, envId);

    logger.info("end CpProjectEnvSys findSelect  ");

    return wrapper.success(resultList);
  }
}
