ApiInfoService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. var _ApiGroupService = App.GetService<IApiGroupService>();
  91. var apiGroup = _ApiGroupService.GetFirst(m => m.id == param.groupId) ?? new ApiGroup();
  92. foreach(int tableId in param.sourceTableId)
  93. {
  94. var table = _DatabaseTableService.GetById(tableId) ?? new DatabaseTable();
  95. var database = _DatabaseInfoService.GetFirst(m => m.id == table.databaseId) ?? new DatabaseInfo();
  96. var fields = _DatabaseFieldService.GetList(m => m.tableId == table.id);
  97. for (int apiKind = 1; apiKind <= 5; apiKind++)
  98. {
  99. var apiParam = new List<ApiParamObject>();
  100. foreach(var field in fields)
  101. {
  102. bool reqParam = false;
  103. bool resParam = false;
  104. if((field.showQuery && apiKind == 1) || (field.showEdit && (apiKind == 3 || apiKind == 4)))
  105. {
  106. reqParam = true;
  107. }
  108. if((apiKind == 2 || apiKind == 4 || apiKind == 5) && field.primaryKey)
  109. {
  110. reqParam = true;
  111. }
  112. if(field.showList && (apiKind == 1 || apiKind == 2))
  113. {
  114. resParam = true;
  115. }
  116. apiParam.Add(new ApiParamObject()
  117. {
  118. id = field.id,
  119. fieldTitle = field.fieldTitle ?? "",
  120. fieldName = field.fieldName ?? "",
  121. fieldType = field.fieldType ?? "",
  122. reqParam = reqParam,
  123. resParam = resParam,
  124. resType = field.fieldType ?? "",
  125. checkAttribute = !string.IsNullOrEmpty(field.validateAttribute) ? field.validateAttribute.Split(',') : Array.Empty<string>(),
  126. fieldDetail = field.fieldDetail ?? "",
  127. });
  128. }
  129. Add(new ApiInfo()
  130. {
  131. serviceId = apiGroup.serviceId,
  132. groupId = param.groupId,
  133. tabName = "基本信息",
  134. apiName = table.tableTitle + (apiKind == 1 ? "列表" : apiKind == 2 ? "详情" : apiKind == 3 ? "添加" : apiKind == 4 ? "修改" : "删除"),
  135. controllerFilePath = table.tableNameUpper,
  136. usageScenarios = 1,
  137. apiKind = apiKind,
  138. methodName = apiKind == 1 ? "get" + table.tableNameFirstUpper + "List" :
  139. apiKind == 2 ? "get" + table.tableNameFirstUpper + "Query" :
  140. apiKind == 3 ? "add" + table.tableNameFirstUpper :
  141. apiKind == 4 ? "update" + table.tableNameFirstUpper :
  142. "delete" + table.tableNameFirstUpper,
  143. apiDetail = apiKind == 1 ? "根据查询条件查询" + table.tableTitle + "列表数据" :
  144. apiKind == 2 ? "根据Id查询" + table.tableTitle + "某一条详情数据" :
  145. apiKind == 3 ? "添加" + table.tableTitle + "数据" :
  146. apiKind == 4 ? "修改" + table.tableTitle + "数据" :
  147. "根据Id删除" + table.tableTitle + "数据",
  148. apiParam = apiParam,
  149. createTime = DateTime.Now,
  150. linkTableId = tableId,
  151. pageFlag = apiKind == 1,
  152. createBy = App.UserName,
  153. });
  154. }
  155. }
  156. }
  157. }
  158. }