ApiInfoService.cs 8.2 KB

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