diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs index a6f6272..bfc2e3c 100644 --- a/ZR.Admin.WebApi/Controllers/CommonController.cs +++ b/ZR.Admin.WebApi/Controllers/CommonController.cs @@ -5,11 +5,13 @@ using Infrastructure.Extensions; using Infrastructure.Model; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using MiniExcelLibs; using Newtonsoft.Json; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; using ZR.Common; using ZR.Model.System; +using ZR.Service.System; using ZR.Service.System.IService; namespace ZR.Admin.WebApi.Controllers @@ -25,7 +27,10 @@ namespace ZR.Admin.WebApi.Controllers private IWebHostEnvironment WebHostEnvironment; private ISysFileService SysFileService; - public CommonController(IOptions options, IWebHostEnvironment webHostEnvironment, ISysFileService fileService) + public CommonController( + IOptions options, + IWebHostEnvironment webHostEnvironment, + ISysFileService fileService) { WebHostEnvironment = webHostEnvironment; SysFileService = fileService; @@ -155,6 +160,91 @@ namespace ZR.Admin.WebApi.Controllers } #endregion + + /// + /// 初始化种子数据 + /// + /// + [HttpGet] + [ApiExplorerSettings(IgnoreApi = true)] + [ActionPermissionFilter(Permission = "common")] + [Log(BusinessType = BusinessType.INSERT, Title = "初始化数据")] + public IActionResult InitSeedData() + { + if (!WebHostEnvironment.IsDevelopment()) + { + return ToResponse(ResultCode.FAIL, "导入数据失败"); + } + var path = Path.Combine(WebHostEnvironment.WebRootPath, "data.xlsx"); + //var sheetNames = MiniExcel.GetSheetNames(path); + SeedDataService seedDataService = new(); + + var sysUser = MiniExcel.Query(path, sheetName: "user").ToList(); + var result1 = seedDataService.InitUserData(sysUser); + + var sysPost = MiniExcel.Query(path, sheetName: "post").ToList(); + var result2 = seedDataService.InitPostData(sysPost); + + var sysRole = MiniExcel.Query(path, sheetName: "role").ToList(); + var result3 = seedDataService.InitRoleData(sysRole); + + var sysUserRole = MiniExcel.Query(path, sheetName: "user_role").ToList(); + var result4 = seedDataService.InitUserRoleData(sysUserRole); + + var sysMenu = MiniExcel.Query(path, sheetName: "menu").ToList(); + var result5 = seedDataService.InitMenuData(sysMenu); + + var sysConfig = MiniExcel.Query(path, sheetName: "config").ToList(); + var result6 = seedDataService.InitConfigData(sysConfig); + + var sysRoleMenu = MiniExcel.Query(path, sheetName: "role_menu").ToList(); + var result7 = seedDataService.InitRoleMenuData(sysRoleMenu); + + var sysDict = MiniExcel.Query(path, sheetName: "dict_type").ToList(); + var result8 = seedDataService.InitDictType(sysDict); + + var sysDictData = MiniExcel.Query(path, sheetName: "dict_data").ToList(); + var result9 = seedDataService.InitDictData(sysDictData); + + var sysDept = MiniExcel.Query(path, sheetName: "dept").ToList(); + var result10 = seedDataService.InitDeptData(sysDept); + + var sysArticleCategory = MiniExcel.Query(path, sheetName: "article_category").ToList(); + var result11 = seedDataService.InitArticleCategoryData(sysArticleCategory); + + var sysTask = MiniExcel.Query(path, sheetName: "task").ToList(); + var result12 = seedDataService.InitTaskData(sysTask); + + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(result1.Item1); + Console.WriteLine(result2.Item1); + Console.WriteLine(result3.Item1); + Console.WriteLine(result4.Item1); + Console.WriteLine(result5.Item1); + Console.WriteLine(result6.Item1); + Console.WriteLine(result7.Item1); + Console.WriteLine(result8.Item1); + Console.WriteLine(result9.Item1); + Console.WriteLine(result10.Item1); + Console.WriteLine(result11.Item1); + Console.WriteLine(result12.Item1); + + return SUCCESS(new + { + result1 = result1.Item1, + result2 = result2.Item1, + result3 = result3.Item1, + result4 = result4.Item1, + result5 = result5.Item1, + result6 = result6.Item1, + result7 = result7.Item1, + result8 = result8.Item1, + result9 = result9.Item1, + result10 = result10.Item1, + result11 = result11.Item1, + result12 = result12.Item1 + }); + } } public class UploadDto diff --git a/ZR.Admin.WebApi/Extensions/LogoExtension.cs b/ZR.Admin.WebApi/Extensions/LogoExtension.cs index 46f7ddd..e9291ed 100644 --- a/ZR.Admin.WebApi/Extensions/LogoExtension.cs +++ b/ZR.Admin.WebApi/Extensions/LogoExtension.cs @@ -1,6 +1,4 @@ using JinianNet.JNTemplate; -using Microsoft.Extensions.DependencyInjection; -using System; using ZR.Common; namespace ZR.Admin.WebApi.Extensions @@ -12,7 +10,7 @@ namespace ZR.Admin.WebApi.Extensions Console.ForegroundColor = ConsoleColor.Blue; var contentTpl = JnHelper.ReadTemplate("", "logo.txt"); var content = contentTpl?.Render(); - + Console.WriteLine(content); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("源码地址: https://gitee.com/izory/ZrAdminNetCore"); diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index e449dcf..d217236 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -27,7 +27,7 @@ builder.Services.AddCors(c => { c.AddPolicy("Policy", policy => { - policy.WithOrigins(corsUrls == null ? Array.Empty() : corsUrls) + policy.WithOrigins(corsUrls ?? Array.Empty()) .AllowAnyHeader()//允许任意头 .AllowCredentials()//允许cookie .AllowAnyMethod();//允许任意方法 @@ -89,7 +89,7 @@ builder.Services.AddSwaggerConfig(); var app = builder.Build(); InternalApp.ServiceProvider = app.Services; -if (builder.Configuration["InitDb"].ParseToBool() == true) +if (builder.Configuration["InitDb"].ParseToBool() == true && app.Environment.IsDevelopment()) { app.Services.InitDb(); } diff --git a/ZR.Admin.WebApi/wwwroot/data.xlsx b/ZR.Admin.WebApi/wwwroot/data.xlsx new file mode 100644 index 0000000..a5c36f1 Binary files /dev/null and b/ZR.Admin.WebApi/wwwroot/data.xlsx differ diff --git a/ZR.CodeGenerator/ZR.CodeGenerator.csproj b/ZR.CodeGenerator/ZR.CodeGenerator.csproj index 7ec26ed..31c8979 100644 --- a/ZR.CodeGenerator/ZR.CodeGenerator.csproj +++ b/ZR.CodeGenerator/ZR.CodeGenerator.csproj @@ -12,6 +12,6 @@ - + diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj index 8fd4128..627c363 100644 --- a/ZR.Model/ZR.Model.csproj +++ b/ZR.Model/ZR.Model.csproj @@ -10,7 +10,7 @@ - + diff --git a/ZR.Repository/ZR.Repository.csproj b/ZR.Repository/ZR.Repository.csproj index fc4e4c2..7ff20e2 100644 --- a/ZR.Repository/ZR.Repository.csproj +++ b/ZR.Repository/ZR.Repository.csproj @@ -15,6 +15,6 @@ - + diff --git a/ZR.Service/System/SeedDataService.cs b/ZR.Service/System/SeedDataService.cs new file mode 100644 index 0000000..4cffdc4 --- /dev/null +++ b/ZR.Service/System/SeedDataService.cs @@ -0,0 +1,222 @@ +using Infrastructure.Extensions; +using SqlSugar.IOC; +using System.Collections.Generic; +using ZR.Common; +using ZR.Model.System; + +namespace ZR.Service.System +{ + public class SeedDataService + { + /// + /// 初始化用户数据 + /// + /// + /// + public (string, object, object) InitUserData(List data) + { + data.ForEach(x => + { + x.Password = "E10ADC3949BA59ABBE56E057F20F883E"; + }); + var db = DbScoped.SugarScope; + db.Ado.BeginTran(); + //db.Ado.ExecuteCommand("SET IDENTITY_INSERT sys_user ON"); + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .SplitError(x => x.Item.UserName.IsEmpty(), "用户名不能为空") + .SplitError(x => !Tools.CheckUserName(x.Item.UserName), "用户名不符合规范") + .WhereColumns(it => it.UserId)//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2}) + .ToStorage(); + var result = x.AsInsertable.OffIdentity().ExecuteCommand();//插入可插入部分; + //db.Ado.ExecuteCommand("SET IDENTITY_INSERT sys_user OFF"); + db.Ado.CommitTran(); + + string msg = $"[用户数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + /// + /// 菜单数据 + /// + /// + /// + public (string, object, object) InitMenuData(List data) + { + var db = DbScoped.SugarScope; + db.Ado.BeginTran(); + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.MenuId)//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2}) + .ToStorage(); + var result = x.AsInsertable.OffIdentity().ExecuteCommand();//插入可插入部分; + db.Ado.CommitTran(); + + string msg = $"[菜单数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + /// + /// 角色菜单数据 + /// + /// + /// + public (string, object, object) InitRoleMenuData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => new { it.Menu_id, it.Role_id }) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand();//插入可插入部分; + + string msg = $"[角色菜单] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + /// + /// 初始化部门数据 + /// + /// + /// + public (string, object, object) InitDeptData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.DeptId) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand(); + + string msg = $"[部门数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + public (string, object, object) InitPostData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.PostCode) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand(); + + string msg = $"[岗位数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + public (string, object, object) InitRoleData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.RoleKey) + .ToStorage(); + var result = x.AsInsertable.OffIdentity().ExecuteCommand(); + + string msg = $"[角色数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + public (string, object, object) InitUserRoleData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => new { it.RoleId, it.UserId }) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand(); + + string msg = $"[用户角色] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + /// + /// 系统配置 + /// + /// + /// + public (string, object, object) InitConfigData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.ConfigKey) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand(); + + string msg = $"[系统配置] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + /// + /// 字典 + /// + /// + /// + public (string, object, object) InitDictType(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.DictType) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand(); + + string msg = $"[字典管理] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + /// + /// 字典数据 + /// + /// + /// + public (string, object, object) InitDictData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => new { it.DictType, it.DictValue }) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand(); + + string msg = $"[字典数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + /// + /// 文章目录 + /// + /// + /// + public (string, object, object) InitArticleCategoryData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.Name) + .ToStorage(); + var result = x.AsInsertable.OffIdentity().ExecuteCommand(); + + string msg = $"[字典数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + + /// + /// 任务 + /// + /// + /// + public (string, object, object) InitTaskData(List data) + { + var db = DbScoped.SugarScope; + var x = db.Storageable(data) + .SplitInsert(it => it.NotAny()) + .WhereColumns(it => it.Name) + .ToStorage(); + var result = x.AsInsertable.ExecuteCommand(); + + string msg = $"[任务数据] 插入{x.InsertList.Count} 错误数据{x.ErrorList.Count} 总共{x.TotalList.Count}"; + return (msg, x.ErrorList, x.IgnoreList); + } + } +}