예제 #1
0
        /// <summary>
        /// 生成实体
        /// </summary>
        /// <param name="tableEntities"></param>
        public void GenerateEntity(List <TableEntity> tableEntities)
        {
            string         ignoreColumnNames = "crt_date,crt_user_id,crt_user_name,mnt_date,mnt_user_id,mnt_user_name"; //忽略的列
            string         time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string         namespaceTemplate = "EP.Entity.{0}Entity";
            string         className         = "";
            EntityTemplate entityTemplate    = new EntityTemplate();

            foreach (var tableEntity in tableEntities)
            {
                string   usingStr    = "using NHibernate.Mapping.Attributes;";
                var      enTableName = tableEntity.EnglishName;
                var      cnTableName = tableEntity.ChineseName;
                string[] splits      = enTableName.Split('_');
                if (splits.Length > 2)
                {
                    int startIndex = splits[0].Length + splits[1].Length + 2;
                    className = enTableName.Substring(startIndex) + "Entity";
                }
                string        namespaceStr  = string.Format(namespaceTemplate, tableEntity.EnglishName.Split('_')[1].ToUpper()) + "\r\n{";
                StringBuilder columnBuilder = new StringBuilder();
                string        fileDes       = cnTableName + "实体";
                var           fileDesLen    = Encoding.Default.GetByteCount(fileDes);
                var           createrLen    = Encoding.Default.GetByteCount(Tools.Creater);
                string        fileNode      = entityTemplate.FileNode.Replace("{Time}", time)
                                              .Replace("{Creater}", Tools.Creater + "".PadRight(26 - createrLen))
                                              .Replace("{FileDes}", fileDes + "".PadRight(85 - fileDesLen));
                var foreignKey = "\r\n        #endregion\r\n\r\n        #region 关联实体\r\n";

                bool isIncludeAll    = false;
                var  ignoreColuNames = ignoreColumnNames.Split(',');
                foreach (string coluName in ignoreColuNames)
                {
                    isIncludeAll = false;
                    foreach (var column in tableEntity.ColumnEntityList)
                    {
                        if (column.EnglishNameCol.ToLower() == coluName.ToLower())
                        {
                            isIncludeAll = true;
                            break;
                        }
                    }
                    if (!isIncludeAll)
                    {
                        break;
                    }
                }

                if (tableEntity.ColumnEntityList.Any(x => x.PropertyType == "DateTime"))
                {
                    usingStr = "using System;\r\n" + usingStr;
                }
                if (tableEntity.ColumnEntityList.Any(x => x.EnglishNameCol.ToLower() == "company_id" || x.EnglishNameCol.ToLower() == "dept_id"))
                {
                    usingStr += "\r\nusing EP.Entity.AAAEntity;";
                }

                #region 类文件注释

                var classNode = entityTemplate.ClassNode.Replace("{FileDes}", fileDes)
                                .Replace("{ModelNum}", "pcitc_ep_entity_class_" + className)
                                .Replace("{Time}", time)
                                .Replace("{Creater}", Tools.Creater)
                                .Replace("{TableName}", enTableName.ToLower())
                                .Replace("{ClassName}", isIncludeAll ? className + " : BasicInfoEntity" : className) + "\r\n    {\r\n        #region Model";

                #endregion

                foreach (ColumnEntity columnEntity in tableEntity.ColumnEntityList)
                {
                    #region 参数属性

                    if (!ignoreColuNames.Contains(columnEntity.EnglishNameCol.ToLower()))
                    {
                        if ((columnEntity.ChineseNameCol.Contains("(") || columnEntity.ChineseNameCol.Contains("(")) &&
                            (columnEntity.ChineseNameCol.Contains("1") && columnEntity.ChineseNameCol.Contains("2")
                             ||
                             columnEntity.ChineseNameCol.Contains("0") && columnEntity.ChineseNameCol.Contains("1")))
                        {
                            columnEntity.PropertyType = "int";
                        }
                        if (columnEntity.EnglishNameCol.ToLower() == "sort_num")
                        {
                            columnEntity.PropertyType = "int";
                        }
                        string column = "";
                        if (columnEntity.IsPrimaryKey)
                        {
                            column = entityTemplate.ColumnKeyParameter.Replace("{ColumnDes}", columnEntity.ChineseNameCol)
                                     .Replace("{Field}", columnEntity.EnglishNameCol.ToLower())
                                     .Replace("{SequenceName}", "s" + LetterConvert.StrToLower(enTableName).Substring(1))
                                     .Replace("{Type}", columnEntity.PropertyType)
                                     .Replace("{ColumnName}", LetterConvert.ConvertStr(columnEntity.EnglishNameCol));
                        }
                        else
                        {
                            column = entityTemplate.ColumnParameter.Replace("{ColumnDes}", columnEntity.ChineseNameCol)
                                     .Replace("{Field}", columnEntity.EnglishNameCol.ToLower())
                                     .Replace("{Type}", columnEntity.PropertyType)
                                     .Replace("{ColumnName}", LetterConvert.ConvertStr(columnEntity.EnglishNameCol));
                        }
                        columnBuilder.Append(column);
                    }

                    #endregion

                    #region 外键字段

                    if (columnEntity.IsForeignKey)
                    {
                        if (columnEntity.EnglishNameCol.ToLower() == "company_id")
                        {
                            foreignKey += @"
        /// <summary>
        /// 企业实体
        /// </summary>
        [ManyToOne(Name = ""Company"", ClassType = typeof(OUEntity), Lazy = Laziness.Proxy,
            Column = ""company_id"", Unique = true, Insert = false, Update = false)]
        public virtual OUEntity Company { get; set; }
";
                        }
                        else if (columnEntity.EnglishNameCol.ToLower() == "dept_id")
                        {
                            foreignKey += @"

        /// <summary>
        /// 部门实体
        /// </summary>
        [ManyToOne(Name = ""Dept"", ClassType = typeof(OUEntity), Lazy = Laziness.Proxy, Column = ""dept_id"",
            Unique = true, Insert = false, Update = false)]
        public virtual OUEntity Dept { get; set; }";
                        }
                        else
                        {
                            var entityName = LetterConvert.ConvertStr(columnEntity.EnglishNameCol.Replace("_ID", "").Replace("_id", "").Replace("_Id", ""));
                            var tbEntity   = Tools.StorageTables.Find(x => x.ChineseName == columnEntity.AssociatedTable);
                            if (tbEntity == null)
                            {
                                continue;
                            }
                            var names = tbEntity.EnglishName.Split('_');
                            if (entityName.ToLower() != names[2].ToLower())
                            {
                                entityName = names[2];
                            }
                            string entityDes = tbEntity.ChineseName;

                            if (entityName.ToLower().Contains("voc"))
                            {
                                Regex reg = new Regex("voc", RegexOptions.IgnoreCase);
                                entityName = reg.Replace(entityName, "VOC");
                            }
                            var columnName = columnEntity.EnglishNameCol.ToLower();

                            foreignKey += entityTemplate.ForeignKeyEntity.Replace("{ForeignKeyDes}", entityDes)
                                          .Replace("{ForeignKey}", entityName)
                                          .Replace("{Field}", columnName);
                        }
                    }

                    #endregion
                }
                if (tableEntity.ColumnEntityList.Any(x => x.IsForeignKey == true))
                {
                    columnBuilder.Append(foreignKey);
                }
                columnBuilder.Append("\r\n		#endregion").Append("\r\n    }\r\n}");
                string entityContent = string.Format(entityTemplate.CreateEntity, fileNode, usingStr, namespaceStr, classNode, columnBuilder);

                Tools.File2Path(Tools.GeneratePath + "\\Entity\\" + className + ".cs", entityContent, EnumCommon.LogEnum.EntityLog);
            }
        }
예제 #2
0
        private void GenerateEntity(GenerateCore.Tables tables, GenerateCore generateCore)
        {
            string         namespaceTemplate = "EP.Entity.{0}Entity";
            string         time              = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            EntityTemplate entityTemplate    = new EntityTemplate();
            string         ignoreColumnNames = "crt_date,crt_user_id,crt_user_name,mnt_date,mnt_user_id,mnt_user_name"; //忽略的列

            // 生成输出
            if (tables.Count > 0)
            {
                StringBuilder entityContent = new StringBuilder();

                var manager = GenerateCore.Manager.Create(null, entityContent);
                manager.StartHeader();
                manager.EndBlock();
                int num = 1;
                foreach (GenerateCore.Table tbl in from t in tables where !t.Ignore select t)
                {
                    Tools.LogAction($"生成第{num++}张表!", EnumCommon.LogEnum.EntityLog);

                    #region 属性

                    string usingStr   = "using NHibernate.Mapping.Attributes;";
                    string foreignKey = "\r\n        #endregion\r\n\r\n        #region 关联实体\r\n";
                    //查询表中外键关联表,用以生成关联实体
                    var           consTables    = generateCore.LoadConstraints(tbl.Name);
                    StringBuilder columnBuilder = new StringBuilder();
                    string        className     = tbl.Name;
                    string        fileDes       = (tbl.Comments == null ? className : tbl.Comments) + "实体";
                    string        namespaceStr  = string.Format(namespaceTemplate, className.Split('_')[1].ToUpper()) + "\r\n{";
                    string[]      splits        = tbl.Name.Split('_');
                    if (splits.Length > 2)
                    {
                        int startIndex = splits[0].Length + splits[1].Length + 2;
                        className = tbl.Name.Substring(startIndex);
                    }
                    bool isIncludeAll    = false;
                    var  ignoreColuNames = ignoreColumnNames.Split(',');
                    foreach (string coluName in ignoreColuNames)
                    {
                        isIncludeAll = tbl.Columns.Any(column => column.Name.ToLower() == coluName.ToLower());
                        if (!isIncludeAll)
                        {
                            break;
                        }
                    }
                    manager.StartNewFile(LetterConvert.ConvertStr(className) + "Entity.cs");

                    var    fileDesLen = Encoding.Default.GetByteCount(fileDes);
                    var    createrLen = Encoding.Default.GetByteCount(Tools.Creater);
                    string fileNode   = entityTemplate.FileNode.Replace("{Time}", time)
                                        .Replace("{Creater}", Tools.Creater + "".PadRight(26 - createrLen))
                                        .Replace("{FileDes}", fileDes + "".PadRight(85 - fileDesLen));

                    if (tbl.Columns.Any(x => x.PropertyName == "DateTime"))
                    {
                        usingStr = "using System;\r\n" + usingStr;
                    }

                    if (
                        tbl.Columns.Any(
                            x => x.PropertyName.ToLower() == "company_id" || x.PropertyName.ToLower() == "dept_id"))
                    {
                        usingStr += "\r\nusing EP.Entity.AAAEntity;";
                    }

                    #endregion

                    #region 类文件注释

                    var classNode = entityTemplate.ClassNode.Replace("{FileDes}", fileDes)
                                    .Replace("{ModelNum}", "pcitc_ep_entity_class_" + className)
                                    .Replace("{Time}", time)
                                    .Replace("{Creater}", Tools.Creater)
                                    .Replace("{TableName}", className.ToLower())
                                    .Replace("{ClassName}",
                                             isIncludeAll ? className + " : BasicInfoEntity" : className) +
                                    "\r\n    {\r\n        #region Model";

                    #endregion

                    #region 参数属性

                    bool isIdField = true;
                    foreach (GenerateCore.Column col in tbl.Columns)
                    {
                        if (!col.IsPK)
                        {
                            if ((col.Comments.Contains("(") || col.Comments.Contains("(")) &&
                                (col.Comments.Contains("1") && col.Comments.Contains("2") ||
                                 col.Comments.Contains("0") && col.Comments.Contains("1")))
                            {
                                col.PropertyType = col.PropertyType.Replace("decimal", "int");
                            }
                            string column = "";
                            if (col.IsPK)
                            {
                                column = entityTemplate.ColumnKeyParameter.Replace("{ColumnDes}", col.Comments)
                                         .Replace("{Field}", col.PropertyName.ToLower())
                                         .Replace("{SequenceName}", "s" + LetterConvert.StrToLower(tbl.Name).Substring(1))
                                         .Replace("{Type}", col.PropertyType)
                                         .Replace("{ColumnName}", LetterConvert.ConvertStr(col.PropertyName));
                            }
                            else if (tbl.IsView && isIdField)
                            {
                                isIdField = false;
                                column    = entityTemplate.ColumnViewParameter.Replace("{ColumnDes}", col.Comments)
                                            .Replace("{Field}", col.PropertyName.ToLower())
                                            .Replace("{Type}", col.PropertyType)
                                            .Replace("{ColumnName}", LetterConvert.ConvertStr(col.PropertyName));
                            }
                            else
                            {
                                column = entityTemplate.ColumnParameter.Replace("{ColumnDes}", col.Comments)
                                         .Replace("{Field}", col.PropertyName.ToLower())
                                         .Replace("{Type}", col.PropertyType)
                                         .Replace("{ColumnName}", LetterConvert.ConvertStr(col.PropertyName));
                            }
                            columnBuilder.Append(column);
                        }
                    }

                    #endregion

                    #region 外键字段

                    if (consTables.Count > 0)
                    {
                        consTables.ForEach(x =>
                        {
                            if (consTables.Count > 0)
                            {
                                var columnName = x.OrColumnName.ToLower();
                                var entityName = LetterConvert.ConvertStr(columnName.Replace("_ID", ""));

                                if (columnName == "company_id")
                                {
                                    foreignKey += @"
        /// <summary>
        /// 企业实体
        /// </summary>
        [ManyToOne(Name = ""Company"", ClassType = typeof(OUEntity), Lazy = Laziness.Proxy,
            Column = ""company_id"", Unique = true, Insert = false, Update = false)]
        public virtual OUEntity Company { get; set; }";
                                }
                                else if (columnName == "dept_id")
                                {
                                    foreignKey += @"

        /// <summary>
        /// 部门实体
        /// </summary>
        [ManyToOne(Name = ""Dept"", ClassType = typeof(OUEntity), Lazy = Laziness.Proxy, Column = ""dept_id"",
            Unique = true, Insert = false, Update = false)]
        public virtual OUEntity Dept { get; set; }";
                                }
                                else
                                {
                                    if (entityName.ToLower().Contains("voc"))
                                    {
                                        Regex reg = new Regex("voc",
                                                              RegexOptions.IgnoreCase | RegexOptions.IgnoreCase);
                                        entityName = reg.Replace(entityName, "VOC");
                                    }
                                    foreignKey += entityTemplate.ForeignKeyEntity
                                                  .Replace("{ForeignKeyDes}", x.TableComments)
                                                  .Replace("{ForeignKey}", entityName)
                                                  .Replace("{Field}", columnName);
                                }
                            }
                        });
                    }
                    #endregion

                    columnBuilder.Append("\r\n		#endregion").Append("\r\n    }\r\n}");
                    string content = string.Format(entityTemplate.CreateEntity, fileNode, usingStr, namespaceStr,
                                                   classNode, columnBuilder);
                    entityContent.Append(content);
                    manager.EndBlock();
                }
                manager.StartFooter();
                manager.EndBlock();
                manager.Process(true);
            }
        }
예제 #3
0
        /// <summary>
        /// 生成实体
        /// </summary>
        /// <param name="tableEntities"></param>
        public void GeneratePojo(List <TableEntity> tableEntities)
        {
            string       ignoreColumnNames = "crt_date,crt_user_id,crt_user_name,mnt_date,mnt_user_id,mnt_user_name"; //忽略的列
            string       time         = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string       className    = "";
            PojoTemplate pojoTemplate = new PojoTemplate();

            tableEntities.ForEach(x => x.ColumnEntityList.ToList().ForEach(z =>
            {
                if (z.PropertyType == "decimal")
                {
                    z.PropertyType = "Long";
                }
                else if (z.PropertyType == "decimal?")
                {
                    z.PropertyType = "Integer";
                }
                else if (z.PropertyType == "int")
                {
                    z.PropertyType = "Integer";
                }
                else if (z.PropertyType == "string")
                {
                    z.PropertyType = "String";
                }
            }));

            foreach (var tableEntity in tableEntities)
            {
                List <Tuple <string, string, string> > listAttribute  = new List <Tuple <string, string, string> >();
                List <Tuple <string, string, string> > listForeignKey = new List <Tuple <string, string, string> >();

                string   usingStr    = " ";
                var      enTableName = tableEntity.EnglishName;
                var      cnTableName = tableEntity.ChineseName;
                string[] splits      = enTableName.Split('_');
                if (splits.Length > 2)
                {
                    int startIndex = splits[0].Length + splits[1].Length + 2;
                    className = enTableName.Substring(startIndex);
                }
                StringBuilder columnBuilder = new StringBuilder();
                string        fileDes       = cnTableName;
                var           foreignKey    = "";

                bool isIncludeAll    = false;
                var  ignoreColuNames = ignoreColumnNames.Split(',');
                foreach (string coluName in ignoreColuNames)
                {
                    isIncludeAll = false;
                    foreach (var column in tableEntity.ColumnEntityList)
                    {
                        if (column.EnglishNameCol.ToLower() == coluName.ToLower())
                        {
                            isIncludeAll = true;
                            break;
                        }
                    }
                    if (!isIncludeAll)
                    {
                        break;
                    }
                }

                #region 类文件注释

                var classNode = pojoTemplate.ClassNode.Replace("{FileDes}", fileDes)
                                .Replace("{ModelNum}", "pcitc_pojo_class_" + className)
                                .Replace("{Time}", time)
                                .Replace("{Creater}", Tools.Creater)
                                .Replace("{SequenceName}", "s" + LetterConvert.StrToLower(enTableName).Substring(1))
                                .Replace("{TableName}", enTableName.ToLower())
                                .Replace("{ClassName}", isIncludeAll ? className + " extends BasicInfo" : className) + " {";

                #endregion

                foreach (ColumnEntity columnEntity in tableEntity.ColumnEntityList)
                {
                    #region 参数属性

                    if (!ignoreColuNames.Contains(columnEntity.EnglishNameCol.ToLower()))
                    {
                        if ((columnEntity.ChineseNameCol.Contains("(") || columnEntity.ChineseNameCol.Contains("(")) &&
                            (columnEntity.ChineseNameCol.Contains("1") && columnEntity.ChineseNameCol.Contains("2")
                             ||
                             columnEntity.ChineseNameCol.Contains("0") && columnEntity.ChineseNameCol.Contains("1")))
                        {
                            columnEntity.PropertyType = "Integer";
                        }
                        if (columnEntity.EnglishNameCol.ToLower() == "sort_num")
                        {
                            columnEntity.PropertyType = "Integer";
                        }

                        string firstToLower = LetterConvert.ConvertStr(columnEntity.EnglishNameCol.Substring(0, 1).ToLower() + columnEntity.EnglishNameCol.Substring(1));
                        listAttribute.Add(new Tuple <string, string, string>(columnEntity.PropertyType, LetterConvert.ConvertStr(columnEntity.EnglishNameCol), firstToLower));

                        string column = "";
                        if (columnEntity.IsPrimaryKey)
                        {
                            column = pojoTemplate.ColumnKeyParameter.Replace("{ColumnDes}", columnEntity.ChineseNameCol)
                                     .Replace("{Field}", columnEntity.EnglishNameCol.ToLower())
                                     .Replace("{SequenceName}", "s" + LetterConvert.StrToLower(enTableName).Substring(1))
                                     .Replace("{Type}", columnEntity.PropertyType)
                                     .Replace("{ColumnName}", firstToLower);
                        }
                        else
                        {
                            column = pojoTemplate.ColumnParameter.Replace("{ColumnDes}", columnEntity.ChineseNameCol)
                                     .Replace("{Field}", columnEntity.EnglishNameCol.ToLower())
                                     .Replace("{Type}", columnEntity.PropertyType)
                                     .Replace("{ColumnName}", firstToLower);
                        }
                        columnBuilder.Append(column);
                    }

                    #endregion

                    #region 外键字段

                    if (columnEntity.IsForeignKey)
                    {
                        var entityName = LetterConvert.ConvertStr(columnEntity.EnglishNameCol.Replace("_ID", "").Replace("_id", "").Replace("_Id", ""));
                        var tbEntity   = Tools.StorageTables.Find(x => x.ChineseName == columnEntity.AssociatedTable);
                        if (tbEntity == null)
                        {
                            continue;
                        }
                        var names = tbEntity.EnglishName.Split('_');
                        if (entityName.ToLower() != names[2].ToLower())
                        {
                            entityName = names[2];
                        }
                        string entityDes = tbEntity.ChineseName.ToLower();

                        var columnName = columnEntity.EnglishNameCol;

                        string firstToLower = entityName.Substring(0, 1).ToLower() + entityName.Substring(1);
                        listForeignKey.Add(new Tuple <string, string, string>(entityName, LetterConvert.ConvertStr(entityName), firstToLower));

                        foreignKey += pojoTemplate.ForeignKeyEntity.Replace("{ForeignKeyDes}", entityDes)
                                      .Replace("{ForeignKey}", entityName)
                                      .Replace("{Field}", columnName)
                                      .Replace("{lowForeignKey}", firstToLower);
                    }

                    #endregion
                }
                if (tableEntity.ColumnEntityList.Any(x => x.IsForeignKey == true))
                {
                    columnBuilder.Append(foreignKey);
                    listAttribute = listAttribute.Concat(listForeignKey).ToList();
                }

                foreach (var tuple in listAttribute)
                {
                    columnBuilder.Append(pojoTemplate.GetAttrubute.Replace("{Field}", tuple.Item1)
                                         .Replace("{ColumnName}", tuple.Item2)
                                         .Replace("{firstToLower}", tuple.Item3));

                    columnBuilder.Append(pojoTemplate.SetAttrubute.Replace("{Field}", tuple.Item1)
                                         .Replace("{ColumnName}", tuple.Item2)
                                         .Replace("{firstToLower}", tuple.Item3));
                }

                columnBuilder.Append("\r\n}");
                string entityContent = string.Format(pojoTemplate.CreateEntity, usingStr, classNode, columnBuilder);

                Tools.File2Path(Tools.GeneratePath + "\\Pojo\\" + className + ".java", entityContent, EnumCommon.LogEnum.EntityLog);
            }
        }