模型查询接口增加按子表字段查询
This commit is contained in:
97
visualdev/Tnb.Vengine/Domain/OutputParser.cs
Normal file
97
visualdev/Tnb.Vengine/Domain/OutputParser.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using JNPF.Common.Extension;
|
||||
using Tnb.Vengine.DataAccess;
|
||||
|
||||
namespace Tnb.Vengine.Domain
|
||||
{
|
||||
public class OutputParser
|
||||
{
|
||||
private readonly IDataAccess _dataAccess;
|
||||
private readonly Vmodel _root;
|
||||
private List<string> _outputs;
|
||||
private Dictionary<string, OutputSelect> _selectProps = new Dictionary<string, OutputSelect>();
|
||||
private Dictionary<string, Vmodel?> _navModels = new Dictionary<string, Vmodel?>();
|
||||
public OutputParser(IDataAccess dataAccess, Vmodel rootModel, string output)
|
||||
{
|
||||
_dataAccess = dataAccess;
|
||||
_root = rootModel;
|
||||
_outputs = output.Split(',').Distinct().ToList();
|
||||
ParseOutputStr();
|
||||
}
|
||||
/// <summary>
|
||||
/// 按模型组织要输出的属性
|
||||
/// </summary>
|
||||
private void ParseOutputStr()
|
||||
{
|
||||
foreach (var outStr in _outputs)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(outStr))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
string vmPath;
|
||||
int n = outStr.LastIndexOf('.');
|
||||
if (n < 1)
|
||||
{
|
||||
vmPath = Vmodel.MAIN_ALIES;
|
||||
}
|
||||
else
|
||||
{
|
||||
vmPath = outStr.Substring(0, n);
|
||||
}
|
||||
if (!_selectProps.ContainsKey(vmPath))
|
||||
{
|
||||
_selectProps.Add(vmPath, new OutputSelect { navPaths = vmPath.Split(',').ToList(), vmPath = vmPath });
|
||||
}
|
||||
var outSelect = _selectProps[vmPath];
|
||||
outSelect.propCodes.Add(outStr.Substring(n + 1));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadNavModel()
|
||||
{
|
||||
var navProps = _root.navProps.Where(a => _selectProps.Values.Any(b => b.vmPath.StartsWith(a.code + "."))).ToList();
|
||||
await LoadVmodelNavigateAsync(navProps);
|
||||
}
|
||||
private async Task LoadVmodelNavigateAsync(List<VmNavProp> navProps)
|
||||
{
|
||||
var db = _dataAccess.GetSqlSugar();
|
||||
|
||||
var vmids = navProps.Select(a => a.vmid).Distinct().ToList();
|
||||
var navs = await db.Queryable<Vmodel>().Where(a => vmids.Contains(a.id)).ToDictionaryAsync(a => a.id, a => a);
|
||||
foreach (var navProp in navProps)
|
||||
{
|
||||
navProp.naviModel = (Vmodel)navs.GetOrDefault(navProp.vmid);
|
||||
navProp.naviModel.navProps.Where(a => _selectProps.Values.Any(b => b.vmPath.StartsWith(a.code + "."))).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class OutputSelect
|
||||
{
|
||||
public string vmId { get; set; } = string.Empty;
|
||||
public string vmCode { get; set; } = string.Empty;
|
||||
public string vmPath { get; set; } = string.Empty;
|
||||
public eNavigateType navType { get; set; } = eNavigateType.None;
|
||||
public List<string> navPaths { get; set; } = new List<string>();
|
||||
|
||||
public List<string> propCodes { get; set; } = new List<string>();
|
||||
public List<string> fieldCodes { get; set; } = new List<string>();
|
||||
|
||||
public OutputSelect()
|
||||
{
|
||||
|
||||
}
|
||||
public OutputSelect(Vmodel model)
|
||||
{
|
||||
vmId = model.id;
|
||||
vmCode = model.vmCode;
|
||||
vmPath = Vmodel.MAIN_ALIES;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,6 +164,7 @@ public class VmSelectProp
|
||||
public const string MAIN_ALIES = "m";
|
||||
public string code { get; set; } = string.Empty;
|
||||
public string field { get; set; } = string.Empty;
|
||||
public List<string> navPath { get; set; } = new List<string>();
|
||||
public string navCode { get; set; } = MAIN_ALIES;
|
||||
public ePropType propType { get; set; }
|
||||
public eNavigateType navType { get; set; }
|
||||
|
||||
@@ -111,7 +111,7 @@ public class VmDbProp : VmBaseProp
|
||||
/// 获取默认值文本
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object? GetDefaultValueString()
|
||||
public string? GetDefaultValueString()
|
||||
{
|
||||
string val = "";
|
||||
if (!required) return val;
|
||||
|
||||
@@ -23,6 +23,8 @@ namespace Tnb.Vengine.Domain;
|
||||
[SugarTable("sys_vmodel")]
|
||||
public partial class Vmodel : Entity
|
||||
{
|
||||
public const string MAIN_ALIES = "m";
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
@@ -146,7 +148,8 @@ public partial class Vmodel : Entity
|
||||
}
|
||||
|
||||
#endregion Properties
|
||||
|
||||
//private Dictionary<string, string>? _mapField2Prop = null;
|
||||
//private Dictionary<string, string>? _mapProp2Field = null;
|
||||
/// <summary>
|
||||
/// 通过实体创建模型
|
||||
/// </summary>
|
||||
@@ -351,11 +354,30 @@ public partial class Vmodel : Entity
|
||||
if (filter == null) return wheres;
|
||||
foreach (var item in filter)
|
||||
{
|
||||
VmDbProp? prop = null;
|
||||
// TODO 按子表条件查询
|
||||
if (item.Key.Contains("."))
|
||||
{
|
||||
var codes = item.Key.Split('.');
|
||||
if (codes.Length >= 2)
|
||||
{
|
||||
var navProp = navProps.FirstOrDefault(a => a.code == codes[0]);
|
||||
if (navProp != null && navProp.naviModel != null)
|
||||
{
|
||||
var dbProp = navProp.naviModel.dbProps.FirstOrDefault(a => a.code == codes[1]);
|
||||
if (dbProp != null)
|
||||
{
|
||||
prop = new VmDbProp();
|
||||
prop.field = codes[0] + "." + dbProp.field;
|
||||
prop.csType = dbProp.csType;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
prop = dbProps.FirstOrDefault(a => a.code == item.Key);
|
||||
}
|
||||
var prop = dbProps.FirstOrDefault(a => a.code == item.Key);
|
||||
if (prop == null) continue;
|
||||
if (item.Value is JArray val)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user