using Vo;
using Microsoft.AspNetCore.Mvc;
using Model;
using Services;
using Model.Base;
using Vo.Admin;
using Mapster;
using Infrastructure;
using Dto.Admin;
using Common;
using Util;
using Custom;
using Nacos.V2.Utils;
using LitJson;
namespace Controllers.Admin
{
///
/// ApiInfo)Controller
///
// [Route("${Router}$")]
// [ApiExplorerSettings(GroupName = "ApiInfo")]
public class ApiInfoController : BaseController
{
///
/// api分组接口
///
private readonly IApiInfoService _ApiInfoService;
private readonly IApiGroupService _ApiGroupService;
private readonly IApiControllerService _ApiControllerService;
private readonly IDatabaseFieldService _DatabaseFieldService;
public ApiInfoController(IApiInfoService ApiInfoService, IApiGroupService ApiGroupService, IApiControllerService ApiControllerService, IDatabaseFieldService DatabaseFieldService)
{
_ApiInfoService = ApiInfoService;
_ApiGroupService = ApiGroupService;
_ApiControllerService = ApiControllerService;
_DatabaseFieldService = DatabaseFieldService;
}
/// 分页参数
///
/// 列表
///
/// 分页对象
/// 参数请求体
/// 列表
[HttpGet]
[Route("/v1/omega_source/ApiInfo/getApiInfoList")]
public IActionResult getApiInfoList([FromQuery] PagerInfo page, [FromQuery] ApiInfo param)
{
var response = _ApiInfoService.getApiInfoList(page, param);
return SUCCESS(response);
}
///
/// 字段关联接口列表
///
/// 参数请求体
/// 字段关联接口列表
[HttpGet]
[Route("/v1/omega_source/ApiInfo/getApiInfoListByProId")]
public IActionResult getApiInfoListByProId([FromQuery] PagerInfo page, [FromQuery] ApiInfo param)
{
var response = _ApiInfoService.getApiInfoListByProId(page, param);
return SUCCESS(response);
}
///
/// 详情
///
/// 参数请求体
/// 详情
[HttpGet]
[Route("/v1/omega_source/ApiInfo/getApiInfoQuery")]
public IActionResult getApiInfoQuery([FromQuery] ApiInfo param)
{
var response = _ApiInfoService.GetFirst(m => m.id == param.id).Adapt();
int tableId = response.linkTableId;
var fieldNames = response.apiParam.Select(m => m.fieldName).ToList();
var databaseFields = _DatabaseFieldService.GetList(m => m.tableId == tableId && !fieldNames.Contains(m.fieldName));
foreach(var item in databaseFields)
{
response.apiParam.Add(new ApiParamObject()
{
fieldName = item.fieldName,
fieldTitle = item.fieldTitle,
fieldType = item.fieldType,
editType = _ApiInfoService.getEditType(item.fieldTitle, item.fieldType).name
});
}
foreach(var item in response.apiParam)
{
tableId = item.linkTableId;
if(tableId > 0)
{
fieldNames = item.linkParam.Select(m => m.fieldName).ToList();
databaseFields = _DatabaseFieldService.GetList(m => m.tableId == tableId && !fieldNames.Contains(m.fieldName));
foreach(var field in databaseFields)
{
item.linkParam.Add(new ApiParamObject()
{
fieldName = field.fieldName,
fieldTitle = field.fieldTitle,
fieldType = field.fieldType,
editType = _ApiInfoService.getEditType(field.fieldTitle, field.fieldType).name
});
}
}
}
return SUCCESS(response);
}
///
/// 添加
///
/// 参数请求体
/// 添加
[HttpPost]
[Route("/v1/omega_source/ApiInfo/addApiInfo")]
public IActionResult addApiInfo([FromBody] AddApiInfoDto param)
{
var exists = _ApiInfoService.Any(m => m.apiName == param.apiName && m.tabName == param.tabName && m.methodName == param.methodName);
if (exists)
{
return ToResponse(ResultCode.FAIL, "请勿重复创建相同接口");
}
var modal = param.Adapt().ToCreate(HttpContext);
var check = _ApiControllerService.Any(m => m.controllerFilePath == param.controllerFilePath);
if(!check)
{
_ApiControllerService.Add(new ApiController()
{
controllerFilePath = param.controllerFilePath,
serviceId = param.serviceId
});
}
foreach(var item in modal.apiParam)
{
var linkTablePrimaryKey = (_DatabaseFieldService.GetFirst(m => m.tableId == item.linkTableId && m.primaryKey) ?? _DatabaseFieldService.GetFirst(m => m.tableId == item.linkTableId)) ?? new DatabaseField();
item.linkTablePrimaryKeyName = PublicFunction.transferName(linkTablePrimaryKey.fieldName, 1);
item.editType = _ApiInfoService.getEditType(item.fieldTitle, item.fieldType).name;
}
var response = _ApiInfoService.InsertReturnIdentity(modal);
return SUCCESS(response);
}
///
/// 修改
///
/// 参数请求体
/// 修改
[HttpPut]
[Route("/v1/omega_source/ApiInfo/updateApiInfo")]
public IActionResult updateApiInfo([FromBody] AddApiInfoDto param)
{
var modal = param.Adapt().ToUpdate(HttpContext);
var check = _ApiControllerService.Any(m => m.controllerFilePath == param.controllerFilePath);
if(!check)
{
_ApiControllerService.Add(new ApiController()
{
controllerFilePath = param.controllerFilePath,
serviceId = param.serviceId
});
}
foreach(var item in modal.apiParam)
{
var linkTablePrimaryKey = (_DatabaseFieldService.GetFirst(m => m.tableId == item.linkTableId && m.primaryKey) ?? _DatabaseFieldService.GetFirst(m => m.tableId == item.linkTableId)) ?? new DatabaseField();
item.linkTablePrimaryKeyName = PublicFunction.transferName(linkTablePrimaryKey.fieldName, 1);
string newEditType = _ApiInfoService.getEditType(item.fieldTitle, item.fieldType).name;
if(string.IsNullOrEmpty(item.editType))
{
item.editType = newEditType;
}
}
var response = _ApiInfoService.Update(modal, true);
return SUCCESS(response);
}
///
/// 前端接口设置
///
/// 参数请求体
/// 前端接口设置
[HttpPut]
[Route("/v1/omega_source/ApiInfo/updateApiInfoForFrontend")]
public IActionResult updateApiInfoForFrontend([FromBody] AddApiInfoDto param)
{
var modal = param.Adapt().ToUpdate(HttpContext);
foreach(var item in modal.apiParam)
{
var linkTablePrimaryKey = (_DatabaseFieldService.GetFirst(m => m.tableId == item.linkTableId && m.primaryKey) ?? _DatabaseFieldService.GetFirst(m => m.tableId == item.linkTableId)) ?? new DatabaseField();
item.linkTablePrimaryKeyName = PublicFunction.transferName(linkTablePrimaryKey.fieldName, 1);
string newEditType = _ApiInfoService.getEditType(item.fieldTitle, item.fieldType).name;
if(string.IsNullOrEmpty(item.editType))
{
item.editType = newEditType;
}
}
var editItem = _ApiInfoService.GetFirst(m => m.id == modal.id);
if(editItem != null)
{
editItem.apiParam = modal.apiParam;
_ApiInfoService.Update(editItem);
}
return SUCCESS("ok");
}
///
/// 删除
///
/// ID
/// 删除
[HttpDelete]
[Route("/v1/omega_source/ApiInfo/deleteApiInfo/{id}")]
public IActionResult deleteApiInfo(int id)
{
var response = _ApiInfoService.Delete(id);
return SUCCESS(response);
}
///
/// 批量删除
///
/// 参数请求体
/// 批量删除
[HttpPut]
[Route("/v1/omega_source/ApiInfo/batchDeleteApiInfo")]
public IActionResult batchDeleteApiInfo([FromBody] BatchDeleteApiInfoDto param)
{
foreach(var id in param.idList)
{
_ApiInfoService.Delete(id);
}
return SUCCESS("ok");
}
///
/// 克隆接口
///
/// 参数请求体
[HttpPost]
[Route("/v1/omega_source/ApiInfo/copyApiInfo")]
public IActionResult copyApiInfo([FromBody] CopyApiInfoDto param)
{
var group = _ApiGroupService.GetFirst(m => m.id == param.targetApiGroupId) ?? new ApiGroup();
_ApiInfoService.copyApiInfo(param, group);
return SUCCESS("ok");
}
///
/// 初始化接口
///
/// 初始化接口
[HttpPost]
[Route("/v1/omega_source/ApiInfo/initApiInfo")]
public IActionResult initApiInfo([FromBody] InitApiInfoDto param)
{
_ApiInfoService.initApiInfo(param);
return SUCCESS("ok");
}
///
/// 获取前端生成数据
///
/// 参数请求体
/// 获取前端生成数据
[HttpPost]
[Route("/v1/omega_source/ApiInfo/downloadJson")]
public IActionResult downloadJson([FromBody] DownloadJsonDto param)
{
List idList = Tools.SpitIntArrary(param.apiId).ToList();
var editTypes = _ApiInfoService.getEditTypes();
var list = _ApiInfoService.GetList(m => idList.Contains(m.id));
foreach(ApiInfo item in list)
{
foreach(var paramItem in item.apiParam)
{
paramItem.editTypeData = editTypes.FirstOrDefault(m => m.name == paramItem.editType) ?? new EditTypeFrontend();
}
}
return SUCCESS(list);
}
///
/// 测试接口
///
/// 测试接口
[HttpPost]
[Route("/v1/omega_source/ApiInfo/testApiInfo")]
public IActionResult testApiInfo([FromBody] TestApiInfoDto param)
{
var groups = _ApiGroupService.GetList();
var result = _ApiInfoService.testApiInfo(param, groups);
return SUCCESS(result);
}
}
}