diff --git a/IRaCIS.Core.API/appsettings.Production.json b/IRaCIS.Core.API/appsettings.Production.json index 5aa029aa1..5dade4242 100644 --- a/IRaCIS.Core.API/appsettings.Production.json +++ b/IRaCIS.Core.API/appsettings.Production.json @@ -6,20 +6,32 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - //"RootUrl": "http://123.56.181.144:8060/", "ConnectionStrings": { - "RemoteNew": "Server=101.132.193.237,1433;Database=IRaCIS.Production;User ID=sa;Password=zhanying2021;TrustServerCertificate=true", - "Hangfire": "Server=101.132.193.237,1433;Database=Hangfire.IRaCIS.Production;User ID=sa;Password=zhanying2021;TrustServerCertificate=true" + //"RemoteNew": "Server=101.132.193.237,1433;Database=Prod_IRC;User ID=sa;Password=zhanying2021;TrustServerCertificate=true", + //"Hangfire": "Server=101.132.193.237,1433;Database=Prod_IRC_Hangfire;User ID=sa;Password=zhanying2021;TrustServerCertificate=true" + "RemoteNew": "Server=47.117.164.182,1432;Database=Prod_IRC;User ID=sa;Password=zhanying2021;TrustServerCertificate=true", + "Hangfire": "Server=47.117.164.182,1432;Database=Prod_IRC_Hangfire;User ID=sa;Password=zhanying2021;TrustServerCertificate=true" }, - - "AliyunOSS": { - "regionId": "cn-shanghai", - "region": "oss-cn-shanghai", - "endpoint": "https://oss-cn-shanghai.aliyuncs.com", - "accessKeyId": "LTAI5tKvzs7ed3UfSpNk3xwQ", - "accessKeySecret": "zTIceGEShlZDGnLrCFfIGFE7TXVRio", - "bucketName": "zy-irc-store", - "viewEndpoint": "https://zy-irc-cache.oss-cn-shanghai.aliyuncs.com" + "ObjectStoreService": { + "ObjectStoreUse": "AliyunOSS", + "AliyunOSS": { + "regionId": "cn-shanghai", + "endpoint": "https://oss-cn-shanghai.aliyuncs.com", + "accessKeyId": "LTAI5tKvzs7ed3UfSpNk3xwQ", + "accessKeySecret": "zTIceGEShlZDGnLrCFfIGFE7TXVRio", + "bucketName": "zy-irc-store", + "roleArn": "acs:ram::1899121822495495:role/oss-upload", + "viewEndpoint": "https://zy-irc-cache.oss-cn-shanghai.aliyuncs.com", + "region": "oss-cn-shanghai" + }, + "MinIO": { + "endpoint": "http://192.168.3.68", + "port": "8001", + "useSSL": false, + "accessKey": "IDFkwEpWej0b4DtiuThL", + "secretKey": "Lhuu83yMhVwu7c1SnjvGY6lq74jzpYqifK6Qtj4h", + "bucketName": "test" + } }, "BasicSystemConfig": { diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index be1de8758..d83be7e79 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -146,9 +146,7 @@ namespace IRaCIS.Application.Services { - await _dicRepository._dbContext.Subject.Where(t => t.Id == Guid.Empty).ExecuteUpdateAsync(t => t - .SetProperty(c => EF.Property(c, "UpdateTime"), u => DateTime.Now) - .SetProperty(c => c.FirstName, u => "NewUserName")); + var aa= _dicRepository._dbContext.Subject.Where(t => t.Id == Guid.Empty).ExecuteUpdate("FirstName","ddd"); await _repository.BatchUpdateAsync(t => t.Id == Guid.Empty, u => new Subject() { FirstName = "fddd", LastName = "sss" }); diff --git a/IRaCIS.Core.Infra.EFCore/Repository/DynamicRelationalExtensions.cs b/IRaCIS.Core.Infra.EFCore/Repository/DynamicRelationalExtensions.cs new file mode 100644 index 000000000..4eadf8408 --- /dev/null +++ b/IRaCIS.Core.Infra.EFCore/Repository/DynamicRelationalExtensions.cs @@ -0,0 +1,80 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace IRaCIS.Core.Infra.EFCore +{ + + public static class DynamicRelationalExtensions + { + static MethodInfo UpdateMethodInfo = + typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdate)); + + static MethodInfo UpdateAsyncMethodInfo = + typeof(RelationalQueryableExtensions).GetMethod(nameof(RelationalQueryableExtensions.ExecuteUpdateAsync)); + + public static int ExecuteUpdate(this IQueryable query, string fieldName, object? fieldValue) + { + var updateBody = BuildUpdateBody(query.ElementType, + new Dictionary { { fieldName, fieldValue } }); + + return (int)UpdateMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody }); + } + + + public static int ExecuteUpdate(this IQueryable query, IReadOnlyDictionary fieldValues) + { + var updateBody = BuildUpdateBody(query.ElementType, fieldValues); + + return (int)UpdateMethodInfo.MakeGenericMethod(query.ElementType).Invoke(null, new object?[] { query, updateBody }); + } + + + static LambdaExpression BuildUpdateBody(Type entityType, IReadOnlyDictionary fieldValues) + { + var setParam = Expression.Parameter(typeof(SetPropertyCalls<>).MakeGenericType(entityType), "s"); + var objParam = Expression.Parameter(entityType, "e"); + + Expression setBody = setParam; + + foreach (var pair in fieldValues) + { + var propExpression = Expression.PropertyOrField(objParam, pair.Key); + var valueExpression = ValueForType(propExpression.Type, pair.Value); + + // s.SetProperty(e => e.SomeField, value) + setBody = Expression.Call(setBody, nameof(SetPropertyCalls.SetProperty), + new[] { propExpression.Type }, Expression.Lambda(propExpression, objParam), valueExpression); + + } + + // s => s.SetProperty(e => e.SomeField, value) + var updateBody = Expression.Lambda(setBody, setParam); + + return updateBody; + } + + static Expression ValueForType(Type desiredType, object? value) + { + if (value == null) + { + return Expression.Default(desiredType); + } + + if (value.GetType() != desiredType) + { + return Expression.Convert(Expression.Constant(value), desiredType); + } + + return Expression.Constant(value); + } + } + +}