using Attribute;
using Model;
using Model.Base;
using Repository;
using Service;
using Microsoft.AspNetCore.Mvc;
using Vo.Admin;
using Common;
using Custom;
using Tasks;
using Infrastructure;
using Dto.Admin;
using Feign;
using Util;
using LitJson;
namespace Services
{
///
/// 接口分组Service业务层处理
///
[AppService(ServiceType = typeof(IApiGroupService), ServiceLifetime = LifeTime.Transient)]
public class ApiGroupService : BaseService, IApiGroupService
{
private readonly IApiInfoService _ApiInfoService;
public ApiGroupService(IApiInfoService ApiInfoService)
{
_ApiInfoService = ApiInfoService;
}
///
/// 接口分组-列表
///
/// 参数请求体
/// 分页参数
/// 列表
public PagedInfo getApiGroupList([FromQuery] PagerInfo page, [FromQuery] ApiGroup param)
{
//拼装查询条件
var predicate = Expressionable.Create();
predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId);
predicate = predicate.AndIF(!string.IsNullOrEmpty(param.groupName), m => m.groupName.Contains(param.groupName));
predicate = predicate.AndIF(param.appId > 0, m => m.appId == param.appId);
var response = Queryable()
.Where(predicate.ToExpression())
.OrderByDescending(m => m.id)
.ToPage(page);
return response;
}
///
/// 生成api文档
///
/// 服务id
public MakeApiDocVo makeApiDoc(MakeApiDocDto param, TokenModel loginUser)
{
var service = Feign.IProject.getProjectService(param.serviceId);
var apiGroups = GetList(m => m.serviceId == param.serviceId);
var apiInfos = _ApiInfoService.GetList(m => m.serviceId == param.serviceId);
if(param.groupId > 0)
{
apiGroups = apiGroups.Where(m => m.id == param.groupId).ToList();
apiInfos = apiInfos.Where(m => m.groupId == param.groupId).ToList();
}
foreach(var apiInfo in apiInfos)
{
apiInfo.groupTitle = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupTitle;
apiInfo.groupName = apiGroups.FirstOrDefault(m => m.id == apiInfo.groupId).groupName;
}
string id = service.id.ToString() + param.groupId.ToString();
Dictionary pushData = new Dictionary();
pushData.Add("serviceName", service.serviceName);
pushData.Add("serviceTitle", service.serviceTitle);
pushData.Add("apiDocId", id);
pushData.Add("apiGroup", apiGroups);
pushData.Add("apiInfo", apiInfos);
MakeHelper.Push(pushData, "apifoxDoc", "apidoc", loginUser, id);
var options = App.OptionsSetting;
return new MakeApiDocVo()
{
docUrl = options.BaseConfigs.gatewayHost + "/v1/omega_make/StaticFile/apiDocQuery?id=" + id + "&fileType=apidoc"
};
}
///
/// 同步api到管理平台
///
/// 服务id
public string sycnApiToSystem(SycnApiToSystemDto param)
{
var apiGroupService = App.GetService();
var apiInfoService = App.GetService();
var tableService = App.GetService();
var project = IProject.getProject(param.projectId);
string ApiListHost = "";
string Hosts = "";
if(param.env == "test")
{
ApiListHost = project.apiListNoticeHostDev ?? "";
Hosts = project.gatewayHostDev ?? "";
}
else
{
ApiListHost = project.apiListNoticeHost ?? "";
Hosts = project.gatewayHost ?? "";
}
var projectService = IProject.getProjectService(param.serviceId);
List checkStrs = new List();
List> dic = new List>();
string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
int Id = 0;
var projectServiceList = IProject.getProjectServiceList(param.projectId);
foreach(var proService in projectServiceList)
{
Id += 1;
int serId = proService.id;
string serviceTitle = proService.serviceTitle ?? "";
string serviceName = proService.serviceName ?? "";
string Language = proService.devLanguage ?? "";
string[] HostList = Hosts.Split(':');
string Host = HostList[0];
string Port = "80";
if(HostList.Length > 1)
{
Port = HostList[1];
}
List> child = new List>();
var apiInfos = apiInfoService.GetList(m => m.serviceId == serId);
if(param.serviceId > 0)
{
apiInfos = apiInfos.Where(m => m.serviceId == param.serviceId).ToList();
}
if(param.groupId > 0)
{
apiInfos = apiInfos.Where(m => m.groupId == param.groupId).ToList();
}
foreach(var apiInfo in apiInfos)
{
var linkTable = tableService.GetFirst(m => m.id == apiInfo.linkTableId) ?? new DatabaseTable();
string Gategory = apiInfo.controllerName ?? "";
string Router = "";
var group = apiGroupService.GetList(m => m.id == apiInfo.groupId);
if (group.Count > 0)
{
Router = group[0].groupName ?? "";
}
string MethodName = apiInfo.methodName ?? "";
string TabName = apiInfo.tabName ?? "";
string Title = apiInfo.apiName ?? "";
string ApiType = apiInfo.apiKind.ToString();
string Method = "get";
string ApiKey = "";
if(string.IsNullOrEmpty(ApiKey))
{
ApiKey = Gategory + MethodName;
}
if(ApiType == "3") Method = "post";
if(ApiType == "4") Method = "put";
if(ApiType == "5") Method = "delete";
Dictionary subItem = new Dictionary();
string ApiRouter = "/v1/" + Router + "/" + Gategory + "/" + MethodName;
subItem.Add("api_router", ApiRouter);
subItem.Add("api_port", Port);
if(Port == "443")
{
subItem.Add("api_host", "https://" + Host);
}
else
{
subItem.Add("api_host", "http://" + Host);
}
subItem.Add("api_key", ApiKey);
subItem.Add("api_name", TabName + "-" + Title);
subItem.Add("api_method", Method);
subItem.Add("api_path", "/" + Gategory + "/" + linkTable.tableNameUpper + "/index");
subItem.Add("api_en_name", linkTable.tableNameUpper ?? "");
child.Add(subItem);
}
Dictionary item = new Dictionary();
item.Add("id", Id);
item.Add("group_project", param.projectId);
item.Add("group_kind", 1);
item.Add("group_remark", proService.serviceTitle ?? "");
item.Add("group_name", PublicFunction.transferName(proService.serviceName ?? "", 2));
item.Add("child", child);
dic.Add(item);
}
string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
string result = "";
if(project.encryptKind == "AES")
{
JsonData encryptParam = JsonMapper.ToObject(project.encryptParam ?? "{}");
string reqData = CryptHelper.AesEncrypt(req, encryptParam["encrypt_key"].ToString(), encryptParam["encrypt_iv"].ToString(), encryptParam["encrypt_model"].ToString(), encryptParam["encrypt_padding"].ToString());
result = Function.PostWebRequest(ApiListHost, reqData, "application/json");
JsonData jsonObj = JsonMapper.ToObject(result);
if(jsonObj["status"].ToString() == "1")
{
return "同步成功";
}
}
else
{
result = Function.PostWebRequest(ApiListHost, "value=" + req);
if(result == "success")
{
return "同步成功";
}
}
return "同步失败";
}
}
}