ApiInfoService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Attribute;
  2. using Model;
  3. using Model.Base;
  4. using Repository;
  5. using Service;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Vo.Admin;
  8. using Dto.Admin;
  9. using Infrastructure;
  10. namespace Services
  11. {
  12. /// <summary>
  13. /// 接口信息Service业务层处理
  14. /// </summary>
  15. [AppService(ServiceType = typeof(IApiInfoService), ServiceLifetime = LifeTime.Transient)]
  16. public class ApiInfoService : BaseService<ApiInfo>, IApiInfoService
  17. {
  18. /// <summary>
  19. /// 接口信息-列表
  20. /// </summary>
  21. /// <param name="param">参数请求体</param>
  22. /// <param name="page">分页参数</param>
  23. /// <returns>列表</returns>
  24. public PagedInfo<GetApiInfoListVo> getApiInfoList([FromQuery] PagerInfo page, [FromQuery] ApiInfo param)
  25. {
  26. //拼装查询条件
  27. var predicate = Expressionable.Create<ApiInfo>();
  28. predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId);
  29. predicate = predicate.AndIF(param.groupId > 0, m => m.groupId == param.groupId);
  30. predicate = predicate.AndIF(!string.IsNullOrEmpty(param.tabName), m => m.tabName.Contains(param.tabName));
  31. predicate = predicate.AndIF(!string.IsNullOrEmpty(param.apiName), m => m.apiName.Contains(param.apiName));
  32. predicate = predicate.AndIF(param.apiKind > 0, m => m.apiKind == param.apiKind);
  33. var response = Queryable()
  34. .Where(predicate.ToExpression())
  35. .OrderByDescending(m => m.id)
  36. .ToPage<ApiInfo, GetApiInfoListVo>(page);
  37. return response;
  38. }
  39. /// <summary>
  40. /// 接口信息-字段关联接口列表
  41. /// </summary>
  42. /// <param name="param">参数请求体</param>
  43. /// <param name="page">分页参数</param>
  44. /// <returns>字段关联接口列表</returns>
  45. public PagedInfo<GetApiInfoListByProIdVo> getApiInfoListByProId([FromQuery] PagerInfo page, [FromQuery] ApiInfo param)
  46. {
  47. //拼装查询条件
  48. var predicate = Expressionable.Create<ApiInfo>();
  49. predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId);
  50. var response = Queryable()
  51. .Where(predicate.ToExpression())
  52. .OrderByDescending(m => m.id)
  53. .ToPage<ApiInfo, GetApiInfoListByProIdVo>(page);
  54. return response;
  55. }
  56. /// <summary>
  57. /// 克隆接口
  58. /// </summary>
  59. /// <param name="param">参数请求体</param>
  60. public void copyApiInfo([FromBody] CopyApiInfoDto param, ApiGroup group)
  61. {
  62. // var group = _ApiGroupService.GetFirst(m => m.id == param.targetGroupId) ?? new ApiGroup();
  63. List<int> apiIdList = param.sourceApiId.ToList();
  64. var sourceApis = GetList(m => apiIdList.Contains(m.id)).ToList();
  65. foreach(ApiInfo sourceApi in sourceApis)
  66. {
  67. ApiInfo newApi = new ApiInfo();
  68. newApi.serviceId = group.serviceId;
  69. newApi.groupId = param.targetGroupId;
  70. newApi.tabName = sourceApi.tabName;
  71. newApi.apiName = sourceApi.apiName;
  72. newApi.controllerFilePath = sourceApi.controllerFilePath;
  73. newApi.usageScenarios = sourceApi.usageScenarios;
  74. newApi.apiKind = sourceApi.apiKind;
  75. newApi.methodName = sourceApi.methodName;
  76. newApi.apiDetail = sourceApi.apiDetail;
  77. newApi.apiParam = sourceApi.apiParam;
  78. newApi.createTime = DateTime.Now;
  79. Add(newApi);
  80. }
  81. }
  82. /// <summary>
  83. /// 初始化接口
  84. /// </summary>
  85. public void initApiInfo([FromBody] InitApiInfoDto param)
  86. {
  87. var _DatabaseInfoService = App.GetService<IDatabaseInfoService>();
  88. var _DatabaseTableService = App.GetService<IDatabaseTableService>();
  89. var _DatabaseFieldService = App.GetService<IDatabaseFieldService>();
  90. foreach(int tableId in param.sourceTableId)
  91. {
  92. var table = _DatabaseTableService.GetById(tableId) ?? new DatabaseTable();
  93. var database = _DatabaseInfoService.GetFirst(m => m.id == table.databaseId) ?? new DatabaseInfo();
  94. var fields = _DatabaseFieldService.GetList(m => m.tableId == table.id);
  95. for (int apiKind = 1; apiKind <= 5; apiKind++)
  96. {
  97. var apiParam = new List<ApiParamObject>();
  98. foreach(var field in fields)
  99. {
  100. bool reqParam = false;
  101. bool resParam = false;
  102. if((field.showQuery && apiKind == 1) || (field.showEdit && (apiKind == 3 || apiKind == 4)))
  103. {
  104. reqParam = true;
  105. }
  106. if((apiKind == 2 || apiKind == 4 || apiKind == 5) && field.primaryKey)
  107. {
  108. reqParam = true;
  109. }
  110. if(field.showList && (apiKind == 1 || apiKind == 2))
  111. {
  112. resParam = true;
  113. }
  114. apiParam.Add(new ApiParamObject()
  115. {
  116. id = field.id,
  117. fieldTitle = field.fieldTitle,
  118. fieldName = field.fieldName,
  119. fieldType = field.fieldType,
  120. reqParam = reqParam,
  121. resParam = resParam,
  122. resType = field.fieldType,
  123. checkAttribute = field.validateAttribute.Split(','),
  124. fieldDetail = field.fieldDetail,
  125. });
  126. }
  127. Add(new ApiInfo()
  128. {
  129. serviceId = database.serverId,
  130. groupId = param.groupId,
  131. tabName = table.tableName,
  132. apiName = table.tableName + (apiKind == 1 ? "列表" : apiKind == 2 ? "详情" : apiKind == 3 ? "添加" : apiKind == 4 ? "修改" : "删除"),
  133. controllerFilePath = "/api/" + database.dbName + "/" + table.tableName,
  134. usageScenarios = 1,
  135. apiKind = apiKind,
  136. methodName = apiKind == 1 ? "get" + Utils.transferName(table.tableName) + "List" :
  137. apiKind == 2 ? "get" + Utils.transferName(table.tableName) + "Query" :
  138. apiKind == 3 ? "add" + Utils.transferName(table.tableName) :
  139. apiKind == 4 ? "update" + Utils.transferName(table.tableName) :
  140. "delete" + Utils.transferName(table.tableName),
  141. apiDetail = apiKind == 1 ? "根据查询条件查询" + table.tableName + "列表数据" :
  142. apiKind == 2 ? "根据Id查询" + table.tableName + "某一条详情数据" :
  143. apiKind == 3 ? "添加" + table.tableName + "数据" :
  144. apiKind == 4 ? "修改" + table.tableName + "数据" :
  145. "根据Id删除" + table.tableName + "数据",
  146. apiParam = apiParam,
  147. createTime = DateTime.Now,
  148. });
  149. }
  150. }
  151. }
  152. }
  153. }