From 139594d06cdca549abbfe462ecaf208d97a8dc4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=AC=A3?= <83699462@qq.com> Date: Thu, 11 Jan 2024 17:28:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=B8=81=E5=88=AB?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DS.Module.Nuget/DS.Module.Nuget.csproj | 2 + .../DS.Module.SqlSugar/SqlSugarAppStartup.cs | 14 +++ .../DS.Module.SqlSugar/SqlsugarInstall.cs | 16 ++- .../System/Method/CommonService.cs | 28 ++--- .../System/Method/PermissionService.cs | 2 + .../DS.WMS.Core/System/Method/UserService.cs | 7 +- .../Controllers/CostExchangeRateController.cs | 16 ++- ds-wms-service/DS.WMS.FinanceApi/Program.cs | 5 +- .../DS.WMS.FinanceApi/appsettings.json | 6 +- .../Ds.Modules.DsEntity/Base/BasEntity.cs | 59 +++++----- .../Ds.Modules.DsEntity.csproj | 5 + .../Ds.Modules.DsEntity/Snowflake/Net.cs | 6 + .../Command/CostExchangeRateCommand.cs | 34 +++++- .../Ds.WMS.Finance.MediatR/Entity/Currency.cs | 106 +++++++++++++++++- .../Ds.WMS.Finance.MediatR/GlobalUsings.cs | 4 +- .../handlers/GetCostBasicHandler.cs | 33 +++++- 16 files changed, 275 insertions(+), 68 deletions(-) create mode 100644 ds-wms-service/Ds.Modules.DsEntity/Snowflake/Net.cs diff --git a/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj b/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj index 1f8b316c..ed6a16de 100644 --- a/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj +++ b/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj @@ -28,6 +28,8 @@ + + diff --git a/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs b/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs index f384e65f..47a01bf6 100644 --- a/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs +++ b/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs @@ -9,7 +9,21 @@ namespace DS.Module.SqlSugar public void ConfigureServices(IServiceCollection services) { // 配置服务 + //builder.Services.AddScoped(provider => + //{ + // var connectionConfig = new SqlSugar.ConnectionConfig + // { + // ConnectionString = builder.Configuration.GetConnectionString("testConnection"), // 这里替换为你的连接字符串 + // DbType = SqlSugar.DbType.MySql, // 数据库类型 + // IsAutoCloseConnection = true, // 自动释放数据务,如果存在事务,在事务结束后释放 + // InitKeyType = SqlSugar.InitKeyType.Attribute // 从实体特性中读取主键自增列信息 + // }; + // var sqlSugarClient = new SqlSugar.SqlSugarClient(connectionConfig); + // sqlSugarClient.CodeFirst.InitTables(typeof(Code_Currency)); + + // return sqlSugarClient; + //}); // 其他服务配置... } diff --git a/ds-wms-service/DS.Module.SqlSugar/SqlsugarInstall.cs b/ds-wms-service/DS.Module.SqlSugar/SqlsugarInstall.cs index fa0346fc..2460b726 100644 --- a/ds-wms-service/DS.Module.SqlSugar/SqlsugarInstall.cs +++ b/ds-wms-service/DS.Module.SqlSugar/SqlsugarInstall.cs @@ -1,13 +1,11 @@ using DS.Module.Core; +using DS.Module.Core.Data; using DS.Module.Core.Extensions; using DS.Module.UserModule; using Microsoft.Extensions.DependencyInjection; using NLog; using SqlSugar; using System.Linq.Dynamic.Core; -using System.Linq.Expressions; -using System.Reflection; -using DS.Module.Core.Data; using LogLevel = NLog.LogLevel; namespace DS.Module.SqlSugar; @@ -140,11 +138,11 @@ public static class SqlsugarInstall { entityInfo.SetValue(1288018625843826688); } - } + } + - if (entityInfo.PropertyName == "Deleted") - entityInfo.SetValue(false); + entityInfo.SetValue(false); } // 更新操作 @@ -153,15 +151,15 @@ public static class SqlsugarInstall if (entityInfo.PropertyName == "UpdateTime") entityInfo.SetValue(DateTime.Now); if (entityInfo.PropertyName == "UpdateBy" && user != null) - entityInfo.SetValue(user.UserId); + entityInfo.SetValue(user.UserId); } }; dbProvider.Aop.OnError = (exp) => //执行SQL 错误事件 { Logger.Error(DateTime.Now.ToString() + "\r\n" + exp.Sql + "\r\n" + exp.Parametres); }; - - dbProvider.QueryFilter.AddTableFilter(m => m.TenantId == user.GetTenantId()); + + // dbProvider.QueryFilter.AddTableFilter(m => m.TenantId == user.GetTenantId()); //全局软删除过滤 dbProvider.QueryFilter.AddTableFilter(m => m.Deleted == false); } diff --git a/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs b/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs index cf0495a5..5e1417c5 100644 --- a/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs +++ b/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs @@ -5,14 +5,14 @@ using DS.Module.UserModule; using DS.WMS.Core.System.Dtos; using DS.WMS.Core.System.Entity; using DS.WMS.Core.System.Interface; +using Mapster; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; using SqlSugar; using System.Diagnostics; using System.Runtime.InteropServices; -using Mapster; -using Newtonsoft.Json; namespace DS.WMS.Core.System.Method; @@ -79,7 +79,7 @@ public class CommonService : ICommonService public DataResult GetUserInfo() { var userId = long.Parse(user.UserId); - var tenantId = user.GetTenantId(); + var tenantId = long.Parse(user.GetTenantId()); var data = db.Queryable().Filter(null, true).Where(x => x.Id == userId) .Select(a => new CurrentUserViewModel @@ -396,13 +396,13 @@ public class CommonService : ICommonService return DataResult.Successed("密码修改成功!"); } - + #endregion 修改密码 - - - - #region 租户注册 + + + + #region 租户注册 /// /// 租户注册 @@ -463,7 +463,7 @@ public class CommonService : ICommonService //写入注册用户 var userEntity = new SysUser - { + { UserCode = req.AdminUserCode, UserName = req.Name, Password = req.AdminUserPassword, @@ -473,18 +473,18 @@ public class CommonService : ICommonService Email = "", Phone = req.Phone, UserType = UserTypeEnum.Admin.ToEnumInt(), //管理员 - Status = 0, //正常 + Status = 0, //正常 TenantId = 1288018625843826688, - }; - var adminUser = db.Insertable(userEntity).ExecuteReturnEntity(); + }; + var adminUser = db.Insertable(userEntity).ExecuteReturnEntity(); //写入用户角色 指定角色 var roleUser = new SysRoleUser { RoleId = 1744922920100171776, - UserId = adminUser.Id + UserId = adminUser.Id }; db.Insertable(roleUser).ExecuteCommand(); - + db.Ado.CommitTran(); return DataResult.Successed("企业注册成功!请登录后完善相关资料并提交审核!"); } diff --git a/ds-wms-service/DS.WMS.Core/System/Method/PermissionService.cs b/ds-wms-service/DS.WMS.Core/System/Method/PermissionService.cs index bcf596d3..626ebe64 100644 --- a/ds-wms-service/DS.WMS.Core/System/Method/PermissionService.cs +++ b/ds-wms-service/DS.WMS.Core/System/Method/PermissionService.cs @@ -326,4 +326,6 @@ public class PermissionService : IPermissionService } return DataResult.Successed("更新成功!"); } + + } \ No newline at end of file diff --git a/ds-wms-service/DS.WMS.Core/System/Method/UserService.cs b/ds-wms-service/DS.WMS.Core/System/Method/UserService.cs index 92ebbfc2..43b2e2b9 100644 --- a/ds-wms-service/DS.WMS.Core/System/Method/UserService.cs +++ b/ds-wms-service/DS.WMS.Core/System/Method/UserService.cs @@ -3,6 +3,7 @@ using DS.Module.Core.Extensions; using DS.Module.Core.Helpers; using DS.Module.UserModule; using DS.WMS.Core.System.Dtos; +using DS.WMS.Core.System.Entity; using DS.WMS.Core.System.Interface; using Microsoft.Extensions.DependencyInjection; using SqlSugar; @@ -92,7 +93,7 @@ public class UserService : IUserService // data.CompanyId = "Comcab2d43f60454327af30a131fc1d3abd"; //data.GID = Guid.NewGuid(); - + var entity = db.Insertable(data).ExecuteReturnEntity(); if (model.RoleIds.Count() > 0) @@ -125,7 +126,7 @@ public class UserService : IUserService var existRoles = db.Queryable().Where(x => x.UserId == userId).Select(n => n.RoleId) .ToArray(); - + IEnumerable delRoles = existRoles.AsQueryable().Except(model.RoleIds); if (delRoles.Count() > 0) { @@ -133,7 +134,7 @@ public class UserService : IUserService .Where(it => it.UserId == userId && delRoles.Contains(it.RoleId)) .ExecuteCommand(); } - + IEnumerable addRoles = model.RoleIds.AsQueryable().Except(existRoles); if (addRoles.Count() > 0) { diff --git a/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs b/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs index 3c6387c1..3346b936 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs +++ b/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs @@ -7,13 +7,12 @@ [ApiController] public class CostExchangeRateController : DsBaseController { - /// - /// Get + /// 根据ID查询汇率基本信息 /// /// [HttpGet("GetCostBasic")] - public async Task GetCostBasic(Guid id) + public async Task GetCostBasic(long id) { var getUserQuery = new CostExchangeRateCommand { Id = id }; @@ -21,5 +20,16 @@ return Ok(result); } + + /// + /// 币别添加 + /// + /// + [HttpPost("AddForCostBasic")] + public async Task AddForCostBasic(CostExchangeForAddCommand dto) + { + var result = await Mediator.Send(dto); + return Ok(result); + } } } diff --git a/ds-wms-service/DS.WMS.FinanceApi/Program.cs b/ds-wms-service/DS.WMS.FinanceApi/Program.cs index 47f7e47e..b714ea0a 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/Program.cs +++ b/ds-wms-service/DS.WMS.FinanceApi/Program.cs @@ -13,12 +13,15 @@ builder.Services.AddScoped(provider => { var connectionConfig = new SqlSugar.ConnectionConfig { - ConnectionString = builder.Configuration.GetConnectionString("YourConnectionStringName"), // 滻Ϊַ + ConnectionString = builder.Configuration.GetConnectionString("testConnection"), // 滻Ϊַ DbType = SqlSugar.DbType.MySql, // ݿ IsAutoCloseConnection = true, // Զͷͷ InitKeyType = SqlSugar.InitKeyType.Attribute // ʵжȡϢ + }; var sqlSugarClient = new SqlSugar.SqlSugarClient(connectionConfig); + // sqlSugarClient.CodeFirst.InitTables(typeof(Code_Currency)); + return sqlSugarClient; }); // ע MediatR diff --git a/ds-wms-service/DS.WMS.FinanceApi/appsettings.json b/ds-wms-service/DS.WMS.FinanceApi/appsettings.json index 3059e92f..3de4a703 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/appsettings.json +++ b/ds-wms-service/DS.WMS.FinanceApi/appsettings.json @@ -5,6 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" - + "AllowedHosts": "*", + "ConnectionStrings": { + "testConnection":"server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_dev" + } } diff --git a/ds-wms-service/Ds.Modules.DsEntity/Base/BasEntity.cs b/ds-wms-service/Ds.Modules.DsEntity/Base/BasEntity.cs index ad89cabc..ceeacf0e 100644 --- a/ds-wms-service/Ds.Modules.DsEntity/Base/BasEntity.cs +++ b/ds-wms-service/Ds.Modules.DsEntity/Base/BasEntity.cs @@ -1,5 +1,4 @@ -using System.ComponentModel; -using SqlSugar; +using SqlSugar; namespace Ds.Modules.DsEntity.Base { @@ -8,63 +7,64 @@ namespace Ds.Modules.DsEntity.Base /// /// 备注 /// - [Description("备注")] - [SugarColumn(ColumnDataType = "300")] - public string DsNote { get; set; } + + [SqlSugar.SugarColumn(ColumnDescription = "备注")] + public string? DsNote { get; set; } = "0qaqq"; /// /// 创建时间 /// - [Description("创建时间")] - [SugarColumn(IsOnlyIgnoreUpdate = true, ColumnDescription = "创建时间", InsertServerTime = true)] - public DateTime DsAddTime { get; set; } + + [SqlSugar.SugarColumn(ColumnDescription = "创建时间")] + public DateTime DsAddTime { get; set; } = DateTime.Now; /// /// 创建人 /// - [Description("创建人")] - [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true, IsNullable = true, ColumnDescription = "创建人")] - public string DsAddUserInfo { get; set; } + + [SqlSugar.SugarColumn(ColumnDescription = "创建人")] + public string? DsAddUserInfo { get; set; } = "admin"; /// /// 修改人 /// - [Description("修改人")] - [SqlSugar.SugarColumn(IsNullable = true, ColumnDescription = "修改人")] - public string DsUpdateUserInfo { get; set; } + + [SqlSugar.SugarColumn(ColumnDescription = "修改人")] + public string? DsUpdateUserInfo { get; set; } = "admin"; /// /// 更新时间 /// - [Description("更新时间")] - [SqlSugar.SugarColumn(ColumnDescription = "更新时间", UpdateServerTime = true)] - public DateTime DsUpdateTime { get; set; } + + [SqlSugar.SugarColumn(ColumnDescription = "更新时间")] + public DateTime DsUpdateTime { get; set; } = DateTime.Now; /// /// 删除 /// - [Description("删除")] + [SqlSugar.SugarColumn(ColumnDescription = "是否删除")] public bool DsIsDel { get; set; } = false; /// /// 删除时间 /// - [Description("删除时间")] + [SqlSugar.SugarColumn(ColumnDescription = "删除时间")] public DateTime DsDeleteTime { get; set; } = DateTime.Now; /// /// 删除人 /// - [Description("删除人")] - [SqlSugar.SugarColumn(IsNullable = true, ColumnDescription = "删除人")] - public string DsDeleteUserInfo { get; set; } + + [SqlSugar.SugarColumn(ColumnDescription = "删除人")] + public string? DsDeleteUserInfo { get; set; } = "admin"; /// /// 排序 /// - public int DsSort { get; set; } + [SqlSugar.SugarColumn(ColumnDescription = "排序")] + public int DsSort { get; set; } = 0; } /// @@ -142,16 +142,23 @@ namespace Ds.Modules.DsEntity.Base /// public abstract class BaseEntityGuid : BaseComEntity { + public BaseEntityGuid() + { + var worker = new Snowflake.Core.IdWorker(1, 1); + Id = worker.NextId(); + } + /// /// 主键ID /// [Description("主键ID")] - [SugarColumn(IsPrimaryKey = true, Length = 100, ColumnDescription = "主键ID")] - public Guid Id { get; set; } = Guid.NewGuid(); + [SqlSugar.SugarColumn(IsPrimaryKey = true, Length = 100, ColumnDescription = "主键ID")] + public long Id { get; set; } /// /// 租户编号 /// - public string TenantNumber { get; set; } + [SqlSugar.SugarColumn(ColumnDescription = "租户编号")] + public long TenantNumber { get; set; } } } \ No newline at end of file diff --git a/ds-wms-service/Ds.Modules.DsEntity/Ds.Modules.DsEntity.csproj b/ds-wms-service/Ds.Modules.DsEntity/Ds.Modules.DsEntity.csproj index 437700c6..77fb06f3 100644 --- a/ds-wms-service/Ds.Modules.DsEntity/Ds.Modules.DsEntity.csproj +++ b/ds-wms-service/Ds.Modules.DsEntity/Ds.Modules.DsEntity.csproj @@ -6,6 +6,11 @@ enable + + + + + diff --git a/ds-wms-service/Ds.Modules.DsEntity/Snowflake/Net.cs b/ds-wms-service/Ds.Modules.DsEntity/Snowflake/Net.cs new file mode 100644 index 00000000..81f83520 --- /dev/null +++ b/ds-wms-service/Ds.Modules.DsEntity/Snowflake/Net.cs @@ -0,0 +1,6 @@ +namespace Snowflake +{ + internal class Net + { + } +} \ No newline at end of file diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs index 3239a730..2d83504d 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs @@ -8,6 +8,38 @@ /// /// 主键 /// - public Guid Id { get; set; } + public long Id { get; set; } + } + + /// + /// 根据ID查询汇率基本信息 + /// + public class CostExchangeForAddCommand : IRequest + { + /// + /// 币别代码 + /// + + public string? CodeName { get; set; } + /// + /// 币别名称 + /// + + public string? Name { get; set; } + /// + /// 描述 + /// + + public string? Description { get; set; } + /// + /// 默认对人民币汇率,录入费用时如果汇率设置里没有设置汇率,提此处汇率 + /// + + public decimal? DefaultRate { get; set; } = Math.Round(1.123456789m, 8); + /// + /// 财务软件对应代码 + /// + + public string? FinanceSoftCodes { get; set; } } } diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/Entity/Currency.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/Entity/Currency.cs index e9f2068d..4620084c 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/Entity/Currency.cs +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/Entity/Currency.cs @@ -5,14 +5,112 @@ /// /// 主键 /// - public Guid Id { get; set; } + public long Id { get; set; } + + /// + /// 币别代码 + /// + + public string? CodeName { get; set; } + /// + /// 币别名称 + /// + + public string? Name { get; set; } + /// + /// 描述 + /// + + public string? Description { get; set; } + /// + /// 默认对人民币汇率,录入费用时如果汇率设置里没有设置汇率,提此处汇率 + /// + + public decimal? DefaultRate { get; set; } = Math.Round(1.123456789m, 8); + /// + /// 财务软件对应代码 + /// + + public string? FinanceSoftCodes { get; set; } + } + public class AddCurrencyDto + { } - public class Currency + /// + /// 币别 + /// + [SugarTable("Ds_Op_Code_Currency")] + public class Code_Currency : BaseEntityGuid { /// - /// 主键 + /// 币别代码 + /// + [SugarColumn(ColumnDescription = "币别代码")] + public string? CodeName { get; set; } + /// + /// 币别名称 + /// + [SugarColumn(ColumnDescription = "币别名称")] + public string? Name { get; set; } + /// + /// 描述 + /// + [SugarColumn(ColumnDescription = "描述")] + public string? Description { get; set; } + /// + /// 默认对人民币汇率,录入费用时如果汇率设置里没有设置汇率,提此处汇率 + /// + [SugarColumn(ColumnDescription = "默认对人民币汇率")] + public decimal? DefaultRate { get; set; } = Math.Round(1.123456789m, 8); + /// + /// 财务软件对应代码 + /// + [SugarColumn(ColumnDescription = "财务软件对应代码")] + public string? FinanceSoftCodes { get; set; } + + } + + /// + /// 币别 + /// + [SugarTable("Ds_Op_Currency_Exchange")] + public class Currency_Exchange : BaseEntityGuid + { + /// + /// /*币别代码*/ + /// + [SugarColumn(ColumnDescription = "币别代码")] + public long Code_Currency_Id { get; set; } + /// + /// *应收汇率*/ + /// + [SugarColumn(ColumnDescription = "应收汇率")] + public decimal Drvalue { get; set; } = Math.Round(1.123456789m, 8); + /// + /// /*开始日期*/ + /// + [SugarColumn(ColumnDescription = "开始日期")] + public DateTime StartTime { get; set; } = DateTime.Today; + /// + /// /*结束日期*/ + /// + [SugarColumn(ColumnDescription = "结束日期")] + public DateTime EndTime { get; set; } = DateTime.Today; + /// + /// /*应付汇率*/ + /// + [SugarColumn(ColumnDescription = "应付汇率")] + public decimal Crvalue { get; set; } = Math.Round(1.123456789m, 8); + /// + /// 本位币 + /// + [SugarColumn(ColumnDescription = "本位币")] + public string Localcurr { get; set; } = "RMB"; + /// + /// 是否是默认 /// - public Guid Id { get; set; } + [SugarColumn(ColumnDescription = "是否是默认")] + public bool IsDefault { get; set; } = false; } } diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/GlobalUsings.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/GlobalUsings.cs index 75a20829..fea46af9 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/GlobalUsings.cs +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/GlobalUsings.cs @@ -1,3 +1,5 @@ -global using Ds.WMS.Finance.MediatR.Command; +global using Ds.Modules.DsEntity.Base; +global using Ds.WMS.Finance.MediatR.Command; global using Ds.WMS.Finance.MediatR.Entity; global using MediatR; +global using SqlSugar; diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs index 7102e950..16285283 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs @@ -1,6 +1,4 @@ -using SqlSugar; - -namespace Ds.WMS.Finance.MediatR.handlers +namespace Ds.WMS.Finance.MediatR.handlers { /// /// 具体的业务逻辑 @@ -12,6 +10,8 @@ namespace Ds.WMS.Finance.MediatR.handlers { _db = db; } + + /// /// 具体的业务逻辑 /// @@ -20,11 +20,36 @@ namespace Ds.WMS.Finance.MediatR.handlers /// async Task IRequestHandler.Handle(CostExchangeRateCommand request, CancellationToken cancellationToken) { - return await _db.Queryable() + var list = await _db.Queryable() .Where(x => x.Id == request.Id) .Select() .FirstAsync(); + return await Task.FromResult(list); + } + } + /// + /// 添加 + /// + public class AddCostBasicHandler : IRequestHandler + { + private readonly ISqlSugarClient _db; + public AddCostBasicHandler(ISqlSugarClient db) + { + _db = db; + } + public async Task Handle(CostExchangeForAddCommand request, CancellationToken cancellationToken) + { + var list = await _db.Insertable(new Code_Currency() + { + + CodeName = request.CodeName, + DefaultRate = request.DefaultRate, + Description = request.Description, + FinanceSoftCodes = request.FinanceSoftCodes, + Name = request.Name + }).ExecuteCommandAsync() > 0; + return await Task.FromResult(list); } } } From b798e1fe96d949658d9f3507a475216084a6a1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=AC=A3?= <83699462@qq.com> Date: Fri, 12 Jan 2024 16:16:28 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=8A=A8=E6=80=81startup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DS.Module.Nuget/DS.Module.Nuget.csproj | 1 + .../DS.Module.SqlSugar.csproj | 4 +- .../DS.Module.SqlSugar/Ds_Repository.cs | 29 ----- .../DS.Module.SqlSugar/SqlSugarAppStartup.cs | 33 ++---- .../System/Method/CommonService.cs | 2 +- .../Controllers/CostExchangeRateController.cs | 12 ++ .../DS.WMS.FinanceApi.csproj | 8 +- ds-wms-service/DS.WMS.FinanceApi/Program.cs | 50 +++++---- .../DS.WMS.FinanceApi/appsettings.json | 17 ++- .../DsStartupAttribute.cs | 18 +++ .../Ds_StartupAttribute.cs | 104 ------------------ .../Ds.Module.AppStartup/IDynamicStartup.cs | 9 ++ .../Command/CostExchangeRateCommand.cs | 12 +- .../Ds.WMS.Finance.MediatR.csproj | 2 + .../MediatRAppStartup.cs | 27 +++++ .../handlers/GetCostBasicHandler.cs | 23 +++- .../Ds.WMS.WebCore/Ds.WMS.WebCore.csproj | 18 +++ .../WebApplicationBuilderExtensions.cs | 76 +++++++++++++ ds-wms-service/ds-wms-service.sln | 11 +- 19 files changed, 265 insertions(+), 191 deletions(-) delete mode 100644 ds-wms-service/DS.Module.SqlSugar/Ds_Repository.cs create mode 100644 ds-wms-service/Ds.Module.AppStartup/DsStartupAttribute.cs delete mode 100644 ds-wms-service/Ds.Module.AppStartup/Ds_StartupAttribute.cs create mode 100644 ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs create mode 100644 ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs create mode 100644 ds-wms-service/Ds.WMS.WebCore/Ds.WMS.WebCore.csproj create mode 100644 ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs diff --git a/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj b/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj index ed6a16de..f27964eb 100644 --- a/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj +++ b/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj @@ -21,6 +21,7 @@ + diff --git a/ds-wms-service/DS.Module.SqlSugar/DS.Module.SqlSugar.csproj b/ds-wms-service/DS.Module.SqlSugar/DS.Module.SqlSugar.csproj index 4dcf551b..b082e67c 100644 --- a/ds-wms-service/DS.Module.SqlSugar/DS.Module.SqlSugar.csproj +++ b/ds-wms-service/DS.Module.SqlSugar/DS.Module.SqlSugar.csproj @@ -7,7 +7,7 @@ - + @@ -17,4 +17,6 @@ + + diff --git a/ds-wms-service/DS.Module.SqlSugar/Ds_Repository.cs b/ds-wms-service/DS.Module.SqlSugar/Ds_Repository.cs deleted file mode 100644 index 1e8f52cc..00000000 --- a/ds-wms-service/DS.Module.SqlSugar/Ds_Repository.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Microsoft.Extensions.Configuration; -using SqlSugar; -using System.Configuration; - -namespace DS.Module.SqlSugar -{ - public class Ds_Repository where T : class, new() - { - private readonly ISqlSugarClient _sqlSugarClient; - private readonly IConfiguration _configuration; - public Ds_Repository(IConfiguration _configuration) - { - var connectionConfig = new ConnectionConfig - { - ConnectionString = _configuration.GetConnectionString("YourConnectionStringName"), // 这里替换为你的连接字符串 - DbType = DbType.SqlServer, // 数据库类型 - IsAutoCloseConnection = true, // 自动释放数据务,如果存在事务,在事务结束后释放 - InitKeyType = InitKeyType.Attribute // 从实体特性中读取主键自增列信息 - }; - _sqlSugarClient = new SqlSugarClient(connectionConfig); - } - - public List GetAll() - { - return _sqlSugarClient.Queryable().ToList(); - } - - } -} diff --git a/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs b/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs index 47a01bf6..1d3664a6 100644 --- a/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs +++ b/ds-wms-service/DS.Module.SqlSugar/SqlSugarAppStartup.cs @@ -1,36 +1,23 @@ using Ds.Module.AppStartup; -using Microsoft.Extensions.DependencyInjection; - +using Microsoft.AspNetCore.Builder; namespace DS.Module.SqlSugar { - public class SqlSugarAppStartup : Ds_StartupAttribute + + [DsStartup(7)] + public class SqlSugarAppStartup : IDynamicStartup { - public void ConfigureServices(IServiceCollection services) - { - // 配置服务 - //builder.Services.AddScoped(provider => - //{ - // var connectionConfig = new SqlSugar.ConnectionConfig - // { - // ConnectionString = builder.Configuration.GetConnectionString("testConnection"), // 这里替换为你的连接字符串 - // DbType = SqlSugar.DbType.MySql, // 数据库类型 - // IsAutoCloseConnection = true, // 自动释放数据务,如果存在事务,在事务结束后释放 - // InitKeyType = SqlSugar.InitKeyType.Attribute // 从实体特性中读取主键自增列信息 - // }; - // var sqlSugarClient = new SqlSugar.SqlSugarClient(connectionConfig); - // sqlSugarClient.CodeFirst.InitTables(typeof(Code_Currency)); + public void Configure(WebApplication app) + { - // return sqlSugarClient; - //}); - // 其他服务配置... } - public void Configure(object app) + public void ConfigureServices(WebApplicationBuilder builder) { - // 配置中间件 - // 其他中间件配置... + builder.Services.AddSqlsugarInstall(); } + + } } diff --git a/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs b/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs index 5e1417c5..75f5436d 100644 --- a/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs +++ b/ds-wms-service/DS.WMS.Core/System/Method/CommonService.cs @@ -79,7 +79,7 @@ public class CommonService : ICommonService public DataResult GetUserInfo() { var userId = long.Parse(user.UserId); - var tenantId = long.Parse(user.GetTenantId()); + var tenantId = (user.GetTenantId()); var data = db.Queryable().Filter(null, true).Where(x => x.Id == userId) .Select(a => new CurrentUserViewModel diff --git a/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs b/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs index 3346b936..5be37f9e 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs +++ b/ds-wms-service/DS.WMS.FinanceApi/Controllers/CostExchangeRateController.cs @@ -20,7 +20,19 @@ return Ok(result); } + /// + /// 根据ID删除或者禁用相关的币别 + /// + /// + [HttpGet("DelCostBasic")] + public async Task DelCostBasic(long id) + { + var getUserQuery = new CostExchangeForDelCommand { Id = id }; + var result = await Mediator.Send(getUserQuery); + + return Ok(result); + } /// /// 币别添加 /// diff --git a/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj b/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj index 0a6b1268..e9d271a6 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj +++ b/ds-wms-service/DS.WMS.FinanceApi/DS.WMS.FinanceApi.csproj @@ -14,13 +14,7 @@ - - - - - - - + diff --git a/ds-wms-service/DS.WMS.FinanceApi/Program.cs b/ds-wms-service/DS.WMS.FinanceApi/Program.cs index b714ea0a..a4449d1f 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/Program.cs +++ b/ds-wms-service/DS.WMS.FinanceApi/Program.cs @@ -1,35 +1,42 @@ +using Ds.WMS.WebCore; using Microsoft.OpenApi.Models; using System.Reflection; var builder = WebApplication.CreateBuilder(args); +// ʹö̬ +builder.UseDynamicStartups(); + + // Add services to the container. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddControllers(); +// ʹչ Startup //// ע SqlSugarClient -builder.Services.AddScoped(provider => -{ - var connectionConfig = new SqlSugar.ConnectionConfig - { - ConnectionString = builder.Configuration.GetConnectionString("testConnection"), // 滻Ϊַ - DbType = SqlSugar.DbType.MySql, // ݿ - IsAutoCloseConnection = true, // Զͷͷ - InitKeyType = SqlSugar.InitKeyType.Attribute // ʵжȡϢ - - }; - var sqlSugarClient = new SqlSugar.SqlSugarClient(connectionConfig); - // sqlSugarClient.CodeFirst.InitTables(typeof(Code_Currency)); - - return sqlSugarClient; -}); +//builder.Services.AddScoped(provider => +//{ +// var connectionConfig = new SqlSugar.ConnectionConfig +// { +// ConnectionString = builder.Configuration.GetConnectionString("testConnection"), // 滻Ϊַ +// DbType = SqlSugar.DbType.MySql, // ݿ +// IsAutoCloseConnection = true, // Զͷͷ +// InitKeyType = SqlSugar.InitKeyType.Attribute // ʵжȡϢ + +// }; +// var sqlSugarClient = new SqlSugar.SqlSugarClient(connectionConfig); +// // sqlSugarClient.CodeFirst.InitTables(typeof(Code_Currency)); + +// return sqlSugarClient; +//}); // ע MediatR -var assembly = AppDomain.CurrentDomain.Load("Ds.WMS.Finance.MediatR"); -builder.Services.AddMediatR(c => -{ - c.RegisterServicesFromAssembly(assembly); -}); +//var assembly = AppDomain.CurrentDomain.Load("Ds.WMS.Finance.MediatR"); + +//builder.Services.AddMediatR(c => +//{ +// c.RegisterServicesFromAssembly(assembly); +//}); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); @@ -41,6 +48,8 @@ builder.Services.AddSwaggerGen(c => //builder.Services.AddMediatR(Assembly.GetExecutingAssembly()); var app = builder.Build(); +app.UseDynamicStartups(); + // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) //{ @@ -50,6 +59,7 @@ var app = builder.Build(); // м Swagger Ϊ JSON ս app.UseSwagger(); + // м swagger-uiָ Swagger JSON ս app.UseSwaggerUI(c => { diff --git a/ds-wms-service/DS.WMS.FinanceApi/appsettings.json b/ds-wms-service/DS.WMS.FinanceApi/appsettings.json index 3de4a703..a763830c 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/appsettings.json +++ b/ds-wms-service/DS.WMS.FinanceApi/appsettings.json @@ -7,6 +7,21 @@ }, "AllowedHosts": "*", "ConnectionStrings": { - "testConnection":"server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_dev" + "testConnection": "server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_dev" + }, + "DBInfo": { + "DefaultDbConnId": "1288018625843826688", + "DefaultDbType": 0, + "DefaultDbString": "server=60.209.125.238;port=32006;uid=root;pwd=Djy@Mysql.test;database=shippingweb8_dev", + "DBS": [ + { + "ConnId": "1595354960864874496", + "DBType": 1, + "Enabled": false, + "HitRate": 40, + "Connection": "Data Source=47.105.193.36,11435;Initial Catalog=SHIPPINGWEB_JNHJ;Integrated Security=False;Connect Timeout=500;User ID=sa;Password=Ds20040201", + "ProviderName": "System.Data.SqlClient" + } + ] } } diff --git a/ds-wms-service/Ds.Module.AppStartup/DsStartupAttribute.cs b/ds-wms-service/Ds.Module.AppStartup/DsStartupAttribute.cs new file mode 100644 index 00000000..e2dd8f5a --- /dev/null +++ b/ds-wms-service/Ds.Module.AppStartup/DsStartupAttribute.cs @@ -0,0 +1,18 @@ +namespace Ds.Module.AppStartup +{ + /// + /// + /// + [AttributeUsage(AttributeTargets.Class)] + public class DsStartupAttribute : Attribute + { + public int Order { get; set; } + + public DsStartupAttribute(int order) + { + Order = order; + } + } + + +} \ No newline at end of file diff --git a/ds-wms-service/Ds.Module.AppStartup/Ds_StartupAttribute.cs b/ds-wms-service/Ds.Module.AppStartup/Ds_StartupAttribute.cs deleted file mode 100644 index 5637f2f5..00000000 --- a/ds-wms-service/Ds.Module.AppStartup/Ds_StartupAttribute.cs +++ /dev/null @@ -1,104 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using System.Reflection; - -namespace Ds.Module.AppStartup -{ - /// - /// - /// - [AttributeUsage(AttributeTargets.Class)] - public class Ds_StartupAttribute : Attribute - { } - - public interface IDs_App_Startup - { - void ConfigureServices(IServiceCollection services); - - void Configure(object app); - } - - /// - /// - /// - public static class Ds_App_Startup - { - public static void Run() - { - try - { - var startupTypes = Assembly.GetCallingAssembly().GetTypes() - .Where(type => type.GetCustomAttribute() != null); - - var services = new ServiceCollection(); - - foreach (var startupType in startupTypes) - { - var startupInstance = Activator.CreateInstance(startupType) as IDs_App_Startup; - startupInstance?.ConfigureServices(services); - } - - // 构建 IServiceProvider - - var serviceProvider = services.BuildServiceProvider(); - - foreach (var startupType in startupTypes) - { - var startupInstance = Activator.CreateInstance(startupType); - var configureMethod = startupType.GetMethod("Configure"); - - if (configureMethod != null) - { - configureMethod.Invoke(startupInstance, new[] { serviceProvider }); - } - } - } - catch (Exception ex) - { - // 友好的错误消息和日志记录 - Console.WriteLine($"An error occurred: {ex.Message}"); - // 日志记录 - // Logger.LogError(ex, "An error occurred during startup."); - } - } - } - - //// 示例启动类 - //[MyFrameworkStartup] - //public class MyFrameworkStartup : IMyFrameworkStartup - //{ - // public void ConfigureServices(IServiceCollection services) - // { - // // 配置服务 - // services.AddTransient(); - // // 其他服务配置... - // } - - // public void Configure(object app) - // { - // // 配置中间件 - // // 其他中间件配置... - // } - //} - - // 示例服务 - //public interface IMyService - //{ - // void DoSomething(); - //} - - //public class MyService : IMyService - //{ - // public void DoSomething() - // { - // Console.WriteLine("Doing something..."); - // } - //} - - //internal class Program - //{ - // private static void Main() - // { - // MyFramework.Run(); - // } - //} -} \ No newline at end of file diff --git a/ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs b/ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs new file mode 100644 index 00000000..2db8f867 --- /dev/null +++ b/ds-wms-service/Ds.Module.AppStartup/IDynamicStartup.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.Builder; +namespace Ds.Module.AppStartup +{ + public interface IDynamicStartup + { + void ConfigureServices(WebApplicationBuilder builder); + void Configure(WebApplication app); + } +} \ No newline at end of file diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs index 2d83504d..9a53d53c 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/Command/CostExchangeRateCommand.cs @@ -12,7 +12,17 @@ } /// - /// 根据ID查询汇率基本信息 + /// 根据ID删除或者禁用币别 + /// + public class CostExchangeForDelCommand : IRequest + { + /// + /// 主键 + /// + public long Id { get; set; } + } + /// + /// 新增币别 /// public class CostExchangeForAddCommand : IRequest { diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/Ds.WMS.Finance.MediatR.csproj b/ds-wms-service/Ds.WMS.Finance.MediatR/Ds.WMS.Finance.MediatR.csproj index eff7b09e..3e7aa103 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/Ds.WMS.Finance.MediatR.csproj +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/Ds.WMS.Finance.MediatR.csproj @@ -8,7 +8,9 @@ + + diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs new file mode 100644 index 00000000..4fd5736e --- /dev/null +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs @@ -0,0 +1,27 @@ +using Ds.Module.AppStartup; +using DS.Module.SqlSugar; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace Ds.WMS.Finance.MediatR +{ + [DsStartup(10)] + public class MediatRAppStartup : IDynamicStartup + { + public void Configure(WebApplication app) + { + + } + + public void ConfigureServices(WebApplicationBuilder builder) + { + builder.Services.AddSqlsugarInstall(); + var assembly = AppDomain.CurrentDomain.Load("Ds.WMS.Finance.MediatR"); + + builder.Services.AddMediatR(c => + { + c.RegisterServicesFromAssembly(assembly); + }); + } + } +} diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs index 16285283..83a645df 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/handlers/GetCostBasicHandler.cs @@ -10,8 +10,6 @@ { _db = db; } - - /// /// 具体的业务逻辑 /// @@ -52,4 +50,25 @@ return await Task.FromResult(list); } } + + /// + /// 删除 + /// + public class DelCostBasicHandler : IRequestHandler + { + private readonly ISqlSugarClient _db; + public DelCostBasicHandler(ISqlSugarClient db) + { + _db = db; + } + public async Task Handle(CostExchangeForDelCommand request, CancellationToken cancellationToken) + { + var list = await _db.Updateable() + .Where(p => p.Id.Equals(request.Id)) + .IgnoreColumns(true) + .SetColumns(p => p.DsIsDel == true) + .ExecuteCommandAsync() > 0; + return await Task.FromResult(list); + } + } } diff --git a/ds-wms-service/Ds.WMS.WebCore/Ds.WMS.WebCore.csproj b/ds-wms-service/Ds.WMS.WebCore/Ds.WMS.WebCore.csproj new file mode 100644 index 00000000..3620853f --- /dev/null +++ b/ds-wms-service/Ds.WMS.WebCore/Ds.WMS.WebCore.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + diff --git a/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs b/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs new file mode 100644 index 00000000..ec867c07 --- /dev/null +++ b/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs @@ -0,0 +1,76 @@ +using Ds.Module.AppStartup; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using System.Reflection; +using System.Runtime.Loader; + +namespace Ds.WMS.WebCore +{ + public static class WebApplicationBuilderExtensions + { + public static WebApplicationBuilder UseDynamicStartups(this WebApplicationBuilder builder) + { + + + + var assembly = Assembly.Load("DS.WMS.FinanceApi"); + var types = assembly.GetTypes() + .Where(t => t.GetCustomAttributes().Any()) + .ToList(); + + var dynamictups = AssemblyLoadContext.Default.Assemblies + .SelectMany(a => a.GetTypes()) + .Select(t => new + { + Type = t, + Attribute = t.GetCustomAttribute() + }) + .Where(x => x.Attribute != null) + .OrderBy(x => x.Attribute.Order) + .Select(x => Activator.CreateInstance(x.Type)) + .OfType().ToList(); + + + var dynamicStartups = AppDomain.CurrentDomain.GetAssemblies() + .SelectMany(a => a.GetTypes()) + .Select(t => new + { + Type = t, + Attribute = t.GetCustomAttribute() + }) + .Where(x => x.Attribute != null) + .OrderBy(x => x.Attribute.Order) + .Select(x => Activator.CreateInstance(x.Type)) + .OfType(); + + + foreach (var startup in dynamicStartups) + { + startup.ConfigureServices(builder); + } + + builder.Services.AddSingleton(dynamicStartups); + + return builder; + } + } + + public static class WebApplicationExtensions + { + public static WebApplication UseDynamicStartups(this WebApplication app) + { + var dynamicStartups = app.Services.GetRequiredService>(); + + foreach (var startup in dynamicStartups) + { + startup.Configure(app); + } + + return app; + } + } + + +} + + diff --git a/ds-wms-service/ds-wms-service.sln b/ds-wms-service/ds-wms-service.sln index 7d60b909..4ce8af98 100644 --- a/ds-wms-service/ds-wms-service.sln +++ b/ds-wms-service/ds-wms-service.sln @@ -51,11 +51,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.Module.Mailkit", "Ds.Mod EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.Modules.DsEntity", "Ds.Modules.DsEntity\Ds.Modules.DsEntity.csproj", "{2DC9559E-AFC4-4C95-88DB-EFF8E04CB03A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DS.WMS.FinanceApi", "DS.WMS.FinanceApi\DS.WMS.FinanceApi.csproj", "{1FC0D999-2532-4999-84B3-CBA55644B7A6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DS.WMS.FinanceApi", "DS.WMS.FinanceApi\DS.WMS.FinanceApi.csproj", "{1FC0D999-2532-4999-84B3-CBA55644B7A6}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Finance", "Finance", "{8A1A89DE-7D82-4771-82F4-BDDB3AA11A4E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ds.WMS.Finance.MediatR", "Ds.WMS.Finance.MediatR\Ds.WMS.Finance.MediatR.csproj", "{B6DB4CF2-BF1D-49D6-96FB-3B1A2E78062B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.WMS.Finance.MediatR", "Ds.WMS.Finance.MediatR\Ds.WMS.Finance.MediatR.csproj", "{B6DB4CF2-BF1D-49D6-96FB-3B1A2E78062B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ds.WMS.WebCore", "Ds.WMS.WebCore\Ds.WMS.WebCore.csproj", "{9FB4A2AF-2414-4258-8B16-287EA3EB098D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -155,6 +157,10 @@ Global {B6DB4CF2-BF1D-49D6-96FB-3B1A2E78062B}.Debug|Any CPU.Build.0 = Debug|Any CPU {B6DB4CF2-BF1D-49D6-96FB-3B1A2E78062B}.Release|Any CPU.ActiveCfg = Release|Any CPU {B6DB4CF2-BF1D-49D6-96FB-3B1A2E78062B}.Release|Any CPU.Build.0 = Release|Any CPU + {9FB4A2AF-2414-4258-8B16-287EA3EB098D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9FB4A2AF-2414-4258-8B16-287EA3EB098D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9FB4A2AF-2414-4258-8B16-287EA3EB098D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9FB4A2AF-2414-4258-8B16-287EA3EB098D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -183,6 +189,7 @@ Global {2DC9559E-AFC4-4C95-88DB-EFF8E04CB03A} = {518DB9B5-80A8-4B2C-8570-52BD406458DE} {1FC0D999-2532-4999-84B3-CBA55644B7A6} = {8A1A89DE-7D82-4771-82F4-BDDB3AA11A4E} {B6DB4CF2-BF1D-49D6-96FB-3B1A2E78062B} = {8A1A89DE-7D82-4771-82F4-BDDB3AA11A4E} + {9FB4A2AF-2414-4258-8B16-287EA3EB098D} = {518DB9B5-80A8-4B2C-8570-52BD406458DE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {66115F23-94B4-43C0-838E-33B5CF77F788} From 6eca261184a977d3f6b1bf8bd95020376691269e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=AC=A3?= <83699462@qq.com> Date: Mon, 15 Jan 2024 09:22:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89startup=E7=9B=B8=E5=85=B3=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DS.Module.Nuget/DS.Module.Nuget.csproj | 1 + ds-wms-service/DS.WMS.FinanceApi/Program.cs | 29 ++---------- .../MediatRAppStartup.cs | 9 +++- .../Ds.WMS.WebCore/LoadAndExecuteAssembly.cs | 35 ++++++++++++++ .../WebApplicationBuilderExtensions.cs | 47 ++++++++++--------- 5 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 ds-wms-service/Ds.WMS.WebCore/LoadAndExecuteAssembly.cs diff --git a/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj b/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj index f27964eb..f92bcd85 100644 --- a/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj +++ b/ds-wms-service/DS.Module.Nuget/DS.Module.Nuget.csproj @@ -21,6 +21,7 @@ + diff --git a/ds-wms-service/DS.WMS.FinanceApi/Program.cs b/ds-wms-service/DS.WMS.FinanceApi/Program.cs index a4449d1f..a2c90a41 100644 --- a/ds-wms-service/DS.WMS.FinanceApi/Program.cs +++ b/ds-wms-service/DS.WMS.FinanceApi/Program.cs @@ -4,8 +4,7 @@ using System.Reflection; var builder = WebApplication.CreateBuilder(args); -// ʹö̬ -builder.UseDynamicStartups(); + // Add services to the container. @@ -14,29 +13,6 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddControllers(); // ʹչ Startup -//// ע SqlSugarClient -//builder.Services.AddScoped(provider => -//{ -// var connectionConfig = new SqlSugar.ConnectionConfig -// { -// ConnectionString = builder.Configuration.GetConnectionString("testConnection"), // 滻Ϊַ -// DbType = SqlSugar.DbType.MySql, // ݿ -// IsAutoCloseConnection = true, // Զͷͷ -// InitKeyType = SqlSugar.InitKeyType.Attribute // ʵжȡϢ - -// }; -// var sqlSugarClient = new SqlSugar.SqlSugarClient(connectionConfig); -// // sqlSugarClient.CodeFirst.InitTables(typeof(Code_Currency)); - -// return sqlSugarClient; -//}); -// ע MediatR -//var assembly = AppDomain.CurrentDomain.Load("Ds.WMS.Finance.MediatR"); - -//builder.Services.AddMediatR(c => -//{ -// c.RegisterServicesFromAssembly(assembly); -//}); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); @@ -44,8 +20,9 @@ builder.Services.AddSwaggerGen(c => var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); c.IncludeXmlComments(xmlPath); }); +// ʹö̬ +builder.UseDynamicStartups(); -//builder.Services.AddMediatR(Assembly.GetExecutingAssembly()); var app = builder.Build(); app.UseDynamicStartups(); diff --git a/ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs b/ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs index 4fd5736e..1af12b52 100644 --- a/ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs +++ b/ds-wms-service/Ds.WMS.Finance.MediatR/MediatRAppStartup.cs @@ -12,7 +12,6 @@ namespace Ds.WMS.Finance.MediatR { } - public void ConfigureServices(WebApplicationBuilder builder) { builder.Services.AddSqlsugarInstall(); @@ -22,6 +21,14 @@ namespace Ds.WMS.Finance.MediatR { c.RegisterServicesFromAssembly(assembly); }); + + //builder.Services.AddSwaggerGen(c => + // { + // c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); + // var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + // var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); + // c.IncludeXmlComments(xmlPath); + // }); } } } diff --git a/ds-wms-service/Ds.WMS.WebCore/LoadAndExecuteAssembly.cs b/ds-wms-service/Ds.WMS.WebCore/LoadAndExecuteAssembly.cs new file mode 100644 index 00000000..a5d20b7c --- /dev/null +++ b/ds-wms-service/Ds.WMS.WebCore/LoadAndExecuteAssembly.cs @@ -0,0 +1,35 @@ +using System.Runtime.Loader; +namespace Ds.WMS.WebCore +{ + public class LoadAndExecuteAssembly + { + + public static void LoadAndExecuteAssemblyF(string assemblyPath) + { + // 创建一个新的AssemblyLoadContext实例来加载程序集 + var assemblyLoadContext = new AssemblyLoadContext(null, isCollectible: true); + + // 使用AssemblyLoadContext.LoadFromAssemblyPath方法来加载程序集 + var assembly = assemblyLoadContext.LoadFromAssemblyPath(assemblyPath); + + // 获取程序集中的类型 + var types = assembly.GetTypes(); + + // 遍历类型并执行某些操作 + foreach (var type in types) + { + // 这里只是一个示例,你可以根据需要执行其他操作 + // 假设我们有一个名为"Execute"的无参数方法 + var method = type.GetMethod("Execute"); + if (method != null) + { + var instance = Activator.CreateInstance(type); + method.Invoke(instance, null); + } + } + + // 卸载程序集 + assemblyLoadContext.Unload(); + } + } +} diff --git a/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs b/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs index ec867c07..09bad0a9 100644 --- a/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs +++ b/ds-wms-service/Ds.WMS.WebCore/WebApplicationBuilderExtensions.cs @@ -2,38 +2,22 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using System.Reflection; -using System.Runtime.Loader; namespace Ds.WMS.WebCore { + /// + /// + /// public static class WebApplicationBuilderExtensions { public static WebApplicationBuilder UseDynamicStartups(this WebApplicationBuilder builder) { - - var assembly = Assembly.Load("DS.WMS.FinanceApi"); - var types = assembly.GetTypes() - .Where(t => t.GetCustomAttributes().Any()) - .ToList(); - - var dynamictups = AssemblyLoadContext.Default.Assemblies - .SelectMany(a => a.GetTypes()) - .Select(t => new - { - Type = t, - Attribute = t.GetCustomAttribute() - }) - .Where(x => x.Attribute != null) - .OrderBy(x => x.Attribute.Order) - .Select(x => Activator.CreateInstance(x.Type)) - .OfType().ToList(); - - var dynamicStartups = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) - .Select(t => new + .Select(t => + new { Type = t, Attribute = t.GetCustomAttribute() @@ -43,7 +27,26 @@ namespace Ds.WMS.WebCore .Select(x => Activator.CreateInstance(x.Type)) .OfType(); - + // var list = AssemblyLoadContext.Default.Assemblies.SelectMany(a => a.GetTypes()) + //.Select(t => new + //{ + // Type = t, + // Attribute = t.GetCustomAttribute() + //}) + //// .Where(x => x.Attribute != null) + // .OfType() + //.ToList(); + // var dynamicStartups = AssemblyLoadContext.Default.Assemblies + //.SelectMany(a => a.GetTypes()) + //.Select(t => new + //{ + // Type = t, + // Attribute = t.GetCustomAttribute() + //}) + //.Where(x => x.Attribute != null) + //.OrderBy(x => x.Attribute.Order) + //.Select(x => Activator.CreateInstance(x.Type)) + //.OfType(); foreach (var startup in dynamicStartups) { startup.ConfigureServices(builder);