| | |
| | | else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate)) |
| | | { |
| | | propertyInfo.SetValue(obj, DateTime.Now); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | propertyInfo.SetValue(obj, property.GetValue(entity)); |
| | |
| | | } |
| | | } |
| | | return DeleteData(entity); |
| | | |
| | | |
| | | } |
| | | |
| | | //public bool DeleteAndMoveIntoHty(TEntity entity, OperateTypeEnum operateType) |
| | | //{ |
| | | // // æ ¸å¿é»è¾ï¼ç¨äºå¡ä¿è¯ååæ§ï¼å¼å¸¸æè·é¿å
æµç¨ä¸æï¼æ¥å¿è¾
å©ææ¥ |
| | | // bool isSuccess = false; |
| | | // string entityTypeName = entity?.GetType().Name ?? "æªç¥å®ä½"; |
| | | |
| | | // try |
| | | // { |
| | | // // åç½®æ ¡éªï¼å®ä½ä¸è½ä¸ºç©º |
| | | // if (entity == null) |
| | | // { |
| | | // return false; |
| | | // } |
| | | |
| | | // Type entityType = entity.GetType(); |
| | | // Assembly assembly = entityType.Assembly; |
| | | // string htyTypeName = $"{entityType.FullName}_Hty"; |
| | | // Type? htyType = assembly.GetType(htyTypeName); |
| | | |
| | | // // 1. æ£æ¥åå²è¡¨ç±»åæ¯å¦åå¨ |
| | | // if (htyType == null) |
| | | // { |
| | | |
| | | // return false; |
| | | // } |
| | | |
| | | // // 2. å建åå²è¡¨å®ä¾ï¼å¤çæ åæé 彿°ä¸åå¨çæ
åµï¼ |
| | | // object? htyObj; |
| | | // try |
| | | // { |
| | | // htyObj = Activator.CreateInstance(htyType); |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // // _logger.LogError(ex, "DeleteAndMoveIntoHtyï¼å建åå²è¡¨å®ä¾ {HtyTypeName} 失败", htyTypeName); |
| | | // return false; |
| | | // } |
| | | // if (htyObj == null) |
| | | // { |
| | | // // _logger.LogWarning("DeleteAndMoveIntoHtyï¼åå²è¡¨å®ä¾ {HtyTypeName} åå»ºç»æä¸ºnull", htyTypeName); |
| | | // return false; |
| | | // } |
| | | |
| | | // // 3. è·åæ ¸å¿å±æ§ï¼æå®BindingFlagsç¡®ä¿è·åå
Œ
±å®ä¾å±æ§ï¼ |
| | | // BindingFlags propFlags = BindingFlags.Public | BindingFlags.Instance; |
| | | // PropertyInfo? keyPro = typeof(TEntity).GetKeyProperty(); // èªå®ä¹æ¹æ³éç¡®ä¿è¿åéç©ºï¼æ¤å¤å¢å å¤ç©º |
| | | // PropertyInfo? operateTypePro = htyType.GetProperty(nameof(IBaseHistoryEntity.OperateType), propFlags); |
| | | // PropertyInfo? sourceIdPro = htyType.GetProperty(nameof(IBaseHistoryEntity.SourceId), propFlags); |
| | | |
| | | // // æ ¡éªæ ¸å¿å±æ§æ¯å¦åå¨ |
| | | // if (keyPro == null) |
| | | // { |
| | | // //_logger.LogError("DeleteAndMoveIntoHtyï¼å®ä½ {EntityType} æªæ¾å°ä¸»é®å±æ§", entityType.FullName); |
| | | // return false; |
| | | // } |
| | | // if (operateTypePro == null) |
| | | // { |
| | | // //_logger.LogError("DeleteAndMoveIntoHtyï¼åå²è¡¨ {HtyTypeName} æªæ¾å°OperateType屿§", htyTypeName); |
| | | // return false; |
| | | // } |
| | | // if (sourceIdPro == null) |
| | | // { |
| | | // // _logger.LogError("DeleteAndMoveIntoHtyï¼åå²è¡¨ {HtyTypeName} æªæ¾å°SourceId屿§", htyTypeName); |
| | | // return false; |
| | | // } |
| | | |
| | | // // 4. èµå¼æ ¸å¿å±æ§ï¼æ ¡éªç±»åå¹é
ï¼é¿å
SetValueæå¼å¸¸ï¼ |
| | | // try |
| | | // { |
| | | // // å¤çOperateTypeç±»åå¹é
ï¼è¥åå²è¡¨å±æ§æ¯æä¸¾ç±»åï¼ç´æ¥ä¼ æä¸¾èéå符串 |
| | | // if (operateTypePro.PropertyType == typeof(OperateTypeEnum)) |
| | | // { |
| | | // operateTypePro.SetValue(htyObj, operateType); |
| | | // } |
| | | // else if (operateTypePro.PropertyType == typeof(string)) |
| | | // { |
| | | // operateTypePro.SetValue(htyObj, operateType.ToString()); |
| | | // } |
| | | // else |
| | | // { |
| | | // //_logger.LogError("DeleteAndMoveIntoHtyï¼åå²è¡¨ {HtyTypeName} çOperateType屿§ç±»å {PropType} ä¸å¹é
ï¼ä»
æ¯ææä¸¾/å符串ï¼", htyTypeName, operateTypePro.PropertyType.FullName); |
| | | // return false; |
| | | // } |
| | | |
| | | // // èµå¼SourceIdï¼æ ¡éªç±»åå¹é
ï¼ |
| | | // object sourceIdValue = keyPro.GetValue(entity)!; |
| | | // if (sourceIdPro.PropertyType != sourceIdValue.GetType()) |
| | | // { |
| | | // sourceIdValue = Convert.ChangeType(sourceIdValue, sourceIdPro.PropertyType); // ç±»åè½¬æ¢ |
| | | // } |
| | | // sourceIdPro.SetValue(htyObj, sourceIdValue); |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // // _logger.LogError(ex, "DeleteAndMoveIntoHtyï¼åå²è¡¨ {HtyTypeName} æ ¸å¿å±æ§èµå¼å¤±è´¥", htyTypeName); |
| | | // return false; |
| | | // } |
| | | |
| | | // // 5. èµå¼å
¶ä»å±æ§ï¼æé¤æ ¸å¿å±æ§ï¼ |
| | | // List<PropertyInfo> htyProperties = htyType.GetProperties(propFlags) |
| | | // .Where(x => x.Name != operateTypePro.Name |
| | | // && x.Name != sourceIdPro.Name |
| | | // && x.Name != keyPro.Name) |
| | | // .ToList(); |
| | | |
| | | // foreach (PropertyInfo htyProp in htyProperties) |
| | | // { |
| | | // PropertyInfo? entityProp = entityType.GetProperty(htyProp.Name, propFlags); |
| | | // if (entityProp == null) continue; // å®ä½æ è¯¥å±æ§åè·³è¿ |
| | | |
| | | // try |
| | | // { |
| | | // object propValue; |
| | | // // å¤çä¿®æ¹äººï¼é¿å
App.User空å¼ç¨ |
| | | // if (htyProp.Name == nameof(BaseEntity.Modifier)) |
| | | // { |
| | | // propValue = App.User?.UserId > 0 ? App.User?.UserName : "System"; |
| | | // } |
| | | // // å¤çä¿®æ¹æ¶é´ |
| | | // else if (htyProp.Name == nameof(BaseEntity.ModifyDate)) |
| | | // { |
| | | // propValue = DateTime.Now; |
| | | // } |
| | | // // å
¶ä»å±æ§ä»åå®ä½åå¼ |
| | | // else |
| | | // { |
| | | // propValue = entityProp.GetValue(entity) ?? DBNull.Value; // å¤çnullå¼ |
| | | // } |
| | | |
| | | // // ç±»å转æ¢åèµå¼ï¼é¿å
ç±»åä¸å¹é
ï¼ |
| | | // if (propValue != DBNull.Value && propValue != null) |
| | | // { |
| | | // propValue = Convert.ChangeType(propValue, htyProp.PropertyType); |
| | | // } |
| | | // htyProp.SetValue(htyObj, propValue); |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // // _logger.LogWarning(ex, "DeleteAndMoveIntoHtyï¼åå²è¡¨ {HtyTypeName} 屿§ {PropName} èµå¼å¤±è´¥ï¼è·³è¿è¯¥å±æ§", htyTypeName, htyProp.Name); |
| | | // } |
| | | // } |
| | | // try |
| | | // { |
| | | // // æ§è¡æå
¥åå²è¡¨ |
| | | // int insertRows = _db.InsertableByObject(htyObj).AS(entityType.Name + "_Hty").ExecuteCommand(); |
| | | |
| | | // if (insertRows <= 0) |
| | | // { |
| | | // // _logger.LogError("DeleteAndMoveIntoHtyï¼åå²è¡¨ {HtyTypeName} æå
¥å¤±è´¥ï¼å½±åè¡æ°0ï¼", htyTypeName); |
| | | // _db.InsertableByObject(htyObj).AS(entityType.Name + "_Hty").ExecuteCommand(); |
| | | // return false; |
| | | // } |
| | | // // æå
¥æååæ§è¡å é¤ |
| | | // bool deleteSuccess = DeleteData(entity); |
| | | // if (!deleteSuccess) |
| | | // { |
| | | // //_logger.LogError("DeleteAndMoveIntoHtyï¼å®ä½ {EntityType} å é¤å¤±è´¥", entityType.FullName); |
| | | // DeleteData(entity); |
| | | // return false; |
| | | // } |
| | | // // æäº¤äºå¡ |
| | | |
| | | // isSuccess = true; |
| | | // //_logger.LogInformation("DeleteAndMoveIntoHtyï¼å®ä½ {EntityType} å·²æåç§»å
¥åå²è¡¨å¹¶å é¤åæ°æ®", entityType.FullName); |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | |
| | | // // _logger.LogError(ex, "DeleteAndMoveIntoHtyï¼äºå¡æ§è¡å¤±è´¥ï¼æå
¥åå²è¡¨/å é¤åæ°æ®ï¼", entityType.FullName); |
| | | // return false; |
| | | // } |
| | | // } |
| | | // catch (Exception ex) |
| | | // { |
| | | // // _logger.LogError(ex, "DeleteAndMoveIntoHtyï¼å¤çå®ä½ {EntityTypeName} æ¶åçæªæè·å¼å¸¸", entityTypeName); |
| | | // return false; |
| | | // } |
| | | |
| | | // return isSuccess; |
| | | //} |
| | | |
| | | |
| | | |
| | | 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) |
| | | { |
| | | Type type = typeof(TEntity); |
| | | Assembly assembly = type.Assembly; |
| | | Type? htyType = assembly.GetType(type.FullName + "_Hty"); |
| | | if (htyType != 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? 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) |
| | | object? obj = Activator.CreateInstance(htyType); |
| | | if (obj != 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) |
| | | operateTypePro.SetValue(obj, operateType.ToString()); |
| | | sourceIdPro.SetValue(obj, keyPro.GetValue(item)); |
| | | for (int i = 0; i < propertyInfos.Count; i++) |
| | | { |
| | | 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); |
| | | 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)); |
| | | } |
| | | } |
| | | if (property != null) |
| | | { |
| | | if (propertyInfo.Name == nameof(BaseEntity.Modifier)) |
| | | { |
| | | propertyInfo.SetValue(obj, App.User.UserId > 0 ? App.User.UserName : "System"); |
| | | } |
| | | list.Add(obj); |
| | | else if (propertyInfo.Name == nameof(BaseEntity.ModifyDate)) |
| | | { |
| | | propertyInfo.SetValue(obj, DateTime.Now); |
| | | } |
| | | else |
| | | { |
| | | propertyInfo.SetValue(obj, property.GetValue(item)); |
| | | } |
| | | } |
| | | } |
| | | if (list.Count > 0) |
| | | _db.InsertableByObject(list).AS(type.Name + "_Hty").ExecuteCommand(); |
| | | |
| | | list.Add(obj); |
| | | } |
| | | } |
| | | return DeleteData(entities); |
| | | if (list.Count > 0) |
| | | _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, |