From 0fb0f17319ecf71d66b96a6acfd07f754be9443e Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期四, 24 十月 2024 13:44:38 +0800
Subject: [PATCH] WCS添加穿梭车信息表,修改任务信息表
---
项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs | 318 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 318 insertions(+), 0 deletions(-)
diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs"
new file mode 100644
index 0000000..4a92f35
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Seed/DBSeed.cs"
@@ -0,0 +1,318 @@
+锘縰sing Castle.Components.DictionaryAdapter.Xml;
+using Microsoft.AspNetCore.Mvc.Rendering;
+using Newtonsoft.Json;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Dynamic;
+using System.Linq;
+using System.Net.Sockets;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Core.Const;
+using WIDESEA_Core.DB;
+using WIDESEA_Core.Helper;
+using WIDESEA_Core.Tenants;
+
+namespace WIDESEA_Core.Seed
+{
+ public class DBSeed
+ {
+ private static string SeedDataFolder = "WIDESEA_DB.DBSeed.Json/{0}.tsv";
+
+ /// <summary>
+ /// 寮傛娣诲姞绉嶅瓙鏁版嵁
+ /// </summary>
+ /// <param name="myContext"></param>
+ /// <param name="WebRootPath"></param>
+ /// <returns></returns>
+ public static void SeedAsync(DBContext dbContext, string WebRootPath)
+ {
+ try
+ {
+ if (string.IsNullOrEmpty(WebRootPath))
+ {
+ throw new Exception("鑾峰彇wwwroot璺緞鏃讹紝寮傚父锛�");
+ }
+
+ SeedDataFolder = Path.Combine(WebRootPath, SeedDataFolder);
+
+ Console.WriteLine("************ WIDESEA DataBase Set *****************");
+
+ Console.WriteLine($"Master DB Type: {DBContext.DbType}");
+
+ Console.WriteLine();
+
+ // 鍒涘缓鏁版嵁搴�
+ Console.WriteLine($"Create Database(The Db Id:{DBContext.ConnId})...");
+
+ if (DBContext.DbType != SqlSugar.DbType.Oracle)
+ {
+ dbContext.Db.DbMaintenance.CreateDatabase();
+ ConsoleHelper.WriteSuccessLine($"Database Created Successfully!");
+ }
+ else
+ {
+ //Oracle 鏁版嵁搴撲笉鏀寔璇ユ搷浣�
+ ConsoleHelper.WriteSuccessLine($"Oracle 鏁版嵁搴撲笉鏀寔璇ユ搷浣滐紝鍙墜鍔ㄥ垱寤篛racle鏁版嵁搴�!");
+ }
+
+ // 鍒涘缓鏁版嵁搴撹〃锛岄亶鍘嗘寚瀹氬懡鍚嶇┖闂翠笅鐨刢lass锛�
+ // 娉ㄦ剰涓嶈鎶婂叾浠栧懡鍚嶇┖闂翠笅鐨勪篃娣诲姞杩涙潵銆�
+ Console.WriteLine("Create Tables...");
+
+ var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
+ var referencedAssemblies = Directory.GetFiles(path, MainDb.AssemblyName).Select(Assembly.LoadFrom).ToArray();
+
+ var modelTypes = referencedAssemblies
+ .SelectMany(a => a.DefinedTypes)
+ .Select(type => type.AsType())
+ .Where(x => x.IsClass && x.Namespace is MainDb.EntityNameSpace && x.GetCustomAttribute<SugarTable>() != null)
+ .ToList();
+
+ modelTypes.ForEach(t =>
+ {
+ //var diffString = dbContext.Db.CodeFirst.GetDifferenceTables(t).ToDiffString();
+ // 杩欓噷鍙敮鎸佹坊鍔犺〃锛屼笉鏀寔鍒犻櫎
+ // 濡傛灉鎯宠鍒犻櫎锛屾暟鎹簱鐩存帴鍙抽敭鍒犻櫎锛屾垨鑰呰仈绯籗qlSugar浣滆�咃紱
+ IDbMaintenance dbMaintenance = dbContext.Db.DbMaintenance;
+ if (!dbMaintenance.IsAnyTable(t.Name, false))
+ {
+ ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] Created Successfully");
+ dbContext.Db.CodeFirst.InitTables(t);
+
+ string seedData = FileHelper.ReadFile(string.Format(SeedDataFolder, t.Name), Encoding.UTF8);
+
+ #region AddSeedData
+ if (seedData != "涓嶅瓨鍦ㄧ浉搴旂殑鐩綍")
+ {
+ List<Dictionary<string, object>>? dicFile = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(seedData);
+
+ if (dicFile != null && dicFile.Count > 0)
+ {
+ List<Dictionary<string, object>> dic = new List<Dictionary<string, object>>();
+
+ List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList();
+ var a = t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name));
+
+ List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => columnNames.Contains(x.Name)).ToList();
+ for (int j = 0; j < dicFile.Count; j++)
+ {
+ Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
+ for (int i = 0; i < propertyInfos.Count; i++)
+ {
+ PropertyInfo propertyInfo = propertyInfos[i];
+ SugarColumn? sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>();
+ if (sugarColumn != null)
+ {
+ if (!sugarColumn.IsIgnore)
+ {
+ keyValuePairs.Add(propertyInfo.Name, dicFile[j][propertyInfo.Name]);
+ }
+ }
+ }
+ dic.Add(keyValuePairs);
+ }
+
+ if (dic.Count > 0)
+ {
+ for (int i = 0; i < dic.Count; i++)
+ {
+ if (dic[i].ContainsKey("CreateDate"))
+ dic[i]["CreateDate"] = DateTime.Now;
+ else
+ dic[i].Add("CreateDate", DateTime.Now);
+ }
+ string str = $"SET IDENTITY_INSERT {t.Name} ON;";
+
+ str += dbContext.Db.Insertable(dic).AS(t.Name).ToSqlString();
+
+ str += ($"SET IDENTITY_INSERT {t.Name} OFF;");
+
+ dbContext.Db.Ado.ExecuteCommand(str);
+
+ ConsoleHelper.WriteSuccessLine($"Table [{t.Name}] SeedData Added Successfully");
+ }
+ }
+ }
+ #endregion
+ }
+ else
+ {
+ List<string> columnNames = dbContext.Db.DbMaintenance.GetColumnInfosByTableName(t.Name, false).Select(x => x.DbColumnName).ToList();
+ if (t.GetProperties().FirstOrDefault(x => !columnNames.Contains(x.Name)) != null)
+ {
+ bool isChange = true;
+ List<PropertyInfo> propertyInfos = t.GetProperties().Where(x => !columnNames.Contains(x.Name)).ToList();
+ for (int i = 0; i < propertyInfos.Count; i++)
+ {
+ PropertyInfo propertyInfo = propertyInfos[i];
+ SugarColumn? sugarColumn = propertyInfo.GetCustomAttribute<SugarColumn>();
+ if (sugarColumn != null)
+ {
+ if (!sugarColumn.IsIgnore)
+ {
+ if (!sugarColumn.IsNullable)
+ {
+ isChange = false;
+ break;
+ }
+ }
+ }
+ }
+ if (isChange)
+ dbContext.Db.CodeFirst.InitTables(t);
+ }
+ }
+ });
+ ConsoleHelper.WriteSuccessLine($"Tables Created Successfully!");
+ Console.WriteLine();
+
+
+ }
+ catch (Exception ex)
+ {
+ // 1銆佽嫢鏄疢ysql,鏌ョ湅甯歌闂:https://github.com/anjoy8/Blog.Core/issues/148#issue-776281770
+ //2銆佽嫢鏄疧racle,鏌ョ湅甯歌闂:https://github.com/anjoy8/Blog.Core/issues/148#issuecomment-752340231
+ throw new Exception("閿欒锛�" + ex.Message);
+ }
+ }
+
+ /// <summary>
+ /// 鍒濆鍖� 澶氱鎴�
+ /// </summary>
+ /// <param name="dbContext"></param>
+ /// <returns></returns>
+ public static async Task TenantSeedAsync(DBContext dbContext)
+ {
+
+ if (BaseDBConfig.MutiConnectionString.Where(x => x.ConnId != MainDb.CurrentDbConnId).Any())
+ {
+ Console.WriteLine($@"Init Multi Tenant Db");
+ foreach (MutiDBOperate tenant in BaseDBConfig.MutiConnectionString.Where(x => x.ConnId != MainDb.CurrentDbConnId))
+ {
+
+ Console.WriteLine($@"Init Multi Tenant Db : {tenant.ConnId}");
+ ConnectionConfig connectionConfig = new ConnectionConfig()
+ {
+ ConfigId = tenant.ConnId,
+ ConnectionString = tenant.Connection,
+ IsAutoCloseConnection = true,
+ MoreSettings = new ConnMoreSettings()
+ {
+ IsAutoRemoveDataCache = true
+ },
+ DbType = (DbType)tenant.DbType,
+ };
+ await InitTenantSeedAsync(dbContext.Db.AsTenant(), connectionConfig);
+ }
+
+ Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db Finish");
+ }
+
+ //tenants = await myContext.Db.Queryable<SysTenant>().Where(s => s.TenantType == TenantTypeEnum.Tables).ToListAsync();
+ //if (tenants.Any())
+ //{
+ // await InitTenantSeedAsync(myContext, tenants);
+ //}
+ }
+
+ #region 澶氱鎴� 澶氬簱 鍒濆鍖�
+
+ /// <summary>
+ /// 鍒濆鍖栧搴�
+ /// </summary>
+ /// <param name="itenant"></param>
+ /// <param name="config"></param>
+ /// <returns></returns>
+ public static async Task InitTenantSeedAsync(ITenant itenant, ConnectionConfig config)
+ {
+ Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db");
+ //itenant.RemoveConnection(config.ConfigId);
+ itenant.AddConnection(config);
+
+ var db = itenant.GetConnectionScope(config.ConfigId);
+
+ db.DbMaintenance.CreateDatabase();
+ ConsoleHelper.WriteSuccessLine($"Init Multi Tenant Db : {config.ConfigId} Database created successfully!");
+
+ Console.WriteLine($@"Init Multi Tenant Db : {config.ConfigId} Create Tables");
+
+ // 鑾峰彇鎵�鏈夊疄浣撹〃-鍒濆鍖栫鎴蜂笟鍔¤〃
+ var entityTypes = TenantUtil.GetTenantEntityTypes(TenantTypeEnum.Db);
+ if (!entityTypes.Any()) return;
+ foreach (var entityType in entityTypes)
+ {
+ //var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
+ //if (splitTable == null)
+ db.CodeFirst.InitTables(entityType);
+ //else
+ // db.CodeFirst.SplitTables().InitTables(entityType);
+
+ Console.WriteLine(entityType.Name);
+ }
+ Console.WriteLine(DateTime.Now + $@"Init Multi Tenant Db Finish");
+ //澶氱鎴峰垵濮嬪寲绉嶅瓙鏁版嵁
+ //await TenantSeedDataAsync(db, TenantTypeEnum.Db);
+ }
+
+ #endregion
+
+ //private static async Task TenantSeedDataAsync(ISqlSugarClient db, TenantTypeEnum tenantType)
+ //{
+ // // 鑾峰彇鎵�鏈夌瀛愰厤缃�-鍒濆鍖栨暟鎹�
+ // var seedDataTypes = AssemblysExtensions.GetAllAssemblies().SelectMany(s => s.DefinedTypes)
+ // .Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass)
+ // .Where(u =>
+ // {
+ // var esd = u.GetInterfaces().FirstOrDefault(i => i.HasImplementedRawGeneric(typeof(IEntitySeedData<>)));
+ // if (esd is null)
+ // {
+ // return false;
+ // }
+
+ // var eType = esd.GenericTypeArguments[0];
+ // return eType.IsTenantEntity(tenantType);
+ // });
+ // if (!seedDataTypes.Any()) return;
+ // foreach (var seedType in seedDataTypes)
+ // {
+ // dynamic instance = Activator.CreateInstance(seedType);
+ // //鍒濆鍖栨暟鎹�
+ // {
+ // var seedData = instance.InitSeedData();
+ // if (seedData != null && Enumerable.Any(seedData))
+ // {
+ // var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
+ // var entity = db.EntityMaintenance.GetEntityInfo(entityType);
+
+ // if (!await db.Queryable(entity.DbTableName, "").AnyAsync())
+ // {
+ // await db.Insertable(Enumerable.ToList(seedData)).ExecuteCommandAsync();
+ // Console.WriteLine($"Table:{entity.DbTableName} init success!");
+ // }
+ // }
+ // }
+
+ // //绉嶅瓙鏁版嵁
+ // {
+ // var seedData = instance.SeedData();
+ // if (seedData != null && Enumerable.Any(seedData))
+ // {
+ // var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
+ // var entity = db.EntityMaintenance.GetEntityInfo(entityType);
+
+ // await db.Storageable(Enumerable.ToList(seedData)).ExecuteCommandAsync();
+ // Console.WriteLine($"Table:{entity.DbTableName} seedData success!");
+ // }
+ // }
+
+ // //鑷畾涔夊鐞�
+ // {
+ // await instance.CustomizeSeedData(db);
+ // }
+ // }
+ //}
+ }
+}
--
Gitblit v1.9.3