|
|
using System.Reflection;
|
|
|
using System.Text;
|
|
|
using DS.Module.Core;
|
|
|
using DS.Module.Core.Modules;
|
|
|
using DS.WMS.Core.System.Entity;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Newtonsoft.Json;
|
|
|
using SqlSugar;
|
|
|
|
|
|
namespace DS.WMS.Core.DBSeed;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 初始化数据库
|
|
|
/// </summary>
|
|
|
public class DBSeedModule : DSAppModule
|
|
|
{
|
|
|
private static string SeedDataFolder = "DBInitData.json/{0}.tsv";
|
|
|
SqlSugar.ISqlSugarClient db;
|
|
|
|
|
|
public override void ApplicationInitialization(ApplicationContext context)
|
|
|
{
|
|
|
var builder = context.GetApplicationBuilder();
|
|
|
var env = DSIocManage.Instance.GetService<IHostingEnvironment>();
|
|
|
// var env = context.ServiceProvider.GetService<IHostingEnvironment>();
|
|
|
var basePath = env.WebRootPath;
|
|
|
SeedDataFolder = Path.Combine(basePath, SeedDataFolder);
|
|
|
try
|
|
|
{
|
|
|
db = context.ServiceProvider.GetService(typeof(ISqlSugarClient)) as ISqlSugarClient;
|
|
|
|
|
|
// 创建数据库
|
|
|
Console.WriteLine($"Create Database...");
|
|
|
db.DbMaintenance.CreateDatabase();
|
|
|
Console.WriteLine($"Database created successfully!");
|
|
|
// 创建数据库表,遍历指定命名空间下的class,
|
|
|
// 注意不要把其他命名空间下的也添加进来。
|
|
|
Console.WriteLine("Create Tables...");
|
|
|
var modelTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
|
|
|
where t.IsClass && t.Namespace == "DS.WMS.Core.System.Entity"
|
|
|
select t;
|
|
|
modelTypes.ToList().ForEach(t =>
|
|
|
{
|
|
|
if (!db.DbMaintenance.IsAnyTable(t.Name))
|
|
|
{
|
|
|
Console.WriteLine(t.Name);
|
|
|
db.CodeFirst.InitTables(t);
|
|
|
}
|
|
|
});
|
|
|
Console.WriteLine("Tables created successfully!");
|
|
|
Console.WriteLine();
|
|
|
Console.WriteLine($"Seeding database data...");
|
|
|
|
|
|
#region SysUser
|
|
|
|
|
|
if (!db.Queryable<SysUser>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysUser>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysUser"), Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysUser seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysUser already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region SysRole
|
|
|
|
|
|
if (!db.Queryable<SysRole>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysRole>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysRole"), Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysRole seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysRole already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region SysPermission
|
|
|
|
|
|
if (!db.Queryable<SysPermission>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysPermission>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysPermission"),
|
|
|
Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysPermission seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysPermission already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region SysRoleUser
|
|
|
|
|
|
if (!db.Queryable<SysRoleUser>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysRoleUser>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysRoleUser"),
|
|
|
Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysRoleUser seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysRoleUser already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
#region SysCompany
|
|
|
|
|
|
if (!db.Queryable<SysCompany>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysCompany>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysCompany"),
|
|
|
Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysCompany seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysRoleUser already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
#region SysUserCompany
|
|
|
|
|
|
if (!db.Queryable<SysUserCompany>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysUserCompany>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysUserCompany"),
|
|
|
Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysUserCompany seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysUserCompany already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
#region SysRolePermission
|
|
|
|
|
|
if (!db.Queryable<SysRolePermission>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysRolePermission>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysRolePermission"),
|
|
|
Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysRolePermission seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysRolePermission already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
#region SysPrint
|
|
|
|
|
|
if (!db.Queryable<SysPrint>().Any())
|
|
|
{
|
|
|
var temp = JsonConvert.DeserializeObject<List<SysPrint>>(
|
|
|
FileHelper.ReadFile(string.Format(SeedDataFolder, "SysPrint"),
|
|
|
Encoding.UTF8));
|
|
|
db.Insertable(temp).ExecuteCommand();
|
|
|
Console.WriteLine("Table:SysPrint seed success!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Console.WriteLine("Table:SysPrint already exists...");
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
Console.WriteLine();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Console.WriteLine("DBSeed异常:" + ex.ToString());
|
|
|
}
|
|
|
}
|
|
|
} |