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

import com.alibaba.fastjson.JSON;
import cn.gjing.tools.auth.annotation.RequiredPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.dataPool.dto.dataRule.*;
import net.northking.cctp.dataPool.api.dataRule.service.DataRuleApiService;
import net.northking.cctp.dataPool.dto.dataSets.AtuDataSetFeignDto;
import net.northking.cctp.dataPool.dto.dataSets.AtuScriptInputSetQuoteFieldDto;
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/DataRules")
@RestController
public class DataRuleRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(DataRuleRestfulCtrl.class);

  @Autowired
  private DataRuleApiService apiService;

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

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

  /**
   * Restful接口-delete： 删除数据规则表
   *
   * @param uuid       主键
   * @return 删除的记录数量
   */
  @ApiOperation(value = "删除：数据规则表")
  @RequiredPermissions("ui-deleteDataRules")
  @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 queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：数据规则表")
  @RequiredPermissions("testData")
  @PostMapping(value = "/paging/query/DataRules",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<DataRuleDetailDto>> pagingQuery(
          @RequestBody @Validated QueryByPage<DataRuleQueryDto> queryByPage)
  {
    ResultWrapper<Pagination<DataRuleDetailDto>> wrapper = new ResultWrapper<>();
    Pagination<DataRuleDetailDto> page = apiService.pagingQuery(queryByPage);
    return wrapper.success(page);
  }

  @ApiOperation("测试规则")
  @PostMapping("/executeRule")
  public ResultWrapper<List<String>> executeRule(@RequestBody DataRuleExecuteDto dto){
    ResultWrapper<List<String>> wrapper = new ResultWrapper<>();
    List<String> result = apiService.executeRule(dto);
    return wrapper.success(result);
  }


  @ApiOperation(value = "查询模糊数据集名称集合")
  @PostMapping(value = "/query/queryFuzzyDataNameMap",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public Map<String, String> queryFuzzyDataNameMap(@RequestBody AtuDataSetFeignDto setFeignDto){
    return apiService.queryFuzzyDataNameMap(setFeignDto);
  }

  @ApiOperation(value = "查询模糊数据引用信息")
  @PostMapping(value = "/query/queryFuzzyDataQuoteInfo")
  public ResultWrapper<AtuScriptInputSetQuoteFieldDto> queryFuzzyDataQuoteInfo(@RequestBody AtuScriptInputSetQuoteFieldDto atuScriptInputSetQuoteFieldDto){
    logger.debug(" start queryFuzzyDataQuoteInfo  parmas => {}", JSON.toJSONString(atuScriptInputSetQuoteFieldDto));

    ResultWrapper<AtuScriptInputSetQuoteFieldDto> wrapper = new ResultWrapper<>();
    AtuScriptInputSetQuoteFieldDto result = apiService.queryFuzzyDataQuoteInfo(atuScriptInputSetQuoteFieldDto);

    logger.debug(" end queryFuzzyDataQuoteInfo  result => {}", JSON.toJSONString(result));

    return wrapper.success(result);
  }

}
