// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using AspectCore.Extensions.Reflection;
using System.Dynamic;
namespace Admin.NET.Core.Service;
///
///
///
public class SelectTable : ISingleton
{
private readonly IdentityService _identitySvc;
private readonly TableMapper _tableMapper;
private readonly ISqlSugarClient _db;
public SelectTable(IdentityService identityService, TableMapper tableMapper, ISqlSugarClient dbClient)
{
_identitySvc = identityService;
_tableMapper = tableMapper;
_db = dbClient;
}
///
/// 判断表名是否正确,若不正确则抛异常
///
///
///
public virtual bool IsTable(string table)
{
return _db.DbMaintenance.GetTableInfoList().Any(it => it.Name.Equals(table, StringComparison.CurrentCultureIgnoreCase))
? true
: throw new Exception($"表名【{table}】不正确!");
}
///
/// 判断表的列名是否正确,如果不正确则抛异常,更早地暴露给调用方
///
///
///
///
public virtual bool IsCol(string table, string col)
{
return _db.DbMaintenance.GetColumnInfosByTableName(table).Any(it => it.DbColumnName.Equals(col, StringComparison.CurrentCultureIgnoreCase))
? true
: throw new Exception($"表【{table}】不存在列【{col}】!请检查输入参数");
}
///
/// 查询列表数据
///
///
///
///
///
///
///
public virtual Tuple GetTableData(string subtable, int page, int count, int query, string json, JObject dd)
{
var role = _identitySvc.GetSelectRole(subtable);
if (!role.Item1)
throw new Exception(role.Item2);
var selectrole = role.Item2;
subtable = _tableMapper.GetTableName(subtable);
var values = JObject.Parse(json);
page = values["page"] == null ? page : int.Parse(values["page"].ToString());
count = values["count"] == null ? count : int.Parse(values["count"].ToString());
query = values["query"] == null ? query : int.Parse(values["query"].ToString());
values.Remove("page");
values.Remove("count");
// 构造查询过程
var tb = SugarQueryable(subtable, selectrole, values, dd);
// 实际会在这里执行
if (query == 1) // 1-总数
{
return new Tuple(null, tb.MergeTable().Count());
}
else
{
if (page > 0) // 分页
{
int total = 0;
if (query == 0)
return new Tuple(tb.ToPageList(page, count), total); // 0-对象
else
return new Tuple(tb.ToPageList(page, count, ref total), total); // 2-以上全部
}
else // 列表
{
IList l = tb.ToList();
return query == 0 ? new Tuple(l, 0) : new Tuple(l, l.Count);
}
}
}
///
/// 解析并查询
///
///
///
public virtual JObject Query(string queryJson)
{
var queryJobj = JObject.Parse(queryJson);
return Query(queryJobj);
}
///
/// 单表查询
///
///
/// 返回数据的节点名称 默认为 infos
///
public virtual JObject QuerySingle(JObject queryObj, string nodeName = "infos")
{
var resultObj = new JObject();
var total = 0;
foreach (var item in queryObj)
{
var key = item.Key.Trim();
if (key.EndsWith("[]"))
{
total = QuerySingleList(resultObj, item, nodeName);
}
else if (key.Equals("func"))
{
ExecFunc(resultObj, item);
}
else if (key.Equals("total@") || key.Equals("total"))
{
resultObj.Add("total", total);
}
}
return resultObj;
}
///
/// 获取查询语句
///
///
///
public virtual string ToSql(JObject queryObj)
{
foreach (var item in queryObj)
{
if (item.Key.Trim().EndsWith("[]"))
return ToSql(item);
}
return string.Empty;
}
///
/// 解析并查询
///
///
///
public virtual JObject Query(JObject queryObj)
{
var resultObj = new JObject();
int total;
foreach (var item in queryObj)
{
var key = item.Key.Trim();
if (key.Equals("[]")) // 列表
{
total = QueryMoreList(resultObj, item);
resultObj.Add("total", total); // 只要是列表查询都自动返回总数
}
else if (key.EndsWith("[]"))
{
total = QuerySingleList(resultObj, item);
}
else if (key.Equals("func"))
{
ExecFunc(resultObj, item);
}
else if (key.Equals("total@") || key.Equals("total"))
{
// resultObj.Add("total", total);
continue;
}
else // 单条
{
var template = GetFirstData(key, item.Value.ToString(), resultObj);
if (template != null)
resultObj.Add(key, JToken.FromObject(template));
}
}
return resultObj;
}
// 动态调用方法
private object ExecFunc(string funcname, object[] param, Type[] types)
{
var method = typeof(FuncList).GetMethod(funcname);
var reflector = method.GetReflector();
var result = reflector.Invoke(new FuncList(), param);
return result;
}
// 生成sql
private string ToSql(string subtable, int page, int count, int query, string json)
{
var values = JObject.Parse(json);
page = values["page"] == null ? page : int.Parse(values["page"].ToString());
count = values["count"] == null ? count : int.Parse(values["count"].ToString());
query = values["query"] == null ? query : int.Parse(values["query"].ToString());
values.Remove("page");
values.Remove("count");
subtable = _tableMapper.GetTableName(subtable);
var tb = SugarQueryable(subtable, "*", values, null);
var sqlObj = tb.Skip((page - 1) * count).Take(10).ToSql();
return sqlObj.Key;
}
///
/// 查询第一条数据
///
///
///
///
///
///
private dynamic GetFirstData(string subtable, string json, JObject job)
{
var role = _identitySvc.GetSelectRole(subtable);
if (!role.Item1)
throw new Exception(role.Item2);
var selectrole = role.Item2;
subtable = _tableMapper.GetTableName(subtable);
var values = JObject.Parse(json);
values.Remove("page");
values.Remove("count");
var tb = SugarQueryable(subtable, selectrole, values, job).First();
var dic = (IDictionary)tb;
foreach (var item in values.Properties().Where(it => it.Name.EndsWith("()")))
{
if (item.Value.IsNullOrEmpty())
{
var func = item.Value.ToString().Substring(0, item.Value.ToString().IndexOf("("));
var param = item.Value.ToString().Substring(item.Value.ToString().IndexOf("(") + 1).TrimEnd(')');
var types = new List();
var paramss = new List