bo/api/v3/bi/db/aicode/{template}

http://localhost:5900/bo/api/v3/bi/db/aicode/db2appid?ignore=true&ownerId=tosoft&sysId&mediaType=text&suffix=sql

[
     {
        "connName":"ConnBo",
        "databaseName":"tosoft_auth_bo",
        "tableOutPath":"~/App_Temp/{{table_owner_id}}/AiCode/db2appid/{{table_conn_name}}/{{table_name_hline}}.sql",
        "outPath":"~/App_Temp/{{global_owner_id}}/AiCode/db2appid/{{global_conn_name}}/all.sql",
        "tableNames":[{
            "tableName":"bo_job"
        }]
    }
]

[
     {
        "connName":"ConnBo",
        "databaseName":"tosoft_auth_bo",
        "tableNames":[]
    }
]

sbOutPath.Replace("{{table_name_lowner}}", tableL);
sbOutPath.Replace("{{table_name_class}}", tableC);
sbOutPath.Replace("{{table_name_field}}", tableF);
sbOutPath.Replace("{{table_name_hline}}", tableH);
sbOutPath.Replace("{{table_owner_id}}", ownerId);
sbOutPath.Replace("{{table_sys_id}}", sysId);

 if (!string.IsNullOrEmpty(conn.OutPath))
{
    StringBuilder sbOutPath = new StringBuilder(conn.OutPath);
    sbOutPath.Replace("{{global_owner_id}}", ownerId);
    sbOutPath.Replace("{{global_sys_id}}", sysId);
    sbOutPath.Replace("{{global_conn_name}}", conn.ConnName);
    FileUtil.CreateFileAndContentAsync(sbOutPath.ToString(), sbDbSql.ToString(), encoding);
}

/bo/api/v3/bi/db/aicode/all

/bo/api/v3/bi/db/aicode/all?ignore=true&ownerId=tosoft&sysId&mediaType=text&suffix=sql

[
    {
        "template":"db2appid",
        "suffix":"sql",
        "databasePos":[
             {
                "connName":"ConnBo",
                "databaseName":"tosoft_auth_bo",
                "tableOutPath":"~/App_Temp/{{table_owner_id}}/AiCode/db2appid/{{table_conn_name}}/{{table_name_hline}}.sql",
                "outPath":"~/App_Temp/{{global_owner_id}}/AiCode/db2appid/{{global_conn_name}}/all.sql",
                "tableNames":[{
                    "tableName":"bo_job"
                }]
            }, {
                "connName":"ConnBs",
                "databaseName":"tosoft_auth_bs",
                "tableOutPath":"~/App_Temp/{{table_owner_id}}/AiCode/db2appid/{{table_conn_name}}/{{table_name_hline}}.sql",
                "outPath":"~/App_Temp/{{global_owner_id}}/AiCode/db2appid/{{global_conn_name}}/all.sql",
                "tableNames":[]
            }
        ]
    }
]

tables.sql

sbTableLine.Replace("{{columns}}", sbColumnLines.ToString());
sbTableLine.Replace("{{db_name}}", conn.DatabaseName);
sbTableLine.Replace("{{table_name}}", table.Name.ToLower());
sbTableLine.Replace("{{table_field}}", RegexUtil.ConvertDownLine2CamelCase(table.Name));                    
sbSql.Replace("{{owner_id}}", ownerId);
sbSql.Replace("{{sys_id}}", sysId);

columns.sql

sbColumnLine.Replace("{{column_name}}", colunm.Name.ToLower());
sbColumnLine.Replace("{{column_title}}", colunm.Title);
sbColumnLine.Replace("{{column_field}}", RegexUtil.ConvertDownLine2CamelCase(colunm.Name));
sbColumnLine.Replace("{{column_data_type}}", colunm.DataType);
sbColumnLine.Replace("{{column_max_length}}", colunm.MaxLength?.ToString());
sbColumnLine.Replace("{{column_is_primary}}", colunm.IsPrimary == true ? "1" : "0");
sbColumnLine.Replace("{{column_is_null}}", colunm.IsNullValue == true ? "1" : "0");
sbColumnLine.Replace("{{column_is_required}}", colunm.IsRequired == true ? "1" : "0");

代码说明

\rg3-pf\RG3.BO.BI.DB\Controller\DbAiCodeController.cs




            string indexsSqlF = null;
            if (FileUtil.ExistsFilesByVirPath(indexsVirF))
            {
                indexsSqlF = await FileUtil.ReadFileAndContentByVirUrlAsync(indexsVirF, encoding);
            }

            string primarysVirF = $"~/App_Data/Templates/AiCode/{template}/primarys.{suffix}";
            string primarysSqlF = null;
            if (FileUtil.ExistsFilesByVirPath(primarysVirF))
            {
                primarysSqlF = await FileUtil.ReadFileAndContentByVirUrlAsync(primarysVirF, encoding);
            }

            string defaultsVirF = $"~/App_Data/Templates/AiCode/{template}/defaults.{suffix}";
            string defaultsSqlF = null;
            if (FileUtil.ExistsFilesByVirPath(defaultsVirF))
            {
                defaultsSqlF = await FileUtil.ReadFileAndContentByVirUrlAsync(defaultsVirF, encoding);
            }

            string structParamsVirF = $"~/App_Data/Templates/AiCode/{template}/struct-params.{suffix}";
            string structParamsSqlF = null;
            if (FileUtil.ExistsFilesByVirPath(structParamsVirF))
            {
                structParamsSqlF = await FileUtil.ReadFileAndContentByVirUrlAsync(structParamsVirF, encoding);
            }


            string structSetsVirF = $"~/App_Data/Templates/AiCode/{template}/struct-sets.{suffix}";
            string structSetsSqlF = null;
            if (FileUtil.ExistsFilesByVirPath(structSetsVirF))
            {
                structSetsSqlF = await FileUtil.ReadFileAndContentByVirUrlAsync(structSetsVirF, encoding);
            }

            string structToStrinsVirF = $"~/App_Data/Templates/AiCode/{template}/struct-tostrings.{suffix}";
            string structToStringsSqlF = null;
            if (FileUtil.ExistsFilesByVirPath(structToStrinsVirF))
            {
                structToStringsSqlF = await FileUtil.ReadFileAndContentByVirUrlAsync(structToStrinsVirF, encoding);
            }

            foreach (var conn in body)
            {
                StringBuilder sbDbSql = new StringBuilder();
                PFGlobalParameter pf = new PFGlobalParameter
                {
                    ConnName = conn.ConnName,
                    OwnerId = ownerId,
                    SysId = sysId
                };
                ResultPager<SqlTable> result = new ResultPager<SqlTable>();
                result.Data = _dbTableService.GetMedaTableAndViewByDataBase(ownerId, sysId, pf.ConnName);
                IEnumerable<SqlColumn> lstColumns = new List<SqlColumn>();
                if (!string.IsNullOrEmpty(columnSqlF))
                {
                    lstColumns = _dbColumnService.GetTableColumnByTbName(ownerId, sysId, pf.ConnName, result.Data.Select(temp => temp.TbName.ToLower()));
                }
                foreach (var table in result.Data)
                {
                    if (conn.TableNames.Any() && conn.TableNames.Count(temp => temp.TableName.ToLower() == table.Name.ToLower()) == 0)
                    {
                        continue;
                    }

                    #region 列模板
                    StringBuilder sbColumnLines = new StringBuilder();
                    var lstCurCol = lstColumns.Where(temp => (temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower());
                    if (lstCurCol.Any())
                    {
                        foreach (var colunm in lstCurCol)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(columnSqlF, conn, colunm, sbColumnLine);
                            sbColumnLines.AppendLine(sbColumnLine.ToString());
                        }
                    }
                    #endregion 列模板


                    #region 备注模板
                    StringBuilder sbCommentLines = new StringBuilder();
                    var lstCurComment = lstColumns.Where(temp => (temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower());
                    if (lstCurComment.Any())
                    {
                        foreach (var colunm in lstCurComment)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(commentsSqlF, conn, colunm, sbColumnLine);
                            sbCommentLines.AppendLine(sbColumnLine.ToString());
                        }
                    }
                    #endregion 备注模板

                    #region 索引模板
                    StringBuilder sbIndexLines = new StringBuilder();
                    var lstCurIndex = lstColumns.Where(temp => (temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower());
                    if (lstCurIndex.Any())
                    {
                        foreach (var colunm in lstCurIndex)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(indexsSqlF, conn, colunm, sbColumnLine);
                            sbIndexLines.AppendLine(sbColumnLine.ToString());
                        }
                    }
                    #endregion 索引模板

                    #region 主键模板

                    StringBuilder sbPrimarysLines = new StringBuilder();
                    var lstCurPrimarys = lstColumns.Where(temp => (temp.IsPrimary == true && (temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower()));
                    if (lstCurPrimarys.Any())
                    {
                        foreach (var colunm in lstCurPrimarys)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(primarysSqlF, conn, colunm, sbColumnLine);
                            sbPrimarysLines.AppendLine(sbColumnLine.ToString());
                        }
                    }
                    #endregion 主键模板

                    #region 默认值模板
                    StringBuilder sbDefaultsLines = new StringBuilder();
                    var lstCurDefaults = lstColumns.Where(temp => (!string.IsNullOrEmpty(temp.DefaultText) && (temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower()));
                    if (lstCurPrimarys.Any())
                    {
                        foreach (var colunm in lstCurDefaults)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(defaultsSqlF, conn, colunm, sbColumnLine);
                            sbDefaultsLines.AppendLine(sbColumnLine.ToString());
                        }
                    }
                    #endregion 默认值模板


                    #region struct-params模板
                    StringBuilder sbStructParmsLines = new StringBuilder();
                    var lstCurStructParams = lstColumns.Where(temp => ((temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower()));
                    if (lstCurStructParams.Any())
                    {
                        foreach (var colunm in lstCurStructParams)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(structParamsSqlF, conn, colunm, sbColumnLine);
                            sbStructParmsLines.Append(sbColumnLine.ToString());
                        }
                    }
                    #endregion struct-params模板


                    #region struct-sets模板
                    StringBuilder sbStructSetsLines = new StringBuilder();
                    var lstCurStructSets = lstColumns.Where(temp => ((temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower()));
                    if (lstCurStructSets.Any())
                    {
                        foreach (var colunm in lstCurStructSets)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(structSetsSqlF, conn, colunm, sbColumnLine);
                            sbStructSetsLines.AppendLine(sbColumnLine.ToString());
                        }
                    }
                    #endregion struct-params模板

                    #region struct-tostrings模板
                    StringBuilder sbStructToStringsLines = new StringBuilder();
                    var lstCurToStrings = lstColumns.Where(temp => ((temp.TbName ?? temp.TableName).ToLower() == table.Name.ToLower()));
                    if (lstCurToStrings.Any())
                    {
                        foreach (var colunm in lstCurToStrings)
                        {
                            StringBuilder sbColumnLine = new StringBuilder();
                            ConvertTemplate(structToStringsSqlF, conn, colunm, sbColumnLine);
                            sbStructToStringsLines.AppendLine(sbColumnLine.ToString());
                        }
                    }
                    #endregion struct-tostrings模板


                    StringBuilder sbTableLine = new StringBuilder();
                    var tableLU = table.Name.ToUpper();
                    var tableL = table.Name.ToLower();
                    var tableH = table.Name.ToLower().Replace("_", "-");
                    var tableF = RegexUtil.ConvertDownLine2CamelCase(table.Name.ToLower());
                    var tableC = RegexUtil.ConvertDownLine2CamelCase(table.Name.ToLower(), false);
                    sbTableLine.Append(tableSqlF);
                    sbTableLine.Replace("{{columns}}", sbColumnLines.ToString().TrimEnd(',').TrimEnd(",\r\n".ToCharArray())); // 列文件占位符
                    sbTableLine.Replace("{{comments}}", sbCommentLines.ToString()); // 列备注占位符
                    sbTableLine.Replace("{{indexs}}", sbIndexLines.ToString()); // 列索引占位符
                    sbTableLine.Replace("{{primarys}}", sbPrimarysLines.ToString()); // 列主键占位符
                    sbTableLine.Replace("{{defaults}}", sbDefaultsLines.ToString()); // 默认值占位符
                    sbTableLine.Replace("{{struct-params}}", sbStructParmsLines.ToString().TrimEnd(',')); // 构造函数占位符
                    sbTableLine.Replace("{{struct-tostrings}}", sbStructToStringsLines.ToString().TrimStart(',')); // 重新构造函数
                    sbTableLine.Replace("{{struct-sets}}", sbStructSetsLines.ToString()); // 构造函数值重写
                    sbTableLine.Replace("{{db_name}}", conn.DatabaseName); // 数据库名
                    sbTableLine.Replace("{{table_comment}}", table.TableComment ?? tableL); // 小写下划线表名
                    sbTableLine.Replace("{{table_name}}", tableL); // 小写下划线表名
                    sbTableLine.Replace("{{table_name_upper_line}}", tableLU); // 大写下划线表名
                    sbTableLine.Replace("{{table_name_lowner_line}}", tableL); // 小写下划线表名
                    sbTableLine.Replace("{{table_name_upper}}", tableC); // 大写驼峰
                    sbTableLine.Replace("{{table_name_class}}", tableC); // 类名
                    sbTableLine.Replace("{{table_name_field}}", tableF); //小写驼峰表名
                    sbTableLine.Replace("{{table_name_hline}}", tableH); // 横杆连接
                    sbTableLine.Replace("{{table_field}}", tableF); //小写驼峰表名
                    sbTableLine.Replace("{{owner_id}}", ownerId); //所有者
                    sbTableLine.Replace("{{sys_id}}", sysId); //系统
                    sbTableLine.Replace("{{namespace_name}}", conn.NamespaceName); //命名空间名称
                    sbTableLine.Replace("{{package_name}}", conn.PackageName); //java包名称
                    sbTableLine.Replace("{{table_conn_name}}", conn.ConnName); //java包名称
                    sbTableLine.Replace("{{tablespace_name}}", conn.TablespaceName); //表空间     
                    sbTableLine.Replace("{{file_author}}", conn.Author ?? "ai"); //自动生成        
                    sbTableLine.Replace("{{file_datetime}}", DateTimeUtil.Now().ToString()); //自动生成     
                    sbTableLine.Replace("{{file_date}}", DateTimeUtil.Now().ToString("yyyy-MM-DD")); //自动生成     
                    sbTableLine.Replace("{{file_time}}", DateTimeUtil.Now().ToString("HH:mm")); //自动生成
                                                                                                //
                    #region 主键模板  表格里面主键
                    if (lstCurPrimarys.Any())
                    {
                        foreach (var colunm in lstCurPrimarys)
                        {
                            ConvertTemplate(primarysSqlF, conn, colunm, sbTableLine);
                        }
                    }
                    #endregion 主键模板

                    #region 输出代码到对应文件
                    if (!string.IsNullOrEmpty(conn.TableOutPath))
                    {
                        StringBuilder sbOutPath = new StringBuilder(conn.TableOutPath);
                        sbOutPath.Replace("{{table_name_lowner_line}}", tableL);// 小写下划线
                        sbOutPath.Replace("{{table_name_upper_line}}", tableLU); // 大写下划线表名
                        sbOutPath.Replace("{{table_name_lowner}}", tableL);// 小写下划线
                        sbOutPath.Replace("{{table_name_class}}", tableC); // 类名,大写驼峰
                        sbOutPath.Replace("{{table_name_upper}}", tableC); // 大写驼峰
                        sbOutPath.Replace("{{table_name_field}}", tableF);// 小写驼峰 
                        sbOutPath.Replace("{{table_name_hline}}", tableH);// 横杆  - 
                        sbOutPath.Replace("{{owner_id}}", ownerId); //项目所有者
                        sbOutPath.Replace("{{table_owner_id}}", ownerId); //项目所有者
                        sbOutPath.Replace("{{table_sys_id}}", sysId); //系统ID
                        sbOutPath.Replace("{{table_conn_name}}", conn.ConnName);// 数据库连接串
                        sbOutPath.Replace("{{namespace_name}}", conn.NamespaceName); //命名空间名称
                        sbOutPath.Replace("{{package_name}}", conn.PackageName); //java包名称
                        sbOutPath.Replace("{{tablespace_name}}", conn.TablespaceName); //表空间       
                        FileUtil.CreateFileAndContentAsync(sbOutPath.ToString(), sbTableLine.ToString(), encoding);
                    }
                    #endregion 输出代码到对应文件
                    sbSql.AppendLine(sbTableLine.ToString());
                    sbDbSql.AppendLine(sbTableLine.ToString());
                }


                #region 输出代码到对应文件
                if (!string.IsNullOrEmpty(conn.OutPath))
                {
                    StringBuilder sbOutPath = new StringBuilder(conn.OutPath);
                    sbOutPath.Replace("{{global_owner_id}}", ownerId);
                    sbOutPath.Replace("{{global_sys_id}}", sysId);
                    sbOutPath.Replace("{{global_conn_name}}", conn.ConnName);
                    sbOutPath.Replace("{{table_conn_name}}", conn.ConnName);// 数据库连接串
                    sbOutPath.Replace("{{owner_id}}", ownerId); //项目所有者
                    dkv.Key = sbOutPath.ToString();
                    FileUtil.CreateFileAndContentAsync(sbOutPath.ToString(), sbDbSql.ToString(), encoding);
                }
                #endregion 输出代码到对应文件
            }
            dkv.Value = sbSql.ToString();
            return dkv;
        }

        /// <summary>
        /// 模板关键字转换
        /// </summary>
        /// <param name="columnSqlF"></param>
        /// <param name="conn"></param>
        /// <param name="colunm"></param>
        /// <param name="sbColumnLine"></param>

        private void ConvertTemplate(string columnSqlF, DatabasePo conn, SqlColumn colunm, StringBuilder sbColumnLine)
        {
            var dataTypeL = colunm.DataType.ToLower();
            sbColumnLine.Append(columnSqlF);
            sbColumnLine.Replace("{{column_name}}", colunm.Name.ToLower());// 字段名称 小写
            sbColumnLine.Replace("{{column_name_line_upper}}", colunm.Name.ToUpper());// 字段名称 大写
            sbColumnLine.Replace("{{column_title}}", colunm.Title.Replace("\r", " ").Replace("\n", " ").Replace("\\\r", " ").Replace("\\\\n", " "));// 字段标题
            sbColumnLine.Replace("{{column_field}}", colunm.FieldLowerCase);// RegexUtil.ConvertDownLine2CamelCase(colunm.Name.ToLower()));// 小写驼峰
            sbColumnLine.Replace("{{column_field_upper}}", colunm.FieldUpperCase);// RegexUtil.ConvertDownLine2CamelCase(colunm.Name.ToLower(), false)); //大写驼峰
            sbColumnLine.Replace("{{column_data_type}}", colunm.DataType); //数据库类型
            if (dataTypeL == "int" || dataTypeL == "long" || dataTypeL == "double" || dataTypeL == "float")
            {
                sbColumnLine.Replace("{{column_max_db_text}}", $""); //带括号长度
            }
            else if (dataTypeL == "decimal")
            {
                sbColumnLine.Replace("{{column_max_db_text}}", $"({colunm.MaxLength},{colunm.ColDecimal})"); //带括号长度
            }
            else if (colunm.MaxLength > 10000)
            {
                sbColumnLine.Replace("{{column_max_db_text}}", ""); //带括号长度
            }
            else
            {
                sbColumnLine.Replace("{{column_max_db_text}}", colunm.MaxLength > 0 ? $"({colunm.MaxLength})" : ""); //带括号长度
            }
            sbColumnLine.Replace("{{column_max_length}}", colunm.MaxLength?.ToString()); //长度
            sbColumnLine.Replace("{{column_is_primary}}", colunm.IsPrimary == true ? "1" : "0"); //是否主键
            sbColumnLine.Replace("{{column_is_null}}", colunm.IsNullValue == true ? "1" : "0"); // 是否为空
            sbColumnLine.Replace("{{column_is_null_db}}", colunm.IsNullValue == true ? "not null" : ""); // 是否为空  带括号
            sbColumnLine.Replace("{{column_is_required}}", colunm.IsRequired == true ? "1" : "0"); //是否必填
            #region 数据类型转换处理
            sbColumnLine.Replace("{{column_type_database}}", colunm.DataType); //数据库数据类型
            sbColumnLine.Replace("{{column_type_net}}", _configuration.GetValue<string>($"databaseMappingTypeNet:{dataTypeL}:bizDataType") ?? colunm.DataType); //.netcore数据类型
            sbColumnLine.Replace("{{column_type_java}}", _configuration.GetValue<string>($"databaseMappingTypeJava:{dataTypeL}:bizDataType") ?? colunm.DataType); //java数据类型
            sbColumnLine.Replace("{{column_type_javasript}}", _configuration.GetValue<string>($"databaseMappingTypeJavascript:{dataTypeL}:bizDataType") ?? colunm.DataType); //javascript数据类型
            sbColumnLine.Replace("{{column_typescript_type}}", _configuration.GetValue<string>($"databaseMappingTypeTypeScript:{dataTypeL}:bizDataType") ?? colunm.DataType); //typescript数据类型
            sbColumnLine.Replace("{{column_type_golang}}", _configuration.GetValue<string>($"databaseMappingTypeGoLang:{dataTypeL}:bizDataType") ?? colunm.DataType); //golang数据类型
            sbColumnLine.Replace("{{column_type_rust}}", _configuration.GetValue<string>($"databaseMappingTypeRust:{dataTypeL}:bizDataType") ?? colunm.DataType); //rust数据类型
            sbColumnLine.Replace("{{column_type_ios}}", _configuration.GetValue<string>($"databaseMappingTypeIos:{dataTypeL}:bizDataType") ?? colunm.DataType); //iso数据类型
            sbColumnLine.Replace("{{column_type_andriod}}", _configuration.GetValue<string>($"databaseMappingTypeAndriod:{dataTypeL}:bizDataType") ?? colunm.DataType); //andriod数据类型
            sbColumnLine.Replace("{{column_type_chh}}", _configuration.GetValue<string>($"databaseMappingTypeChh:{dataTypeL}:bizDataType") ?? colunm.DataType); //c++数据类型
            sbColumnLine.Replace("{{column_type_db}}", _configuration.GetValue<string>($"databaseMappingType{conn.TargetDbType}:{dataTypeL}:bizDataType") ?? colunm.DataType); //mysql 2 oracle
            #endregion 数据类型转换处理
        }
文档更新时间: 2023-11-05 14:13   作者:admin