完善通用接口输入参数

This commit is contained in:
2023-11-21 00:19:45 +08:00
parent 079eec500b
commit 70de358f97
17 changed files with 375 additions and 226 deletions

View File

@@ -5,6 +5,7 @@
using JNPF.Common.Security;
using Mapster;
using Microsoft.AspNetCore.Http;
using Tnb.Core;
namespace Tnb.Vengine.Domain;
@@ -17,6 +18,9 @@ public class VmBaseInput
public object? extra { get; set; }
}
/// <summary>
/// 查询单条数据的输入参数
/// </summary>
public class VmGetInput : VmBaseInput
{
/// <summary>
@@ -27,16 +31,38 @@ public class VmGetInput : VmBaseInput
/// <summary>
/// 过滤条件
/// </summary>
public string? q { get; set; }
public DObject q { get; set; } = new DObject();
/// <summary>
/// 输出字段
/// </summary>
public string o { get; set; } = "*";
/// <summary>
/// 从HttpContext中加载查询参数
/// </summary>
/// <param name="context"></param>
public void LoadFromHttpContext(HttpContext? context)
{
if (context == null) return;
string[] filter = new string[] { "id", "q", "o" };
foreach (var item in context.Request.Query.Where(a => !filter.Contains(a.Key)))
{
if (item.Value.Count > 1)
{
q.Add(item.Key, item.Value.ToArray());
}
else
{
q.Add(item.Key, item.Value.ToString());
}
}
}
}
public class VmGetListInput : VmBaseInput
/// <summary>
/// 查询列表数据的输入参数
/// </summary>
public class VmQueryInput : VmBaseInput
{
/// <summary>
/// 当前页数
@@ -61,48 +87,37 @@ public class VmGetListInput : VmBaseInput
/// <summary>
/// 过滤条件
/// </summary>
public string? q { get; set; }
public DObject q { get; set; } = new DObject();
/// <summary>
/// 输出字段
/// </summary>
public string o { get; set; } = "*";
}
/// <summary>
/// 获取多条数据输入参数
/// </summary>
public class VmQueryInput : VmGetListInput
{
/// <summary>
/// 查询条件
/// </summary>
public new DObject? q { get; set; }
/// <summary>
/// 添加一个查询条件
/// 从HttpContext中加载查询参数
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddQueryPara(string key, object value)
/// <param name="context"></param>
public void LoadFromHttpContext(HttpContext? context)
{
if (q == null) q = new DObject();
q.Add(key, value);
}
/// <summary>
/// 添加一个查询条件
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void AddQueryParaIf(bool condition, string key, object value)
{
if(condition) AddQueryPara(key, value);
if (context == null) return;
string[] filter = new string[] { "pnum", "psize", "sort", "k", "q", "o" };
foreach (var item in context.Request.Query.Where(a => !filter.Contains(a.Key)))
{
if (item.Value.Count > 1)
{
q.Add(item.Key, item.Value.ToArray());
}
else
{
q.Add(item.Key, item.Value.ToString());
}
}
}
}
/// <summary>
/// 新增数据输入参数
/// 新增或修改数据输入参数
/// </summary>
public class VmEditInput : VmBaseInput
{

View File

@@ -5,6 +5,7 @@
using JNPF.Common.Extension;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using Yitter.IdGenerator;
namespace Tnb.Vengine.Domain;
@@ -74,6 +75,15 @@ public class VmDbProp : VmBaseProp
#endregion Properties
/// <summary>
/// 获取可空类型的字符串名称
/// </summary>
/// <returns></returns>
public string GetCsType()
{
return required ? csType : csType + "?";
}
/// <summary>
/// 获取默认值
/// </summary>

View File

@@ -88,13 +88,8 @@ internal class VmQueryParser
/// <summary>
/// 解析查询参数
/// </summary>
private void ParseQueryPara(DObject? query)
private void ParseQueryPara(DObject query)
{
if (query == null)
{
return;
}
foreach (KeyValuePair<string, object> item in query)
{
string[] codes = item.Key.Split(NAVI_SEPERATE);

View File

@@ -24,6 +24,7 @@ namespace Tnb.Vengine.Domain;
public partial class Vmodel : Entity
{
private static string[] SOFT_DELETED = new string[] { "deleted", "isDeleted", "softDeleted" };
private static string[] AUTO_FILL_PROP = new string[] { "createTime", "createId", "updateTime", "modifyTime", "updateId", "modifyId" };
#region Properties
@@ -195,7 +196,7 @@ public partial class Vmodel : Entity
/// <returns></returns>
public VmDbProp GetPrimary()
{
var key = dbProps.FirstOrDefault(a => a.pkey);
var key = dbProps.FirstOrDefault(a => a.pkey);
ThrowIf.IsNull(key, $"模型({fullCode})没有定义主键属性");
return key;
}
@@ -238,7 +239,7 @@ public partial class Vmodel : Entity
/// <returns></returns>
public DObject GetDefaultDObject()
{
DObject obj = new ();
DObject obj = new();
foreach (VmDbProp p in dbProps)
{
obj.Add(p.code, p.GetDefaultValue()!);
@@ -246,13 +247,30 @@ public partial class Vmodel : Entity
return obj;
}
/// <summary>
/// 获取属性字符串
/// </summary>
/// <param name="isCreate"></param>
/// <returns></returns>
public string GetPropStr(bool includePkey, bool includeAutoProp)
{
var strs = new List<string>();
foreach (var col in dbProps.OrderBy(a => a.ordinal))
{
if (!includeAutoProp && AUTO_FILL_PROP.Contains(col.code)) continue;
if (!includePkey && col.pkey) continue;
strs.Add(col.GetCsType() + " " + col.code);
}
return string.Join(", ", strs);
}
/// <summary>
/// 转换为待新增的实体对象
/// </summary>
/// <returns></returns>
public DObject ToCreateEntity(DObject input, IUserManager user)
{
DObject obj = new ();
DObject obj = new();
foreach (VmDbProp p in dbProps)
{
if (input.ContainsKey(p.code))
@@ -286,7 +304,7 @@ public partial class Vmodel : Entity
/// <returns></returns>
public DObject ToUpdateEntity(DObject input, IUserManager user)
{
DObject obj = new ();
DObject obj = new();
foreach (VmDbProp p in dbProps)
{
if (input.ContainsKey(p.code))