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
{
///
/// 服务基类
///
public abstract class ServiceBase
{
///
/// 获取服务容器
///
protected IServiceProvider ServiceProvider { get; private set; }
///
/// 获取主库访问对象
///
protected ISqlSugarClient Db { get; private set; }
///
/// 获取业务库访问对象
///
protected ISaasDbService SaasService { get; private set; }
readonly Lazy commonService;
SqlSugarScopeProvider? _tenantDb;
///
/// 获取业务库访问对象
///
protected SqlSugarScopeProvider TenantDb
{
get
{
_tenantDb ??= SaasService.GetBizDbScopeById(User.TenantId);
return _tenantDb;
}
}
IUser _user;
///
/// 获取用户相关信息
///
public virtual IUser User => _user;
///
/// 日志记录对象
///
public Lazy> Logger { get; private set; }
///
/// 初始化
///
/// 服务提供程序
protected ServiceBase(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
_user = serviceProvider.GetRequiredService();
Db = serviceProvider.GetRequiredService();
SaasService = serviceProvider.GetRequiredService();
commonService = new Lazy(serviceProvider.GetRequiredService());
Logger = new Lazy>(serviceProvider.GetRequiredService>());
}
///
/// 获取查询对象
///
/// 对象类型
///
protected internal ISugarQueryable AsQueryable() where T : class
{
(ISugarQueryable query, _) = commonService.Value.GetVisibleDataRuleFilter(TenantDb)
.GetAwaiter().GetResult();
return query;
}
///
/// 查找审批模板
///
/// 审批类型
///
protected internal async Task FindTemplateAsync(TaskBaseTypeEnum auditType)
{
long tid = long.Parse(User.TenantId);
return await TenantDb.Queryable().OrderByDescending(x => x.CreateTime).Take(1)
.FirstAsync(x => x.Status == StatusEnum.Enable && x.AuditType == auditType);
}
}
}