You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.3 KiB
C#

using DS.Module.Core;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Flow.Entity;
using DS.WMS.Core.Sys.Interface;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace DS.WMS.Core
{
/// <summary>
/// 服务基类
/// </summary>
public abstract class ServiceBase
{
/// <summary>
/// 获取服务容器
/// </summary>
protected IServiceProvider ServiceProvider { get; private set; }
/// <summary>
/// 获取主库访问对象
/// </summary>
protected ISqlSugarClient Db { get; private set; }
/// <summary>
/// 获取业务库访问对象
/// </summary>
readonly ISaasDbService saasService;
readonly Lazy<ICommonService> commonService;
SqlSugarScopeProvider? _tenantDb;
/// <summary>
/// 获取业务库访问对象
/// </summary>
protected SqlSugarScopeProvider TenantDb
{
get
{
_tenantDb ??= saasService.GetBizDbScopeById(User.TenantId);
return _tenantDb;
}
}
readonly IUser _user;
/// <summary>
/// 获取用户相关信息
/// </summary>
public virtual IUser User => _user;
/// <summary>
/// 当前用户ID
/// </summary>
public readonly long UserId;
/// <summary>
/// 日志记录对象
/// </summary>
public Lazy<ILogger<ServiceBase>> Logger { get; private set; }
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider">服务提供程序</param>
protected ServiceBase(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
_user = serviceProvider.GetRequiredService<IUser>();
if (_user != null)
UserId = long.Parse(User.UserId);
Db = serviceProvider.GetRequiredService<ISqlSugarClient>();
saasService = serviceProvider.GetRequiredService<ISaasDbService>();
commonService = new Lazy<ICommonService>(serviceProvider.GetRequiredService<ICommonService>());
Logger = new Lazy<ILogger<ServiceBase>>(serviceProvider.GetRequiredService<ILogger<ServiceBase>>());
}
/// <summary>
/// 获取查询对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <returns></returns>
protected internal ISugarQueryable<T> AsQueryable<T>() where T : class
{
(ISugarQueryable<T> query, _) = commonService.Value.GetVisibleDataRuleFilter<T>(TenantDb)
.GetAwaiter().GetResult();
return query;
}
/// <summary>
/// 查找审批模板
/// </summary>
/// <param name="auditType">审批类型</param>
/// <returns></returns>
protected internal async Task<FlowTemplate> FindTemplateAsync(TaskBaseTypeEnum auditType)
{
long tid = long.Parse(User.TenantId);
return await TenantDb.Queryable<FlowTemplate>().OrderByDescending(x => x.CreateTime).Take(1)
.FirstAsync(x => x.Status == StatusEnum.Enable && x.AuditType == auditType);
}
}
}