Jelajahi Sumber

完善初始化接口,克隆接口

lichunlei 7 bulan lalu
induk
melakukan
90c3f070eb

+ 14 - 1
Controllers/Admin/ApiInfoController.cs

@@ -136,6 +136,19 @@ namespace Controllers.Admin
         }
 
 
+        /// <summary>
+        /// 克隆接口
+        /// </summary>
+        /// <param name="param">参数请求体</param>
+        [HttpPost]
+        [Route("/v1/omega_source/ApiInfo/copyApiInfo")]
+        public IActionResult copyApiInfo([FromBody] CopyApiInfoDto param)
+        {
+            _ApiInfoService.copyApiInfo(param);
+            return SUCCESS("ok");
+        }
+
+
         /// <summary>
         /// 初始化接口
         /// </summary>
@@ -144,7 +157,7 @@ namespace Controllers.Admin
         [Route("/v1/omega_source/ApiInfo/initApiInfo")]
         public IActionResult initApiInfo([FromBody] InitApiInfoDto param)
         {
-            
+            _ApiInfoService.initApiInfo(param);
             return SUCCESS("ok");
         }
 

+ 26 - 0
Model/Dto/Admin/CopyApiInfoDto.cs

@@ -0,0 +1,26 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+using Model.Base;
+
+namespace Dto.Admin
+{
+    /// <summary>
+    /// 克隆字段
+    /// </summary>
+    public class CopyApiInfoDto
+    {
+        /// <summary>
+        /// 来源字段ID
+        /// </summary>
+        public int[] sourceApiId { get; set; }
+
+
+        /// <summary>
+        /// 目标表ID
+        /// </summary>
+        public int targetGroupId { get; set; }
+
+
+
+    }
+}

+ 7 - 1
Model/Dto/Admin/InitApiInfoDto.cs

@@ -12,7 +12,13 @@ namespace Dto.Admin
         /// <summary>
         /// 数据表ID
         /// </summary>
-        public string sourceTableId { get; set; }
+        public int[] sourceTableId { get; set; }
+
+
+        /// <summary>
+        /// 分组ID
+        /// </summary>
+        public int groupId { get; set; }
 
     }
 }

+ 104 - 1
Services/ApiInfoService.cs

@@ -16,6 +16,19 @@ namespace Services
     [AppService(ServiceType = typeof(IApiInfoService), ServiceLifetime = LifeTime.Transient)]
     public class ApiInfoService : BaseService<ApiInfo>, IApiInfoService
     {
+        private readonly IApiGroupService _ApiGroupService;
+        private readonly IDatabaseInfoService _DatabaseInfoService;
+        private readonly IDatabaseTableService _DatabaseTableService;
+        private readonly IDatabaseFieldService _DatabaseFieldService;
+
+        public ApiInfoService(IApiGroupService ApiGroupService, IDatabaseInfoService DatabaseInfoService, IDatabaseTableService DatabaseTableService, IDatabaseFieldService DatabaseFieldService)
+        {
+            _ApiGroupService = ApiGroupService;
+            _DatabaseInfoService = DatabaseInfoService;
+            _DatabaseTableService = DatabaseTableService;
+            _DatabaseFieldService = DatabaseFieldService;
+        }
+
         /// <summary>
         /// 接口信息-列表
         /// </summary>
@@ -57,12 +70,102 @@ namespace Services
         }
 
 
+        /// <summary>
+        /// 克隆接口
+        /// </summary>
+        /// <param name="param">参数请求体</param>
+        public void copyApiInfo([FromBody] CopyApiInfoDto param)
+        {
+            var group = _ApiGroupService.GetFirst(m => m.id == param.targetGroupId) ?? new ApiGroup();
+            List<int> apiIdList = param.sourceApiId.ToList();
+            var sourceApis = GetList(m => apiIdList.Contains(m.id)).ToList();
+            foreach(ApiInfo sourceApi in sourceApis)
+            {
+                ApiInfo newApi = new ApiInfo();
+                newApi.serviceId = group.serviceId;
+                newApi.groupId = param.targetGroupId;
+                newApi.tabName = sourceApi.tabName;
+                newApi.apiName = sourceApi.apiName;
+                newApi.controllerFilePath = sourceApi.controllerFilePath;
+                newApi.usageScenarios = sourceApi.usageScenarios;
+                newApi.apiKind = sourceApi.apiKind;
+                newApi.methodName = sourceApi.methodName;
+                newApi.apiDetail = sourceApi.apiDetail;
+                newApi.apiParam = sourceApi.apiParam;
+                newApi.createTime = DateTime.Now;
+                Add(newApi);
+            }
+        }
+
+
         /// <summary>
         /// 初始化接口
         /// </summary>
         public void initApiInfo([FromBody] InitApiInfoDto param)
         {
-            
+            foreach(int tableId in param.sourceTableId)
+            {
+                var table = _DatabaseTableService.GetById(tableId) ?? new DatabaseTable();
+                var database = _DatabaseInfoService.GetFirst(m => m.id == table.databaseId) ?? new DatabaseInfo();
+                var fields = _DatabaseFieldService.GetList(m => m.tableId == table.id);
+                for (int apiKind = 1; apiKind <= 5; apiKind++)
+                {
+                    var apiParam = new List<ApiParamObject>();
+                    foreach(var field in fields)
+                    {
+                        bool reqParam = false;
+                        bool resParam = false;
+                        if((field.showQuery && apiKind == 1) || (field.showEdit && (apiKind == 3 || apiKind == 4)))
+                        {
+                            reqParam = true;
+                        }
+                        if((apiKind == 2 || apiKind == 4 || apiKind == 5) && field.primaryKey)
+                        {
+                            reqParam = true;
+                        }
+                        if(field.showList && (apiKind == 1 || apiKind == 2))
+                        {
+                            resParam = true;
+                        }
+
+                        apiParam.Add(new ApiParamObject()
+                        {
+                            id = field.id,
+                            fieldTitle = field.fieldTitle,
+                            fieldName = field.fieldName,
+                            fieldType = field.fieldType,
+                            reqParam = reqParam,
+                            resParam = resParam,
+                            resType = field.fieldType,
+                            checkAttribute = field.validateAttribute.Split(','),
+                            fieldDetail = field.fieldDetail,
+                        });
+                    }
+
+                    Add(new ApiInfo()
+                    {
+                        serviceId = 0,
+                        groupId = param.groupId,
+                        tabName = table.tableName,
+                        apiName = table.tableName + (apiKind == 1 ? "列表" : apiKind == 2 ? "详情" : apiKind == 3 ? "添加" : apiKind == 4 ? "修改" : "删除"),
+                        controllerFilePath = "/api/" + database.dbName + "/" + table.tableName,
+                        usageScenarios = 1,
+                        apiKind = apiKind,
+                        methodName = apiKind == 1 ? "get" + Utils.transferName(table.tableName) + "List" :
+                                     apiKind == 2 ? "get" + Utils.transferName(table.tableName) + "Query" :
+                                     apiKind == 3 ? "add" + Utils.transferName(table.tableName) :
+                                     apiKind == 4 ? "update" + Utils.transferName(table.tableName) :
+                                     "delete" + Utils.transferName(table.tableName),
+                        apiDetail = apiKind == 1 ? "根据查询条件查询" + table.tableName + "列表数据" :
+                                     apiKind == 2 ? "根据Id查询" + table.tableName + "某一条详情数据" :
+                                     apiKind == 3 ? "添加" + table.tableName + "数据" :
+                                     apiKind == 4 ? "修改" + table.tableName + "数据" :
+                                     "根据Id删除" + table.tableName + "数据",
+                        apiParam = apiParam,
+                        createTime = DateTime.Now,
+                    });
+                }
+            }
         }
     }
 }

+ 8 - 6
Services/DatabaseFieldService.cs

@@ -16,11 +16,11 @@ namespace Services
     [AppService(ServiceType = typeof(IDatabaseFieldService), ServiceLifetime = LifeTime.Transient)]
     public class DatabaseFieldService : BaseService<DatabaseField>, IDatabaseFieldService
     {
-        private readonly IDatabaseFieldService _DatabaseFieldService;
+        private readonly IDatabaseTableService _DatabaseTableService;
 
-        public DatabaseFieldService(IDatabaseFieldService DatabaseFieldService)
+        public DatabaseFieldService(IDatabaseTableService DatabaseTableService)
         {
-            _DatabaseFieldService = DatabaseFieldService;
+            _DatabaseTableService = DatabaseTableService;
         }
 
         /// <summary>
@@ -53,12 +53,14 @@ namespace Services
         /// <param name="param">参数请求体</param>
         public void copyDatabaseField([FromBody] CopyDatabaseFieldDto param)
         {
+            var targetTable = _DatabaseTableService.GetFirst(m => m.id == param.targetTableId) ?? new DatabaseTable();
+
             List<int> fieldIdList = param.sourceFieldId.ToList();
-            var sourceFields = _DatabaseFieldService.GetList(m => fieldIdList.Contains(m.id)).ToList();
+            var sourceFields = GetList(m => fieldIdList.Contains(m.id)).ToList();
             foreach(DatabaseField sourceField in sourceFields)
             {
                 DatabaseField newField = new DatabaseField();
-                newField.databaseId = 0;
+                newField.databaseId = targetTable.databaseId;
                 newField.tableId = param.targetTableId;
                 newField.tabId = sourceField.tabId;
                 newField.fieldTitle = sourceField.fieldTitle;
@@ -86,7 +88,7 @@ namespace Services
                 newField.linkFieldId = sourceField.linkFieldId;
                 newField.apiId = sourceField.apiId;
                 newField.createTime = DateTime.Now;
-                _DatabaseFieldService.Add(newField);
+                Add(newField);
             }
         }
     }

+ 8 - 0
Services/IService/IApiInfoService.cs

@@ -25,6 +25,14 @@ namespace Services
         /// <returns>字段关联接口列表</returns>
         PagedInfo<GetApiInfoListByProIdVo> getApiInfoListByProId([FromQuery] PagerInfo page, [FromQuery] ApiInfo param);
 
+
+        /// <summary>
+        /// 克隆接口
+        /// </summary>
+        /// <param name="param">参数请求体</param>
+        void copyApiInfo([FromBody] CopyApiInfoDto param);
+        
+
         /// <summary>
         /// 初始化接口
         /// </summary>

+ 55 - 5
Util/Utils.cs

@@ -1,14 +1,64 @@
 public class Utils
 {
     
-    #region 打控制台日志
-
-    public static void WriteLog(string msg, string filename = "message")
+    /// <summary>
+    /// 写日志
+    /// </summary>
+    /// <param name="message">日志内容</param>
+    /// <param name="fileName">文件名称</param>
+    public static void WriteLog(string message, string fileName = "message")
     {
-        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + filename + "--" + msg);
+        Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--" + fileName + "--" + message);
     }
 
-    #endregion
+    /// <summary>
+    /// 命名转换
+    /// </summary>
+    /// <param name="name">名称</param>
+    /// <param name="startUpperNum">从第几个单词开始首字母大写</param>
+    /// <returns>转换后名称</returns>
+    public static string transferName(string name, int startUpperNum = 1)
+    {
+        if(string.IsNullOrEmpty(name))
+        {
+            return "";
+        }
+        string result = "";
+        string[] list = name.Trim('_').Split('_');
+        if(list.Length == 1)
+        {
+            if(startUpperNum == 1)
+            {
+                return name.Substring(0, 1).ToUpper() + name.Substring(1);
+            }
+            else if(startUpperNum > 1)
+            {
+                return name.Substring(0, 1).ToLower() + name.Substring(1);
+            }
+            else
+            {
+                return name;
+            }
+        }
+        int index = 0;
+        foreach(string sub in list)
+        {
+            index += 1;
+            if(index >= startUpperNum)
+            {
+                result += sub.Substring(0, 1).ToUpper() + sub.Substring(1).ToLower();
+            }
+            else
+            {
+                result += sub.ToLower();
+            }
+        }
+        if(startUpperNum > 1)
+        {
+            result = result.Substring(0, 1).ToLower() + result.Substring(1);
+        }
+        return result;
+    }
 
 
 }