255 lines
9.0 KiB
C#
255 lines
9.0 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 服务基类
|
|
/// </summary>
|
|
//[Inject(Lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped)]
|
|
public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : BaseEntity, new()
|
|
{
|
|
//protected readonly SugarRepository<TEntity> _db;
|
|
private readonly SugarRepository<TEntity> _db;
|
|
|
|
|
|
public BaseService(SugarRepository<TEntity> repository)
|
|
{
|
|
_db = repository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建(自增ID)
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> CreateAsync(TEntity entity)
|
|
{
|
|
return await _db.InsertAsync(entity);
|
|
}
|
|
/// <summary>
|
|
/// 创建
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> CreateAsync(List<TEntity> entity)
|
|
{
|
|
return await _db.InsertRangeAsync(entity);
|
|
}
|
|
/// <summary>
|
|
/// 创建(自增ID)
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<long> InsertReturnBigIdentityAsync(TEntity entity)
|
|
{
|
|
return await _db.InsertReturnBigIdentityAsync(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建并返回雪花ID
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<long> CreateReturnSnowflakeIdAsync(TEntity entity)
|
|
{
|
|
return await _db.InsertReturnSnowflakeIdAsync(entity);
|
|
}
|
|
/// <summary>
|
|
/// 创建并返回雪花ID
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<List<long>> CreateReturnSnowflakeIdAsync(List<TEntity> entity)
|
|
{
|
|
return await _db.InsertReturnSnowflakeIdAsync(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 物理删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> DeleteByIdsAsync(params dynamic[] ids)
|
|
{
|
|
return await _db.DeleteByIdsAsync(ids);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 物理删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> DeleteByIdsAsync(params long[] ids)
|
|
{
|
|
return await _db.Context.Deleteable<TEntity>().Where(x => ids.Contains(x.Id)).ExecuteCommandHasChangeAsync();
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 更新
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> UpdateAsync(TEntity entity)
|
|
{
|
|
return await _db.UpdateAsync(entity);
|
|
}
|
|
/// <summary>
|
|
/// 批量更新
|
|
/// </summary>
|
|
/// <param name="entity"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> UpdateAsync(List<TEntity> entity)
|
|
{
|
|
return await _db.UpdateRangeAsync(entity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新指定字段
|
|
/// </summary>
|
|
/// <param name="columns"></param>
|
|
/// <param name="whereExpression"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<bool> UpdateColumsAsync(Expression<Func<TEntity, TEntity>> columns, Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
return await _db.UpdateAsync(columns, whereExpression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> GetByIdAsync(long id)
|
|
{
|
|
return await _db.GetByIdAsync(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ID获取
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> whereExpression = null)
|
|
{
|
|
if (whereExpression == null)
|
|
{
|
|
return await _db.GetListAsync();
|
|
}
|
|
return await _db.GetListAsync(whereExpression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件获取单个
|
|
/// </summary>
|
|
/// <param name="whereExpression"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> GetSingleAsync(Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
return await _db.GetSingleAsync(whereExpression);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件获取第一个
|
|
/// </summary>
|
|
/// <param name="whereExpression"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<TEntity> GetFirstAsync(Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
return await _db.GetFirstAsync(whereExpression);
|
|
}
|
|
public virtual async Task<PageResponse<TEntity>> GetPageListAsync<T>(PageRequest pagination, Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
|
|
{
|
|
var pageModel = pagination.Adapt<PageModel>();
|
|
List<TEntity> 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<TEntity>
|
|
{
|
|
Items = result,
|
|
Total = pageModel.TotalCount,
|
|
};
|
|
}
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="whereExpression"></param>
|
|
/// <param name="pagination"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<PageResponse<TEntity>> GetPageListAsync(PageRequest pagination, Expression<Func<TEntity, bool>> whereExpression = null)
|
|
{
|
|
var pageModel = pagination.Adapt<PageModel>();
|
|
|
|
List<TEntity> 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<TEntity>
|
|
{
|
|
Items = result,
|
|
Total = pageModel.TotalCount,
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="whereExpression"></param>
|
|
/// <param name="pagination"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task<PageResponse<T>> GetPageListAsync<T>(PageRequest pagination, Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
var pageModel = pagination.Adapt<PageModel>();
|
|
|
|
List<TEntity> 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<T>
|
|
{
|
|
Items = result.Adapt<List<T>>(),
|
|
Total = pageModel.TotalCount,
|
|
};
|
|
}
|
|
public virtual async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> whereExpression)
|
|
{
|
|
return await _db.IsAnyAsync(whereExpression);
|
|
}
|
|
//protected async Task<SqlSugarTransaction> 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);
|
|
//}
|
|
|
|
}
|
|
}
|