2
schangxiang@126.com
2024-06-22 b87302f2fc46913b73561f9f996bce3f33a48f1c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using iWare.Wms.Core;
using iWare.Wms.Core.Util.LowCode.Front.Code;
using iWare.Wms.Core.Util.LowCode.Front.Model;
using Furion;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.Extras.iWare.Wms.Entity;
using Furion.Extras.iWare.Wms.Util.LowCode.Front.Code;
using Furion.FriendlyException;
using Furion.ViewEngine;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Newtonsoft.Json;
using System.Text;
 
namespace iWare.Wms.Application.CodeGen
{
    /// <summary>
    /// 代码生成器服务
    /// </summary>
    [Route("api/[Controller]")]
    [ApiDescriptionSettings(Name = "CodeGenerate", Order = 100)]
    public class CodeGenerateService : ICodeGenService, IDynamicApiController, ITransient
    {
        private readonly IRepository<SysCodeGen> _sysCodeGenRep; // 代码生成器仓储
        private readonly IRepository<SysLowCode> _sysLowCodeRep; // 代码生成器仓储
        private readonly ICodeGenConfigService _codeGenConfigService;
        private readonly IViewEngine _viewEngine;
 
        private readonly IRepository<SysMenu> _sysMenuRep; // 菜单表仓储
 
        public CodeGenerateService(IRepository<SysCodeGen> sysCodeGenRep,
                              IRepository<SysLowCode> sysLowCodeRep,
                              ICodeGenConfigService codeGenConfigService,
                              IViewEngine viewEngine,
                              IRepository<SysMenu> sysMenuRep)
        {
            _sysCodeGenRep = sysCodeGenRep;
            _sysLowCodeRep = sysLowCodeRep;
            _codeGenConfigService = codeGenConfigService;
            _viewEngine = viewEngine;
            _sysMenuRep = sysMenuRep;
        }
 
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<PageResult<SysCodeGen>> QueryCodeGenPageList([FromQuery] CodeGenPageInput input)
        {
            //if(input.)
            //// 加入配置表中
            //_codeGenConfigService.AddList(GetColumnList(input.Adapt<AddCodeGenInput>()), codeGen);
 
            var tableName = !string.IsNullOrEmpty(input.TableName?.Trim());
            var codeGens = await _sysCodeGenRep.DetachedEntities
                                               .Where((tableName, u => EF.Functions.Like(u.TableName, $"%{input.TableName.Trim()}%")))
                                               .ToADPagedListAsync(input.PageNo, input.PageSize);
            return codeGens;
        }
 
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task AddCodeGen(AddCodeGenInput input)
        {
            var isExist = await _sysCodeGenRep.DetachedEntities.AnyAsync(u => u.TableName == input.TableName);
            if (isExist)
                throw Oops.Oh(ErrorCode.D1400);
 
            if (input.LowCodeId != null && input.LowCodeId > 0)
            {
                isExist = await _sysCodeGenRep.DetachedEntities.AnyAsync(u => u.LowCodeId == input.LowCodeId);
            }
 
            if (!isExist)
            {
                var codeGen = input.Adapt<SysCodeGen>();
                var newCodeGen = await codeGen.InsertNowAsync();
 
                // 加入配置表中
                await _codeGenConfigService.DelAndAddList(GetColumnList(input), newCodeGen.Entity);
            }
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="inputs"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public Task DeleteCodeGen(List<DeleteCodeGenInput> inputs)
        {
            if (inputs == null || inputs.Count < 1) return Task.Run(() => { });
 
            var taskList = new List<Task>();
            inputs.ForEach(u =>
            {
                taskList.Add(_sysCodeGenRep.DeleteAsync(u.Id));
                // 删除配置表中
                taskList.Add(_codeGenConfigService.Delete(u.Id));
            });
            return Task.WhenAll(taskList);//等待所有任务完成
        }
 
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("edit")]
        public async Task UpdateCodeGen(UpdateCodeGenInput input)
        {
            var isExist = await _sysCodeGenRep.DetachedEntities.AnyAsync(u => u.TableName == input.TableName && u.Id != input.Id);
            if (isExist)
                throw Oops.Oh(ErrorCode.D1400);
 
            var codeGen = input.Adapt<SysCodeGen>();
 
            // 加入配置表中
            _codeGenConfigService.DelAndAddList(GetColumnList(input.Adapt<AddCodeGenInput>()), codeGen);
        }
 
        /// <summary>
        /// 刷新配置表
        /// </summary>
        /// <returns></returns>
        [HttpGet("refresh/{id}")]
        public void Refresh(long id)
        {
            var item = _sysCodeGenRep.Where(x => x.Id == id).FirstOrDefault();
            // 加入配置表中
            _codeGenConfigService.DelAndAddList(GetColumnList(item.Adapt<AddCodeGenInput>()), item);
        }
 
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<SysCodeGen> GetCodeGen([FromQuery] QueryCodeGenInput input)
        {
            return await _sysCodeGenRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id);
        }
 
        /// <summary>
        /// 获取数据库库集合
        /// </summary>
        /// <returns></returns>
        [HttpGet("DatabaseList")]
        public List<DatabaseOutput> GetDatabaseList()
        {
            var DbContextLocators = AppDomain.CurrentDomain.GetAssemblies()
                        .SelectMany(
                            a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IDbContextLocator)))
                        )
                        .Select(x => new DatabaseOutput { DatabaseName = x.Name, DatabaseComment = x.FullName })
                        .ToList();
 
            return DbContextLocators;
        }
 
        /// <summary>
        /// 获取数据库表(实体)集合
        /// </summary>
        /// <returns></returns>
        [HttpGet("InformationList")]
        public List<TableOutput> GetTableList(string dbContextLocatorName)
        {
            var dbContext = Db.GetDbContext();//默认数据库
            if (!string.IsNullOrEmpty(dbContextLocatorName))
            {
                var dbContentLocator = AppDomain.CurrentDomain.GetAssemblies()
                           .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IDbContextLocator)))).Where(x => x.Name == dbContextLocatorName).FirstOrDefault();
 
                dbContext = Db.GetDbContext(dbContentLocator);
            }
            // 获取实体类型属性
            //var entityType = dbContext.Model.GetEntityTypes()要改成 var entityType = dbContext.GetService<IDesignTimeModel>().Model.GetEntityTypes()
 
            return dbContext.GetService<IDesignTimeModel>().Model.GetEntityTypes().Select(u => new TableOutput
            {
                DatabaseName = dbContextLocatorName,
                TableName = u.GetDefaultTableName(),
                TableComment = u.GetComment()
            }).ToList();
        }
 
        /// <summary>
        /// 根据表名获取列
        /// </summary>
        /// <returns></returns>
        [HttpGet("ColumnList/{databaseName}/{tableName}")]
        public List<TableColumnOuput> GetColumnListByTableName(string databaseName, string tableName)
        {
            var dbContext = Db.GetDbContext();//默认数据库
            if (!string.IsNullOrEmpty(databaseName))
            {
                var dbContentLocator = AppDomain.CurrentDomain.GetAssemblies()
                           .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IDbContextLocator)))).Where(x => x.Name == databaseName).FirstOrDefault();
 
                dbContext = Db.GetDbContext(dbContentLocator);
            }
            // 获取实体类型属性
            var entityType = dbContext.GetService<IDesignTimeModel>().Model.GetEntityTypes().FirstOrDefault(u => u.ClrType.Name == tableName);
            if (entityType == null) return null;
 
            // 获取原始类型属性
            var type = entityType.ClrType;
            if (type == null) return null;
 
            // 按原始类型的顺序获取所有实体类型属性(不包含导航属性,会返回null)
            return type.GetProperties().Select(propertyInfo => entityType.FindProperty(propertyInfo.Name))
                       .Where(p => p != null).Select(p => new TableColumnOuput
                       {
                           ColumnName = p.Name,
                           ColumnKey = p.IsKey().ToString(),
                           DataType = p.PropertyInfo.PropertyType.ToString(),
                           NetType = CodeGenUtil.ConvertDataType(p.PropertyInfo.PropertyType.ToString()),
                           ColumnComment = p.GetComment()
                       }).ToList();
        }
 
        /// <summary>
        /// 获取数据表列(实体属性)集合
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public List<TableColumnOuput> GetColumnList([FromQuery] AddCodeGenInput input)
        {
            var dbContext = Db.GetDbContext();//默认数据库
            if (!string.IsNullOrEmpty(input.DatabaseName))
            {
                var dbContentLocator = AppDomain.CurrentDomain.GetAssemblies()
                           .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IDbContextLocator)))).Where(x => x.Name == input.DatabaseName).FirstOrDefault();
 
                dbContext = Db.GetDbContext(dbContentLocator);
            }
            // 获取实体类型属性
            var entityType = dbContext.GetService<IDesignTimeModel>().Model.GetEntityTypes()
                .FirstOrDefault(u => u.ClrType.Name == input.TableName);
            if (entityType == null) return null;
 
            // 获取原始类型属性
            var type = entityType.ClrType;
            if (type == null) return null;
 
            // 按原始类型的顺序获取所有实体类型属性(不包含导航属性,会返回null)
            return type.GetProperties().Select(propertyInfo => entityType.FindProperty(propertyInfo.Name))
                       .Where(p => p != null)
                       .Select(p => new TableColumnOuput
                       {
                           ColumnName = p.Name,
                           ColumnKey = p.IsKey().ToString(),
                           DataType = p.PropertyInfo.PropertyType.ToString(),
                           ColumnComment = p.GetComment()
                       }).ToList();
        }
 
        /// <summary>
        /// 代码生成_本地项目
        /// </summary>
        /// <returns></returns>
        [HttpPost("runLocal")]
        public async Task RunLocal(SysCodeGen input)
        {
            var templatePathList = GetTemplatePathList();
            var targetPathList = GetTargetPathList(input);
            for (var i = 0; i < templatePathList.Count; i++)
            {
                var tContent = File.ReadAllText(templatePathList[i]);
 
                var tableFieldList = await _codeGenConfigService.List(new CodeGenConfig() { CodeGenId = input.Id }); // 字段集合
 
                tableFieldList.ForEach(u =>
                {
                    switch (u.NetType.ToLower())
                    {
                        case "int":
                        case "int32":
                        case "long":
                        case "guid":
                        case "decimal":
                        case "datetime":
                        case "datetimeoffset":
                            u.NetTypeIsNullLable = "?";
                            break;
                    }
                    u.OriginalColumnName = u.ColumnName;
                });
 
                if (i >= 6) // 适应前端首字母小写
                {
                    tableFieldList.ForEach(u =>
                    {
                        u.ColumnName = u.ColumnName.Substring(0, 1).ToLower() + u.ColumnName[1..];
                    });
                }
 
                var queryWhetherList = tableFieldList.Where(u => u.QueryWhether == YesOrNot.Y.ToString()).ToList(); // 前端查询集合
 
                string FormDesign = "";
 
                if (input.LowCodeId != null && input.LowCodeId > 0)
                {
                    FormDesign = _sysLowCodeRep.Where(x => x.Id == input.LowCodeId).Select(x => x.FormDesign).FirstOrDefault();
                }
 
                List<Front_Dynamic> dynamicLoad_dict = new List<Front_Dynamic>();
                Dictionary<string, List<string>> dynamicData = new Dictionary<string, List<string>>();
 
                if (!string.IsNullOrEmpty(FormDesign))
                {
                    try
                    {
                        var AllDynamic = FormDesign.ConvertToFront().AllFront().AllDynamic();
 
                        AllDynamic.Where(x => x.Dynamic).Select(x => x.DynamicKey).ToList().ForEach(item =>
                        {
                            dynamicData.Add(item, new List<string>());
                            var d = item.GetDynamic();
                            if (d != null)
                            {
                                if (d.Head == "dict")
                                {
                                    dynamicLoad_dict.Add(d);
                                }
                            }
                        });
                    }
                    catch { }
                }
 
                try
                {
                    var tResult = _viewEngine.RunCompileFromCached(tContent, new
                    {
                        input.AuthorName,
                        input.BusName,
                        input.NameSpace,
                        input.ProName,
                        input.DatabaseName,
                        ClassName = input.TableName,
                        CamelizeClassName = input.TableName.Substring(0, 1).ToLower() + input.TableName[1..], //首字母小写
                        QueryWhetherList = queryWhetherList,
                        TableField = tableFieldList,
                        input.LowCodeId,
                        FormDesign,
                        DynamicData = JsonConvert.SerializeObject(dynamicData),
                        DynamicLoad_Dict = dynamicLoad_dict,
                        IsFile = tableFieldList.Where(x => x.DtoNetType.Contains("Front_FileDto")).Any(),
                        FileTableField = tableFieldList.Where(x => x.DtoNetType.Contains("Front_FileDto")).ToList()
                    });
 
                    var dirPath = new DirectoryInfo(targetPathList[i]).Parent.FullName;
                    if (!Directory.Exists(dirPath))
                        Directory.CreateDirectory(dirPath);
                    File.WriteAllText(targetPathList[i], tResult, Encoding.UTF8);
                }
                catch (Exception ex)
                {
                    throw Oops.Oh($"错误模板:{templatePathList[i]}。错误信息:{ex.Message}。");
                }
            }
 
            await AddMenu(input.DatabaseName.Substring(0, 5), input.TableName, input.BusName, input.MenuApplication, input.MenuPid);
        }
 
        private async Task AddMenu(string menucodePre, string className, string busName, string application, long pid)
        {
            // 定义菜单编码前缀
            var codePrefix = menucodePre + "_" + className.ToLower();//改为取数据库定位器的前五个字母方便区分业务 //"dilon_" + className.ToLower();
 
            // 先删除该表已生成的菜单列表
            var menus = await _sysMenuRep.DetachedEntities.Where(u => u.Code == codePrefix || u.Code.StartsWith(codePrefix + "_")).ToListAsync();
            await _sysMenuRep.DeleteAsync(menus);
 
            // 如果 pid 为 0 说明为顶级菜单, 需要创建顶级目录
            if (pid == 0)
            {
                // 目录
                var menuType0 = new SysMenu
                {
                    Pid = 0,
                    Pids = "[0],",
                    Name = busName + "管理",
                    Code = codePrefix,
                    Type = MenuType.DIR,
                    Icon = "robot",
                    Router = "/" + className.ToLower(),
                    Component = "PageView",
                    Application = application
                };
                pid = _sysMenuRep.InsertNowAsync(menuType0).GetAwaiter().GetResult().Entity.Id;
            }
            // 由于后续菜单会有修改, 需要判断下 pid 是否存在, 不存在报错
            else if (!await _sysMenuRep.DetachedEntities.AnyAsync(e => e.Id == pid))
                throw Oops.Oh(ErrorCode.D1505);
 
            // 菜单
            var menuType1 = new SysMenu
            {
                Pid = pid,
                Pids = "[0],[" + pid + "],",
                Name = busName + "管理",
                Code = codePrefix + "_mgr",
                Type = MenuType.MENU,
                Router = "/" + className.ToLower(),
                Component = "main/" + className + "/index",
                Application = application,
                OpenType = MenuOpenType.COMPONENT
            };
            var pid1 = _sysMenuRep.InsertNowAsync(menuType1).GetAwaiter().GetResult().Entity.Id;
 
            // 按钮-page
            var menuType2 = new SysMenu
            {
                Pid = pid1,
                Pids = "[0],[" + pid + "],[" + pid1 + "],",
                Name = busName + "查询",
                Code = codePrefix + "_mgr_page",
                Type = MenuType.BTN,
                Permission = className + ":page",
                Application = application,
            }.InsertAsync();
 
            // 按钮-detail
            var menuType2_1 = new SysMenu
            {
                Pid = pid1,
                Pids = "[0],[" + pid + "],[" + pid1 + "],",
                Name = busName + "详情",
                Code = codePrefix + "_mgr_detail",
                Type = MenuType.BTN,
                Permission = className + ":detail",
                Application = application,
            }.InsertAsync();
 
            // 按钮-add
            var menuType2_2 = new SysMenu
            {
                Pid = pid1,
                Pids = "[0],[" + pid + "],[" + pid1 + "],",
                Name = busName + "增加",
                Code = codePrefix + "_mgr_add",
                Type = MenuType.BTN,
                Permission = className + ":add",
                Application = application,
            }.InsertAsync();
 
            // 按钮-delete
            var menuType2_3 = new SysMenu
            {
                Pid = pid1,
                Pids = "[0],[" + pid + "],[" + pid1 + "],",
                Name = busName + "删除",
                Code = codePrefix + "_mgr_delete",
                Type = MenuType.BTN,
                Permission = className + ":delete",
                Application = application,
            }.InsertAsync();
 
            // 按钮-edit
            var menuType2_4 = new SysMenu
            {
                Pid = pid1,
                Pids = "[0],[" + pid + "],[" + pid1 + "],",
                Name = busName + "编辑",
                Code = codePrefix + "_mgr_edit",
                Type = MenuType.BTN,
                Permission = className + ":edit",
                Application = application,
            }.InsertAsync();
        }
 
        /// <summary>
        /// 获取模板文件路径集合
        /// </summary>
        /// <returns></returns>
        private List<string> GetTemplatePathList()
        {
            var templatePath = App.WebHostEnvironment.WebRootPath + @"\Template\";
            return new List<string>()
            {
                templatePath + "Service.cs.vm",
                templatePath + "IService.cs.vm",
                templatePath + "Input.cs.vm",
                templatePath + "Output.cs.vm",
                templatePath + "Dto.cs.vm",
                templatePath + "Mapper.cs.vm",
                templatePath + "index.vue.vm",
                templatePath + "addForm.vue.vm",
                templatePath + "editForm.vue.vm",
                templatePath + "Manage.js.vm",
            };
        }
 
        /// <summary>
        /// 设置生成文件路径
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private List<string> GetTargetPathList(SysCodeGen input)
        {
            var backendPath = new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent.FullName + @"\" + input.NameSpace + @"\Service\" + input.TableName + @"\";
            var servicePath = backendPath + input.TableName + "Service.cs";
            var iservicePath = backendPath + "I" + input.TableName + "Service.cs";
            var inputPath = backendPath + @"Dto\" + input.TableName + "Input.cs";
            var outputPath = backendPath + @"Dto\" + input.TableName + "Output.cs";
            var viewPath = backendPath + @"Dto\" + input.TableName + "Dto.cs";
            var mapperPath = backendPath + @"Map\" + input.TableName + "Mapper.cs";
            var frontendPath = new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent.Parent.FullName + @"\iwara-wms-web\src\views\main\";
            var indexPath = frontendPath + input.TableName + @"\index.vue";
            var addFormPath = frontendPath + input.TableName + @"\addForm.vue";
            var editFormPath = frontendPath + input.TableName + @"\editForm.vue";
            var apiJsPath = new DirectoryInfo(App.WebHostEnvironment.ContentRootPath).Parent.Parent.FullName + @"\iwara-wms-web\src\api\modular\main\" + input.TableName + "Manage.js";
 
            return new List<string>()
            {
                servicePath,
                iservicePath,
                inputPath,
                outputPath,
                viewPath,
                mapperPath,
                indexPath,
                addFormPath,
                editFormPath,
                apiJsPath
            };
        }
    }
}