You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
6.6 KiB
197 lines
6.6 KiB
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>
|
|
/// 添加实体类并返回 id
|
|
/// </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="list">实体类列表</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<bool> AddList([Required][FromForm] List<T> list)
|
|
{
|
|
return await _baseRep.InsertRangeAsync(list);
|
|
}
|
|
|
|
/// <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="list">实体类列表</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<bool> UpdateList([Required][FromForm] List<T> list)
|
|
{
|
|
return await _baseRep.UpdateRangeAsync(list);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除实体类
|
|
/// </summary>
|
|
/// <param name="id">实体类 id</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<bool> Delete([Required][FromForm] long id)
|
|
{
|
|
return await _baseRep.DeleteByIdAsync(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除实体类
|
|
/// </summary>
|
|
/// <param name="ids">实体类数组</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<bool> DeleteArray([Required][FromForm] string ids)
|
|
{
|
|
object[] objects = ids.ToSplitObjects();
|
|
return await _baseRep.DeleteByIdsAsync(objects);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 假删除实体类
|
|
/// </summary>
|
|
/// <param name="id">实体类 id</param>
|
|
/// <param name="isDelete">是否假删除</param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<bool> FakeDelete([Required][FromForm] long id, [FromForm] bool isDelete = true)
|
|
{
|
|
return await _baseRep.AsUpdateable(new T { Id = id, IsDelete = isDelete })
|
|
.UpdateColumns(it => it.IsDelete)
|
|
.ExecuteCommandAsync() > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取实体类
|
|
/// </summary>
|
|
/// <param name="id">实体类 id</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public virtual async Task<T> Get([Required] long id)
|
|
{
|
|
T entity = await _baseRep.GetByIdAsync(id);
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取实体类
|
|
/// </summary>
|
|
/// <param name="queryJson">equal:id,isDelete; between:[startTime,endTime];</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public virtual async Task<T> GetByQ([Required] string queryJson)
|
|
{
|
|
JObject queryObj = queryJson.ToJObject();
|
|
|
|
var exp = Expressionable.Create<T>();
|
|
if (queryObj.TryGetValue("Id", StringComparison.OrdinalIgnoreCase, out JToken Id))
|
|
exp.And(it => it.Id.Equals(Id));
|
|
if (queryObj.TryGetValue("IsDelete", StringComparison.OrdinalIgnoreCase, out JToken isDelete))
|
|
exp.And(it => it.IsDelete.Equals(isDelete));
|
|
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 entity = await _baseRep.AsQueryable()
|
|
.Where(exp.ToExpression())
|
|
.OrderBy(it => it.CreateTime)
|
|
.FirstAsync();
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量获取实体类
|
|
/// </summary>
|
|
/// <param name="queryJson">equal:isDelete; 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("IsDelete", StringComparison.OrdinalIgnoreCase, out JToken isDelete))
|
|
exp.And(it => it.IsDelete.Equals(isDelete));
|
|
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())
|
|
.OrderBy(it => it.CreateTime)
|
|
.ToListAsync();
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量获取分页实体类
|
|
/// </summary>
|
|
/// <param name="queryJson">equal:isDelete; between:[startTime,endTime];</param>
|
|
/// <param name="pagination">index,size</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public virtual async Task<List<T>> GetPageList([Required] string pagination, string queryJson = "")
|
|
{
|
|
Pagination pageObj = pagination.ToObject<Pagination>();
|
|
JObject queryObj = queryJson.ToJObject();
|
|
|
|
var exp = Expressionable.Create<T>();
|
|
if (queryObj.TryGetValue("IsDelete", StringComparison.OrdinalIgnoreCase, out JToken isDelete))
|
|
exp.And(it => it.IsDelete.Equals(isDelete));
|
|
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())
|
|
.OrderBy(it => it.CreateTime)
|
|
.ToPageListAsync(pageObj.Index, pageObj.Size);
|
|
return list;
|
|
}
|
|
}
|