Camera Information System
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.
 
 
 
 

192 lines
5.8 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>
///
/// </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(new T { Id = id, IsDelete = true })
.UpdateColumns(it => it.IsDelete)
.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">equal:id,isDelete; between:[startTime,endTime];</param>
/// <returns></returns>
[HttpGet]
public virtual async Task<T> GetQ(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())
.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())
.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(string pagination, string queryJson = "")
{
JObject queryObj = queryJson.ToJObject();
Pagination pageObj = pagination.ToObject<Pagination>();
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())
.ToPageListAsync(pageObj.Index, pageObj.Size);
return list;
}
}