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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.project.api.env.service.CpProjectEnvDsApiService;
import net.northking.cctp.project.dto.env.*;
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;
import java.util.Map;

/**
 * 系统环境数据源信息 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "系统环境数据源信息")
@RequestMapping("/v1/CpProjectEnvDss")
@RestController
public class CpProjectEnvDsRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(CpProjectEnvDsRestfulCtrl.class);

  @Autowired
  private CpProjectEnvDsApiService apiService;

  /**
   * Restful接口-post： 新增系统环境数据源信息
   *
   * @param inputDto         接口数据传输对象
   * @return 数据库主键
   */
  @ApiOperation(value = "新增：系统环境数据源信息")
  @RequiredPermissions("ui-saveEnvDss")
  @PostMapping(
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> add(
      @RequestBody @Validated CpProjectEnvDsAddDto inputDto)
  {
    logger.info("start CpProjectEnvDs add  params => {}", JSON.toJSONString(inputDto));

    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String uuid = apiService.add(inputDto);

    logger.info("end CpProjectEnvDs add  ");

    return wrapper.success(uuid);
  }

  /**
   * Restful接口-put： 更新系统环境数据源信息
   *
   * @param inputDto         接口数据传输对象
   * @return 修改的记录数量
   */
  @ApiOperation(value = "更新：系统环境数据源信息")
  @RequiredPermissions("ui-updateEnvDss")
  @PutMapping(
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> update(
      @RequestBody @Validated CpProjectEnvDsUpdateDto inputDto) throws IllegalAccessException
  {

    logger.info("start CpProjectEnvDs update  params => {}", JSON.toJSONString(inputDto));

    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.updateByPK(inputDto);

    logger.info("end CpProjectEnvDs update  ");

    return wrapper.success(count);
  }

  /**
   * Restful接口-get： 获取系统环境数据源信息
   *
   * @param uuid       数据库主键
   * @return 接口数据传输对象
   */
  @ApiOperation(value = "获取：系统环境数据源信息")
  @GetMapping(
      value = "/{uuid}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<CpProjectEnvDsDetailDto> findByPK(
      @PathVariable("uuid") String uuid)
  {
    logger.info("start CpProjectEnvDs findByPK  uuid => {}", uuid);

    ResultWrapper<CpProjectEnvDsDetailDto> wrapper = new ResultWrapper<>();
    CpProjectEnvDsDetailDto user = apiService.findByPK(uuid);

    logger.info("end CpProjectEnvDs findByPK  ");

    return wrapper.success(user);
  }

  /**
   * Restful接口-delete： 删除系统环境数据源信息
   *
   * @param uuid       数据库主键
   * @return 删除的记录数量
   */
  @ApiOperation(value = "删除：系统环境数据源信息")
  @RequiredPermissions("ui-deleteEnvDss")
  @DeleteMapping(
      value = "/{uuid}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> deleteByPK(
      @PathVariable("uuid") String uuid)
  {
    logger.info("start CpProjectEnvDs deleteByPK  uuid => {}", uuid);

    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.deleteByPK(uuid);

    logger.info("end CpProjectEnvDs deleteByPK  ");

    return wrapper.success(count);
  }

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

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

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

    logger.info("end CpProjectEnvDs findSelect  ");

    return wrapper.success(resultList);
  }

  /**
   * 测试连接
   * @param connDto 连接参数
   * @return 结果
   */
  @ApiOperation(value = "测试连接")
  @PostMapping("/testConnection")
  public ResultWrapper<Boolean> checkConnection(@Validated @RequestBody CpProjectEnvDsConnDto connDto){

    logger.info("start CpProjectEnvDs checkConnection  params => {}", JSON.toJSONString(connDto));

    ResultWrapper<Boolean> wrapper = new ResultWrapper<>();
    Boolean flag = apiService.checkConnection(connDto);

    logger.info("end CpProjectEnvDs checkConnection  ");

    return wrapper.success(flag);
  }

  /**
   * 根据id测试连接
   * @param uuid 数据源id
   * @return 结果
   */
  @ApiOperation(value = "根据数据源id测试连接")
  @GetMapping("/testConnection")
  public ResultWrapper<Boolean> checkConnectionById(@RequestParam String uuid){

    logger.info("start CpProjectEnvDs checkConnectionById  uuid => {}", uuid);

    ResultWrapper<Boolean> wrapper = new ResultWrapper<>();
    Boolean flag = apiService.checkConnectionById(uuid);

    logger.info("end CpProjectEnvDs checkConnectionById  ");

    return wrapper.success(flag);
  }

  /**
   * sql执行
   * @param executeDto 执行参数
   * @return 结果集
   */
  @ApiOperation(value = "sql执行")
  @PostMapping("/execute")
  public ResultWrapper<Map<String, Object>> executeSql(@Validated @RequestBody CpProjectEnvDsExecuteDto executeDto){

    logger.info("start CpProjectEnvDs executeSql  params => {}", JSON.toJSONString(executeDto));

    ResultWrapper<Map<String, Object>> wrapper = new ResultWrapper<>();
    Map<String, Object> resultMap = apiService.executeSql(executeDto);

    logger.info("end CpProjectEnvDs executeSql  ");

    return wrapper.success(resultMap);
  }

}
