2024-06-28 00:14:15 +08:00
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
using Mapster;
|
2025-02-01 21:53:05 +08:00
|
|
|
|
using Medallion.Threading;
|
2024-01-25 10:06:32 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-02-26 13:42:37 +08:00
|
|
|
|
using Microsoft.AspNetCore.RateLimiting;
|
2024-01-25 10:06:32 +08:00
|
|
|
|
using Volo.Abp.Application.Services;
|
2025-02-01 21:53:05 +08:00
|
|
|
|
using Volo.Abp.DistributedLocking;
|
2025-02-22 15:26:00 +08:00
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
|
|
|
|
|
using Volo.Abp.MultiTenancy;
|
2024-06-27 18:37:49 +08:00
|
|
|
|
using Volo.Abp.Settings;
|
2024-01-25 01:43:00 +08:00
|
|
|
|
using Volo.Abp.Uow;
|
2024-01-25 10:06:32 +08:00
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.Dtos.Banner;
|
2024-01-25 01:43:00 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities.Forum;
|
2024-01-25 10:06:32 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Authorization;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Extensions;
|
2024-06-27 18:37:49 +08:00
|
|
|
|
using Yi.Framework.SettingManagement.Domain;
|
2024-01-25 01:43:00 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2023-12-14 14:15:56 +08:00
|
|
|
|
|
2023-12-15 23:44:35 +08:00
|
|
|
|
namespace Yi.Abp.Application.Services
|
2023-12-14 14:15:56 +08:00
|
|
|
|
{
|
2024-01-25 01:43:00 +08:00
|
|
|
|
/// <summary>
|
2024-01-25 10:06:32 +08:00
|
|
|
|
/// 常用魔改及扩展示例
|
2024-01-25 01:43:00 +08:00
|
|
|
|
/// </summary>
|
2023-12-14 14:15:56 +08:00
|
|
|
|
public class TestService : ApplicationService
|
|
|
|
|
|
{
|
2024-01-25 10:20:46 +08:00
|
|
|
|
/// <summary>
|
2024-01-25 10:26:10 +08:00
|
|
|
|
/// 属性注入
|
|
|
|
|
|
/// 不推荐,坑太多,容易把自己玩死,简单的东西可以用一用
|
2024-01-25 10:20:46 +08:00
|
|
|
|
/// </summary>
|
2024-05-22 14:35:08 +08:00
|
|
|
|
public ISqlSugarRepository<BannerAggregateRoot> sqlSugarRepository { get; set; }
|
2024-01-25 10:06:32 +08:00
|
|
|
|
|
2023-12-14 14:15:56 +08:00
|
|
|
|
/// <summary>
|
2024-01-25 10:06:32 +08:00
|
|
|
|
/// 动态Api
|
2023-12-14 14:15:56 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-01-25 10:06:32 +08:00
|
|
|
|
[HttpGet("hello-world")]
|
2023-12-14 14:15:56 +08:00
|
|
|
|
public string GetHelloWorld(string? name)
|
|
|
|
|
|
{
|
2024-01-25 10:06:32 +08:00
|
|
|
|
//会自动添加前缀,而不是重置,更符合习惯
|
|
|
|
|
|
//如果需要重置以"/"根目录开头即可
|
|
|
|
|
|
//你好世界
|
2023-12-14 14:15:56 +08:00
|
|
|
|
return name ?? "HelloWord";
|
|
|
|
|
|
}
|
2024-01-25 01:43:00 +08:00
|
|
|
|
|
2024-09-01 03:06:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异常处理
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet("error")]
|
|
|
|
|
|
public string GetError()
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("业务异常");
|
|
|
|
|
|
throw new Exception("系统异常");
|
|
|
|
|
|
}
|
2025-02-01 21:53:05 +08:00
|
|
|
|
|
2024-01-25 10:06:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// SqlSugar
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<object> GetSqlSugarDbAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
//用户体验优先,可直接使用Db操作,依赖抽象
|
|
|
|
|
|
return await sqlSugarRepository._DbQueryable.ToListAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-25 01:43:00 +08:00
|
|
|
|
/// <summary>
|
2024-01-25 10:26:10 +08:00
|
|
|
|
/// 工作单元
|
2024-01-25 01:43:00 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task GetUowAsync()
|
|
|
|
|
|
{
|
2024-01-25 10:26:10 +08:00
|
|
|
|
//魔改
|
2024-11-30 23:45:19 +08:00
|
|
|
|
// 用户体验优先,万金油模式,支持高并发。支持单、多线程并发安全,支持多线程工作单元,支持。。。
|
|
|
|
|
|
// 不支持多线程无工作单元,应由工作单元统一管理(来自abp工作单元设计)
|
2024-08-03 22:31:30 +08:00
|
|
|
|
// 请注意,如果requiresNew: true只有在没有工作单元内使用,嵌套子工作单元,默认值false即可
|
2024-01-25 10:06:32 +08:00
|
|
|
|
// 自动在各个情况处理db客户端最优解之一
|
2024-06-11 22:34:36 +08:00
|
|
|
|
int i = 3;
|
2024-01-25 01:43:00 +08:00
|
|
|
|
List<Task> tasks = new List<Task>();
|
2024-06-11 22:34:36 +08:00
|
|
|
|
await sqlSugarRepository.GetListAsync();
|
|
|
|
|
|
await sqlSugarRepository.GetListAsync();
|
2024-01-25 01:43:00 +08:00
|
|
|
|
while (i > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
tasks.Add(Task.Run(async () =>
|
|
|
|
|
|
{
|
2024-11-30 23:45:19 +08:00
|
|
|
|
//以下操作是错误的,不允许在新线程中,直接操作db,所有db操作应放在工作单元内,应由工作单元统一管理-来自abp工作单元设计
|
|
|
|
|
|
//await sqlSugarRepository.InsertAsync(new BannerAggregateRoot { Name = "插入2" });
|
2024-01-25 10:06:32 +08:00
|
|
|
|
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: true))
|
2024-01-25 01:43:00 +08:00
|
|
|
|
{
|
2024-05-22 14:35:08 +08:00
|
|
|
|
await sqlSugarRepository.InsertAsync(new BannerAggregateRoot { Name = "插入1" });
|
2024-01-25 01:43:00 +08:00
|
|
|
|
await uow.CompleteAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
2024-05-22 14:35:08 +08:00
|
|
|
|
await sqlSugarRepository.InsertAsync(new BannerAggregateRoot { Name = "插入3" });
|
2024-01-25 01:43:00 +08:00
|
|
|
|
i--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
|
|
}
|
2024-01-25 10:06:32 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 当前用户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public void GetCurrentUser()
|
|
|
|
|
|
{
|
|
|
|
|
|
//当token鉴权之后,可以直接获取
|
|
|
|
|
|
if (CurrentUser.Id is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//权限
|
|
|
|
|
|
CurrentUser.GetPermissions();
|
|
|
|
|
|
|
|
|
|
|
|
//角色信息
|
|
|
|
|
|
CurrentUser.GetRoleInfo();
|
|
|
|
|
|
|
|
|
|
|
|
//部门id
|
|
|
|
|
|
CurrentUser.GetDeptId();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 数据权限
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void GetDataFilter()
|
|
|
|
|
|
{
|
|
|
|
|
|
//这里会数据权限过滤
|
|
|
|
|
|
using (DataFilter.DisablePermissionHandler())
|
|
|
|
|
|
{
|
|
|
|
|
|
//这里不会数据权限过滤
|
|
|
|
|
|
}
|
|
|
|
|
|
//这里会数据权限过滤
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对象映射
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void GetMapper()
|
|
|
|
|
|
{
|
|
|
|
|
|
//直接无脑Adapt,无需配置
|
2024-05-22 14:35:08 +08:00
|
|
|
|
var entity = new BannerAggregateRoot();
|
2024-01-25 10:06:32 +08:00
|
|
|
|
var dto = entity.Adapt<BannerGetListOutputDto>();
|
|
|
|
|
|
}
|
2024-02-26 13:42:37 +08:00
|
|
|
|
|
2024-06-27 18:37:49 +08:00
|
|
|
|
private static int RequestNumber { get; set; } = 0;
|
2025-02-01 21:53:05 +08:00
|
|
|
|
|
2024-02-26 13:42:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 速率限制
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
// [DisableRateLimiting]
|
|
|
|
|
|
//[EnableRateLimiting("sliding")]
|
|
|
|
|
|
public int GetRateLimiting()
|
|
|
|
|
|
{
|
|
|
|
|
|
RequestNumber++;
|
|
|
|
|
|
return RequestNumber;
|
|
|
|
|
|
}
|
2024-06-27 18:37:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ISettingProvider _settingProvider { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public ISettingManager _settingManager { get; set; }
|
2025-02-01 21:53:05 +08:00
|
|
|
|
|
2024-06-27 18:37:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 系统配置模块
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<string> GetSettingAsync()
|
|
|
|
|
|
{
|
2024-06-27 22:39:09 +08:00
|
|
|
|
//DDD需要提前定义
|
2024-06-27 18:37:49 +08:00
|
|
|
|
//默认来说,不提供修改操作,配置应该独立
|
2024-06-27 22:39:09 +08:00
|
|
|
|
var enableOrNull = await _settingProvider.GetOrNullAsync("DDD");
|
2024-06-27 18:37:49 +08:00
|
|
|
|
|
|
|
|
|
|
//如果要进行修改,可使用yi.framework下的ISettingManager
|
2024-06-27 22:39:09 +08:00
|
|
|
|
await _settingManager.SetGlobalAsync("DDD", "false");
|
2024-06-27 18:37:49 +08:00
|
|
|
|
|
2024-06-27 22:39:09 +08:00
|
|
|
|
var enableOrNull2 = await _settingManager.GetOrNullGlobalAsync("DDD");
|
2024-06-27 18:37:49 +08:00
|
|
|
|
|
2024-06-27 22:39:09 +08:00
|
|
|
|
//当然,他的独特地方,是支持来自多个模块,例如配置文件?
|
2024-06-28 00:14:15 +08:00
|
|
|
|
var result = await _settingManager.GetOrNullConfigurationAsync("Test");
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-06-27 22:39:09 +08:00
|
|
|
|
return result ?? string.Empty;
|
2024-06-27 18:37:49 +08:00
|
|
|
|
}
|
2025-02-01 21:53:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分布式送abp版本:abp套了一层娃。但是纯粹鸡肋,不建议使用这个
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public IAbpDistributedLock AbpDistributedLock { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分布式锁推荐使用版本:yyds,分布式锁永远的神!
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public IDistributedLockProvider DistributedLock { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分布式锁
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>强烈吐槽一下abp,正如他们所说,abp的分布式锁单纯为了自己用,一切还是以DistributedLock为主</remarks>>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<string> GetDistributedLockAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var number = 0;
|
|
|
|
|
|
await Parallel.ForAsync(0, 100, async (i, cancellationToken) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await using (await DistributedLock.AcquireLockAsync("MyLockName"))
|
|
|
|
|
|
{
|
|
|
|
|
|
//执行1秒
|
|
|
|
|
|
number += 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
var number2 = 0;
|
|
|
|
|
|
await Parallel.ForAsync(0, 100, async (i, cancellationToken) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//执行1秒
|
|
|
|
|
|
number2 += 1;
|
|
|
|
|
|
});
|
|
|
|
|
|
return $"加锁结果:{number},不加锁结果:{number2}";
|
|
|
|
|
|
}
|
2025-02-22 15:26:00 +08:00
|
|
|
|
|
|
|
|
|
|
public ICurrentTenant CurrentTenant { get; set; }
|
|
|
|
|
|
public IRepository<BannerAggregateRoot> repository { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 多租户
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<string> GetMultiTenantAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var uow=UnitOfWorkManager.Begin())
|
|
|
|
|
|
{
|
|
|
|
|
|
//此处会实例化一个db,连接默认库
|
|
|
|
|
|
var defautTenantData1= await repository.GetListAsync();
|
|
|
|
|
|
using (CurrentTenant.Change(null,"Default"))
|
|
|
|
|
|
{
|
|
|
|
|
|
var defautTenantData2= await repository.GetListAsync();
|
|
|
|
|
|
await repository.InsertAsync(new BannerAggregateRoot
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = "default",
|
|
|
|
|
|
});
|
|
|
|
|
|
var defautTenantData3= await repository.GetListAsync(x=>x.Name=="default");
|
|
|
|
|
|
}
|
|
|
|
|
|
//此处会实例化一个新的db连接MES
|
|
|
|
|
|
using (CurrentTenant.Change(null,"Mes"))
|
|
|
|
|
|
{
|
|
|
|
|
|
var otherTenantData1= await repository.GetListAsync();
|
|
|
|
|
|
await repository.InsertAsync(new BannerAggregateRoot
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = "Mes1",
|
|
|
|
|
|
});
|
|
|
|
|
|
var otherTenantData2= await repository.GetListAsync(x=>x.Name=="Mes1");
|
|
|
|
|
|
}
|
|
|
|
|
|
//此处会复用Mesdb,不会实例化新的db
|
|
|
|
|
|
using (CurrentTenant.Change(Guid.Parse("33333333-3d72-4339-9adc-845151f8ada0")))
|
|
|
|
|
|
{
|
|
|
|
|
|
var otherTenantData1= await repository.GetListAsync();
|
|
|
|
|
|
await repository.InsertAsync(new BannerAggregateRoot
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = "Mes2",
|
|
|
|
|
|
});
|
|
|
|
|
|
var otherTenantData2= await repository.GetListAsync(x=>x.Name=="Mes2");
|
|
|
|
|
|
}
|
|
|
|
|
|
//此处会将多库进行一起提交,前面的操作有报错,全部回滚
|
|
|
|
|
|
await uow.CompleteAsync();
|
|
|
|
|
|
return "根据租户切换不同的数据库,并管理db实例连接,涉及多库事务统一到最后提交";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-12-14 14:15:56 +08:00
|
|
|
|
}
|
2025-02-01 21:53:05 +08:00
|
|
|
|
}
|