using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using YuanXuan.IM.Common.Entities;
using YuanXuan.IM.Common.Request;
using YuanXuan.IM.Core.Interfaces;
using YuanXuan.IM.Infrastructure.DBContext;
namespace YuanXuan.IM.Core.Services
{
///
/// 服务基类
///
//[Inject(Lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped)]
public class BaseService : IBaseService where TEntity : BaseEntity, new()
{
//protected readonly SugarRepository _db;
private readonly SugarRepository _db;
public BaseService(SugarRepository repository)
{
_db = repository;
}
///
/// 创建(自增ID)
///
///
///
public virtual async Task CreateAsync(TEntity entity)
{
return await _db.InsertAsync(entity);
}
///
/// 创建
///
///
///
public virtual async Task CreateAsync(List entity)
{
return await _db.InsertRangeAsync(entity);
}
///
/// 创建(自增ID)
///
///
///
public virtual async Task InsertReturnBigIdentityAsync(TEntity entity)
{
return await _db.InsertReturnBigIdentityAsync(entity);
}
///
/// 创建并返回雪花ID
///
///
///
public virtual async Task CreateReturnSnowflakeIdAsync(TEntity entity)
{
return await _db.InsertReturnSnowflakeIdAsync(entity);
}
///
/// 创建并返回雪花ID
///
///
///
public virtual async Task> CreateReturnSnowflakeIdAsync(List entity)
{
return await _db.InsertReturnSnowflakeIdAsync(entity);
}
///
/// 物理删除
///
///
///
public virtual async Task DeleteByIdsAsync(params dynamic[] ids)
{
return await _db.DeleteByIdsAsync(ids);
}
///
/// 物理删除
///
///
///
public virtual async Task DeleteByIdsAsync(params long[] ids)
{
return await _db.Context.Deleteable().Where(x => ids.Contains(x.Id)).ExecuteCommandHasChangeAsync();
}
///
/// 更新
///
///
///
public virtual async Task UpdateAsync(TEntity entity)
{
return await _db.UpdateAsync(entity);
}
///
/// 批量更新
///
///
///
public virtual async Task UpdateAsync(List entity)
{
return await _db.UpdateRangeAsync(entity);
}
///
/// 更新指定字段
///
///
///
///
public virtual async Task UpdateColumsAsync(Expression> columns, Expression> whereExpression)
{
return await _db.UpdateAsync(columns, whereExpression);
}
///
/// 根据ID获取
///
///
///
public virtual async Task GetByIdAsync(long id)
{
return await _db.GetByIdAsync(id);
}
///
/// 根据ID获取
///
///
///
public virtual async Task> GetListAsync(Expression> whereExpression = null)
{
if (whereExpression == null)
{
return await _db.GetListAsync();
}
return await _db.GetListAsync(whereExpression);
}
///
/// 根据条件获取单个
///
///
///
public virtual async Task GetSingleAsync(Expression> whereExpression)
{
return await _db.GetSingleAsync(whereExpression);
}
///
/// 根据条件获取第一个
///
///
///
public virtual async Task GetFirstAsync(Expression> whereExpression)
{
return await _db.GetFirstAsync(whereExpression);
}
public virtual async Task> GetPageListAsync(PageRequest pagination, Expression> whereExpression, Expression> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
{
var pageModel = pagination.Adapt();
List result = null;
if (whereExpression != null)
result = await _db.GetPageListAsync(whereExpression, pageModel, orderByExpression, orderByType);
else
result = await _db.GetPageListAsync(x => 1 == 1, pageModel, orderByExpression, orderByType);
return new PageResponse
{
Items = result,
Total = pageModel.TotalCount,
};
}
///
/// 分页查询
///
///
///
///
public virtual async Task> GetPageListAsync(PageRequest pagination, Expression> whereExpression = null)
{
var pageModel = pagination.Adapt();
List result = null;
if (whereExpression != null)
result = await _db.GetPageListAsync(whereExpression, pageModel, orderByExpression: x => x.Id, orderByType: OrderByType.Desc);
else
result = await _db.GetPageListAsync(x => 1 == 1, pageModel, orderByExpression: x => x.Id, orderByType: OrderByType.Desc);
return new PageResponse
{
Items = result,
Total = pageModel.TotalCount,
};
}
///
/// 分页查询
///
///
///
///
public virtual async Task> GetPageListAsync(PageRequest pagination, Expression> whereExpression)
{
var pageModel = pagination.Adapt();
List result = null;
if (whereExpression != null)
result = await _db.GetPageListAsync(whereExpression, pageModel, orderByExpression: x => x.Id, orderByType: OrderByType.Desc);
else
result = await _db.GetPageListAsync(x => 1 == 1, pageModel, orderByExpression: x => x.Id, orderByType: OrderByType.Desc);
return new PageResponse
{
Items = result.Adapt>(),
Total = pageModel.TotalCount,
};
}
public virtual async Task AnyAsync(Expression> whereExpression)
{
return await _db.IsAnyAsync(whereExpression);
}
//protected async Task BeginTransaction(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
//{
// return await _db.Context.AsTenant().UseTranAsync(isolationLevel);
//}
//// 针对不同业务场景使用不同隔离级别
//protected async SqlSugarTransaction BeginSerializableTransaction()
//{
// return BeginTransaction(IsolationLevel.Serializable);
//}
//protected async SqlSugarTransaction BeginRepeatableReadTransaction()
//{
// return BeginTransaction(IsolationLevel.RepeatableRead);
//}
}
}