Exemple #1
0
        public static CodeGenerateDto GenerateDtoFromConfig(string val, out List <string> errors)
        {
            var result = new CodeGenerateDto();

            errors = new List <string>();
            var configDto = JsonConvert.DeserializeObject <CodeGenerateConfig>(val);

            result.BasePath       = configDto.BasePath.Trim('\\');
            result.Entities       = CodeGeneraterHelper.GenerateEntitiesModelFromTableModels(configDto, ref errors);
            result.Enums          = GenerateEnumModelFromConfig(configDto.Enums, ref errors);
            result.ExceptApis     = configDto.ExceptApis;
            result.ExceptServices = configDto.ExceptServices;
            return(result);
        }
Exemple #2
0
        public static CodeGenerateDto GenerateDtoFromPdm(CodeGenerateConfigForPdm configForPdm, out List <string> errors)
        {
            var codeGenerateDto = new CodeGenerateDto();

            errors = new List <string>();
            var pdmString = System.IO.File.ReadAllText(configForPdm.PdmFilePath);
            var xml       = new XmlDocument();

            xml.LoadXml(pdmString);
            var tables       = xml.GetElementsByTagName("o:Table");
            var exceptFields = new List <string> {
                "Id", "CreateTime", "UpdateTime", "Creater", "Updater"
            };
            Func <string, string> tableNameToEntityName = tableName =>
            {
                if (tableName.Contains("_"))
                {
                    return(tableName.Substring(tableName.LastIndexOf('_') + 1));
                }
                return(tableName);
            };

            foreach (XmlElement table in tables)
            {
                if (table.HasAttribute("Id"))
                {
                    var columns   = table.GetElementsByTagName("o:Column");
                    var tableCode = table.GetElementsByTagName("a:Code")?[0]?.InnerText;
                    var tableName = table.GetElementsByTagName("a:Name")?[0]?.InnerText;
                    //var tableComment = table.GetElementsByTagName("a:Comment")?[0]?.InnerText.Replace("\n","").Replace("\r","");
                    var entity = new EntityModel()
                    {
                        Comment   = tableName,
                        Fields    = new List <EntityFieldModel>(),
                        Name      = tableNameToEntityName(tableCode),
                        TableName = tableCode
                    };
                    if (string.IsNullOrEmpty(tableCode) || string.IsNullOrEmpty(tableName))
                    {
                        continue;
                    }
                    foreach (XmlElement column in columns)
                    {
                        var columnCode      = column.GetElementsByTagName("a:Code")?[0]?.InnerText;
                        var columnName      = column.GetElementsByTagName("a:Name")?[0]?.InnerText;
                        var columnDataType  = column.GetElementsByTagName("a:DataType")?[0]?.InnerText;
                        var columnComment   = (column.GetElementsByTagName("a:Comment")?[0]?.InnerText ?? "").Replace("\n", "").Replace("\r", "");
                        var columnLength    = column.GetElementsByTagName("a:Length")?[0]?.InnerText ?? "";
                        var columnMandatory = column.GetElementsByTagName("a:Column.Mandatory")?[0]?.InnerText ?? "0";

                        if (string.IsNullOrEmpty(columnCode) || string.IsNullOrEmpty(columnName) || string.IsNullOrEmpty(columnDataType) || exceptFields.Contains(columnCode, StringComparer.OrdinalIgnoreCase))
                        {
                            continue;
                        }
                        entity.Fields.Add(GetFieldModelByPdmCfg(columnCode, columnName, columnDataType, columnLength, columnComment, columnMandatory));
                    }
                    if (entity.Fields.Count > 0)
                    {
                        codeGenerateDto.Entities.Add(entity);
                    }
                }
            }

            codeGenerateDto.BasePath       = configForPdm.BasePath.Trim('\\');
            codeGenerateDto.Enums          = GenerateEnumModelFromConfig(configForPdm.Enums, ref errors);
            codeGenerateDto.ExceptApis     = configForPdm.ExceptApis;
            codeGenerateDto.ExceptServices = configForPdm.ExceptServices;
            return(codeGenerateDto);
        }