From cde56aae50adc11ff8db84e424d873843c566bfd Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期一, 24 二月 2025 23:40:41 +0800
Subject: [PATCH] 优化WCS移库逻辑

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SqlsugarSetup.cs |  145 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 145 insertions(+), 0 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SqlsugarSetup.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SqlsugarSetup.cs"
new file mode 100644
index 0000000..3fa4e19
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SqlsugarSetup.cs"
@@ -0,0 +1,145 @@
+锘縰sing Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.DependencyInjection;
+using SqlSugar;
+using StackExchange.Profiling;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Core.AOP;
+using WIDESEA_Core.DB;
+using WIDESEA_Core.Helper;
+using WIDESEA_Core.LogHelper;
+using WIDESEA_Core.Seed;
+
+namespace WIDESEA_Core
+{
+    /// <summary>
+    /// SqlSugar 鍚姩鏈嶅姟
+    /// </summary>
+    public static class SqlsugarSetup
+    {
+        private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions());
+
+        public static void AddSqlsugarSetup(this IServiceCollection services)
+        {
+            if (services == null) throw new ArgumentNullException(nameof(services));
+
+            // 榛樿娣诲姞涓绘暟鎹簱杩炴帴
+            //MainDb.CurrentDbConnId = AppSettings.app(new string[] { "MainDB" });
+
+            // SqlSugarScope鏄嚎绋嬪畨鍏紝鍙娇鐢ㄥ崟渚嬫敞鍏�
+            // 鍙傝�冿細https://www.donet5.com/Home/Doc?typeId=1181
+            services.AddScoped<ISqlSugarClient>(o =>
+            {
+                var memoryCache = o.GetRequiredService<IMemoryCache>();
+
+                // 杩炴帴瀛楃涓�
+                var listConfig = new List<ConnectionConfig>
+                {
+                    new ConnectionConfig
+                    {
+                        ConfigId = MainDb.CurrentDbConnId,
+                        ConnectionString = DBContext.GetMainConnectionDb().Connection,
+                        IsAutoCloseConnection = true,
+                        DbType = MainDb.DbType,
+                        AopEvents = new AopEvents
+                        {
+                            OnLogExecuting = (sql, p) =>
+                            {
+                                  Parallel.For(0, 1, e =>
+                                  {
+                                        MiniProfiler.Current.CustomTiming("SQL锛�", GetParas(p) + "銆怱QL璇彞銆戯細" + sql);
+                                      //Console.Out.WriteLine(sql);
+                                      //Console.Out.WriteLine();
+                                  });
+                            }
+                        }
+                    }
+                };
+                #region 浠庡簱
+                //var listConfig_Slave = new List<SlaveConnectionConfig>();
+                //BaseDBConfig.MutiConnectionString.ForEach(s =>
+                //{
+                //    if(s.ConnId != MainDb.CurrentDbConnId)
+                //    {
+                //        listConfig_Slave.Add(new SlaveConnectionConfig()
+                //        {
+                //            HitRate = s.HitRate,
+                //            ConnectionString = s.Connection
+                //        });
+                //    }
+
+                //});
+
+                //BaseDBConfig.MutiConnectionString.ForEach(m =>
+                //{
+                //    listConfig.Add(new ConnectionConfig()
+                //    {
+                //        ConfigId = m.ConnId.ObjToString().ToLower(),
+                //        ConnectionString = m.Connection,
+                //        DbType = (DbType)m.DbType,
+                //        IsAutoCloseConnection = true,
+                //        MoreSettings = new ConnMoreSettings()
+                //        {
+                //            //IsWithNoLockQuery = true,
+                //            IsAutoRemoveDataCache = true
+                //        },
+                //        // 浠庡簱
+                //        //SlaveConnectionConfigs = listConfig_Slave,
+                //        // 鑷畾涔夌壒鎬�
+                //        ConfigureExternalServices = new ConfigureExternalServices()
+                //        {
+                //            DataInfoCacheService = new SqlSugarMemoryCacheService(memoryCache),
+                //            EntityService = (property, column) =>
+                //            {
+                //                if (column.IsPrimarykey && property.PropertyType == typeof(int))
+                //                {
+                //                    column.IsIdentity = true;
+                //                }
+                //            }
+                //        },
+                //        InitKeyType = InitKeyType.Attribute,
+                //        AopEvents = new AopEvents()
+                //        {
+                //            OnError = x =>
+                //            {
+                //                Console.WriteLine(x.Sql);
+                //            }
+                //        }
+                //    }
+                //   );
+                //});
+                #endregion
+
+                SqlSugarClient sqlSugarClient = new SqlSugarClient(listConfig, db =>
+                {
+                    db.Aop.DataExecuting = SqlSugarAop.DataExecuting;
+                });
+                return sqlSugarClient;
+            });
+        }
+
+        private static string GetWholeSql(SugarParameter[] paramArr, string sql)
+        {
+            foreach (var param in paramArr)
+            {
+                sql.Replace(param.ParameterName, param.Value.ObjToString());
+            }
+
+            return sql;
+        }
+
+        private static string GetParas(SugarParameter[] pars)
+        {
+            string key = "銆怱QL鍙傛暟銆戯細";
+            foreach (var param in pars)
+            {
+                key += $"{param.ParameterName}:{param.Value}\n";
+            }
+
+            return key;
+        }
+    }
+}

--
Gitblit v1.9.3