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 SqlSugar;
namespace DS.WMS.Core
{
///
/// 服务基类
///
public abstract class ServiceBase
{
///
/// 获取服务容器
///
protected IServiceProvider ServiceProvider { get; private set; }
///
/// 获取用户相关信息
///
protected IUser User { get; private set; }
///
/// 获取主库访问对象
///
protected ISqlSugarClient Db { get; private set; }
///
/// 获取业务库访问对象
///
protected ISaasDbService SaasService { get; private set; }
readonly Lazy commonService;
SqlSugarScopeProvider? _tenantDb;
///
/// 获取业务库访问对象
///
protected SqlSugarScopeProvider TenantDb
{
get
{
if (_tenantDb == null)
_tenantDb = SaasService.GetBizDbScopeById(User.TenantId);
return _tenantDb;
}
}
///
/// 初始化
///
/// 服务提供程序
protected ServiceBase(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
User = serviceProvider.GetRequiredService();
Db = serviceProvider.GetRequiredService();
SaasService = serviceProvider.GetRequiredService();
commonService = 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);
}
}
}