/// <summary> /// 获取不同表里列名相同的列名 /// 不包括列名为ID,CommitTime,UpdateTime /// 不包含列名为_ID /// </summary> /// <returns></returns> public static string[] Same_Column_Name_In_TableName(string type = "tinyint") { string typeSql = ""; if (!string.IsNullOrEmpty(type)) { typeSql = " DATA_TYPE='" + type + "' AND "; } string SQLStr = string.Format(Sql_Same_Column_Name, typeSql); DataTable tables = UtilSqlserver.SqlExecute(SQLStr); string[] Result = new string[tables.Rows.Count]; string Column_Name; int i_index = 0; foreach (DataRow item in tables.Rows) { Column_Name = (string)item.ItemArray[1]; if (Column_Name.ToUpper().Contains("_ID") || Column_Name.ToUpper().Equals("ID") || Column_Name.ToUpper().Equals("COMMITTIME") || Column_Name.ToUpper().Equals("UPDATETIME")) { } else { Result.SetValue(Column_Name, i_index); i_index += 1; } } return(Result); }
/// <summary> /// 1.后台普通的显示cshtml文件【多个文件】 /// 如果是大文本列,需生成@Html.Raw(ViewBag.OnlineEditorHtml),默认不生成 /// [模板文件]:view/view.txt /// [生成文件名称]:ClassName /// [生成文件后缀名]:.cshtml /// </summary> private void CreateNormalView() { string ClassName = "Admin"; string InstanceName = "admin"; string Table_Comment = "系统管理员"; string Template_Name, Content, Content_New, OnlineEditorHtml = "", ComboTreeInitHtml = ""; string Relation_Table_Name; string Column_Name, Column_Type, Column_Length; foreach (string Table_Name in TableList) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/view/view.txt"; Content = UtilFile.ReadFile2String(Template_Name); ClassName = Table_Name; ComboTreeInitHtml = ""; OnlineEditorHtml = ""; if (TableInfoList.ContainsKey(Table_Name)) { Table_Comment = TableInfoList[Table_Name]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = t_c[0]; } InstanceName = UtilString.LcFirst(ClassName); Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Content_New = Content_New.Replace("{$InstanceName}", InstanceName); Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; Column_Type = entry.Value["Type"]; Column_Length = entry.Value["Length"]; Relation_Table_Name = Column_Name.Replace("_ID", ""); if (Relation_Table_Name.ToUpper().Equals("PARENT")) { ComboTreeInitHtml = @" <script type=""text/javascript"" src=""@Url.Content(""~/Content/common/js/ajax/ext/shared/components/ComboBoxTree.js"")""></script>"; } int iLength = UtilNumber.Parse(Column_Length); if (ColumnIsTextArea(Column_Name, Column_Type, iLength)) { OnlineEditorHtml = " @Html.Raw(ViewBag.OnlineEditorHtml)"; } } Content_New = Content_New.Replace("{$OnlineEditorHtml}", OnlineEditorHtml); Content_New = Content_New.Replace("{$ComboTreeInitHtml}", ComboTreeInitHtml); //存入目标文件内容 UtilFile.WriteString2FileEncodingGbk(Save_Dir + ClassName + ".cshtml", Content_New); } } }
public string GetPrivateVariableName() { string privateVariableName; if (IsPrimaryKey()) { privateVariableName = "_" + Column_Name.ToLowerInvariant(); } else if (this.IsForeignKey()) { // subtracting 4 characters accounts for starting at index 1, and excluding suffix of "_ID" privateVariableName = "_" + Column_Name.Substring(0, 1).ToLowerInvariant() + Column_Name.Substring(1); if (privateVariableName.EndsWith("_ID")) { privateVariableName = privateVariableName.Substring(0, privateVariableName.Length - 3); //Debug.WriteLine("Column: "+ _Column_Name +" maps to "+ privateVariableName); } else if (privateVariableName.EndsWith("ID")) { privateVariableName = privateVariableName.Substring(0, privateVariableName.Length - 2); //Debug.WriteLine("Column: " + _Column_Name + " maps to " + privateVariableName); } } else { privateVariableName = "_" + Column_Name.Substring(0, 1).ToLowerInvariant() + Column_Name.Substring(1); } return(privateVariableName); }
public string GetPropertyName() { if (this.IsForeignKey()) { if (Column_Name.EndsWith("_ID")) { return(Column_Name.Substring(0, Column_Name.Length - 3)); } else if (Column_Name.EndsWith("ID")) { return(Column_Name.Substring(0, Column_Name.Length - 2)); } else { return(Column_Name); } } else { return(Column_Name); } }
/// <summary> /// 1.生成实体类【多个文件】 /// [模板文件]:domain/domain.txt /// [生成文件名]:ClassName /// [生成文件后缀名]:.cs /// </summary> private void CreateDomain() { string ClassName = "Admin"; string Table_Comment = "系统管理员"; string Template_Name, Unit_Template, Content, Content_New; string UnitColumnDefine; string Column_Name, Column_Type, Column_Comment, Column_Null; string Relation_ClassName, Relation_InstanceName, Relation_Table_Name; string Relation_Unit_Template = "", Relation_Table_Comment = "", Relation_UnitColumnDefine = "";//相关列数据对象定义 string OneHasMany_Table_Comment = "", OneHasMany_Unit_Template = "", OneHasMany_UnitColumnDefine = ""; foreach (string Table_Name in TableList) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/domain.txt"; Content = UtilFile.ReadFile2String(Template_Name); ClassName = Table_Name; if (TableInfoList.ContainsKey(Table_Name)) { Table_Comment = TableInfoList[Table_Name]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = ""; foreach (string Comment_Single_Line in t_c) { Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } } else { Table_Comment = " /// " + Table_Comment; } Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; UnitColumnDefine = ""; Relation_UnitColumnDefine = ""; OneHasMany_UnitColumnDefine = ""; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; Column_Comment = entry.Value["Comment"]; Column_Type = entry.Value["Type"]; string[] c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Column_Comment = ""; foreach (string Comment_Single_Line in c_c) { Column_Comment += " /// " + Comment_Single_Line + "\r\n"; } Column_Comment = Column_Comment.Substring(0, Column_Comment.Length - 2); } else { Column_Comment = " /// " + Column_Comment; } Unit_Template = @" /// <summary> {$Column_Comment} /// </summary> public {$Column_Type} {$Column_Name} { get; set; } "; bool IsNull = true; Column_Null = entry.Value["Null"]; if (Column_Null.Equals("否")) { IsNull = false; } Column_Type = ColumnTypeByDbDefine(Column_Type, Column_Name, IsNull); Unit_Template = Unit_Template.Replace("{$Column_Comment}", Column_Comment); Unit_Template = Unit_Template.Replace("{$Column_Type}", Column_Type); Unit_Template = Unit_Template.Replace("{$Column_Name}", Column_Name); UnitColumnDefine += Unit_Template; if (Column_Name.Contains("_ID")) { Relation_ClassName = Column_Name.Replace("_ID", ""); if (TableList.Contains(Relation_ClassName)) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/httpdata.txt"; Content = UtilFile.ReadFile2String(Template_Name); Relation_InstanceName = UtilString.LcFirst(Relation_ClassName); Relation_Table_Name = Relation_ClassName; if (TableInfoList.ContainsKey(Relation_Table_Name)) { Relation_Table_Comment = TableInfoList[Relation_Table_Name]["Comment"]; t_c = Relation_Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Relation_Table_Comment = ""; foreach (string Comment_Single_Line in c_c) { Relation_Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } Relation_Table_Comment = Relation_Table_Comment.Substring(0, Column_Comment.Length - 2); } else { Relation_Table_Comment = " /// " + Relation_Table_Comment; } //多对一关系需定义 Relation_Unit_Template = @" /// <summary> {$Relation_Table_Comment} /// </summary> public virtual {$Relation_ClassName} {$Relation_ClassName} { get; set; }"; Relation_Unit_Template = Relation_Unit_Template.Replace("{$Relation_ClassName}", Relation_ClassName); Relation_Unit_Template = Relation_Unit_Template.Replace("{$Relation_Table_Comment}", Relation_Table_Comment); Relation_UnitColumnDefine += Relation_Unit_Template; } } } } if (OneHasManyDefine.ContainsKey(Table_Name)) { List <string> lOneHasMany = OneHasManyDefine[Table_Name]; foreach (string OneHasMany_TableName in lOneHasMany) { OneHasMany_Table_Comment = TableInfoList[OneHasMany_TableName]["Comment"]; t_c = OneHasMany_Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { OneHasMany_Table_Comment = ""; foreach (string Comment_Single_Line in t_c) { OneHasMany_Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } OneHasMany_Table_Comment = OneHasMany_Table_Comment.Substring(0, OneHasMany_Table_Comment.Length - 2); } else { OneHasMany_Table_Comment = " /// " + OneHasMany_Table_Comment; } OneHasMany_Unit_Template = @" /// <summary> {$OneHasMany_Table_Comment} /// </summary> public virtual ICollection<{$OneHasMany_TableName}> {$OneHasMany_TableName} { get; set; } "; OneHasMany_Unit_Template = OneHasMany_Unit_Template.Replace("{$OneHasMany_Table_Comment}", OneHasMany_Table_Comment); OneHasMany_Unit_Template = OneHasMany_Unit_Template.Replace("{$OneHasMany_TableName}", OneHasMany_TableName); OneHasMany_UnitColumnDefine += OneHasMany_Unit_Template; } } UnitColumnDefine += Relation_UnitColumnDefine; UnitColumnDefine += OneHasMany_UnitColumnDefine; Content_New = Content_New.Replace("{$ColumnDefines}", UnitColumnDefine); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + ClassName + ".cs", Content_New); } } }
/// <summary> /// 4.实体类有外键的实体类需要生成HttpData 文件 /// 例如Admin有外键Department_ID,会生成Department的HttpData类【多个文件】 /// [模板文件]:domain/httpdata.txt|domain/httpdatadefine.txt /// [生成文件名称]:ClassName|ClassName /// [生成文件后缀名]:.ashx.cs|.ashx" /// </summary> private void CreateHttpData() { string Relation_ClassName = "Admin"; string Relation_InstanceName = "admin"; string Table_Comment = "系统管理员"; string Relation_Table_Name, Relation_Column_Name = "", Relation_Column_Level; string UnitTemplate, Template_Name, Content, Content_New; string ClassName; string Column_Name, Column_Comment; foreach (string Table_Name in TableList) { ClassName = Table_Name; Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; Column_Comment = entry.Value["Comment"]; if (Column_Name.Contains("_ID")) { Relation_ClassName = Column_Name.Replace("_ID", ""); Relation_Table_Name = Relation_ClassName; if (TableList.Contains(Relation_ClassName) || (Relation_Table_Name.ToUpper().Equals("PARENT"))) { if (Relation_Table_Name.ToUpper().Equals("PARENT")) { string Save_Dir_Tree = Save_Dir + Path.DirectorySeparatorChar + "Tree" + Path.DirectorySeparatorChar; if (!Directory.Exists(Save_Dir_Tree)) { UtilFile.CreateDir(Save_Dir_Tree); } //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/httpdatatree.txt"; Content = UtilFile.ReadFile2String(Template_Name); Relation_InstanceName = UtilString.LcFirst(ClassName); Relation_Column_Name = ""; Relation_Column_Level = ""; Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[ClassName]; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { Relation_Column_Name = relation_entry.Key; if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { break; } } foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { if (UtilString.Contains(relation_entry.Key.ToUpper(), "LEVEL")) { Relation_Column_Level = relation_entry.Key; break; } } if (string.IsNullOrEmpty(Relation_Column_Level)) { foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { if (UtilString.Contains(relation_entry.Key.ToUpper(), "TYPE")) { Relation_Column_Level = relation_entry.Key; break; } } bool IsPermitNull = true; if (entry.Value["Null"].Equals("否")) { IsPermitNull = false; } if (IsPermitNull) { UnitTemplate = @" int? level=Convert.ToInt16({$InstanceName}.{$Relation_Column_Level});"; } else { UnitTemplate = @" int level=Convert.ToInt16({$InstanceName}.{$Relation_Column_Level});"; } } else { bool IsPermitNull = true; if (entry.Value["Null"].Equals("否")) { IsPermitNull = false; } if (IsPermitNull) { UnitTemplate = @" int? level={$InstanceName}.{$Relation_Column_Level};"; } else { UnitTemplate = @" int level={$InstanceName}.{$Relation_Column_Level};"; } } UnitTemplate = UnitTemplate.Replace("{$InstanceName}", Relation_InstanceName); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Level}", Relation_Column_Level); if (TableInfoList.ContainsKey(ClassName) && TableInfoList[ClassName].ContainsKey("Comment")) { Table_Comment = TableInfoList[ClassName]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = t_c[0]; } } Column_Comment = entry.Value["Comment"]; string[] c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length >= 1) { Column_Comment = c_c[0]; } Column_Comment = Column_Comment.Replace("标识", ""); Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Content_New = Content_New.Replace("{$InstanceName}", Relation_InstanceName); Content_New = Content_New.Replace("{$Relation_Column_Name}", Relation_Column_Name); Content_New = Content_New.Replace("{$Relation_Column_Level_Name}", Relation_Column_Level); Content_New = Content_New.Replace("{$Relation_Column_Level}", UnitTemplate); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir_Tree + ClassName + "Tree.ashx.cs", Content_New); //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/httpdatatreedefine.txt"; Content = UtilFile.ReadFile2String(Template_Name); Content_New = Content.Replace("{$ClassName}", ClassName); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir_Tree + ClassName + "Tree.ashx", Content_New); } else if (TableInfoList.ContainsKey(Relation_Table_Name)) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/httpdata.txt"; Content = UtilFile.ReadFile2String(Template_Name); if (TableInfoList.ContainsKey(Relation_ClassName)) { Table_Comment = TableInfoList[Relation_ClassName]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = t_c[0]; } Relation_InstanceName = UtilString.LcFirst(Relation_ClassName); Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[Relation_Table_Name]; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { Relation_Column_Name = relation_entry.Key; if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { break; } } Content_New = Content.Replace("{$ClassName}", Relation_ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Content_New = Content_New.Replace("{$InstanceName}", Relation_InstanceName); Content_New = Content_New.Replace("{$Column_Name}", Column_Name); Content_New = Content_New.Replace("{$Relation_Column_Name}", Relation_Column_Name); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + Relation_ClassName + ".ashx.cs", Content_New); //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/httpdatadefine.txt"; Content = UtilFile.ReadFile2String(Template_Name); Content_New = Content.Replace("{$ClassName}", Relation_ClassName); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + Relation_ClassName + ".ashx", Content_New); } } } } } } }
/// <summary> /// 1a.生成实体类分部类(显示属性) /// </summary> private void CreateDomainPartial() { string ClassName = "Admin"; string InstanceName = "admin"; string Table_Comment = "系统管理员"; string Template_Name, UnitTemplate, Content, Content_New; string Column_Name, Column_Comment, Column_Type, Column_Length; string Relation_ClassName, Relation_Column_Comment, Relation_Table_Name, Relation_Column_Name; string UnitColumnDefine; foreach (string Table_Name in TableList) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/domain.txt"; Content = UtilFile.ReadFile2String(Template_Name); ClassName = Table_Name; if (TableInfoList.ContainsKey(Table_Name)) { Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; UnitColumnDefine = ""; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; Column_Comment = entry.Value["Comment"]; Column_Type = entry.Value["Type"]; Column_Length = entry.Value["Length"]; string[] c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length >= 1) { Column_Comment = c_c[0]; } int iLength = UtilNumber.Parse(Column_Length); if (Column_Type.Equals("tinyint")) { Column_Comment = entry.Value["Comment"]; c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Column_Comment = ""; foreach (string Comment_Single_Line in c_c) { Column_Comment += " /// " + Comment_Single_Line + "\r\n"; } Column_Comment = Column_Comment.Substring(0, Column_Comment.Length - 2); UnitTemplate = @" /// <summary> {$Column_Comment} /// </summary> public String {$Column_Name}Show { get; set; }"; UnitTemplate = UnitTemplate.Replace("{$Column_Comment}", Column_Comment); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); UnitColumnDefine += UnitTemplate; } } else if (Column_Name.Contains("_ID")) { Relation_ClassName = Column_Name.Replace("_ID", ""); if (TableList.Contains(Relation_ClassName) || (Relation_ClassName.ToUpper().Equals("PARENT"))) { if (Relation_ClassName.ToUpper().Equals("PARENT")) { Relation_Table_Name = Relation_ClassName; Relation_Column_Name = ""; Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[ClassName]; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { Relation_Column_Name = relation_entry.Key; if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { break; } } Column_Comment = Column_Comment.Replace("标识", ""); UnitTemplate = @" /// <summary> /// 显示{$Column_Comment} /// </summary> public String {$Relation_Column_Name}_Parent { get; set; } /// <summary> /// 显示{$Column_Comment}[全] /// </summary> public String {$ClassName}ShowAll { get; set; } "; UnitTemplate = UnitTemplate.Replace("{$Column_Comment}", Column_Comment); UnitTemplate = UnitTemplate.Replace("{$ClassName}", ClassName); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Name}", Relation_Column_Name); UnitColumnDefine += UnitTemplate; } else if (TableInfoList.ContainsKey(Relation_ClassName)) { Relation_Table_Name = Relation_ClassName; Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[Relation_Table_Name]; Relation_Column_Name = Column_Name; Relation_Column_Comment = Relation_Column_Name; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { Relation_Column_Name = relation_entry.Key; if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { Relation_Column_Comment = relation_entry.Value["Comment"]; break; } } UnitTemplate = @" /// <summary> /// {$Relation_Column_Comment} /// </summary> public String {$Relation_Column_Name} { get; set; }"; UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Comment}", Relation_Column_Comment); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Name}", Relation_Column_Name); UnitColumnDefine += UnitTemplate; } } } else if (ColumnIsTextArea(Column_Name, Column_Type, iLength)) { UnitTemplate = @" /// <summary> /// {$Column_Comment} /// </summary> public String {$Column_Name}Show { get; set; }"; Column_Comment = entry.Value["Comment"]; c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Column_Comment = ""; foreach (string Comment_Single_Line in c_c) { Column_Comment += " /// " + Comment_Single_Line + "\r\n"; } Column_Comment = Column_Comment.Substring(0, Column_Comment.Length - 2); } UnitTemplate = UnitTemplate.Replace("{$Column_Comment}", Column_Comment); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); UnitColumnDefine += UnitTemplate; } } if (OneHasManyDefine.ContainsKey(Table_Name)) { UnitTemplate = @" /// <summary> /// 辅助主键 ///【用于ExtJs Combo主键下拉框显示ID以和主表ID名称区别开来】 /// </summary> public String {$Table_Name}_ID { get; set; }"; UnitTemplate = UnitTemplate.Replace("{$Table_Name}", Table_Name); UnitColumnDefine += UnitTemplate; } if (!string.IsNullOrEmpty(UnitColumnDefine)) { Table_Comment = TableInfoList[Table_Name]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = ""; foreach (string Comment_Single_Line in t_c) { Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } } else { Table_Comment = " /// " + Table_Comment; } Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Content_New = Content_New.Replace("{$InstanceName}", InstanceName); Content_New = Content_New.Replace("{$ColumnDefines}", UnitColumnDefine); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + ClassName + ".cs", Content_New); } } } }
/// <summary> /// 初始化工作 /// </summary> protected void Init() { UtilSqlserver.Database_Name = Database_Name; if (string.IsNullOrEmpty(App_Dir)) { App_Dir = Directory.GetCurrentDirectory(); App_Dir = App_Dir + Path.DirectorySeparatorChar + "Model" + Path.DirectorySeparatorChar; } if (TableInfoList == null) { TableInfoList = UtilSqlserver.TableinfoList(); TableList = UtilSqlserver.TableList().Keys.ToList(); } if (FieldInfos == null) { FieldInfos = new Dictionary <string, Dictionary <string, Dictionary <string, string> > >(); foreach (string Table_Name in TableList) { FieldInfos.Add(Table_Name, UtilSqlserver.FieldInfoList(Table_Name)); } } if (OneHasManyDefine == null) { OneHasManyDefine = new Dictionary <string, List <string> >(); string Column_Name, Relation_ClassName; List <string> lRelation_TableName = null; foreach (string Table_Name in TableList) { Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; if (Column_Name.Contains("_ID")) { Relation_ClassName = Column_Name.Replace("_ID", ""); if (TableList.Contains(Relation_ClassName)) { if (lRelation_TableName == null) { lRelation_TableName = new List <string>(); } if (OneHasManyDefine.Keys.Contains(Relation_ClassName)) { lRelation_TableName = OneHasManyDefine[Relation_ClassName]; } else { lRelation_TableName = new List <string>(); OneHasManyDefine[Relation_ClassName] = lRelation_TableName; } if (!lRelation_TableName.Contains(Table_Name)) { lRelation_TableName.Add(Table_Name); } } } } } } Same_Column_Names = UtilSqlserver.Same_Column_Name_In_TableName(); //UtilObjectDump.WriteLine(Same_Column_Names); }
/// <summary> /// 2.Business/Admin后台所有ExtService服务类【多个文件】 /// [模板文件]:service/extservice.txt|service/extservicedefine.txt /// [生成文件名称]:"ExtService"+ClassName|"ExtService"+ClassName /// [生成文件后缀名]:.ashx.cs|.ashx /// </summary> private void CreateExtService() { string ClassName = "Admin"; string InstanceName = "admin"; string Table_Comment = "系统管理员"; string Service_NameSpace = "AdminManage"; string Template_Name, UnitTemplate, Content, Content_New; string ColumnNameComment, ColumnCommentName, EnumColumnName; string Column_Name, Column_Table_Name, Column_Comment, Column_Type, Column_Length; string ImportConvertDataToShow, ExportConvertShowToData; string SpecialResult = ""; string Relation_ClassName, Relation_InstanceName; string Relation_Table_Name, Relation_Column_Name, TreeInstanceDefine, Relation_Column_Level, RelationFieldTreeRecursive; string ImgUploadSrc = "", Relation_Parent_Init = ""; bool IsImage; foreach (string Table_Name in TableList) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/service/extservice.txt"; Content = UtilFile.ReadFile2String(Template_Name); ClassName = Table_Name; RelationFieldTreeRecursive = ""; TreeInstanceDefine = ""; if (TableInfoList.ContainsKey(Table_Name)) { Table_Comment = TableInfoList[Table_Name]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = t_c[0]; } InstanceName = UtilString.LcFirst(ClassName); Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Content_New = Content_New.Replace("{$InstanceName}", InstanceName); Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; ColumnNameComment = ""; ColumnCommentName = ""; EnumColumnName = ""; SpecialResult = ""; ImportConvertDataToShow = ""; ExportConvertShowToData = ""; ImgUploadSrc = ""; Relation_Parent_Init = ""; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { IsImage = false; Column_Name = entry.Key; Column_Comment = entry.Value["Comment"]; Column_Type = entry.Value["Type"]; Column_Length = entry.Value["Length"]; string[] c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length >= 1) { Column_Comment = c_c[0]; } if (!((Column_Name.ToUpper().Equals(CommitTime_Str.ToUpper())) || (Column_Name.ToUpper().Equals(UpdateTime_Str.ToUpper())))) { if (!Column_Type.Equals("tinyint")) { ColumnNameComment += " {\"" + Column_Name + "\",\"" + Column_Comment + "\"},\r\n"; ColumnCommentName += " {\"" + Column_Comment + "\",\"" + Column_Name + "\"},\r\n"; } } int iLength = UtilNumber.Parse(Column_Length); IsImage = ColumnIsImage(Column_Name, Column_Comment); if (IsImage) { UnitTemplate = @" Dictionary<string,object> uploadResult=this.UploadImage({$InstanceName}Form.Files,""{$Column_Name}Upload"",""{$Column_Name}"",""{$InstanceName}""); if ((uploadResult!=null)&&((bool)uploadResult[""success""]==true)&&(uploadResult.Keys.Contains(""file_name""))){ {$InstanceName}.{$Column_Name}=(string)uploadResult[""file_name""]; }"; UnitTemplate = UnitTemplate.Replace("{$InstanceName}", InstanceName); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); ImgUploadSrc += UnitTemplate; } else if (Column_Type.Equals("tinyint")) { Column_Comment = entry.Value["Comment"]; c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { UnitTemplate = @" {$InstanceName}.{$Column_Name}Show = Enum{$Column_Table_Name}.{$Column_Name}Show(Convert.ToChar({$InstanceName}.{$Column_Name}));"; UnitTemplate = UnitTemplate.Replace("{$InstanceName}", InstanceName); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); Column_Table_Name = Column_Name; if (Array.IndexOf(Same_Column_Names, Column_Name) > -1) { Column_Table_Name = ClassName + "_" + Column_Name; } UnitTemplate = UnitTemplate.Replace("{$Column_Table_Name}", Column_Table_Name); SpecialResult += UnitTemplate; Column_Comment = c_c[0].Trim(); EnumColumnName += "\"" + Column_Name + "\","; ColumnNameComment += " {\"" + Column_Name + "Show\",\"" + Column_Comment + "\"},\r\n"; ColumnCommentName += " {\"" + Column_Comment + "\",\"" + Column_Name + "Show\"},\r\n"; ExportConvertShowToData += " " + InstanceName + "." + Column_Name + "Show = Enum" + Column_Name + "." + Column_Name + "Show(" + InstanceName + "." + Column_Name + ");\r\n"; ImportConvertDataToShow += " " + InstanceName + "." + Column_Name + " = Enum" + Column_Name + "." + Column_Name + "ByShow(" + InstanceName + "." + Column_Name + "Show);\r\n"; } } else if (Column_Name.Contains("_ID")) { Relation_Table_Name = Column_Name.Replace("_ID", ""); Relation_ClassName = Relation_Table_Name; if (TableList.Contains(Relation_ClassName) || (Relation_ClassName.ToUpper().Equals("PARENT"))) { if (Relation_Table_Name.ToUpper().Equals("PARENT")) { Relation_Table_Name = Table_Name; Relation_ClassName = ClassName; Relation_Column_Name = ""; Relation_Column_Level = ""; Relation_InstanceName = UtilString.LcFirst(Relation_ClassName); Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[Table_Name]; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { Relation_Column_Name = relation_entry.Key; break; } } bool IsPermitNull = true; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { if (UtilString.Contains(relation_entry.Key.ToUpper(), "LEVEL")) { Relation_Column_Level = relation_entry.Key; if (relation_entry.Value["Null"].Equals("否")) { IsPermitNull = false; } break; } } TreeInstanceDefine = @" {$Relation_ClassName} {$Relation_InstanceName}_instance;"; if (string.IsNullOrEmpty(Relation_Column_Level)) { foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { if (UtilString.Contains(relation_entry.Key.ToUpper(), "TYPE")) { Relation_Column_Level = relation_entry.Key; break; } } UnitTemplate = @" {$Relation_InstanceName}_instance=null; if ({$Relation_InstanceName}.Parent_ID!=null){ {$Relation_InstanceName}_instance=db.{$Relation_ClassName}.Find({$Relation_InstanceName}.Parent_ID); {$Relation_InstanceName}.{$Relation_Column_Name}_Parent={$Relation_InstanceName}_instance.{$Relation_Column_Name}; } if ({$Relation_InstanceName}_instance!=null){ int level = (int){$Relation_InstanceName}_instance.{$Relation_Column_Level}; {$Relation_InstanceName}.{$Relation_ClassName}ShowAll=this.{$Relation_ClassName}ShowAll({$Relation_InstanceName}.Parent_ID,level); }"; RelationFieldTreeRecursive = @" /// <summary> /// 显示{$Column_Comment}[全] /// 注:采用了递归写法 /// </summary> /// <param name=""Parent_ID"">{$Column_Comment}标识</param> /// <param name=""level"">目录层级</param> /// <returns></returns> private string {$Relation_ClassName}ShowAll(int? Parent_ID,int level) { string {$Relation_ClassName}ShowAll=""""; {$Relation_ClassName} {$Relation_InstanceName}_p=db.{$Relation_ClassName}.Find(Parent_ID); if (level<=0){ {$Relation_ClassName}ShowAll={$Relation_InstanceName}_p.{$Relation_Column_Name}; }else{ Parent_ID={$Relation_InstanceName}_p.Parent_ID; {$Relation_ClassName}ShowAll=this.{$Relation_ClassName}ShowAll(Parent_ID,level-1)+""->""+{$Relation_InstanceName}_p.{$Relation_Column_Name}; } return {$Relation_ClassName}ShowAll; } "; } else { string ColumnTypeNull = "int"; if (IsPermitNull) { ColumnTypeNull += "?"; } UnitTemplate = @" {$Relation_InstanceName}_instance=null; if ({$Relation_InstanceName}.Parent_ID!=null){ {$Relation_InstanceName}_instance=db.{$Relation_ClassName}.Find({$Relation_InstanceName}.Parent_ID); {$Relation_InstanceName}.{$Relation_Column_Name}_Parent={$Relation_InstanceName}_instance.{$Relation_Column_Name}; } if ({$Relation_InstanceName}_instance!=null){ {$ColumnTypeNull} level = {$Relation_InstanceName}_instance.{$Relation_Column_Level}; {$Relation_InstanceName}.{$Relation_ClassName}ShowAll=this.{$Relation_ClassName}ShowAll({$Relation_InstanceName}.Parent_ID,level); }"; UnitTemplate = UnitTemplate.Replace("{$ColumnTypeNull}", ColumnTypeNull); RelationFieldTreeRecursive = @" /// <summary> /// 显示{$Column_Comment}[全] /// 注:采用了递归写法 /// </summary> /// <param name=""Parent_ID"">{$Column_Comment}标识</param> /// <param name=""level"">目录层级</param> /// <returns></returns> private string {$Relation_ClassName}ShowAll(int? Parent_ID,int level) { string {$Relation_ClassName}ShowAll=""""; {$Relation_ClassName} {$Relation_InstanceName}_p=db.{$Relation_ClassName}.Find(Parent_ID); if (level<=1){ {$Relation_ClassName}ShowAll={$Relation_InstanceName}_p.{$Relation_Column_Name}; }else{ Parent_ID={$Relation_InstanceName}_p.Parent_ID; {$Relation_ClassName}ShowAll=this.{$Relation_ClassName}ShowAll(Parent_ID,level-1)+""->""+{$Relation_InstanceName}_p.{$Relation_Column_Name}; } return {$Relation_ClassName}ShowAll; } "; } UnitTemplate = UnitTemplate.Replace("{$Relation_InstanceName}", Relation_InstanceName); UnitTemplate = UnitTemplate.Replace("{$Relation_ClassName}", Relation_ClassName); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Name}", Relation_Column_Name); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Level}", Relation_Column_Level); TreeInstanceDefine = TreeInstanceDefine.Replace("{$Relation_InstanceName}", Relation_InstanceName); TreeInstanceDefine = TreeInstanceDefine.Replace("{$Relation_ClassName}", Relation_ClassName); SpecialResult += UnitTemplate; Column_Comment = entry.Value["Comment"]; c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length >= 1) { Column_Comment = c_c[0]; } Column_Comment = Column_Comment.Replace("标识", ""); RelationFieldTreeRecursive = RelationFieldTreeRecursive.Replace("{$Column_Comment}", Column_Comment); RelationFieldTreeRecursive = RelationFieldTreeRecursive.Replace("{$Relation_InstanceName}", Relation_InstanceName); RelationFieldTreeRecursive = RelationFieldTreeRecursive.Replace("{$Relation_ClassName}", Relation_ClassName); RelationFieldTreeRecursive = RelationFieldTreeRecursive.Replace("{$Relation_Column_Name}", Relation_Column_Name); UnitTemplate = @" if ({$InstanceName}.{$Column_Name} == 0) {$InstanceName}.{$Column_Name} = null;"; UnitTemplate = UnitTemplate.Replace("{$InstanceName}", InstanceName); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); Relation_Parent_Init += UnitTemplate; } else if (TableInfoList.ContainsKey(Relation_Table_Name)) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/httpdata.txt"; Content = UtilFile.ReadFile2String(Template_Name); Relation_InstanceName = UtilString.LcFirst(Relation_ClassName); Relation_Table_Name = Relation_ClassName; Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[Relation_Table_Name]; Relation_Column_Name = Column_Name; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { Relation_Column_Name = relation_entry.Key; if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { break; } } UnitTemplate = @" {$InstanceName}.{$Relation_Column_Name} = {$InstanceName}.{$Relation_ClassName}.{$Relation_Column_Name};"; UnitTemplate = UnitTemplate.Replace("{$InstanceName}", InstanceName); UnitTemplate = UnitTemplate.Replace("{$Relation_ClassName}", Relation_ClassName); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Name}", Relation_Column_Name); SpecialResult += UnitTemplate; UnitTemplate = @" {$Relation_ClassName} {$Relation_InstanceName} = db.{$Relation_ClassName}.Where(e => e.{$Relation_Column_Name}.Equals({$InstanceName}.{$Relation_Column_Name})).SingleOrDefault(); {$InstanceName}.{$Column_Name} = {$Relation_InstanceName}.ID; "; UnitTemplate = UnitTemplate.Replace("{$InstanceName}", InstanceName); UnitTemplate = UnitTemplate.Replace("{$Relation_InstanceName}", Relation_InstanceName); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); UnitTemplate = UnitTemplate.Replace("{$Relation_ClassName}", Relation_ClassName); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Name}", Relation_Column_Name); ImportConvertDataToShow += UnitTemplate; } } } else if (ColumnIsTextArea(Column_Name, Column_Type, iLength)) { UnitTemplate = @" if (!string.IsNullOrEmpty({$InstanceName}.{$Column_Name})) { {$InstanceName}.{$Column_Name}Show = Regex.Replace({$InstanceName}.{$Column_Name}, ""<\\s*img\\s+[^>]*?src\\s*=\\s*(\'|\"")(.*?)\\1[^>]*?\\/?\\s*>"", ""<a href='${2}' target='_blank'>${0}</a>""); {$InstanceName}.{$Column_Name}Show = {$InstanceName}.{$Column_Name}Show.Replace(""\\\"""", """"); }"; UnitTemplate = UnitTemplate.Replace("{$InstanceName}", InstanceName); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); SpecialResult += UnitTemplate; } } ColumnNameComment = ColumnNameComment.Substring(0, ColumnNameComment.Length - 3); ColumnCommentName = ColumnCommentName.Substring(0, ColumnCommentName.Length - 3); Content_New = Content_New.Replace("{$ColumnNameComment}", ColumnNameComment); Content_New = Content_New.Replace("{$ColumnCommentName}", ColumnCommentName); SpecialResult += @" this.Stores.Add((" + ClassName + ")ClearInclude(" + InstanceName + "));"; SpecialResult = SpecialResult.Substring(1, SpecialResult.Length - 1); Content_New = Content_New.Replace("{$SpecialResult}", SpecialResult); Content_New = Content_New.Replace("{$Service_NameSpace}", Service_NameSpace); Content_New = Content_New.Replace("{$CommitTime_Str}", CommitTime_Str); Content_New = Content_New.Replace("{$UpdateTime_Str}", UpdateTime_Str); Content_New = Content_New.Replace("{$EnumColumnName}", EnumColumnName); Content_New = Content_New.Replace("{$ImportConvertDataToShow}", ImportConvertDataToShow); Content_New = Content_New.Replace("{$ExportConvertShowToData}", ExportConvertShowToData); Content_New = Content_New.Replace("{$RelationFieldTreeRecursive}", RelationFieldTreeRecursive); Content_New = Content_New.Replace("{$TreeInstanceDefine}", TreeInstanceDefine); Content_New = Content_New.Replace("{$ImgUploadSrc}", ImgUploadSrc); Content_New = Content_New.Replace("{$Relation_Parent_Init}", Relation_Parent_Init); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + "ExtService" + ClassName + ".ashx.cs", Content_New); //读取原文件内容到内存 Template_Name = @"AutoCode/Model/service/extservicedefine.txt"; Content = UtilFile.ReadFile2String(Template_Name); Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Service_NameSpace}", Service_NameSpace); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + "ExtService" + ClassName + ".ashx", Content_New); } } }