1
hutongqing
2025-01-02 8c6fd742db249ad4cc819cf041eb98d880a3ef73
WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/RepositoryBase.cs
@@ -16,6 +16,9 @@
using WIDESEAWCS_Core.Const;
using WIDESEAWCS_Core.AOP;
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using WIDESEAWCS_Core.DB.Models;
using WIDESEAWCS_Core.Utilities;
using WIDESEAWCS_Core.Enums;
namespace WIDESEAWCS_Core.BaseRepository
{
@@ -66,7 +69,9 @@
                return db;
            }
        }
        /// <summary>
        /// 创建数据库连接对象
        /// </summary>
        public ISqlSugarClient Db => _db;
        public RepositoryBase(IUnitOfWorkManage unitOfWorkManage)
@@ -515,8 +520,8 @@
                orderByModels.Add(orderByModel);
            }
            int totalCount = 0;
            List<TEntity> list = _db.Queryable<TEntity>()
                .WhereIF(!string.IsNullOrEmpty(where), where).OrderBy(orderByModels).ToPageList(pageIndex, pageSize, ref totalCount);
            List<TEntity> list = _db.Queryable<TEntity>().OrderBy(orderByModels)
                .WhereIF(!string.IsNullOrEmpty(where), where).ToPageList(pageIndex, pageSize, ref totalCount);
            return new PageGridData<TEntity>(totalCount, list);
        }
@@ -844,10 +849,102 @@
             .WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }
        //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 bool DeleteAndMoveIntoHty(TEntity entity, OperateTypeEnum operateType)
        {
            Type type = entity.GetType();
            Assembly assembly = type.Assembly;
            Type? htyType = assembly.GetType(type.FullName + "_Hty");
            if (htyType != null)
            {
                object? obj = Activator.CreateInstance(htyType);
                PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
                PropertyInfo? operateTypePro = htyType.GetProperty(nameof(IBaseHistoryEntity.OperateType));
                PropertyInfo? sourceIdPro = htyType.GetProperty(nameof(IBaseHistoryEntity.SourceId));
                if (obj != null && keyPro != null && operateTypePro != null && sourceIdPro != null)
                {
                    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 : "System");
                            }
                            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 DeleteData(entity);
        }
        public bool DeleteAndMoveIntoHty(List<TEntity> entities, OperateTypeEnum 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(IBaseHistoryEntity.OperateType));
                PropertyInfo? sourceIdPro = htyType.GetProperty(nameof(IBaseHistoryEntity.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 : "System");
                                    }
                                    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);
        }
    }
}