|
|
@@ -11,7 +11,7 @@ using System.Text;
|
|
11
|
11
|
|
|
12
|
12
|
namespace Net6Demo_Api.Util
|
|
13
|
13
|
{
|
|
14
|
|
- public class CreateCodeFileHelper: ITransientDependency
|
|
|
14
|
+ public class CreateCodeFileHelper : ITransientDependency
|
|
15
|
15
|
{
|
|
16
|
16
|
private readonly IDbAccessor _db;
|
|
17
|
17
|
private readonly IHostEnvironment _env;
|
|
|
@@ -33,19 +33,24 @@ namespace Net6Demo_Api.Util
|
|
33
|
33
|
}
|
|
34
|
34
|
}
|
|
35
|
35
|
|
|
|
36
|
+ private int _isCover = 0;
|
|
36
|
37
|
/// <summary>
|
|
37
|
38
|
/// 生成代码文件
|
|
38
|
39
|
/// </summary>
|
|
39
|
40
|
/// <param name="tableName"></param>
|
|
40
|
|
- public void CreateCodeFile(string tableName, string controllerName, string moduleName)
|
|
|
41
|
+ public void CreateCodeFile(string tableName, string controllerName, string moduleName, int isOnlyEntity = 0, int isCover = 0)
|
|
41
|
42
|
{
|
|
42
|
|
- var list= GetDataTableWithSql(DbTableSqlDic[_db.DbType]).ToJson().ToObject<List<TableInfo>>();
|
|
|
43
|
+ _isCover = isCover;
|
|
|
44
|
+ var list = GetDataTableWithSql(DbTableSqlDic[_db.DbType]).ToJson().ToObject<List<TableInfo>>();
|
|
43
|
45
|
if (!string.IsNullOrEmpty(tableName)) list = list.Where(p => p.Name.ToLower() == tableName.ToLower()).ToList();
|
|
44
|
46
|
foreach (var item in list)
|
|
45
|
47
|
{
|
|
46
|
|
- CreateEntityFile(item.Name,item.Description, controllerName, moduleName);
|
|
47
|
|
- CreateBusinessFile(item.Name, controllerName, moduleName);
|
|
48
|
|
- CreateControllerFile(item.Name, controllerName, moduleName);
|
|
|
48
|
+ CreateEntityFile(item.Name, item.Description, controllerName, moduleName);
|
|
|
49
|
+ if (isOnlyEntity == 0)
|
|
|
50
|
+ {
|
|
|
51
|
+ CreateBusinessFile(item.Name, controllerName, moduleName);
|
|
|
52
|
+ CreateControllerFile(item.Name, controllerName, moduleName);
|
|
|
53
|
+ }
|
|
49
|
54
|
}
|
|
50
|
55
|
}
|
|
51
|
56
|
|
|
|
@@ -59,7 +64,7 @@ namespace Net6Demo_Api.Util
|
|
59
|
64
|
string pname = _env.ApplicationName;
|
|
60
|
65
|
|
|
61
|
66
|
var dbtype = _db.DbType;
|
|
62
|
|
- var columns = GetDataTableWithSql(DbTableColumnSqlDic[dbtype], new Dictionary<string, string>() { { "@tablename" , tableName } } ).ToJson().ToObject<List<TableColumn>>();
|
|
|
67
|
+ var columns = GetDataTableWithSql(DbTableColumnSqlDic[dbtype], new Dictionary<string, string>() { { "@tablename", tableName } }).ToJson().ToObject<List<TableColumn>>();
|
|
63
|
68
|
int index = 0;
|
|
64
|
69
|
string properties = string.Empty;
|
|
65
|
70
|
string inputproperties = string.Empty;
|
|
|
@@ -75,15 +80,16 @@ namespace Net6Demo_Api.Util
|
|
75
|
80
|
}
|
|
76
|
81
|
|
|
77
|
82
|
Type type = DbTypeToCsharpType(column.Type);
|
|
78
|
|
- string isNullable = column.IsNullable && type.IsValueType ? "?" : "";
|
|
79
|
|
- string description =string.IsNullOrEmpty(column.Description) ? column.Name : column.Description;
|
|
|
83
|
+ //string isNullable = column.IsNullable && type.IsValueType ? "?" : "";
|
|
|
84
|
+ string isNullable = column.IsNullable ? "?" : "";
|
|
|
85
|
+ string description = string.IsNullOrEmpty(column.Description) ? column.Name : column.Description;
|
|
80
|
86
|
properties += $@"
|
|
81
|
87
|
/// <summary>{description}</summary>{key}
|
|
82
|
88
|
public {type.Name}{isNullable} {column.Name} {{ get; set; }}
|
|
83
|
89
|
";
|
|
84
|
90
|
inputproperties += $@"
|
|
85
|
91
|
/// <summary>{description}</summary>
|
|
86
|
|
- public {type.Name}{isNullable} {(column.Name.StartsWith("F_")? column.Name.Substring(2) : column.Name)} {{ get; set; }}
|
|
|
92
|
+ public {type.Name}{isNullable} {(column.Name.StartsWith("F_") ? column.Name.Substring(2) : column.Name)} {{ get; set; }}
|
|
87
|
93
|
";
|
|
88
|
94
|
viewproperties += $@"
|
|
89
|
95
|
/// <summary>{description}</summary>
|
|
|
@@ -308,6 +314,7 @@ namespace {pname}.Business
|
|
308
|
314
|
{
|
|
309
|
315
|
string path = filepath.Substring(0, filepath.LastIndexOf("\\"));
|
|
310
|
316
|
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
|
|
|
317
|
+ if (File.Exists(filepath) && _isCover == 1) File.Delete(filepath);
|
|
311
|
318
|
if (!File.Exists(filepath))
|
|
312
|
319
|
{
|
|
313
|
320
|
FileInfo file = new FileInfo(filepath);
|
|
|
@@ -540,7 +547,7 @@ namespace {pname}.Business
|
|
540
|
547
|
/// <summary>
|
|
541
|
548
|
/// 数据库类型表sql字典
|
|
542
|
549
|
/// </summary>
|
|
543
|
|
- private Dictionary<DatabaseType, string> DbTableSqlDic=new Dictionary<DatabaseType, string>()
|
|
|
550
|
+ private Dictionary<DatabaseType, string> DbTableSqlDic = new Dictionary<DatabaseType, string>()
|
|
544
|
551
|
{
|
|
545
|
552
|
{ DatabaseType.SqlServer,
|
|
546
|
553
|
"select [Name] = a.name, [Description] = g.value from sys.tables a left join sys.extended_properties g on (a.object_id = g.major_id AND g.minor_id = 0 AND g.name= 'MS_Description')"
|
|
|
@@ -694,5 +701,6 @@ order by a.attnum asc;"
|
|
694
|
701
|
|
|
695
|
702
|
private string _description { get; set; }
|
|
696
|
703
|
}
|
|
|
704
|
+
|
|
697
|
705
|
}
|
|
698
|
706
|
}
|