/// <summary> /// 根据类名获取表代表列显示名称 /// </summary> /// <param name="ClassName">数据对象类名</param> /// <param name="IsReturnNull">是否没有就返回Null</param> /// <returns></returns> protected string GetShowFieldNameByClassname(string ClassName, bool IsReturnNull = true) { string TableName = ClassName; Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[TableName]; List <string> FieldNames = FieldInfo.Keys.ToList(); foreach (string FieldName in FieldNames) { string FieldName_Filter = FieldName.ToLower(); if (!FieldName.ToLower().Contains("id")) { if (UtilString.Contains(FieldName.ToLower(), "name", "title")) { return(FieldName); } string ClassName_Filter = ClassName.ToLower(); if (FieldName.ToLower().Contains(ClassName_Filter)) { return(FieldName); } } } if (IsReturnNull) { return(""); } else { return("Name"); } }
/// <summary> /// 列是否是Email /// </summary> /// <param name="Column_Name">列名称</param> /// <param name="Column_Comment">列注释</param> protected bool ColumnIsEmail(string Column_Name, string Column_Comment) { Column_Name = Column_Name.ToUpper(); if (Column_Name.Contains("EMAIL") || (UtilString.Contains(Column_Comment, "邮件", "邮箱") && (!Column_Name.Contains("IS")))) { return(true); } return(false); }
/// <summary> /// 列是否是密码 /// </summary> /// <param name="Table_Name">表名称</param> /// <param name="Column_Name">列名称</param> protected bool ColumnIsPassword(string Table_Name, string Column_Name) { Table_Name = Table_Name.ToUpper(); if (UtilString.Contains(Table_Name, "MEMBER", "ADMIN", "USER")) { Column_Name = Column_Name.ToUpper(); if (Column_Name.Contains("PASSWORD")) { return(true); } } return(false); }
/// <summary> /// 列是否是图片路径 /// </summary> /// <param name="Column_Name">列名称</param> /// <param name="Column_Comment">列注释</param> protected bool ColumnIsImage(string Column_Name, string Column_Comment) { Column_Name = Column_Name.ToUpper(); if (Column_Name.Contains("ID")) { return(false); } if (UtilString.Contains(Column_Name, "PROFILE", "IMAGE", "IMG", "ICO", "LOGO", "PIC")) { return(true); } return(false); }
/// <summary> /// 列是否大量文本输入应该TextArea输入 /// </summary> /// <param name="Column_Name">列名称</param> /// <param name="Column_Type">列类型</param> protected bool ColumnIsTextArea(string Column_Name, string Column_Type, int Column_Length) { Column_Name = Column_Name.ToUpper(); if (Column_Name.Contains("ID")) { return(false); } if ((Column_Length >= 500) && (!UtilString.Contains(Column_Name, "URL", "PROFILE", "IMAGES", "LINK", "ICO", "PASSWORD", "EMAIL", "PHONE", "ADDRESS")) || (UtilString.Contains("INTRO", "MEMO", "CONTENT")) || (Column_Type.ToUpper().Contains("TEXT"))) { return(true); } return(false); }
/// <summary> /// 表关系主键显示配置 /// </summary> /// <param name="Table_Name">表名称</param> /// <param name="relationShowsElement">表关系主键显示配置生成元素</param> private void RelationShowsToConfig(string Table_Name, XElement relationShowsElement) { string ClassName = Table_Name; Dictionary <string, Dictionary <string, string> > Fields = FieldInfos[Table_Name]; foreach (KeyValuePair <string, Dictionary <string, string> > Entry in Fields) { string FieldName = Entry.Key; if (FieldName.ToUpper().Equals("ID")) { continue; } if (UtilString.Contains(FieldName.ToUpper(), "COMMITTIME", "UPDATETIME")) { continue; } if (FieldName.ToUpper().Contains("_ID")) { string Relation_Classname = FieldName.Replace("_ID", ""); Relation_Classname = UtilString.UcFirst(Relation_Classname); string ShowFieldName = ""; if (Relation_Classname.ToUpper().Equals("PARENT")) { ShowFieldName = GetShowFieldNameByClassname(ClassName); if (!string.IsNullOrEmpty(ShowFieldName)) { relationShowsElement.Add(new XElement("show", ShowFieldName, new XAttribute("local_key", FieldName), new XAttribute("relation_class", ClassName))); } } else { if (TableList.Contains(Relation_Classname)) { ShowFieldName = GetShowFieldNameByClassname(Relation_Classname); if (!string.IsNullOrEmpty(ShowFieldName)) { relationShowsElement.Add(new XElement("show", ShowFieldName, new XAttribute("local_key", FieldName), new XAttribute("relation_class", Relation_Classname))); } } } } } }
/// <summary> /// 获取列注释第一行关键词说明 /// </summary> /// <param name="field_comment">列注释</param> /// <param name="Default">默认返回值</param> /// <returns></returns> protected string ColumnCommentKey(string Column_Comment, string Default = "") { if (string.IsNullOrEmpty(Column_Comment)) { Column_Comment = Default; } else { string[] c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Column_Comment = c_c[0]; } if (UtilString.Contains(Column_Comment, "标识", "编号", "主键")) { Column_Comment = Column_Comment.Replace("标识", ""); Column_Comment = Column_Comment.Replace("编号", ""); Column_Comment = Column_Comment.Replace("主键", ""); } } return(Column_Comment); }
/// <summary> /// 表五种关系映射配置 /// </summary> /// <see cref="http://www.xmlplease.com/update-xml-linq" title="Update XML with LINQ to XML in ASP.NET"/> /// <param name="Table_Name">表名称</param> /// <param name="ClassElement">表五种关系映射配置</param> private void RelationFives(string Table_Name, XDocument XmlDoc) { XElement ClassElement = XmlDoc.XPathSelectElement("classes/class[@name='" + Table_Name + "']"); XElement Has_OneElement = new XElement("has_one"); XElement Belong_Has_OneElement = new XElement("belong_has_one"); XElement Has_ManyElement = new XElement("has_many"); XElement Many_ManyElement = new XElement("many_many"); XElement Belongs_Many_ManyElement = new XElement("belongs_many_many"); string ClassName = Table_Name; Dictionary <string, Dictionary <string, string> > Fields = FieldInfos[Table_Name]; XElement HasManyClassElement = null, HasOneClassElement = null; foreach (KeyValuePair <string, Dictionary <string, string> > Entry in Fields) { string FieldName = Entry.Key; if (FieldName.ToUpper().Equals("ID")) { continue; } if (UtilString.Contains(FieldName.ToUpper(), "COMMITTIME", "UPDATETIME")) { continue; } if (FieldName.ToUpper().Contains("_ID")) { string Relation_Classname = FieldName.Replace("_ID", ""); Relation_Classname = UtilString.UcFirst(Relation_Classname); if (Relation_Classname.ToUpper().Equals("PARENT")) { string instance_name = UtilString.LcFirst(ClassName); Belong_Has_OneElement.Add("relationclass", instance_name + "_p", new XAttribute("name", ClassName)); ClassElement.Add(Belong_Has_OneElement); } else { if (TableList.Contains(Relation_Classname)) { //belong_has_one:[当前表有归属表的标识,归属表没有当前表的类名+"_ID"] Dictionary <string, Dictionary <string, string> > Fields_Relation = FieldInfos[Relation_Classname]; if (!Fields_Relation.Keys.Contains(ClassName + "_ID")) { string instance_name = UtilString.LcFirst(Relation_Classname); Belong_Has_OneElement.Add(new XElement("relationclass", instance_name, new XAttribute("name", Relation_Classname))); } //has_many[归属表没有当前表的标识,当前表有归属表的标识] //has_one:[归属表没有当前表的标识,当前表有归属表的标识,并且当前表里归属表的标识为Unique] if (!Fields_Relation.Keys.Contains(ClassName + "_ID")) { string instance_name = UtilString.LcFirst(ClassName) + "s"; bool Is_Unique = false; if (Is_Unique) { HasOneClassElement = XmlDoc.XPathSelectElement("classes/class[@name='" + Relation_Classname + "']"); if (HasManyClassElement.Element("has_one") == null) { Has_OneElement.Add(new XElement("relationclass", instance_name, new XAttribute("name", ClassName))); HasOneClassElement.Add(Has_OneElement); } else { HasOneClassElement.Element("has_one").Add(new XElement("relationclass", instance_name, new XAttribute("name", ClassName))); } } else { bool is_create_hasmany = true; if (IsMany2ManyByClassname(ClassName)) { is_create_hasmany = false; } if (is_create_hasmany) { HasManyClassElement = XmlDoc.XPathSelectElement("classes/class[@name='" + Relation_Classname + "']"); if (HasManyClassElement.Element("has_many") == null) { Has_ManyElement.Add(new XElement("relationclass", instance_name, new XAttribute("name", ClassName))); HasManyClassElement.Add(Has_ManyElement); } else { HasManyClassElement.Element("has_many").Add(new XElement("relationclass", instance_name, new XAttribute("name", ClassName))); } } } } } } } } if (Belong_Has_OneElement.Elements().Count() > 0) { ClassElement.Add(Belong_Has_OneElement); } if (IsMany2ManyByClassname(ClassName)) { Dictionary <string, Dictionary <string, string> > FieldInfo_m2m = FieldInfos[Table_Name]; if (FieldInfo_m2m.Keys.Count() == 5) { //many_many[在关系表中有两个关系主键,并且表名的前半部分是其中一个主键] //belongs_many_many[在关系表中有两个关系主键,并且表名的后半部分是其中一个主键] string[] class_onetwo = new string[2]; int index = 0; foreach (KeyValuePair <string, Dictionary <string, string> > Entry in Fields) { string FieldName = Entry.Key; if (FieldName.ToUpper().Contains("_ID")) { string class_onetwo_element = FieldName.Replace("_ID", ""); class_onetwo.SetValue(FieldName, index); index += 1; } } string ownerClassname = "", belongClassname = "", ownerInstancename = "", belongInstancename = ""; if ((class_onetwo[0] + "Re" + class_onetwo[1]).ToLower().Equals(ClassName.ToLower())) { ownerClassname = class_onetwo[0]; belongClassname = class_onetwo[1]; ownerInstancename = class_onetwo[0] + "s"; belongInstancename = class_onetwo[1] + "s"; } else if ((class_onetwo[1] + "Re" + class_onetwo[0]).ToLower().Equals(ClassName.ToLower())) { ownerClassname = class_onetwo[1]; belongClassname = class_onetwo[0]; ownerInstancename = class_onetwo[1] + "s"; belongInstancename = class_onetwo[0] + "s"; } ownerClassname = UtilString.UcFirst(ownerClassname); belongClassname = UtilString.UcFirst(belongClassname); XElement ClassOwnerElement = XmlDoc.XPathSelectElement("classes/class[@name='" + ownerClassname + "']"); if (ClassOwnerElement != null) { if (ClassOwnerElement.Element("many_many") == null) { Many_ManyElement.Add(new XElement("relationclass", belongInstancename, new XAttribute("name", belongClassname))); ClassOwnerElement.Add(Many_ManyElement); } else { ClassOwnerElement.Element("many_many").Add(new XElement("relationclass", belongInstancename, new XAttribute("name", belongClassname))); } } XElement ClassBelongElement = XmlDoc.XPathSelectElement("classes/class[@name='" + belongClassname + "']"); if (ClassOwnerElement != null) { if (ClassOwnerElement.Element("belongs_many_many") == null) { Belongs_Many_ManyElement.Add(new XElement("relationclass", belongInstancename, new XAttribute("name", belongClassname))); ClassBelongElement.Add(Belongs_Many_ManyElement); } else { ClassBelongElement.Element("belongs_many_many").Add(new XElement("relationclass", ownerInstancename, new XAttribute("name", ownerClassname))); } } } } }
/// <summary> /// 添加查询条件配置 /// </summary> /// <param name="Table_Name">表名称</param> /// <param name="conditionsElement">条件生成元素</param> private void ConditionsToConfig(string Table_Name, XElement conditionsElement) { string ClassName = Table_Name; List <string> Exists_Condition = new List <string>(); string ShowFieldName = ""; if (!IsMany2ManyByClassname(ClassName)) { ShowFieldName = GetShowFieldNameByClassname(ClassName); if (!string.IsNullOrEmpty(ShowFieldName)) { bool IsValid = true; string[] MultiTablesMaybe = Regex.Split(Table_Name, "Re", RegexOptions.IgnoreCase); if (MultiTablesMaybe.Count() > 1) { IsValid = false; foreach (string TableName in MultiTablesMaybe) { if (!TableList.Contains(TableName)) { IsValid = true; break; } } } if (IsValid) { if (!string.IsNullOrEmpty(ShowFieldName)) { conditionsElement.Add(new XElement("condition", ShowFieldName)); Exists_Condition.Add(ShowFieldName); } } } } Dictionary <string, Dictionary <string, string> > Fields = FieldInfos[Table_Name]; foreach (KeyValuePair <string, Dictionary <string, string> > Entry in Fields) { string Fieldname = Entry.Key; if (Fieldname.ToUpper().Equals("ID")) { continue; } if (UtilString.Contains(Fieldname.ToUpper(), "COMMITTIME", "UPDATETIME")) { continue; } if (!string.IsNullOrEmpty(ShowFieldName)) { if (!Exists_Condition.Contains(Fieldname) && UtilString.Contains(Fieldname, "Name", "Title") && (!Fieldname.Equals(ShowFieldName)) && (!Fieldname.ToUpper().Contains("_ID"))) { conditionsElement.Add(new XElement("condition", Fieldname)); Exists_Condition.Add(Fieldname); } if (conditionsElement.Elements().Count() < count_condition) { if (!Exists_Condition.Contains(Fieldname) && UtilString.Contains(Fieldname.ToUpper(), "CODE", "_NO", "STATUS", "TYPE") && (!Fieldname.ToUpper().Contains("_ID"))) { conditionsElement.Add(new XElement("condition", Fieldname)); Exists_Condition.Add(Fieldname); } if (Fieldname.ToUpper().Contains("_ID") && (!Fieldname.ToUpper().Contains("PARENT_ID"))) { string relation_classname = Fieldname.Replace("_ID", ""); relation_classname = UtilString.UcFirst(relation_classname); string ShowFieldName_Relation = ""; if (TableList.Contains(relation_classname)) { ShowFieldName_Relation = GetShowFieldNameByClassname(relation_classname); if (!Exists_Condition.Contains(ShowFieldName_Relation)) { if (!string.IsNullOrEmpty(ShowFieldName_Relation)) { conditionsElement.Add(new XElement("condition", Fieldname, new XAttribute("relation_class", relation_classname), new XAttribute("show_name", ShowFieldName_Relation))); } Exists_Condition.Add(Fieldname); } } } } else { break; } } } }
/// <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> /// 点击生成数据库说明Excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExportexcel_Click(object sender, EventArgs e) { btnExportexcel.Enabled = false; UtilExcelCom.Release(); ExcelBE be = null; Dictionary <string, Dictionary <string, string> > tableInfos; tableInfos = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.TableinfoList() : UtilMysql.TableinfoList(); int rowHeight = 25; //数据库表说明 UtilExcelCom.Current().SetSheet("Summary"); be = new ExcelBE(1, 1, "编号", "A1", "A1", "GRAYDARK", false, 10, 13.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 2, "表名称", "B1", "B1", "GRAYDARK", false, 10, 32.63, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 3, "说明", "C1", "C1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); int rowno = 2; int index = 1; int line = 1; string idIndex = ""; string tablet_name = "", table_comment = ""; string[] commentArr; ArrayList commentList; foreach (Dictionary <string, string> tablename in tableInfos.Values) { table_comment = ""; idIndex = index.ToString(); if (idIndex.Length < 2) { idIndex = "0" + idIndex; } be = new ExcelBE(rowno, 1, "A" + idIndex, "A" + rowno, "A" + rowno, null, false, 10, 13.50, rowHeight, 2, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); tablet_name = tablename["Name"]; be = new ExcelBE(rowno, 2, tablet_name, "B" + rowno, "B" + rowno, null, false, 10, 32.63, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); //添加链接 UtilExcelCom.Current().AddLink(be, tablet_name, "列名"); table_comment = tablename["Comment"]; table_comment = table_comment.Trim(); line = 1; rowHeight = 25; if (UtilString.Contains(table_comment, "\r", "\n")) { rowHeight = 16; commentArr = table_comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); rowHeight = rowHeight * line; commentList = new ArrayList(commentArr); table_comment = ""; foreach (var commenti in commentList) { table_comment += commenti + "\r\n"; line += 1; } line -= 1; table_comment = table_comment.Substring(0, table_comment.Length - 2); rowHeight = rowHeight * line + 8; } be = new ExcelBE(rowno, 3, table_comment, "C" + rowno, "C" + rowno, null, false, 10, 24.50, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); rowno++; index++; } //数据库各表详细说明 Dictionary <string, Dictionary <string, string> > columnInfos; string comment = "", dicComment = ""; foreach (Dictionary <string, string> tablename in tableInfos.Values) { rowHeight = 16; columnInfos = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.FieldInfoList(tablename["Name"]) : UtilMysql.FieldInfoList(tablename["Name"]); UtilExcelCom.Current().AddSheet(tablename["Name"]); be = new ExcelBE(1, 1, "列名", "A1", "A1", "GRAYDARK", false, 10, 36.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 2, "数据类型", "B1", "B1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 3, "长度", "C1", "C1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 4, "允许NULL", "D1", "D1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 5, "键值", "E1", "E1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 6, "说明", "F1", "F1", "GRAYDARK", false, 10, 39, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); bool isDicComment = false; foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { if (columnInfo.Keys.Contains("Comment")) { comment = columnInfo["Comment"]; } else { comment = ""; } if (UtilString.Contains(comment, "\r", "\n")) { if (columnInfo["Type"].Equals("char")) { isDicComment = true; break; } } } if (isDicComment) { be = new ExcelBE(1, 7, "数据字典定义", "G1", "G1", "GRAYDARK", false, 10, 48.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); } int tablerowno = 2; bool isColumnDicComment = false; foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { rowHeight = 16; if (columnInfo.Keys.Contains("Comment")) { comment = columnInfo["Comment"]; } else { comment = ""; } isColumnDicComment = false; dicComment = ""; line = 1; comment = comment.Trim(); if (isDicComment) { if (UtilString.Contains(comment, "\r", "\n")) { commentArr = comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); comment = commentArr[0]; commentList = new ArrayList(commentArr); commentList.Remove(0); foreach (var commenti in commentList) { dicComment += commenti + "\r\n"; line += 1; } line -= 1; dicComment = dicComment.Substring(0, dicComment.Length - 2); rowHeight = rowHeight * line + 8; if (columnInfo["Type"].Equals("char")) { isColumnDicComment = true; } else { comment = dicComment; } } else { comment = comment.Trim(); if (UtilString.Contains(comment, "\r", "\n")) { commentArr = comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); commentList = new ArrayList(commentArr); comment = ""; foreach (var commenti in commentList) { comment += commenti + "\r\n"; line += 1; } line -= 1; comment = comment.Substring(0, comment.Length - 2); rowHeight = rowHeight * line + 8; } } } else { comment = comment.Trim(); if (UtilString.Contains(comment, "\r", "\n")) { commentArr = comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); commentList = new ArrayList(commentArr); comment = ""; foreach (var commenti in commentList) { comment += commenti + "\r\n"; line += 1; } line -= 1; comment = comment.Substring(0, comment.Length - 2); rowHeight = rowHeight * line + 8; } } be = new ExcelBE(tablerowno, 1, columnInfo["Field"], "A" + tablerowno, "A" + tablerowno, null, false, 10, 18, rowHeight, 2, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); string Type_Only = ""; if (cbDbType.SelectedIndex == 0) { Type_Only = columnInfo["Type"]; } else { Type_Only = columnInfo["Type_Only"]; } be = new ExcelBE(tablerowno, 2, Type_Only, "B" + tablerowno, "B" + tablerowno, null, false, 10, 15, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 3, columnInfo["Length"], "C" + tablerowno, "C" + tablerowno, null, false, 10, 9, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 4, columnInfo["Null"], "D" + tablerowno, "D" + tablerowno, null, false, 10, 9, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 5, columnInfo["Fkpk"], "E" + tablerowno, "E" + tablerowno, null, false, 10, 9, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 6, comment, "F" + tablerowno, "F" + tablerowno, null, false, 10, 39, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); if (isDicComment) { if (!isColumnDicComment) { dicComment = ""; } be = new ExcelBE(tablerowno, 7, dicComment, "G" + tablerowno, "G" + tablerowno, null, false, 10, 45, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); } tablerowno++; } } UtilExcelCom.Current().SetActivateSheet(1); //显示Excel UtilExcelCom.Current().DoExport(); string sitename = ConfigurationManager.AppSettings["SiteName"]; UtilExcelCom.Current().Save(Path.Combine(System.Environment.CurrentDirectory, sitename + "数据模型.xlsx")); btnExportexcel.Enabled = true; }
/// <summary> /// 生成数据库主体部分 /// </summary> private static string CreateDbDefine() { string result = "/****** 创建数据库所有表 Script Date:" + DateTime.Now + " ******/\r\n"; string database_name = UtilMysql.Database_Name; if (IsSqlserverDefault) { database_name = UtilString.UcFirst(database_name); } result += "USE " + database_name + "\r\n"; string tablename, refer_tablename, tableComment, columnDefine; string sqlTemplate, column_name, column_type, column_null, column_default, resetSeed; string defaultValue, id_column; Dictionary <string, Dictionary <string, string> > columnInfos; foreach (Dictionary <string, string> tableInfo in tableInfos.Values) { //获取表名 tablename = tableInfo["Name"]; if (IsSqlserverDefault) { tablename = UtilString.UcFirst(tablename); } tableComment = tableInfo["Comment"]; columnInfos = UtilMysql.FieldInfoList(tablename); columnDefine = ""; defaultValue = ""; id_column = "ID"; resetSeed = ""; //获取主键名称 foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { column_name = columnInfo["Field"]; bool IsKeyColumn = false; if (IsSqlserverDefault) { column_name = UtilString.UcFirst(column_name); if (column_name.ToUpper().Equals("ID")) { IsKeyColumn = true; } } else { string[] tbl = tablename.Split('_'); if (tbl.Length > 1) { if (column_name.ToUpper().Contains("ID") && column_name.ToUpper().Contains(tbl[tbl.Length - 1].ToUpper())) { id_column = column_name; IsKeyColumn = true; } } } if (IsKeyColumn) { if (tablesIDTypeGuid.Contains(tablename)) { columnDefine += "[" + id_column + "] [uniqueidentifier] NOT NULL,"; defaultValue += string.Format("ALTER TABLE [dbo].[{0}] ADD CONSTRAINT [DF_{0}_ID] DEFAULT (newid()) FOR " + id_column + "]\r\nGO\r\n", tablename); } else { columnDefine += "[" + id_column + "][int] IDENTITY(1,1) NOT NULL,"; resetSeed += "DBCC CHECKIDENT ('" + tablename + "', RESEED, 1)\r\n"; } } else { //获取列名|列类型|是否Null column_type = columnInfo["Type"]; column_type = ConvertType(column_type); column_null = (columnInfo["Null"].Equals("YES")) ? "NULL" : "NOT NULL"; column_default = columnInfo["Default"]; if (column_name.ToUpper().Contains("_ID")) { refer_tablename = column_name.Replace("_ID", ""); refer_tablename = refer_tablename.Replace("_id", ""); if (refer_tablename.ToUpper().Equals("PARENT")) { refer_tablename = tablename; } if (tablesIDTypeGuid.Contains(refer_tablename)) { column_type = "[uniqueidentifier]"; } else { column_type = "[int]"; } column_null = "NOT NULL"; } if (UtilString.Contains(column_name.ToUpper(), "TIME", "DATE")) { if (UtilString.Contains(column_name.ToUpper(), "TIMES")) { column_type = "[int]"; } else { if (IsSqlserverDefault) { column_type = "[datetime]"; } } } if (!string.IsNullOrEmpty(column_default) && (!column_name.ToUpper().Equals("UPDATETIME"))) { if (IsSqlserverDefault) { if (!column_name.ToUpper().Contains("_ID")) { defaultValue += string.Format("ALTER TABLE [dbo].[{0}] ADD CONSTRAINT [DF_{0}_{1}] DEFAULT ({2}) FOR [{1}]\r\nGO\r\n", tablename, column_name, column_default); } } } if ((column_name.ToUpper().Equals("COMMITTIME")) || (column_name.ToUpper().Equals("UPDATETIME"))) { if (IsSqlserverDefault) { defaultValue += string.Format("ALTER TABLE [dbo].[{0}] ADD CONSTRAINT [DF_{0}_{1}] DEFAULT (getdate()) FOR [{1}]\r\nGO\r\n", tablename, column_name); } } string[] type_length = column_type.Split(new char[4] { '[', ']', '(', ')' }, StringSplitOptions.RemoveEmptyEntries); if (type_length.Length == 2) { string length = type_length[1]; int i_len = UtilNumber.Parse(length); if (i_len > 4000) { i_len = 4000; column_type = "[" + type_length[0] + "]" + "(" + i_len + ")"; } } columnDefine += string.Format(" [{0}] {1} {2},", column_name, column_type, column_null); } columnDefine += "\r\n"; } columnDefine = columnDefine.Substring(0, columnDefine.Length - 2); //生成表脚本 sqlTemplate = @" SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[{0}] ( {1} CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED ([{2}] ASC) WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)ON [PRIMARY] ) ON [PRIMARY] GO "; result += string.Format(sqlTemplate, tablename, columnDefine, id_column); //生成列默认值 result += defaultValue; //自增长重置为0 result += resetSeed; } return(result); }
/// <summary> /// 生成数据库主体部分 /// </summary> private static string CreateDbDefine() { string now = DateTime.Now.ToString(); string result = @" /* Betterlife.Net Sqlserver Convert to Mysql Source Server : localhost_3306 Source Server Version : 50520 Source Host : localhost:3306 Source Database : {0} Target Server Type : MYSQL Target Server Version : 50520 File Encoding : 65001 Date: {1} */ SET FOREIGN_KEY_CHECKS=0; "; result = string.Format(result, UtilSqlserver.Database_Name, now); string tablename, tableComment, columnDefine; string sqlTemplate, column_name, column_comment, column_type, column_default, column_pkeys; List <string> primary_keys; Dictionary <string, Dictionary <string, string> > columnInfos; string[] type_length, p_keys, enum_comments; foreach (Dictionary <string, string> tableInfo in tableInfos.Values) { //获取表名 tablename = tableInfo["Name"]; tablename = UtilString.UcFirst(tablename); tableComment = tableInfo["Comment"]; tableComment = tableComment.Replace("\r", "\\r"); tableComment = tableComment.Replace("\n", "\\n"); columnInfos = UtilSqlserver.FieldInfoList(tablename); columnDefine = ""; column_default = ""; primary_keys = new List <string>(); //获取主键名称 foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { column_name = columnInfo["Field"]; column_name = UtilString.UcFirst(column_name); if (columnInfo.Keys.Contains("Comment")) { column_comment = columnInfo["Comment"]; } else { column_comment = column_name; } if (column_name.ToUpper().Equals("ID")) { columnDefine += " `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '" + column_comment + "',"; primary_keys.Add("`ID`"); } else { //获取列名|列类型|是否Null column_type = columnInfo["Type"]; //column_default = columnInfo["Default"]; if (column_name.ToUpper().Contains("_ID")) { column_type = "int(11) NOT NULL "; primary_keys.Add("`" + column_name + "`"); column_default = "DEFAULT '0'"; } else { if (columnInfo["Null"].Equals("YES") || columnInfo["Null"].Equals("是")) { column_default = "DEFAULT NULL"; } else { column_default = "NOT NULL"; } if (UtilString.Contains(column_name.ToUpper(), "TIME", "DATE")) { if (UtilString.Contains(column_name.ToUpper(), "TIMES")) { column_type = "int"; column_default = "DEFAULT '0'"; } else { column_type = "datetime"; } } } type_length = column_type.Split(new char[4] { '[', ']', '(', ')' }, StringSplitOptions.RemoveEmptyEntries); if (type_length.Length == 1) { column_type = type_length[0]; if (column_type.Equals("nvarchar")) { column_type = "varchar"; } if (column_type.Equals("tinyint")) { enum_comments = Enum_ColumnDefine(column_comment); if ((enum_comments != null) && (enum_comments.Length > 1)) { column_type = "enum(" + string.Join(",", enum_comments) + ")"; } } else { if (!column_type.Equals("datetime") && !type_length.Contains("date")) { string length = columnInfo["Length"]; int i_len = UtilNumber.Parse(length); column_type += "(" + i_len + ")"; } } } column_comment = column_comment.Replace("'", "\""); column_comment = column_comment.Replace("\r", "\\r"); column_comment = column_comment.Replace("\n", "\\n"); column_comment = "COMMENT '" + column_comment + "'"; columnDefine += string.Format(" `{0}` {1} {2} {3},", column_name, column_type, column_default, column_comment); } columnDefine += "\r\n"; } if (columnDefine.Length > 2) { columnDefine = columnDefine.Substring(0, columnDefine.Length - 2); } //生成表脚本 sqlTemplate = @" -- ---------------------------- -- Table structure for `{0}` -- ---------------------------- DROP TABLE IF EXISTS `{0}`; CREATE TABLE `{0}` ( {1} ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='{2}'; "; p_keys = primary_keys.ToArray(); column_pkeys = "\r\n PRIMARY KEY (" + string.Join(",", p_keys) + ")"; columnDefine += column_pkeys; result += string.Format(sqlTemplate, tablename, columnDefine, tableComment); } return(result); }
/// <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); } } }