147 lines
4.7 KiB
C#
147 lines
4.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using SqlSugar;
|
|
using UserCenter.Model.Interface;
|
|
using VideoAnalysisCore.Common;
|
|
|
|
namespace VideoAnalysisCore.Controllers
|
|
{
|
|
[ApiController]
|
|
public class _BaseController : ControllerBase
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 管理员接口
|
|
/// </summary>
|
|
[Authorize(AuthenticationSchemes = Authentication.vdAdmin)]
|
|
[Route("api/[controller]/[action]")]
|
|
public abstract class BackBaseController : _BaseController
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 管理员的公共接口
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public abstract class BackController<T> : BackBaseController where T : class, IDFilter, new()
|
|
{
|
|
readonly Repository<T> _baseRepository;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="baseService"></param>
|
|
public BackController(Repository<T> baseService) : base()
|
|
{
|
|
_baseRepository = baseService;
|
|
}
|
|
/// <summary>
|
|
/// 详情
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("/api/[controller]/{id}")]
|
|
public virtual async Task<dynamic> Info(long id)
|
|
{
|
|
return await _baseRepository.GetByIdAsync(id);
|
|
}
|
|
/// <summary>
|
|
/// 新增/修改
|
|
/// <para>id=0 时新增</para>
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<bool> Edit([FromBody] T model)
|
|
{
|
|
if (model.Id == 0)
|
|
return await _baseRepository.InsertAsync(model);
|
|
else
|
|
return await _baseRepository.UpdateAsync(model);
|
|
}
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<bool> Del([FromBody] params long[] ids)
|
|
{
|
|
var db = _baseRepository;
|
|
int sum = 0;
|
|
if (!typeof(T).IsAssignableFrom(typeof(IDeletedFilter)))
|
|
{
|
|
if(ids.Length==1)
|
|
return await db.DeleteByIdAsync(ids.First());
|
|
else
|
|
return await db.DeleteByIdsAsync(ids.Select(s=>(dynamic)s).ToArray());
|
|
}
|
|
try
|
|
{
|
|
db.Context.Ado.BeginTran();
|
|
foreach (var item in ids)
|
|
sum += await db.AsUpdateable().SetColumns("DeleteState", true)
|
|
.Where("Id=" + item).ExecuteCommandAsync();
|
|
db.Context.Ado.CommitTran();
|
|
}
|
|
catch
|
|
{
|
|
db.Context.Ado.RollbackTran();
|
|
throw;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页/全查 的基础查询函数
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
///
|
|
[NonAction]
|
|
public virtual ISugarQueryable<T> BaseQuery(QueryDto model)
|
|
{
|
|
List<IConditionalModel> where = [.. model.Conditions];
|
|
var d = _baseRepository.AsQueryable()
|
|
.Where(where);
|
|
if ((typeof(T)).GetProperty("DeleteState") != null)
|
|
d = d.Where("DeleteState=0");
|
|
if (!string.IsNullOrEmpty(model.OrderBy))
|
|
return d.OrderByPropertyName(model.OrderBy, model.OrderByType);
|
|
return d;
|
|
}
|
|
/// <summary>
|
|
/// 分页查询
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<dynamic> PageList([FromBody] QueryRequestBase model)
|
|
{
|
|
var sqlquery = BaseQuery(model);
|
|
RefAsync<int> total = 0;
|
|
var data = await sqlquery.ToPageListAsync(model.PageIndex + 1, model.PageSize, total);
|
|
return new PageResult<T>() { Data = data, Total = total };
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询下拉列表
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public virtual async Task<List<ComboModel>> QueryCombo([FromBody] QueryCombo model)
|
|
{
|
|
//if (string.IsNullOrEmpty(model.ValueName) || string.IsNullOrEmpty(model.TextName))
|
|
// Oh.ModelError("ValueName TextName 是必填项");
|
|
var sqlquery = BaseQuery(model);
|
|
var res = await sqlquery.Select<ComboModel>($"{model.TextName} as Text , {model.ValueName} as Value").ToListAsync();
|
|
return res;
|
|
}
|
|
}
|
|
|
|
}
|