183 lines
6.6 KiB
C#
183 lines
6.6 KiB
C#
using BZPT.Domains.IRepositories.Sys;
|
|
using BZPT.Dto.Sys;
|
|
using BZPT.Repositories;
|
|
using Consul.Filtering;
|
|
using DevExtreme.AspNet.Data;
|
|
using NetTaste;
|
|
using NPlatform.Domains.Entity;
|
|
using NPOI.SS.Formula.Functions;
|
|
using ServiceStack;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace BZPT.Domains.Services
|
|
{
|
|
public class SugarServiceBase<TEntity,TDto> :BaseService where TDto : BaseDto where TEntity:EntityBase<string>
|
|
{
|
|
#region CUD
|
|
[Autowired]
|
|
public ILogger<TDto> Loger { get; set; }
|
|
[Autowired]
|
|
public IRepositorySugarBase<TEntity, string> Repository { get; set; }
|
|
|
|
public virtual async Task<INPResult> Add(TDto add)
|
|
{
|
|
var vRst = add.Validates();
|
|
if (vRst.StatusCode != HttpStatusCode.OK.ToInt())
|
|
{
|
|
return vRst;
|
|
}
|
|
|
|
var entity = this.MapperService.Map<TEntity>(add);
|
|
entity.Id = StringObjectIdGenerator.Instance.GenerateId().ToString();
|
|
var rst = await Repository.AddsAsync(entity);
|
|
return Success(entity.Id);
|
|
}
|
|
|
|
public virtual async Task<INPResult> Delete(TDto dto)
|
|
{
|
|
var vRst = dto.Validates();
|
|
if (vRst.StatusCode != HttpStatusCode.OK.ToInt())
|
|
{
|
|
return vRst;
|
|
}
|
|
var rstCount = 0;
|
|
if (!string.IsNullOrWhiteSpace(dto.Id))
|
|
{
|
|
rstCount = await this.Repository.RemoveAsync(dto.Id);
|
|
}
|
|
return new SuccessResult<int>(rstCount);
|
|
}
|
|
|
|
public virtual async Task<INPResult> Delete(string id)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(id))
|
|
{
|
|
return new FailResult<TEntity>($"id 参数不能为空");
|
|
}
|
|
var rstCount = await this.Repository.RemoveAsync(t=>t.Id== id);
|
|
return new SuccessResult<int>(rstCount);
|
|
}
|
|
|
|
|
|
public virtual async Task<INPResult> Delete(Expression< Func<TEntity,bool>> deleteWhere)
|
|
{
|
|
var rstCount = await this.Repository.RemoveAsync(deleteWhere);
|
|
return new SuccessResult<int>(rstCount);
|
|
}
|
|
public virtual async Task<INPResult> Edit(TDto edit)
|
|
{
|
|
var vRst = edit.Validates();
|
|
if (vRst.StatusCode != HttpStatusCode.OK.ToInt())
|
|
{
|
|
return vRst;
|
|
}
|
|
|
|
var entity = this.MapperService.Map<TEntity>(edit);
|
|
var rstCount = await this.Repository.AddOrUpdate(entity);
|
|
return new SuccessResult<int>(rstCount);
|
|
}
|
|
#endregion
|
|
|
|
#region Query
|
|
public override string GetDomainShortName()
|
|
{
|
|
return "请重写GetDomainShortName";
|
|
}
|
|
|
|
public virtual async Task<IListResult<TDto>> GetListAsync(QueryExp exp)
|
|
{
|
|
var vResult = exp.Validates();
|
|
if (vResult.StatusCode == 200)
|
|
{
|
|
var srcItems = await Repository.GetListByExpAsync(exp.GetExp<TEntity>(), exp.GetSelectSorts<TEntity>());
|
|
var dtos = this.MapperService.Map<IEnumerable<TEntity>, ListResult<TDto>>(srcItems);
|
|
return dtos;
|
|
}
|
|
return (IListResult<TDto>)vResult;
|
|
}
|
|
|
|
public virtual async Task<IListResult<TDto>> GetListAsync(Expression<Func<TEntity, bool>> filter)
|
|
{
|
|
var entitys = await Repository.GetListByExpAsync(filter);
|
|
var dtos = MapperService.Map<IEnumerable<TEntity>, IListResult<TDto>>(entitys);
|
|
return dtos;
|
|
}
|
|
public virtual async Task<IListResult<TDto>> GetPageAsync(QueryPageExp exp)
|
|
{
|
|
var vResult = exp.Validates();
|
|
if (vResult.StatusCode == 200)
|
|
{
|
|
var entitys = await Repository.GetPagedAsync(exp.PageNum, exp.PageSize, exp.GetExp<TEntity>(), exp.GetSelectSorts<TEntity>());
|
|
var dtos = MapperService.Map<IListResult<TEntity>, IListResult<TDto>>((IListResult<TEntity>)entitys);
|
|
return dtos;
|
|
}
|
|
return (IListResult<TDto>)vResult;
|
|
}
|
|
|
|
|
|
public virtual async Task<IListResult<TDto>> GetPageAsync(DataSourceLoadOptions loadOptionsexp)
|
|
{
|
|
if (loadOptionsexp.Sort == null || loadOptionsexp.Sort.Length == 0)
|
|
{
|
|
loadOptionsexp.Sort = new SortingInfo[] { new SortingInfo() { Selector = "CreateTime", Desc = true } };
|
|
}
|
|
Type type = typeof(TEntity);
|
|
// 检查是否存在指定名称的接口
|
|
Type interfaceType = type.GetInterface("ILogicDelete");
|
|
if (interfaceType != null && !loadOptionsexp.Filter.Contains("IsDeleted"))
|
|
{
|
|
loadOptionsexp.And(new List<object>(){
|
|
"IsDeleted","=",false });
|
|
}
|
|
var entitys = await Repository.GetPagedAsync(loadOptionsexp);
|
|
var dtos = MapperService.Map<IListResult<TEntity>, IListResult<TDto>>((IListResult<TEntity>)entitys);
|
|
return dtos;
|
|
}
|
|
|
|
public virtual async Task<IListResult<VO>> GetPageAsync<VO>(DataSourceLoadOptions loadOptionsexp)
|
|
{
|
|
if (loadOptionsexp.Sort == null || loadOptionsexp.Sort.Length == 0)
|
|
{
|
|
loadOptionsexp.Sort = new SortingInfo[] { new SortingInfo() { Selector = "CreateTime", Desc = true } };
|
|
}
|
|
Type type = typeof(TEntity);
|
|
// 检查是否存在指定名称的接口
|
|
Type interfaceType = type.GetInterface("ILogicDelete");
|
|
if (interfaceType != null&&!loadOptionsexp.Filter.Contains("IsDeleted"))
|
|
{
|
|
loadOptionsexp.And(new List<object>(){
|
|
"IsDeleted","=",false });
|
|
}
|
|
var entitys = await Repository.GetPagedAsync(loadOptionsexp);
|
|
var dtos = MapperService.Map<IListResult<TEntity>, IListResult<VO>>((IListResult<TEntity>)entitys);
|
|
return dtos;
|
|
}
|
|
|
|
/// <summary>
|
|
/// GetAsync
|
|
/// </summary>
|
|
/// <param name="entityId">ID</param>
|
|
/// <returns></returns>
|
|
public virtual async Task<INPResult> GetAsync(string entityId)
|
|
{
|
|
if (string.IsNullOrEmpty(entityId))
|
|
{
|
|
return base.FailParams<TDto>(nameof(entityId));
|
|
}
|
|
|
|
var entity = await Repository.FindByAsync(entityId);
|
|
var entityVo = MapperService.Map<TDto>(entity);
|
|
return Success(entityVo);
|
|
}
|
|
|
|
|
|
#endregion
|
|
}
|
|
}
|