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/BaseRepository/RepositoryBase.cs |  240 ++++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 170 insertions(+), 70 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/RepositoryBase.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/RepositoryBase.cs"
index 539c96b..f49b6a9 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/RepositoryBase.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/RepositoryBase.cs"
@@ -15,6 +15,12 @@
 using WIDESEA_Core.DB;
 using WIDESEA_Core.Const;
 using WIDESEA_Core.AOP;
+using OfficeOpenXml.FormulaParsing.ExpressionGraph;
+using WIDESEA_Core.Enums;
+using WIDESEA_Core.Utilities;
+using Microsoft.AspNetCore.Mvc.RazorPages;
+using NetTaste;
+using WIDESEA_Core.DB.Models;
 
 namespace WIDESEA_Core.BaseRepository
 {
@@ -30,42 +36,45 @@
                 ISqlSugarClient db = _dbBase;
 
                 //澶氱鎴�
-                var mta = typeof(TEntity).GetCustomAttribute<MultiTenantAttribute>();
-                if (mta is { TenantType: TenantTypeEnum.Db })
-                {
-                    //鑾峰彇绉熸埛淇℃伅 绉熸埛淇℃伅鍙互鎻愬墠缂撳瓨涓嬫潵 
-                    if (App.User is { TenantId: > 0 })
-                    {
-                        dynamic tenant = db.Queryable(MainDb.TenantTableName, "x").Where(MainDb.TenantId, "=", App.User.TenantId).First();
-                        if (tenant != null)
-                        {
-                            var iTenant = db.AsTenant();
-                            if (!iTenant.IsAnyConnection(tenant.TenantId))
-                            {
-                                string conStr = tenant.ConnectionString;
-                                ConnectionConfig connectionConfig = new ConnectionConfig()
-                                {
-                                    ConfigId = tenant.TenantId,
-                                    ConnectionString = conStr.DecryptDES(AppSecret.DB),
-                                    DbType = (SqlSugar.DbType)tenant.DbType,
-                                    IsAutoCloseConnection = true,
-                                    AopEvents = new AopEvents()
-                                    {
-                                        DataExecuting = SqlSugarAop.DataExecuting
-                                    }
-                                };
-                                iTenant.AddConnection(connectionConfig);
-                            }
+                //var mta = typeof(TEntity).GetCustomAttribute<MultiTenantAttribute>();
+                //if (mta is { TenantType: TenantTypeEnum.Db })
+                //{
+                //    //鑾峰彇绉熸埛淇℃伅 绉熸埛淇℃伅鍙互鎻愬墠缂撳瓨涓嬫潵 
+                //    if (App.User is { TenantId: > 0 })
+                //    {
+                //        dynamic tenant = db.Queryable(MainDb.TenantTableName, "x").Where(MainDb.TenantId, "=", App.User.TenantId).First();
+                //        if (tenant != null)
+                //        {
+                //            var iTenant = db.AsTenant();
+                //            if (!iTenant.IsAnyConnection(tenant.TenantId))
+                //            {
+                //                string conStr = tenant.ConnectionString;
+                //                ConnectionConfig connectionConfig = new ConnectionConfig()
+                //                {
+                //                    ConfigId = tenant.TenantId,
+                //                    ConnectionString = conStr.DecryptDES(AppSecret.DB),
+                //                    DbType = (SqlSugar.DbType)tenant.DbType,
+                //                    IsAutoCloseConnection = true,
+                //                    AopEvents = new AopEvents()
+                //                    {
+                //                        DataExecuting = SqlSugarAop.DataExecuting,
 
-                            return iTenant.GetConnection(tenant.TenantId);
-                        }
-                    }
-                }
+                //                    }
+                //                };
+                //                iTenant.AddConnection(connectionConfig);
+                //            }
+
+                //            return iTenant.GetConnection(tenant.TenantId);
+                //        }
+                //    }
+                //}
 
                 return db;
             }
         }
-
+        /// <summary>
+        /// 鍒涘缓鏁版嵁搴撹繛鎺ュ璞�
+        /// </summary>
         public ISqlSugarClient Db => _db;
 
         public RepositoryBase(IUnitOfWorkManage unitOfWorkManage)
@@ -123,7 +132,7 @@
         public virtual int AddData(List<TEntity> listEntity)
         {
             IInsertable<TEntity> insert = _db.Insertable(listEntity);
-            return insert.ExecuteReturnIdentity();
+            return insert.ExecuteCommand();
         }
 
         /// <summary>
@@ -193,7 +202,7 @@
         /// <param name="lstColumns"></param>
         /// <param name="lstIgnoreColumns"></param>
         /// <returns></returns>
-        public virtual bool UpdateData(TEntity entity, List<string> lstColumns, List<string> lstIgnoreColumns = null)
+        public virtual bool UpdateData(TEntity entity, List<string> lstColumns, List<string>? lstIgnoreColumns = null)
         {
             IUpdateable<TEntity> update = _db.Updateable(entity);
 
@@ -255,9 +264,19 @@
         /// <param name="whereExpression"></param>
         /// <param name="orderByFields"></param>
         /// <returns></returns>
-        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, string orderByFields)
+        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy)
         {
-            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).ToList();
+            List<OrderByModel> orderByModels = new List<OrderByModel>();
+            foreach (var item in orderBy)
+            {
+                OrderByModel orderByModel = new OrderByModel()
+                {
+                    FieldName = item.Key,
+                    OrderByType = item.Value
+                };
+                orderByModels.Add(orderByModel);
+            }
+            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).ToList();
         }
 
         /// <summary>
@@ -300,7 +319,7 @@
         /// <param name="whereExpression"></param>
         /// <param name="orderByFields"></param>
         /// <returns></returns>
-        public virtual List<TResult> QueryData<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields)
+        public virtual List<TResult> QueryData<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string orderByFields = "")
         {
             return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(orderByFields), orderByFields).WhereIF(whereExpression != null, whereExpression).Select(expression).ToList();
         }
@@ -334,7 +353,7 @@
         /// <param name="sql"></param>
         /// <param name="parameters"></param>
         /// <returns></returns>
-        public virtual List<TEntity> QueryDataBySql(string sql, SugarParameter[] parameters = null)
+        public virtual List<TEntity> QueryDataBySql(string sql, SugarParameter[]? parameters = null)
         {
             return _db.Ado.SqlQuery<TEntity>(sql, parameters);
         }
@@ -345,7 +364,7 @@
         /// <param name="sql"></param>
         /// <param name="parameters"></param>
         /// <returns></returns>
-        public virtual List<dynamic> QueryDynamicDataBySql(string sql, SugarParameter[] parameters = null)
+        public virtual List<dynamic> QueryDynamicDataBySql(string sql, SugarParameter[]? parameters = null)
         {
             return _db.Ado.SqlQuery<dynamic>(sql, parameters);
         }
@@ -356,7 +375,7 @@
         /// <param name="sql"></param>
         /// <param name="parameters"></param>
         /// <returns></returns>
-        public virtual List<object> QueryObjectDataBySql(string sql, SugarParameter[] parameters = null)
+        public virtual List<object> QueryObjectDataBySql(string sql, SugarParameter[]? parameters = null)
         {
             return _db.Ado.SqlQuery<object>(sql, parameters);
         }
@@ -378,7 +397,7 @@
         /// <param name="sql"></param>
         /// <param name="parameters"></param>
         /// <returns></returns>
-        public virtual DataTable QueryTable(string sql, SugarParameter[] parameters = null)
+        public virtual DataTable QueryTable(string sql, SugarParameter[]? parameters = null)
         {
             return _db.Ado.GetDataTable(sql, parameters);
         }
@@ -443,7 +462,7 @@
         /// <param name="pageSize"></param>
         /// <param name="orderByFields"></param>
         /// <returns></returns>
-        public virtual PageGridData<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields = null)
+        public virtual PageGridData<TEntity> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int pageIndex, int pageSize, string? orderByFields = null)
         {
             int totalCount = 0;
             var list = _db.Queryable<TEntity>()
@@ -498,7 +517,7 @@
             {
                 OrderByModel orderByModel = new OrderByModel()
                 {
-                    FieldName = item.Key,
+                    FieldName = item.Key.FirstLetterToUpper(),
                     OrderByType = item.Value
                 };
                 orderByModels.Add(orderByModel);
@@ -521,13 +540,25 @@
         /// <param name="whereExpression"></param>
         /// <returns></returns>
         public virtual List<TResult> QueryTabs<T, T2, TResult>(
-          Expression<Func<T, T2, object[]>> joinExpression,
-          Expression<Func<T, T2, TResult>> selectExpression,
-          Expression<Func<T, T2, bool>> whereExpressionT1,
-          Expression<Func<TResult, bool>> whereExpression)
+           Expression<Func<T, T2, object[]>> joinExpression,
+           Expression<Func<T, T2, TResult>> selectExpression,
+           Expression<Func<T, T2, bool>> whereExpressionT1,
+           Expression<Func<TResult, bool>> whereExpression)
         {
             List<TResult> list = _db.Queryable(joinExpression).WhereIF(whereExpressionT1 != null, whereExpressionT1)
                .Select(selectExpression)
+               .WhereIF(whereExpression != null, whereExpression).ToList();
+            return list;
+        }
+
+        public virtual List<TResult> QueryTabs<T, T2, TResult>(
+          Expression<Func<T, T2, bool>> joinExpression,
+          Expression<Func<T, T2, TResult>> selectExpression,
+          Expression<Func<T, bool>> whereExpressionT1,
+          Expression<Func<T2, bool>> whereExpressionT2,
+          Expression<Func<TResult, bool>> whereExpression)
+        {
+            List<TResult> list = _db.Queryable<T>().WhereIF(whereExpressionT1 != null, whereExpressionT1).InnerJoin(_db.Queryable<T2>().WhereIF(whereExpressionT2 != null, whereExpressionT2), joinExpression).Select(selectExpression)
                .WhereIF(whereExpression != null, whereExpression).ToList();
             return list;
         }
@@ -544,7 +575,7 @@
         /// <param name="pageIndex"></param>
         /// <param name="pageSize"></param>
         /// <param name="orderByFields"></param>
-        public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, int pageIndex, int pageSize, string orderByFields = null)
+        public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, int pageIndex, int pageSize, string? orderByFields = null)
         {
             int totalCount = 0;
             List<TResult> list = _db.Queryable(joinExpression)
@@ -569,7 +600,7 @@
         /// <param name="pageSize"></param>
         /// <param name="orderByFields"></param>
         /// <returns></returns>
-        public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, Expression<Func<T1, object>> groupExpression, int pageIndex, int pageSize, string orderByFields = null)
+        public virtual PageGridData<TResult> QueryTabsPage<T1, T2, TResult>(Expression<Func<T1, T2, object[]>> joinExpression, Expression<Func<T1, T2, TResult>> selectExpression, Expression<Func<TResult, bool>> whereExpression, Expression<Func<T1, object>> groupExpression, int pageIndex, int pageSize, string? orderByFields = null)
         {
             int totalCount = 0;
             List<TResult> list = _db.Queryable(joinExpression).GroupBy(groupExpression)
@@ -579,13 +610,6 @@
                 .ToPageList(pageIndex, pageSize, ref totalCount);
             return new PageGridData<TResult>(totalCount, list);
         }
-
-        //List<TResult> QueryMuch<T, T2, T3, TResult>(
-        //    Expression<Func<T, T2, T3, object[]>> joinExpression,
-        //    Expression<Func<T, T2, T3, TResult>> selectExpression,
-        //    Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new(){throw new NotImplementedException();}
-        //Task<PageModel<TEntity>> QueryPage(PaginationModel pagination){throw new NotImplementedException();}
-
 
         public Task<TEntity> QureyDataByIdAsync(object id)
         {
@@ -840,27 +864,103 @@
              .WhereIF(whereExpression != null, whereExpression).ToListAsync();
         }
 
-        /// <summary>
-        /// 鏉′欢鏌ヨ鏁版嵁骞舵帓搴�
-        /// </summary>
-        /// <param name="whereExpression"></param>
-        /// <param name="orderByFields"></param>
-        /// <returns></returns>
-        public virtual List<TEntity> QueryData(Expression<Func<TEntity, bool>> whereExpression, Dictionary<string, OrderByType> orderBy)
+        public bool DeleteAndMoveIntoHty(TEntity entity, OperateType operateType)
         {
-            List<OrderByModel> orderByModels = new List<OrderByModel>();
-            foreach (var item in orderBy)
+            Type type = entity.GetType();
+            Assembly assembly = type.Assembly;
+            Type? htyType = assembly.GetType(type.FullName + "_Hty");
+            if (htyType != null)
             {
-                OrderByModel orderByModel = new OrderByModel()
+                object? obj = Activator.CreateInstance(htyType);
+                PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
+                PropertyInfo? operateTypePro = htyType.GetProperty(nameof(OperateType));
+                PropertyInfo? sourceIdPro = htyType.GetProperty("SourceId");
+                if (obj != null && keyPro != null && operateTypePro != null && sourceIdPro != null)
                 {
-                    FieldName = item.Key,
-                    OrderByType = item.Value
-                };
-                orderByModels.Add(orderByModel);
+                    operateTypePro.SetValue(obj, operateType.ToString());
+                    sourceIdPro.SetValue(obj, keyPro.GetValue(entity));
+
+                    List<PropertyInfo> propertyInfos = htyType.GetProperties().Where(x => x.Name != operateTypePro.Name && x.Name != sourceIdPro.Name && x.Name != keyPro.Name).ToList();
+
+                    for (int i = 0; i < propertyInfos.Count; i++)
+                    {
+                        PropertyInfo propertyInfo = propertyInfos[i];
+                        PropertyInfo? property = type.GetProperty(propertyInfo.Name);
+
+                        if (property != null)
+                        {
+                            if (propertyInfo.Name == nameof(BaseEntity.Modifier))
+                            {
+                                propertyInfo.SetValue(obj, App.User.UserId > 0 ? App.User.UserName : App.User.UserId.ToString());
+                            }
+                            else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate))
+                            {
+                                propertyInfo.SetValue(obj, DateTime.Now);
+                            }
+                            else
+                            {
+                                propertyInfo.SetValue(obj, property.GetValue(entity));
+                            }
+                        }
+                    }
+                    _db.InsertableByObject(obj).AS(type.Name + "_Hty").ExecuteCommand();
+                }
             }
-            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderBy(orderByModels).ToList();
+            return DeleteData(entity);
         }
 
+        public bool DeleteAndMoveIntoHty(List<TEntity> entities, OperateType operateType)
+        {
+            Type type = typeof(TEntity);
+            Assembly assembly = type.Assembly;
+            Type? htyType = assembly.GetType(type.FullName + "_Hty");
+            if (htyType != null)
+            {
+                object? obj2 = Activator.CreateInstance(htyType);
+                PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
+                PropertyInfo? operateTypePro = htyType.GetProperty(nameof(OperateType));
+                PropertyInfo? sourceIdPro = htyType.GetProperty("SourceId");
+                if (obj2 != null && keyPro != null && operateTypePro != null && sourceIdPro != null)
+                {
+                    List<PropertyInfo> propertyInfos = htyType.GetProperties().Where(x => x.Name != operateTypePro.Name && x.Name != sourceIdPro.Name && x.Name != keyPro.Name).ToList();
+                    List<object> list = new List<object>();
+                    foreach (var item in entities)
+                    {
+                        object? obj = Activator.CreateInstance(htyType);
+                        if (obj != null)
+                        {
+                            operateTypePro.SetValue(obj, operateType.ToString());
+                            sourceIdPro.SetValue(obj, keyPro.GetValue(item));
+                            for (int i = 0; i < propertyInfos.Count; i++)
+                            {
+                                PropertyInfo propertyInfo = propertyInfos[i];
+                                PropertyInfo? property = type.GetProperty(propertyInfo.Name);
+
+                                if (property != null)
+                                {
+                                    if (propertyInfo.Name == nameof(BaseEntity.Modifier))
+                                    {
+                                        propertyInfo.SetValue(obj, App.User.UserId > 0 ? App.User.UserName : App.User.UserId.ToString());
+                                    }
+                                    else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate))
+                                    {
+                                        propertyInfo.SetValue(obj, DateTime.Now);
+                                    }
+                                    else
+                                    {
+                                        propertyInfo.SetValue(obj, property.GetValue(item));
+                                    }
+                                }
+                            }
+                            list.Add(obj);
+                        }
+                    }
+                    _db.InsertableByObject(list).AS(type.Name + "_Hty").ExecuteCommand();
+
+                }
+            }
+            return DeleteData(entities);
+        }
         //List<TResult> QueryMuch<T, T2, T3, TResult>(
         //    Expression<Func<T, T2, T3, object[]>> joinExpression,
         //    Expression<Func<T, T2, T3, TResult>> selectExpression,

--
Gitblit v1.9.3