From 6647d766320b333e539d6d910f55d2ef16cd8259 Mon Sep 17 00:00:00 2001 From: jianghaiqing Date: Thu, 7 Nov 2024 19:02:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=82=AE=E4=BB=B6=E8=B4=A6?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DS.Module.SqlSugar/ISaasDbService.cs | 9 + .../DS.Module.SqlSugar/SaasDbService.cs | 230 +++++++++++++++++- .../RecvFeeBillWorker.cs | 10 +- .../appsettings.json | 8 +- 4 files changed, 248 insertions(+), 9 deletions(-) diff --git a/ds-wms-service/DS.Module.SqlSugar/ISaasDbService.cs b/ds-wms-service/DS.Module.SqlSugar/ISaasDbService.cs index ceba096c..40ff0340 100644 --- a/ds-wms-service/DS.Module.SqlSugar/ISaasDbService.cs +++ b/ds-wms-service/DS.Module.SqlSugar/ISaasDbService.cs @@ -42,4 +42,13 @@ public interface ISaasDbService /// public SqlSugarScopeProvider GetBizJobDbScopeById(JobDbInitConfig conf); + + /// + /// 获取任务库 + /// + /// + /// + + public SqlSugarScopeProvider GetBizJobDbScopeExtById(JobDbInitConfig conf); + } \ No newline at end of file diff --git a/ds-wms-service/DS.Module.SqlSugar/SaasDbService.cs b/ds-wms-service/DS.Module.SqlSugar/SaasDbService.cs index 4f2fc79b..8ee3e274 100644 --- a/ds-wms-service/DS.Module.SqlSugar/SaasDbService.cs +++ b/ds-wms-service/DS.Module.SqlSugar/SaasDbService.cs @@ -310,7 +310,7 @@ public class SaasDbService : ISaasDbService if (entityInfo.PropertyName == "CreateBy") { - if (!user.UserId.IsNullOrEmpty()) + if (!conf.UserId.IsNullOrEmpty()) { entityInfo.SetValue(conf.UserId); } @@ -321,7 +321,7 @@ public class SaasDbService : ISaasDbService } if (entityInfo.PropertyName == "CreateUserName") { - if (!user.UserId.IsNullOrEmpty()) + if (!conf.UserName.IsNullOrEmpty()) { entityInfo.SetValue(conf.UserName); } @@ -419,4 +419,230 @@ public class SaasDbService : ISaasDbService { return db.GetConnectionScope("1288018625843826688"); } + + public SqlSugarScopeProvider GetBizJobDbScopeExtById(JobDbInitConfig conf) + { + if (!db.IsAnyConnection(conf.TenantId)) + { + var connInfo = GetMasterDb().Queryable().First(x => x.TenantId == conf.TenantId); + //用非默认ConfigId进行测试 + //添加业务库只在当前上下文有效(原理:SqlSugarScope模式入门文档去看) + ICacheService myCache = new SqlSugarCsRedisCache(_serviceProvider); + db.AddConnection(new ConnectionConfig() + { + ConfigId = conf.TenantId, + ConnectionString = connInfo.Connection, + DbType = connInfo.DbType, + IsAutoCloseConnection = true, + ConfigureExternalServices = new ConfigureExternalServices() + { + DataInfoCacheService = myCache //配置我们创建的缓存类,具体用法看标题5 + }, + MoreSettings = new ConnMoreSettings() + { + IsAutoRemoveDataCache = true + } + }); + var dbProvider = db.GetConnectionScope(conf.TenantId); + dbProvider.Ado.CommandTimeOut = 30; + dbProvider.Aop.OnLogExecuting = (sql, pars) => + { + //执行前事件 + //Logger.Log(LogLevel.Info, + // DateTime.Now.ToString() + "\r\n" + + // UtilMethods.GetSqlString(c.DbType, sql, pars)); + + string sqlStr = sql; + foreach (var item in pars) + { + if (item.Value != null && item.Value != DBNull.Value) + { + string? strValue = string.Empty; + if (item.DbType == System.Data.DbType.String) + { + strValue = "'" + (string)item.Value + "'"; + } + else if (item.DbType == System.Data.DbType.Boolean) + { + strValue = (bool)item.Value ? "1" : "0"; + } + else + { + strValue = item.Value.ToString(); + } + + sqlStr = sqlStr.Replace(item.ParameterName, strValue); + } + } + + Console.WriteLine("执行的SQL:" + Environment.NewLine + sqlStr); + }; + + //数据处理事件 + dbProvider.Aop.DataExecuting = (oldValue, entityInfo) => + { + // 新增操作 + if (entityInfo.OperationType == DataFilterType.InsertByObject) + { + if (entityInfo.PropertyName == "Id") + { + if (entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(string)) + { + entityInfo.SetValue(GuidHelper.GetSnowflakeId()); + } + + if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long)) + { + var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue); + if (id == null || (long)id == 0) + entityInfo.SetValue(SnowFlakeSingle.Instance.NextId()); + } + } + + if (entityInfo.PropertyName == "CreateTime") + entityInfo.SetValue(DateTime.Now); + + if (entityInfo.PropertyName == "TenantId") + { + var tenantId = ((dynamic)entityInfo.EntityValue).TenantId; + if (tenantId == null || tenantId == 0) + entityInfo.SetValue(conf.TenantId); + } + if (entityInfo.PropertyName == "TenantName") + { + var tenantId = ((dynamic)entityInfo.EntityValue).TenantId; + if (tenantId == null || tenantId == 0) + entityInfo.SetValue(conf.TenantName); + } + + if (entityInfo.PropertyName == "OrgId") + { + var orgId = ((dynamic)entityInfo.EntityValue).OrgId; + if (orgId == null || orgId == 0) + entityInfo.SetValue(conf.OrgId); + } + + if (entityInfo.PropertyName == "CreateBy") + { + var currVal = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue); + if (!currVal.IsNullOrEmpty()) + { + entityInfo.SetValue(currVal); + } + else + { + if (!conf.UserId.IsNullOrEmpty()) + { + entityInfo.SetValue(conf.UserId); + } + else + { + entityInfo.SetValue(1288018625843826688); + } + } + } + if (entityInfo.PropertyName == "CreateUserName") + { + var currVal = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue); + + if (!currVal.IsNullOrEmpty()) + { + entityInfo.SetValue(currVal); + } + else + { + if (!conf.UserName.IsNullOrEmpty()) + { + entityInfo.SetValue(conf.UserName); + } + else + { + entityInfo.SetValue("超级管理员"); + } + } + } + if (entityInfo.PropertyName == "Deleted") + entityInfo.SetValue(false); + } + // 更新操作 + if (entityInfo.OperationType == DataFilterType.UpdateByObject) + { + if (entityInfo.PropertyName == "UpdateTime") + entityInfo.SetValue(DateTime.Now); + if (entityInfo.PropertyName == "UpdateBy" && user != null) + entityInfo.SetValue(conf.UserId); + if (entityInfo.PropertyName == "UpdateUserName" && user != null) + entityInfo.SetValue(conf.UserName); + + } + }; + + dbProvider.Aop.OnDiffLogEvent = it => + { + //排除日志库操作 + if (Convert.ToInt64(conf.TenantId) != 1288018625843826680) + { + #region 插入日志审计表 + //操作前记录 包含: 字段描述 列名 值 表名 表描述 + var editBeforeData = it.BeforeData; //插入Before为null,之前还没进库 + //操作后记录 包含: 字段描述 列名 值 表名 表描述 + var editAfterData = it.AfterData; + var sql = it.Sql; + var parameter = it.Parameters; + var data = it.BusinessData; //这边会显示你传进来的对象 + var time = it.Time; + var diffType = it.DiffType; //enum insert 、update and delete + + var diffData = SqlSugarDiffUtil.GetDiff(editBeforeData, editAfterData); + + if (diffData.IsNotNull()) + { + var auditData = new SysLogAudit() + { + KeyId = Convert.ToInt64(diffData.Id), + Sql = it.Sql, + Param = JsonConvert.SerializeObject(it.Parameters), + OperateType = diffType.ToString(), + OldValue = JsonConvert.SerializeObject(editBeforeData), + NewValue = JsonConvert.SerializeObject(editAfterData), + DiffData = diffData.DiffData, + AopData = JsonConvert.SerializeObject(it.BusinessData), + }; + db.GetConnection(1288018625843826680).Insertable(auditData).ExecuteCommand(); + } + + #endregion + } + + }; + //dbProvider.Aop.OnExecutingChangeSql = (s, p) => + //{ + // foreach (var item in p) + // { + // if (item.ParameterName.Contains("'(")) + // { + // var oldName = item.ParameterName; + // //重点看这儿,把参数名变成正常的名字 + // item.ParameterName = item.ParameterName + // .Replace("'(", "(") + // .Replace(")'", ")"); + // s = s.Replace(oldName, item.ParameterName); + // }; + // } + // return new KeyValuePair(s, p); + //}; + //var templist = dbProvider.QueryFilter.GeFilterList; + //Console.WriteLine("当前过滤器:"+ JsonConvert.SerializeObject(templist)); + //全局过滤租户Id + dbProvider.QueryFilter.AddTableFilter(m => m.TenantId == conf.TenantId); + //全局过滤机构Id + dbProvider.QueryFilter.AddTableFilter(m => m.OrgId == conf.OrgId); + //全局过滤共享机构Id + dbProvider.QueryFilter.AddTableFilter(x => x.OrgId == conf.OrgId || x.IsShared); + //全局软删除过滤 + dbProvider.QueryFilter.AddTableFilter(m => m.Deleted == false); + } + + return db.GetConnectionScope(conf.TenantId); + } } \ No newline at end of file diff --git a/ds-wms-service/DS.WMS.FeeBillRecvService/RecvFeeBillWorker.cs b/ds-wms-service/DS.WMS.FeeBillRecvService/RecvFeeBillWorker.cs index 17acda4f..6aae1822 100644 --- a/ds-wms-service/DS.WMS.FeeBillRecvService/RecvFeeBillWorker.cs +++ b/ds-wms-service/DS.WMS.FeeBillRecvService/RecvFeeBillWorker.cs @@ -8,6 +8,7 @@ using DS.Module.Core.Data; using DS.Module.SqlSugar; using DS.Module.UserModule; using DS.WMS.Core.Fee.Entity; +using DS.WMS.Core.Flow.Dtos; using DS.WMS.Core.HangfireJob.Interface; using DS.WMS.Core.Info.Entity; using DS.WMS.Core.Op.Entity; @@ -139,7 +140,7 @@ namespace DS.WMS.FeeBillRecvService //}); - var tenantDb = saasService.GetBizJobDbScopeById(new JobDbInitConfig + var tenantDb = saasService.GetBizJobDbScopeExtById(new JobDbInitConfig { //UserId = long.Parse(AppSetting.app(new string[] { "FeeSettings", "UserId" })), //UserName = AppSetting.app(new string[] { "FeeSettings", "UserName" }), @@ -169,7 +170,9 @@ namespace DS.WMS.FeeBillRecvService var codeCurrency = tenantDb.Queryable().ToList(); - var userInfo = db.Queryable().Filter(null, true).First(x => x.Id == booking.OperatorId && x.TenantId == long.Parse("1819549542425694208")); + long tenantId = long.Parse(AppSetting.app(new string[] { "FeeSettings", "TenantId" })); + + var userInfo = db.Queryable().ClearFilter(typeof(ITenantId)).First(x => x.Id == booking.OperatorId && x.TenantId == tenantId); List errorMsgList = new List(); @@ -220,7 +223,7 @@ namespace DS.WMS.FeeBillRecvService custShortName = fee.CustSettleFor?.Trim(); } - var customerInfo = tenantDb.Queryable().ClearFilter(typeof(IOrgId)).First(x => x.ShortName == custShortName); + var customerInfo = tenantDb.Queryable().ClearFilter(typeof(ISharedOrgId)).First(x => x.ShortName == custShortName); newfee.CustomerId = customerInfo.Id; newfee.CustomerName = customerInfo.Description; @@ -268,6 +271,7 @@ namespace DS.WMS.FeeBillRecvService newfee.CreateBy = userInfo.Id; newfee.CreateUserName = userInfo.UserName; newfee.UpdateTime = nowDate; + newfee.FeeStatus = FeeStatus.AuditPassed; newfee.Note = "大简云账单解析"; diff --git a/ds-wms-service/DS.WMS.FeeBillRecvService/appsettings.json b/ds-wms-service/DS.WMS.FeeBillRecvService/appsettings.json index 36c150fc..ad5d7321 100644 --- a/ds-wms-service/DS.WMS.FeeBillRecvService/appsettings.json +++ b/ds-wms-service/DS.WMS.FeeBillRecvService/appsettings.json @@ -21,10 +21,10 @@ }, "FeeSettings": { "ShowName": "昭阳接收邮件账单", - "UserId": "1819549542463442944", - "UserName": "东胜8测试", - "TenantId": "1819549542425694208", - "OrgId": "1819557001806614528", + "UserId": "", + "UserName": "", + "TenantId": "1750335377144680448", + "OrgId": "", "ExchangeName": "billcenter.output.ds7new.444c19c1-0bf5-4709-a08b-c9859ca775e6", "QueueName": "billcenter.output.ds7new.444c19c1-0bf5-4709-a08b-c9859ca775e6", "MQUrl": "amqp://dongsheng8bill:dongsheng8bill@47.104.207.5:12567/billcenter"