// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
namespace Admin.NET.Core.Service;
///
/// APIJSON服务
///
[ApiDescriptionSettings(Order = 100)]
public class APIJSONService : IDynamicApiController, ITransient
{
private readonly ISqlSugarClient _db;
private readonly IdentityService _identityService;
private readonly TableMapper _tableMapper;
private readonly SelectTable _selectTable;
public APIJSONService(ISqlSugarClient db,
IdentityService identityService,
TableMapper tableMapper)
{
_db = db;
_tableMapper = tableMapper;
_identityService = identityService;
_selectTable = new SelectTable(_identityService, _tableMapper, _db);
}
///
/// 统一查询入口
///
///
/// 参数:{"[]":{"SYSLOGOP":{}}}
///
[HttpPost("get")]
public JObject Query([FromBody] JObject jobject)
{
return _selectTable.Query(jobject);
}
///
/// 查询表
///
///
///
///
[HttpPost("get/{table}")]
public JObject QueryByTable([FromRoute] string table, [FromBody] JObject jobject)
{
var ht = new JObject
{
{ table + "[]", jobject }
};
// 自动添加总计数量
if (jobject["query"] != null && jobject["query"].ToString() != "0" && jobject["total@"] == null)
ht.Add("total@", "");
// 每页最大1000条数据
if (jobject["count"] != null && int.Parse(jobject["count"].ToString()) > 1000)
throw Oops.Bah("count分页数量最大不能超过1000");
jobject.Remove("@debug");
var hasTableKey = false;
var ignoreConditions = new List { "page", "count", "query" };
var tableConditions = new JObject(); // 表的其它查询条件,比如过滤、字段等
foreach (var item in jobject)
{
if (item.Key.Equals(table, StringComparison.CurrentCultureIgnoreCase))
{
hasTableKey = true;
break;
}
if (!ignoreConditions.Contains(item.Key.ToLower()))
tableConditions.Add(item.Key, item.Value);
}
foreach (var removeKey in tableConditions)
{
jobject.Remove(removeKey.Key);
}
if (!hasTableKey)
jobject.Add(table, tableConditions);
return Query(ht);
}
///
/// 新增
///
/// 表对象或数组,若没有传Id则后端生成Id
///
[HttpPost("add")]
[UnitOfWork]
public JObject Add([FromBody] JObject tables)
{
var ht = new JObject();
foreach (var table in tables)
{
var talbeName = table.Key.Trim();
var role = _identityService.GetRole();
if (!role.Insert.Table.Contains(talbeName, StringComparer.CurrentCultureIgnoreCase))
throw Oops.Bah($"没权限添加{talbeName}");
JToken result;
// 批量插入
if (table.Value is JArray)
{
var ids = new List