361 lines
13 KiB
C#
361 lines
13 KiB
C#
using AutoMapper;
|
|||
|
|
using JSMachine.WMS.App.Dto;
|
||
|
|
using JSMachine.WMS.App.IService;
|
||
|
|
using JSMachine.WMS.Domain.Entity;
|
||
|
|
using JSMachine.WMS.Domain.IRepository;
|
||
|
|
using JSMachine.WMS.Domain.Repository;
|
||
|
|
using JSMachine.WMS.Infrastructure;
|
||
|
|
using JSMachine.WMS.Infrastructure.LambdaHelp;
|
||
|
|
using SqlSugar;
|
||
|
|
using System.Data;
|
||
|
|
using System.Linq.Expressions;
|
||
|
|
|
||
|
|
namespace JSMachine.WMS.App.ServiceImpl
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 应用层通用服务基类,负责 DTO 与领域实体之间的映射,
|
||
|
|
/// 并将增删改查请求委托给对应的领域仓储。
|
||
|
|
/// </summary>
|
||
|
|
/// <typeparam name="TDTO">应用层数据传输对象类型。</typeparam>
|
||
|
|
/// <typeparam name="TAggregateRoot">领域聚合根类型。</typeparam>
|
||
|
|
public class BaseService<TDTO, TAggregateRoot> : IBaseService<TDTO, TAggregateRoot>
|
||
|
|
where TAggregateRoot : AggregateRoot, IAggregateRoot
|
||
|
|
where TDTO : IRootDto
|
||
|
|
{
|
||
|
|
private IBaseRepository<TAggregateRoot> IBaseRepository;
|
||
|
|
private static IMapper _mapper = GlobalServericeProvidor.GetService<IMapper>();
|
||
|
|
public BaseService(IBaseRepository<TAggregateRoot> baseRepository)
|
||
|
|
{
|
||
|
|
IBaseRepository = baseRepository;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 添加单个 DTO;当 DTO 未提供标识时生成新标识,并维护创建时间和修改时间。
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="dto">待保存的数据传输对象。</param>
|
||
|
|
/// <param name="sqlSugarScope">可选的数据库作用域;为空时使用仓储默认作用域。</param>
|
||
|
|
/// <returns>数据库插入是否成功。</returns>
|
||
|
|
public async virtual Task<bool> Add(TDTO dto, SqlSugarScope sqlSugarScope=null)
|
||
|
|
{
|
||
|
|
if (dto.Id == Guid.Empty)
|
||
|
|
dto.Id = Guid.NewGuid();
|
||
|
|
dto.CreationTime = DateTime.Now;
|
||
|
|
dto.LastModifiedTime = DateTime.Now;
|
||
|
|
|
||
|
|
return await IBaseRepository.Add(_mapper.Map<TAggregateRoot>(dto),sqlSugarScope);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async virtual Task<bool> Add(TDTO dto, params string[] ignoreColumns)
|
||
|
|
{
|
||
|
|
if (dto.Id == Guid.Empty)
|
||
|
|
dto.Id = Guid.NewGuid();
|
||
|
|
dto.CreationTime = DateTime.Now;
|
||
|
|
dto.LastModifiedTime = DateTime.Now;
|
||
|
|
|
||
|
|
return await IBaseRepository.Add(_mapper.Map<TAggregateRoot>(dto), ignoreColumns);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async virtual Task<bool> Add(List<TDTO> entities)
|
||
|
|
{
|
||
|
|
if (entities == null || entities.Count == 0)
|
||
|
|
return false;
|
||
|
|
|
||
|
|
entities.ForEach(p =>
|
||
|
|
{
|
||
|
|
|
||
|
|
if (p.Id == Guid.Empty)
|
||
|
|
p.Id = Guid.NewGuid();
|
||
|
|
|
||
|
|
p.CreationTime = DateTime.Now;
|
||
|
|
p.LastModifiedTime = DateTime.Now;
|
||
|
|
});
|
||
|
|
|
||
|
|
return await IBaseRepository.Add(_mapper.Map<List<TAggregateRoot>>(entities));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> Add(string tableName, Dictionary<string, object> dic)
|
||
|
|
{
|
||
|
|
if (dic.ContainsKey("Id"))
|
||
|
|
dic.Add("Id", Guid.NewGuid().ToString("N"));
|
||
|
|
|
||
|
|
return await IBaseRepository.Add(tableName, dic);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 添加之前判断指定字段是否存在,不存在则添加
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="entity"></param>
|
||
|
|
/// <param name="expression"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public async Task<bool> AddIfNotExist(TDTO entity, Expression<Func<TDTO, bool>> expression)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.AddIfNotExist(_mapper.Map<TAggregateRoot>(entity),
|
||
|
|
_mapper.Map<Expression<Func<TAggregateRoot, bool>>>(expression));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<int> Count()
|
||
|
|
{
|
||
|
|
return await IBaseRepository.Count();
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<int> Count(Expression<Func<TDTO, bool>> expression)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.Count(_mapper.Map<Expression<Func<TAggregateRoot, bool>>>(expression));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> DeleByExpression(Expression<Func<TAggregateRoot, bool>> expression)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.DeleByExpression(expression);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> DeleteBatch(Guid[] ids)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.DeleteBatch(ids);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> DeleteByEntity(TDTO aggregateRoot)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.DeleteByEntity(_mapper.Map<TAggregateRoot>(aggregateRoot));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> DeleteByEntity(List<TDTO> aggregateRoot)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.DeleteByEntity(_mapper.Map<List<TAggregateRoot>>(aggregateRoot));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> DeleteById(Guid id, SqlSugarScope sqlSugarScope=null)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.DeleteById(id,sqlSugarScope);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> EditBatch(List<TDTO> dtos)
|
||
|
|
{
|
||
|
|
dtos.ForEach(p => p.LastModifiedTime = DateTime.Now);
|
||
|
|
|
||
|
|
return await IBaseRepository.EditBatch(_mapper.Map<List<TAggregateRoot>>(dtos));
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 快速批量更新
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="dtos"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public async Task<bool> EditBatchFast(List<TDTO> dtos)
|
||
|
|
{
|
||
|
|
dtos.ForEach(p => p.LastModifiedTime = DateTime.Now);
|
||
|
|
return await IBaseRepository.EditBatchFast(_mapper.Map<List<TAggregateRoot>>(dtos));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> EditByDic(string tableName, Dictionary<string, object> dic)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.EditByDic(tableName, dic);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> EditByDic(string tableName, List<Dictionary<string, object>> dic)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.EditByDic(tableName, dic);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async virtual Task<bool> EditSingal(TDTO aggregateRoot)
|
||
|
|
{
|
||
|
|
aggregateRoot.LastModifiedTime = DateTime.Now;
|
||
|
|
|
||
|
|
//更新的时候不能够更改数据的添加时间
|
||
|
|
return await IBaseRepository.EditSingal(_mapper.Map<TAggregateRoot>(aggregateRoot), new string[] { "CreationTime" });
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> EditSingal(TDTO dto, Expression<Func<TDTO, object>> ignoredColumns)
|
||
|
|
{
|
||
|
|
dto.LastModifiedTime = DateTime.Now;
|
||
|
|
return await IBaseRepository.EditSingal(_mapper.Map<TAggregateRoot>(dto), _mapper.Map<Expression<Func<TAggregateRoot, object>>>(ignoredColumns));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> EditSingal(TDTO dto, string[] ignoredColumns)
|
||
|
|
{
|
||
|
|
dto.LastModifiedTime = DateTime.Now;
|
||
|
|
return await IBaseRepository.EditSingal(_mapper.Map<TAggregateRoot>(dto), ignoredColumns);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> EditSingalWithSpecificCols(TDTO dto, string[] columns)
|
||
|
|
{
|
||
|
|
dto.LastModifiedTime = DateTime.Now;
|
||
|
|
return await IBaseRepository.EditSingalWithSpecificCols(_mapper.Map<TAggregateRoot>(dto), columns);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> Exist(Expression<Func<TDTO, bool>> expression)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.Exist(_mapper.Map<Expression<Func<TAggregateRoot, bool>>>(expression));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> GetAll()
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetAll();
|
||
|
|
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> GetAll(string orderByFiled, OrderByType orderByType)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetAll(LambdaHelper<TAggregateRoot>.GetOrderExpression<object>(orderByFiled),
|
||
|
|
orderByType);
|
||
|
|
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> GetAllFragment(int fragmentSize = 50000)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetAllFragment(fragmentSize);
|
||
|
|
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> GetAllFragment(string orderByFiled, OrderByType orderByType, int fragmentSize = 200)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetAllFragment(
|
||
|
|
LambdaHelper<TAggregateRoot>.GetOrderExpression<object>(orderByFiled),
|
||
|
|
orderByType,
|
||
|
|
fragmentSize);
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> GetBySql(string sql)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetBySql(sql);
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> GetBySql(string sql, Dictionary<string, object> sqlParam)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetBySql(sql, sqlParam);
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<DataTable> GetDataTableBysql(string sql)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.GetDataTableBysql(sql);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<DataTable> GetDataTableBysql(string sql, Dictionary<string, object> sqlParam)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.GetDataTableBysql(sql, sqlParam);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<dynamic>> GetDynamicsBysql(string sql)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.GetDynamicsBysql(sql);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<dynamic>> GetDynamicsBysql(string sql, Dictionary<string, object> sqlParam)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.GetDynamicsBysql(sql, sqlParam);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> GetListByExpression(Expression<Func<TDTO, bool>> expression)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetListByExpression(
|
||
|
|
_mapper.Map<Expression<Func<TAggregateRoot, bool>>>(expression));
|
||
|
|
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 查询满足条件的所有实体(带排序功能)
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="expression"></param>
|
||
|
|
/// <param name="orderByExpress"></param>
|
||
|
|
/// <param name="orderByType">Asc = 0,Desc = 1</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public async Task<List<TDTO>> GetListByExpression(Expression<Func<TAggregateRoot, bool>> expression,
|
||
|
|
string filedName,
|
||
|
|
OrderByType orderByType = OrderByType.Desc)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetListByExpression(
|
||
|
|
_mapper.Map<Expression<Func<TAggregateRoot, bool>>>(expression),
|
||
|
|
filedName,
|
||
|
|
orderByType);
|
||
|
|
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 通过实体查询满足条件的数据
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="dto"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public async Task<List<TDTO>> GetListWhereClass(TDTO dto)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.GetListWhereClass(
|
||
|
|
_mapper.Map<TAggregateRoot>(dto));
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<TDTO> GetSingalByExpression(Expression<Func<TDTO, bool>> expression)
|
||
|
|
{
|
||
|
|
TAggregateRoot data = await IBaseRepository.GetSingalByExpression(
|
||
|
|
_mapper.Map<Expression<Func<TAggregateRoot, bool>>>(expression));
|
||
|
|
|
||
|
|
return _mapper.Map<TDTO>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<TDTO> GetSingalByExpression(
|
||
|
|
Expression<Func<TDTO, bool>> expression,
|
||
|
|
Expression<Func<TAggregateRoot, object>> orderByExpress,
|
||
|
|
OrderByType orderByType)
|
||
|
|
{
|
||
|
|
TAggregateRoot data = await IBaseRepository.GetSingalByExpression(
|
||
|
|
_mapper.Map<Expression<Func<TAggregateRoot, bool>>>(expression), orderByExpress, orderByType);
|
||
|
|
|
||
|
|
return _mapper.Map<TDTO>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<List<TDTO>> QueryByIds(Guid[] ids)
|
||
|
|
{
|
||
|
|
List<TAggregateRoot> data = await IBaseRepository.QueryByIds(ids);
|
||
|
|
return _mapper.Map<List<TDTO>>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<TDTO> QueryByKey(string key)
|
||
|
|
{
|
||
|
|
TAggregateRoot data = await IBaseRepository.QueryByKey(key);
|
||
|
|
return _mapper.Map<TDTO>(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<PagedResult<TDTO>> QueryByPage(PageQueryCondition pageQueryCondition)
|
||
|
|
{
|
||
|
|
PagedResult<TAggregateRoot> dataPage = await IBaseRepository.QueryByPage(pageQueryCondition);
|
||
|
|
|
||
|
|
return new PagedResult<TDTO>(dataPage.TotalRecords,
|
||
|
|
dataPage.PageSize,
|
||
|
|
dataPage.PageNumber,
|
||
|
|
_mapper.Map<List<TDTO>>(dataPage.PageData));
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> ExcuteSql(string sql, params SugarParameter[] @params)
|
||
|
|
{
|
||
|
|
return await IBaseRepository.ExcuteSql(sql, @params);
|
||
|
|
}
|
||
|
|
|
||
|
|
#region 事务操作
|
||
|
|
public async Task BeginTran(SqlSugarScope sqlSugarScope)
|
||
|
|
{
|
||
|
|
await IBaseRepository.BeginTran(sqlSugarScope);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task CommitTran(SqlSugarScope sqlSugarScope)
|
||
|
|
{
|
||
|
|
await IBaseRepository.CommitTran(sqlSugarScope);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task RollbackTran(SqlSugarScope sqlSugarScope)
|
||
|
|
{
|
||
|
|
await IBaseRepository.RollbackTran(sqlSugarScope);
|
||
|
|
}
|
||
|
|
|
||
|
|
public SqlSugarTransaction UseTranAsync()
|
||
|
|
{
|
||
|
|
return IBaseRepository.UseTranAsync();
|
||
|
|
}
|
||
|
|
#endregion
|
||
|
|
}
|
||
|
|
}
|