fajiao
2 years ago
9 changed files with 281 additions and 197 deletions
@ -0,0 +1,165 @@ |
|||||
|
using Furion.DynamicApiController; |
||||
|
using Newtonsoft.Json.Linq; |
||||
|
|
||||
|
namespace Cis.Core.Common; |
||||
|
|
||||
|
[NonController] |
||||
|
public class ServiceBase<T> : IDynamicApiController where T : EntityBase, new() |
||||
|
{ |
||||
|
protected readonly SqlSugarRepository<T> _baseRep; |
||||
|
|
||||
|
public ServiceBase(SqlSugarRepository<T> baseRep) |
||||
|
{ |
||||
|
_baseRep = baseRep; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="entity"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<bool> Add([FromForm] T entity) |
||||
|
{ |
||||
|
return await _baseRep.InsertAsync(entity); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="entity"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<long> AddReturnId([FromForm] T entity) |
||||
|
{ |
||||
|
bool ret = await _baseRep.InsertAsync(entity); |
||||
|
if (!ret) return 0; |
||||
|
return entity.Id; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="array"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<bool> AddArray([FromForm] T[] array) |
||||
|
{ |
||||
|
return await _baseRep.InsertRangeAsync(array); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="entity"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<bool> Update([FromForm] T entity) |
||||
|
{ |
||||
|
return await _baseRep.UpdateAsync(entity); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="array"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<bool> UpdateArray([FromForm] T[] array) |
||||
|
{ |
||||
|
return await _baseRep.UpdateRangeAsync(array); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="id"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<bool> Delete(long id) |
||||
|
{ |
||||
|
return await _baseRep.DeleteByIdAsync(id); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="array"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<bool> DeleteArray(T[] array) |
||||
|
{ |
||||
|
return await _baseRep.DeleteByIdsAsync(array); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="id"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpPost] |
||||
|
public virtual async Task<bool> FakeDelete(long id) |
||||
|
{ |
||||
|
return await _baseRep.AsUpdateable() |
||||
|
.Where(it => it.Id.Equals(id)) |
||||
|
.Where("limit 1") |
||||
|
.ReSetValue(it => it.IsDelete = true) |
||||
|
.ExecuteCommandAsync() > 0; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="id"></param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpGet] |
||||
|
public virtual async Task<T> Get(long id) |
||||
|
{ |
||||
|
T entity = await _baseRep.GetByIdAsync(id); |
||||
|
return entity; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="queryJson">between:[startTime,endTime];</param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpGet] |
||||
|
public virtual async Task<List<T>> GetList(string queryJson = "") |
||||
|
{ |
||||
|
JObject queryObj = queryJson.ToJObject(); |
||||
|
|
||||
|
var exp = Expressionable.Create<T>(); |
||||
|
if (queryObj.TryGetValue("StartTime", StringComparison.OrdinalIgnoreCase, out JToken startTime) |
||||
|
&& queryObj.TryGetValue("EndTime", StringComparison.OrdinalIgnoreCase, out JToken endTime)) |
||||
|
exp.And(it => it.CreateTime >= startTime.ToDateTime() && it.CreateTime <= endTime.ToDateTime()); |
||||
|
|
||||
|
var list = await _baseRep.AsQueryable() |
||||
|
.Where(exp.ToExpression()) |
||||
|
.ToListAsync(); |
||||
|
return list; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="queryJson">between:[startTime,endTime];</param>
|
||||
|
/// <param name="pagination">index,size</param>
|
||||
|
/// <returns></returns>
|
||||
|
[HttpGet] |
||||
|
public virtual async Task<List<T>> GetPageList(string queryJson, string pagination) |
||||
|
{ |
||||
|
JObject queryObj = queryJson.ToJObject(); |
||||
|
Pagination pageObj = pagination.ToObject<Pagination>(); |
||||
|
|
||||
|
var exp = Expressionable.Create<T>(); |
||||
|
if (queryObj.TryGetValue("StartTime", StringComparison.OrdinalIgnoreCase, out JToken startTime) |
||||
|
&& queryObj.TryGetValue("EndTime", StringComparison.OrdinalIgnoreCase, out JToken endTime)) |
||||
|
exp.And(it => it.CreateTime >= startTime.ToDateTime() && it.CreateTime <= endTime.ToDateTime()); |
||||
|
|
||||
|
var list = await _baseRep.AsQueryable() |
||||
|
.Where(exp.ToExpression()) |
||||
|
.ToPageListAsync(pageObj.Index, pageObj.Size); |
||||
|
return list; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue