diff --git a/OpenAuth.App/Form/DragForm.cs b/OpenAuth.App/Form/DragForm.cs new file mode 100644 index 00000000..88069b1c --- /dev/null +++ b/OpenAuth.App/Form/DragForm.cs @@ -0,0 +1,141 @@ +using System; +using System.Linq; +using System.Text; +using Infrastructure; +using Newtonsoft.Json.Linq; +using OpenAuth.Repository; +using OpenAuth.Repository.Interface; +using OpenAuth.Repository.QueryObj; + +namespace OpenAuth.App +{ + /// + /// 拖动表单,根据表单字段创建数据库表 + /// + public class DragForm: IForm + { + private IUnitWork _unitWork; + + public DragForm(IUnitWork unitWork) + { + _unitWork = unitWork; + } + + /** + * 功能: 创建表单数据表格(基于sql server) + */ + public string GetSql(Repository.Domain.Form form, string dbType) + { + if (dbType == Define.DBTYPE_SQLSERVER) //Sql Server + { + // 获取字段并处理 + var jsonArray = JsonHelper.Instance.Deserialize(form.ContentData)["list"]; + + // 数据库名称 + string tableName = form.DbName; + + var exist = _unitWork.FromSql($"select '1' as value from sysobjects where name = '{tableName}' and type = 'U'").SingleOrDefault(); + if (exist != null) return string.Empty; + + // 创建数据表 + StringBuilder sql = new StringBuilder($"CREATE TABLE {tableName} ( [Id] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,"); //主键 + + string sqlDefault = ""; + + foreach (var json in jsonArray) + { + string type = json["type"].ToString(); + string name = json["model"].ToString(); + + sql.Append("[" + name + "] " + field_type_sql(type)); //字段拼接 + + + if ("checkboxs" == type) + sqlDefault += field_type_sql_default(tableName, name, "0"); + else + sqlDefault += field_type_sql_default(tableName, name, "''"); + } + + sql.Append(");"); + + //设置主键 + sql.Append("ALTER TABLE " + tableName + " ADD CONSTRAINT [PK_" + form.DbName + + "] PRIMARY KEY NONCLUSTERED ([Id])"); + sql.Append( + "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) "); + sql.Append("ON [PRIMARY];"); + + //主键默认值 + sql.Append("ALTER TABLE " + tableName + " ADD DEFAULT (newid()) FOR [Id];"); + + return sql + sqlDefault; + } + else + { + // 获取字段并处理 + var jsonArray = JsonHelper.Instance.Deserialize(form.ContentData)["list"]; + + // 数据库名称 + string tableName = form.DbName; + + var exist = _unitWork.FromSql($"select table_name as value from information_schema.tables where table_name ='{tableName}'").SingleOrDefault(); + if (exist != null) return string.Empty; + + // 创建数据表 + StringBuilder sql = new StringBuilder("create table if not exists `" + + tableName + + "` ( Id varchar(50) not null primary key,"); //主键 + + foreach (var json in jsonArray) + { + string type = json["type"].ToString(); + string name = json["model"].ToString(); + + sql.Append("`" + name + "` " + field_type_mysql(type)); //字段拼接 + + } + + sql.Append(");"); + + return sql.ToString(); + } + } + + private string field_type_sql(string leipiplugins) + { + if ("textarea" == leipiplugins || "listctrl" == leipiplugins) + { + return " text NULL ,"; + } + else if ("checkboxs" == leipiplugins) + { + return " int NOT NULL ,"; + } + else + { + return " varchar(255) NULL ,"; + } + } + + private string field_type_mysql(string leipiplugins) + { + if ("textarea" == leipiplugins || "listctrl" == leipiplugins) + { + return " varchar(255) null ,"; + } + else if ("checkboxs" == leipiplugins) + { + return " tinyint not null ,"; + } + else + { + return " varchar(255) NULL ,"; + } + } + + private string field_type_sql_default(string tablename, string field, string defaultValue) + { + return "ALTER TABLE " + tablename + " ADD DEFAULT (" + defaultValue + ") FOR [" + field + "];"; + } + } +} \ No newline at end of file diff --git a/OpenAuth.App/Form/FormApp.cs b/OpenAuth.App/Form/FormApp.cs index fc5c1065..8aadd5df 100644 --- a/OpenAuth.App/Form/FormApp.cs +++ b/OpenAuth.App/Form/FormApp.cs @@ -49,7 +49,7 @@ namespace OpenAuth.App { var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()]; - UnitWork.ExecuteSql(FormUtil.GetSql(obj, dbtype)); + UnitWork.ExecuteSql(FormFactory.CreateForm(obj, this.UnitWork).GetSql(obj, dbtype)); } UnitWork.Save(); } @@ -73,7 +73,7 @@ namespace OpenAuth.App if (!string.IsNullOrEmpty(obj.DbName)) { var dbtype = _appConfiguration.Value.DbTypes[_httpContextAccessor.GetTenantId()]; - UnitWork.ExecuteSql(FormUtil.GetSql(obj, dbtype)); + UnitWork.ExecuteSql(FormFactory.CreateForm(obj,this.UnitWork).GetSql(obj, dbtype)); } } diff --git a/OpenAuth.App/Form/FormFactory.cs b/OpenAuth.App/Form/FormFactory.cs new file mode 100644 index 00000000..7266a0d5 --- /dev/null +++ b/OpenAuth.App/Form/FormFactory.cs @@ -0,0 +1,25 @@ +using System; +using OpenAuth.Repository; +using OpenAuth.Repository.Domain; +using OpenAuth.Repository.Interface; + +namespace OpenAuth.App +{ + public class FormFactory + { + public static IForm CreateForm(Form form, IUnitWork unitWork) + { + if (form.FrmType == 0) + { + return new LeipiForm(unitWork); + }else if (form.FrmType == 1) + { + throw new Exception("自定义表单不需要创建数据库表"); + } + else + { + return new DragForm(unitWork); + } + } + } +} \ No newline at end of file diff --git a/OpenAuth.App/Form/FormUtil.cs b/OpenAuth.App/Form/FormUtil.cs index 69d78f0a..ac117129 100644 --- a/OpenAuth.App/Form/FormUtil.cs +++ b/OpenAuth.App/Form/FormUtil.cs @@ -494,142 +494,5 @@ namespace OpenAuth.App // return temp_html; // } - - - - - - /** - * 功能: 创建表单数据表格(基于sql server) - */ - public static string GetSql(Form form, string dbType){ - - if (dbType == Define.DBTYPE_SQLSERVER) //Sql Server - { - // 获取字段并处理 - var jsonArray = JArray.Parse(form.ContentData); - - // 数据库名称 - string tableName= form.DbName ; - // 创建数据表 - StringBuilder sql =new StringBuilder("if exists ( select * from sysobjects where name = '" - +tableName+"' and type = 'U') drop table " - + tableName +";") ; - - sql.Append("CREATE TABLE " - + tableName - + " ( [Id] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,"); //主键 - - string sqlDefault = ""; - - foreach (var json in jsonArray) - { - string name; - string type = json["leipiplugins"].ToString(); - - if ("checkboxs" == type) - name = json["parse_name"].ToString(); - else - name = json["name"].ToString(); - - sql.Append("[" + name + "] " + field_type_sql(type));//字段拼接 - - - if ("checkboxs" == type) - sqlDefault += field_type_sql_default(tableName, name, "0"); - else - sqlDefault += field_type_sql_default(tableName, name, "''"); - } - - sql.Append(");"); - - //设置主键 - sql.Append("ALTER TABLE "+tableName+" ADD CONSTRAINT [PK_"+form.DbName+"] PRIMARY KEY NONCLUSTERED ([Id])"); - sql.Append( - "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) "); - sql.Append("ON [PRIMARY];"); - - //主键默认值 - sql.Append("ALTER TABLE "+tableName+" ADD DEFAULT (newid()) FOR [Id];"); - - return sql+sqlDefault; - } - else - { - // 获取字段并处理 - var jsonArray = JArray.Parse(form.ContentData); - - // 数据库名称 - string tableName= form.DbName ; - // 创建数据表 - StringBuilder sql =new StringBuilder("create table if not exists `" - + tableName - + "` ( Id varchar(50) not null primary key,") ; //主键 - - - // string sqlDefault = ""; - - foreach (var json in jsonArray) - { - string name; - string type = json["leipiplugins"].ToString(); - - if ("checkboxs" == type) - name = json["parse_name"].ToString(); - else - name = json["name"].ToString(); - - sql.Append("`" + name + "` " + field_type_mysql(type));//字段拼接 - -// -// if ("checkboxs" == type) -// sqlDefault += field_type_sql_default(tableName, name, "0"); -// else -// sqlDefault += field_type_sql_default(tableName, name, "''"); - } - - sql.Append(");"); - - return sql.ToString(); - } - - } - //获取控件字段类型 的sql - private static string field_type_sql(string leipiplugins) - { - if ("textarea"==leipiplugins || "listctrl"==leipiplugins) - { - return " text NULL ,"; - } - else if ("checkboxs"==leipiplugins) - { - return " int NOT NULL ,"; - } - else - { - return " varchar(255) NULL ,"; - } - } - - private static string field_type_mysql(string leipiplugins) - { - if ("textarea"==leipiplugins || "listctrl"==leipiplugins) - { - return " varchar(255) null ,"; - } - else if ("checkboxs"==leipiplugins) - { - return " tinyint not null ,"; - } - else - { - return " varchar(255) NULL ,"; - } - } - - private static string field_type_sql_default(string tablename, string field, string defaultValue) - { - return "ALTER TABLE "+tablename+" ADD DEFAULT ("+defaultValue+") FOR ["+field+"];"; - } } } diff --git a/OpenAuth.App/Form/IForm.cs b/OpenAuth.App/Form/IForm.cs new file mode 100644 index 00000000..082a0602 --- /dev/null +++ b/OpenAuth.App/Form/IForm.cs @@ -0,0 +1,7 @@ +namespace OpenAuth.App +{ + public interface IForm + { + string GetSql(Repository.Domain.Form form, string dbType); + } +} \ No newline at end of file diff --git a/OpenAuth.App/Form/LeipiForm.cs b/OpenAuth.App/Form/LeipiForm.cs new file mode 100644 index 00000000..c7e4979f --- /dev/null +++ b/OpenAuth.App/Form/LeipiForm.cs @@ -0,0 +1,148 @@ +using System.Linq; +using System.Text; +using Infrastructure; +using Newtonsoft.Json.Linq; +using OpenAuth.Repository; +using OpenAuth.Repository.Interface; +using OpenAuth.Repository.QueryObj; + +namespace OpenAuth.App +{ + public class LeipiForm : IForm + { + private IUnitWork _uniwWork; + + public LeipiForm(IUnitWork uniwWork) + { + _uniwWork = uniwWork; + } + /** + * 功能: 创建表单数据表格(基于sql server) + */ + public string GetSql(Repository.Domain.Form form, string dbType) + { + if (dbType == Define.DBTYPE_SQLSERVER) //Sql Server + { + // 获取字段并处理 + var jsonArray = JArray.Parse(form.ContentData); + + // 数据库名称 + string tableName = form.DbName; + var exist = _uniwWork.FromSql($"select '1' as value from sysobjects where name = '{tableName}' and type = 'U'").SingleOrDefault(); + if (exist != null) return string.Empty; + + // 创建数据表 + StringBuilder sql = new StringBuilder($"CREATE TABLE {tableName} ( [Id] varchar(50) COLLATE Chinese_PRC_CI_AS NOT NULL,"); //主键 + string sqlDefault = ""; + + foreach (var json in jsonArray) + { + string name; + string type = json["leipiplugins"].ToString(); + + if ("checkboxs" == type) + name = json["parse_name"].ToString(); + else + name = json["name"].ToString(); + + sql.Append("[" + name + "] " + field_type_sql(type)); //字段拼接 + + + if ("checkboxs" == type) + sqlDefault += field_type_sql_default(tableName, name, "0"); + else + sqlDefault += field_type_sql_default(tableName, name, "''"); + } + + sql.Append(");"); + + //设置主键 + sql.Append("ALTER TABLE " + tableName + " ADD CONSTRAINT [PK_" + form.DbName + + "] PRIMARY KEY NONCLUSTERED ([Id])"); + sql.Append( + "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) "); + sql.Append("ON [PRIMARY];"); + + //主键默认值 + sql.Append("ALTER TABLE " + tableName + " ADD DEFAULT (newid()) FOR [Id];"); + + return sql + sqlDefault; + } + else + { + // 获取字段并处理 + var jsonArray = JArray.Parse(form.ContentData); + + // 数据库名称 + string tableName = form.DbName; + var exist = _uniwWork.FromSql($"select table_name as value from information_schema.tables where table_name ='{tableName}'").SingleOrDefault(); + if (exist != null) return string.Empty; + + // 创建数据表 + StringBuilder sql = new StringBuilder("create table if not exists `" + + tableName + + "` ( Id varchar(50) not null primary key,"); //主键 + + foreach (var json in jsonArray) + { + string name; + string type = json["leipiplugins"].ToString(); + + if ("checkboxs" == type) + name = json["parse_name"].ToString(); + else + name = json["name"].ToString(); + + sql.Append("`" + name + "` " + field_type_mysql(type)); //字段拼接 + +// +// if ("checkboxs" == type) +// sqlDefault += field_type_sql_default(tableName, name, "0"); +// else +// sqlDefault += field_type_sql_default(tableName, name, "''"); + } + + sql.Append(");"); + + return sql.ToString(); + } + } + + private string field_type_sql(string leipiplugins) + { + if ("textarea" == leipiplugins || "listctrl" == leipiplugins) + { + return " text NULL ,"; + } + else if ("checkboxs" == leipiplugins) + { + return " int NOT NULL ,"; + } + else + { + return " varchar(255) NULL ,"; + } + } + + private string field_type_mysql(string leipiplugins) + { + if ("textarea" == leipiplugins || "listctrl" == leipiplugins) + { + return " varchar(255) null ,"; + } + else if ("checkboxs" == leipiplugins) + { + return " tinyint not null ,"; + } + else + { + return " varchar(255) NULL ,"; + } + } + + private string field_type_sql_default(string tablename, string field, string defaultValue) + { + return "ALTER TABLE " + tablename + " ADD DEFAULT (" + defaultValue + ") FOR [" + field + "];"; + } + } +} \ No newline at end of file diff --git a/OpenAuth.App/Test/TestForm.cs b/OpenAuth.App/Test/TestForm.cs index b29cf89a..562c77c4 100644 --- a/OpenAuth.App/Test/TestForm.cs +++ b/OpenAuth.App/Test/TestForm.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using Moq; using OpenAuth.App.Request; using OpenAuth.App.SSO; +using OpenAuth.Repository.Domain; namespace OpenAuth.App.Test { @@ -41,5 +42,19 @@ namespace OpenAuth.App.Test }); Console.WriteLine(JsonHelper.Instance.Serialize(result)); } + + [Test] + public void Update() + { + var app = _autofacServiceProvider.GetService(); + app.Update(new Form() + { + Id = "5815ff07-b457-47a2-8a19-c9080c3575d8", + ContentData = "{\"list\":[{\"type\":\"input\",\"icon\":\"icon-input\",\"name\":\"单行文本\",\"options\":{\"width\":\"100%\",\"defaultValue\":\"\",\"required\":false,\"dataType\":\"string\",\"pattern\":\"\",\"placeholder\":\"\",\"disabled\":false,\"remoteFunc\":\"func_1642332330000_92043\"},\"key\":\"1642332330000_92043\",\"model\":\"Name\",\"rules\":[{\"type\":\"string\",\"message\":\"Name格式不正确\"}]},{\"type\":\"textarea\",\"icon\":\"icon-diy-com-textarea\",\"name\":\"多行文本\",\"options\":{\"width\":\"100%\",\"defaultValue\":\"\",\"required\":false,\"disabled\":false,\"pattern\":\"\",\"placeholder\":\"\",\"remoteFunc\":\"func_1642332331000_66846\"},\"key\":\"1642332331000_66846\",\"model\":\"Price\",\"rules\":[]},{\"type\":\"number\",\"icon\":\"icon-number\",\"name\":\"计数器\",\"options\":{\"width\":\"\",\"required\":false,\"defaultValue\":0,\"min\":0,\"max\":0,\"step\":1,\"disabled\":false,\"controlsPosition\":\"\",\"remoteFunc\":\"func_1642332332000_75553\"},\"key\":\"1642332332000_75553\",\"model\":\"number_1642332332000_75553\",\"rules\":[]},{\"type\":\"text\",\"icon\":\"icon-wenzishezhi-\",\"name\":\"文字\",\"options\":{\"defaultValue\":\"This is a text\",\"customClass\":\"\",\"remoteFunc\":\"func_1642332335000_15010\"},\"key\":\"1642332335000_15010\",\"model\":\"text_1642332335000_15010\",\"rules\":[]},{\"type\":\"textarea\",\"icon\":\"icon-diy-com-textarea\",\"name\":\"多行文本\",\"options\":{\"width\":\"100%\",\"defaultValue\":\"\",\"required\":false,\"disabled\":false,\"pattern\":\"\",\"placeholder\":\"\",\"remoteFunc\":\"func_1642332496000_1253\"},\"key\":\"1642332496000_1253\",\"model\":\"textarea_1642332496000_1253\",\"rules\":[]}],\"config\":{\"labelWidth\":100,\"labelPosition\":\"right\",\"size\":\"small\"}}", + DbName = "stock", + OrgId = "08f41bf6-4388-4b1e-bd3e-2ff538b44b1b", + FrmType = 2 + }); + } } } diff --git a/OpenAuth.Repository/OpenAuthDBContext.cs b/OpenAuth.Repository/OpenAuthDBContext.cs index a911f629..0078df0e 100644 --- a/OpenAuth.Repository/OpenAuthDBContext.cs +++ b/OpenAuth.Repository/OpenAuthDBContext.cs @@ -77,6 +77,7 @@ namespace OpenAuth.Repository modelBuilder.Entity() .HasKey(c => new { c.Id }); modelBuilder.Entity().HasNoKey(); + modelBuilder.Entity().HasNoKey(); } public virtual DbSet Applications { get; set; } @@ -110,6 +111,7 @@ namespace OpenAuth.Repository public virtual DbSet BuilderTables { get; set; } public virtual DbSet BuilderTableColumns { get; set; } //非数据库表格 + public virtual DbSet QueryStringObjs { get; set; } public virtual DbSet SysTableColumns { get; set; } } diff --git a/OpenAuth.Repository/QueryObj/QueryStringObj.cs b/OpenAuth.Repository/QueryObj/QueryStringObj.cs new file mode 100644 index 00000000..c8dc1140 --- /dev/null +++ b/OpenAuth.Repository/QueryObj/QueryStringObj.cs @@ -0,0 +1,11 @@ +namespace OpenAuth.Repository.QueryObj +{ + /// + /// 针对只返回字符串类型的数值。查询SQL必需使用Value作为返回字段 + /// 因为string没有构造函数,不能作为DbSet/DbQuery泛型的参数 + /// + public class QueryStringObj + { + public string Value { get; set; } + } +} \ No newline at end of file diff --git a/OpenAuth.Repository/UnitWork.cs b/OpenAuth.Repository/UnitWork.cs index dddf03d8..f065740c 100644 --- a/OpenAuth.Repository/UnitWork.cs +++ b/OpenAuth.Repository/UnitWork.cs @@ -204,6 +204,7 @@ namespace OpenAuth.Repository public int ExecuteSql(string sql) { + if (string.IsNullOrEmpty(sql)) return 0; return _context.Database.ExecuteSqlRaw(sql); }