修复排序错误

This commit is contained in:
2023-11-16 14:55:37 +08:00
parent 5b49890a4f
commit a5f3d473e6
15 changed files with 261 additions and 154 deletions

View File

@@ -4,17 +4,17 @@
/// 字典对象
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class VmodelSettingAttribute : Attribute
public class VmodelAttribute : Attribute
{
public string? Id { get; set; }
public string? Area { get; set; }
public string? Code { get; set; }
public VmodelSettingAttribute(string id)
public VmodelAttribute(string id)
{
Id = id;
}
public VmodelSettingAttribute(string area, string? code = null)
public VmodelAttribute(string area, string? code)
{
Area = area;
Code = code;

View File

@@ -1,8 +1,9 @@
using SqlSugar;
using Tnb;
namespace JNPF.Common.Contracts;
public class BaseEntity<TKey> : IEntity where TKey : IEquatable<TKey>
public class BaseEntity<TKey> : Entity, IEntity where TKey : IEquatable<TKey>
{
/// <summary>
/// 获取或设置 编号.
@@ -10,6 +11,8 @@ public class BaseEntity<TKey> : IEntity where TKey : IEquatable<TKey>
[SugarColumn(ColumnName = "id", ColumnDescription = "主键", IsPrimaryKey = true)]
public TKey id { get; set; }
public override object[] GetKeys()
{
return [id];
}
}

View File

@@ -5,7 +5,7 @@
using JNPF.Common.Contracts;
namespace Tnb.Vengine.Domain;
namespace Tnb;
[Serializable]
public abstract class Entity : IEntity

View File

@@ -1,4 +1,7 @@
namespace Tnb.Core;
using Mapster;
using SqlSugar;
namespace Tnb.Core;
/// <summary>
/// 字典对象
@@ -13,33 +16,35 @@ public class DObject : Dictionary<string, object>
public DObject(Dictionary<string, object> dictionary) : base(dictionary)
{
}
public void AddCascade(string code, object value)
/// <summary>
/// 将平面结构转换为树形嵌套结构
/// </summary>
/// <param name="codePath">以.号分隔的多级路径</param>
/// <param name="value"></param>
public void AddToCascade(string codePath, object value)
{
var keys = code.Split('.');
var keys = codePath.Split('.');
if (keys.Length == 1)
{
Add(code, value);
Add(codePath, value);
return;
}
DObject temp = this;
for (int i = 0; i < keys.Length; i++)
{
DObject temp = this;
var key = keys[i];
if (i < keys.Length - 1)
{
if (!ContainsKey(keys[i]))
{
temp = new DObject();
Add(keys[i], temp);
var obj = new DObject();
temp[key] = obj;
temp = obj;
}
else
{
temp = (DObject)temp[keys[i]];
}
}
else
{
temp.Add(keys[i], value);
temp[key] = value;
}
}
}
}

View File

@@ -24,6 +24,7 @@ public class VengineAppService : BaseAppService, IVengineAppService
{
private readonly IDataAccess _dataAccess;
private readonly ISqlSugarClient _db;
private readonly Dictionary<string, Vmodel> _models = new();
/// <summary>
/// 构造函数
@@ -34,6 +35,19 @@ public class VengineAppService : BaseAppService, IVengineAppService
_db = _dataAccess.GetSqlSugar();
}
/// <summary>
/// 获取Vmodel
/// </summary>
private async Task<Vmodel> GetVmodelAsync(string id)
{
if (_models.ContainsKey(id))
{
_models[id] = await _dataAccess.GetVmodelAsync(id, false);
}
return _models[id];
}
#region id进行增删改查接口
/// <summary>
@@ -42,14 +56,9 @@ public class VengineAppService : BaseAppService, IVengineAppService
[HttpGet("api/[area]/[controller]/{vmid}/get")]
public async Task<dynamic?> GetAsync(string vmid, [FromQuery] VmGetInput input)
{
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
VmQueryInput arg = input.Adapt<VmQueryInput>();
if (input.id != null)
{
if (arg.q == null) arg.q = new DObject();
arg.q.Add(vm.GetPrimary().code, input.id);
}
var ls = await _dataAccess.QueryDataAsync(vm, arg);
var vm = await GetVmodelAsync(vmid);
var arg = input.ToQueryInput(vm.GetPrimary().code);
var ls = await ListAsync(vmid, arg);
return ls.items.FirstOrDefault();
}
@@ -59,14 +68,7 @@ public class VengineAppService : BaseAppService, IVengineAppService
[HttpGet("api/[area]/[controller]/{vmid}/get-list")]
public async Task<VmPagedOutput> GetListAsync(string vmid, [FromQuery] VmGetListInput input)
{
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
VmQueryInput arg = input.Adapt<VmQueryInput>();
if (!string.IsNullOrEmpty(input.q))
{
arg.q = input.q.ToObject<DObject>();
}
var ls = await _dataAccess.QueryDataAsync(vm, arg);
return ls;
return await ListAsync(vmid, input.ToQueryInput());
}
/// <summary>
@@ -75,7 +77,7 @@ public class VengineAppService : BaseAppService, IVengineAppService
[HttpPost("api/[area]/[controller]/{vmid}/list")]
public async Task<VmPagedOutput> ListAsync(string vmid, [FromBody] VmQueryInput input)
{
var vm = await _dataAccess.GetVmodelAsync(vmid, true);
var vm = await GetVmodelAsync(vmid);
var ls = await _dataAccess.QueryDataAsync(vm, input);
return ls;
}
@@ -86,7 +88,7 @@ public class VengineAppService : BaseAppService, IVengineAppService
[HttpPost("api/[area]/[controller]/{vmid}/create")]
public async Task<dynamic> CreateAsync(string vmid, [FromBody] VmCreateInput input)
{
var vm = await _dataAccess.GetVmodelAsync(vmid);
var vm = await GetVmodelAsync(vmid);
var ret = await _dataAccess.CreateDataAsync(vm, input);
return ret;
}
@@ -97,7 +99,7 @@ public class VengineAppService : BaseAppService, IVengineAppService
[HttpPut("api/[area]/[controller]/{vmid}/update")]
public async Task<dynamic> UpdateAsync(string vmid, [FromBody] VmUpdateInput input)
{
var vm = await _dataAccess.GetVmodelAsync(vmid);
var vm = await GetVmodelAsync(vmid);
var ret = await _dataAccess.UpdateDataAsync(vm, input);
return ret;
}
@@ -108,7 +110,7 @@ public class VengineAppService : BaseAppService, IVengineAppService
[HttpDelete("api/[area]/[controller]/{vmid}/delete")]
public async Task<dynamic> DeleteAsync(string vmid, [FromQuery] VmDeleteInput input)
{
var vm = await _dataAccess.GetVmodelAsync(vmid);
var vm = await GetVmodelAsync(vmid);
var ret = await _dataAccess.DeleteDataAsync(vm, input);
return ret;
}
@@ -119,8 +121,12 @@ public class VengineAppService : BaseAppService, IVengineAppService
private async Task<Vmodel> GetVmodelAsync(string areaCode, string vmCode)
{
var vm = await _dataAccess.GetVmodelAsync(areaCode, vmCode, false);
return vm;
var key = areaCode + "/" + vmCode;
if (_models.ContainsKey(key))
{
_models[key] = await _dataAccess.GetVmodelAsync(areaCode, vmCode, false);
}
return _models[key];
}
/// <summary>
@@ -130,13 +136,8 @@ public class VengineAppService : BaseAppService, IVengineAppService
public async Task<dynamic?> GetAsync(string areaCode, string vmCode, [FromQuery] VmGetInput input)
{
var vm = await GetVmodelAsync(areaCode, vmCode);
VmQueryInput arg = input.Adapt<VmQueryInput>();
if (input.id != null)
{
if (arg.q == null) arg.q = new DObject();
arg.q.Add(vm.GetPrimary().code, input.id);
}
var ls = await _dataAccess.QueryDataAsync(vm, arg);
var arg = input.ToQueryInput(vm.GetPrimary().code);
var ls = await ListAsync(areaCode, vmCode, arg);
return ls.items.FirstOrDefault();
}
@@ -146,14 +147,7 @@ public class VengineAppService : BaseAppService, IVengineAppService
[HttpGet("api/{areaCode}/{vmCode}/get-list")]
public async Task<VmPagedOutput> GetListAsync(string areaCode, string vmCode, [FromQuery] VmGetListInput input)
{
var vm = await GetVmodelAsync(areaCode, vmCode);
VmQueryInput arg = input.Adapt<VmQueryInput>();
if (!string.IsNullOrEmpty(input.q))
{
arg.q = input.q.ToObject<DObject>();
}
var ls = await _dataAccess.QueryDataAsync(vm, arg);
return ls;
return await ListAsync(areaCode, vmCode, input.ToQueryInput());
}
/// <summary>

View File

@@ -4,6 +4,9 @@
/////////////////////////////////////////////////////////////////////////////////
using System.Reflection;
using System.Security.Cryptography;
using Aop.Api.Domain;
using JNPF.Common.Contracts;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -20,10 +23,17 @@ namespace Tnb.Vengine.AppService;
[Authorize]
//[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 1102)]
[Route("api/[area]/[controller]/[action]")]
public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, TGetListOutput, TCreateInput, TUpdateInput> : BaseAppService
where TEntity : Entity
where TGetInput : VmGetInput
where TQueryInput : VmQueryInput
where TGetListInput : VmGetListInput
where TCreateInput : VmCreateInput
where TUpdateInput : VmUpdateInput
{
protected readonly IDataAccess _dataAccess;
protected readonly ISqlSugarClient _db;
private readonly Dictionary<string, Vmodel> _models = new();
/// <summary>
/// 构造函数
@@ -37,13 +47,25 @@ public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
protected virtual async Task<Vmodel> GetVmodelAsync()
{
var tp = typeof(TEntity);
string? area = null, code = null;
var vset = tp.GetCustomAttribute<VmodelSettingAttribute>();
string? id = null, area = null, code = null;
var vset = tp.GetCustomAttribute<VmodelAttribute>();
if (vset != null)
{
id = vset.Id;
area = vset.Area;
code = vset.Code;
}
if (!string.IsNullOrEmpty(id))
{
if (_models.ContainsKey(id))
{
_models[id] = await _dataAccess.GetVmodelAsync(id, false);
//_models[_models[id].fullCode] = _models[id];
}
return _models[id];
}
else
{
if (string.IsNullOrEmpty(area))
{
ThrowIf.IsNullOrEmpty(tp.Namespace, $"类型 {nameof(tp)} 的命名空间不可为空");
@@ -53,55 +75,58 @@ public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
{
code = tp.Name.ToKebab();
}
var vm = await _dataAccess.GetVmodelAsync(area, code, false);
return vm;
var key = area + "/" + code;
if (_models.ContainsKey(key))
{
_models[key] = await _dataAccess.GetVmodelAsync(area, code, false);
}
return _models[key];
}
}
/// <summary>
/// 获取一条 数据信息
/// </summary>
[HttpGet]
public virtual async Task<dynamic> GetAsync([FromQuery] VmGetInput input)
public virtual async Task<TGetOutput> GetAsync([FromQuery] TGetInput input)
{
var vm = await GetVmodelAsync();
VmQueryInput arg = input.Adapt<VmQueryInput>();
TQueryInput arg = input.Adapt<TQueryInput>();
if (input.id != null)
{
if (arg.q == null) arg.q = new DObject();
arg.q.Add(vm.GetPrimary().code, input.id);
}
var ls = await _dataAccess.QueryDataAsync(vm, arg);
return ls.items.FirstOrDefault()!;
var data = (await ListAsync(arg)).items.FirstOrDefault();
return data == null ? default! : data.Adapt<TGetOutput>();
}
/// <summary>
/// 获取多条 数据列表
/// </summary>
[HttpGet]
public virtual async Task<VmPagedOutput> GetListAsync([FromQuery] VmGetListInput input)
public virtual async Task<PagedOutput<TGetListOutput>> GetListAsync([FromQuery] TGetListInput input)
{
var vm = await GetVmodelAsync();
var ls = await _dataAccess.QueryDataAsync(vm, input.ToListInput());
return ls;
return await ListAsync(input.Adapt<TQueryInput>());
}
/// <summary>
/// 获取多条 数据列表
/// </summary>
[HttpPost]
public virtual async Task<VmPagedOutput> ListAsync([FromBody] VmQueryInput input)
public virtual async Task<PagedOutput<TGetListOutput>> ListAsync([FromBody] TQueryInput input)
{
var vm = await GetVmodelAsync();
var ls = await _dataAccess.QueryDataAsync(vm, input);
return ls;
return ls.ToPagedOutput<TGetListOutput>();
}
/// <summary>
/// 新增 数据
/// </summary>
[HttpPost]
public virtual async Task<dynamic> CreateAsync([FromBody] VmCreateInput input)
public virtual async Task<dynamic> CreateAsync([FromBody] TCreateInput input)
{
var vm = await GetVmodelAsync();
var ret = await _dataAccess.CreateDataAsync(vm, input);
@@ -112,7 +137,7 @@ public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
/// 更新 数据
/// </summary>
[HttpPut]
public virtual async Task<dynamic> UpdateAsync([FromBody] VmUpdateInput input)
public virtual async Task<dynamic> UpdateAsync([FromBody] TUpdateInput input)
{
var vm = await GetVmodelAsync();
var ret = await _dataAccess.UpdateDataAsync(vm, input);
@@ -130,3 +155,46 @@ public class VengineAppService<TEntity> : BaseAppService where TEntity : Entity
return ret;
}
}
#region
public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, TGetListOutput> :
VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, TGetListOutput, VmCreateInput, VmUpdateInput>
where TEntity : Entity
where TGetInput : VmGetInput
where TGetListInput : VmGetListInput
where TQueryInput : VmQueryInput
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
public class VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput> :
VengineAppService<TEntity, TGetInput, TGetOutput, TQueryInput, TGetListInput, DObject, VmCreateInput, VmUpdateInput>
where TEntity : Entity
where TGetInput : VmGetInput
where TGetListInput : VmGetListInput
where TQueryInput : VmQueryInput
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
public class VengineAppService<TEntity, TGetInput, TGetOutput> :
VengineAppService<TEntity, TGetInput, TGetOutput, VmQueryInput, VmGetListInput, DObject, VmCreateInput, VmUpdateInput>
where TEntity : Entity
where TGetInput : VmGetInput
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
public class VengineAppService<TEntity> :
VengineAppService<TEntity, VmGetInput, dynamic, VmQueryInput, VmGetListInput, dynamic, VmCreateInput, VmUpdateInput>
where TEntity : Entity
{
public VengineAppService(IDataAccess da) : base(da)
{
}
}
#endregion

View File

@@ -4,6 +4,7 @@
/////////////////////////////////////////////////////////////////////////////////
using JNPF;
using JNPF.Common.Manager;
using JNPF.Common.Security;
using JNPF.ViewEngine;
using Mapster;
@@ -23,13 +24,15 @@ namespace Tnb.Vengine.AppService;
public class VmodelAppService : VengineAppService<Vmodel>, IVmodelAppService
{
private readonly IViewEngine _viewEngine;
private readonly ICacheManager _cache;
/// <summary>
/// 构造函数
/// </summary>
public VmodelAppService(IDataAccess da, IViewEngine viewEngine) : base(da)
public VmodelAppService(IDataAccess da, IViewEngine viewEngine, ICacheManager cache) : base(da)
{
_viewEngine = viewEngine;
_cache = cache;
}
/// <summary>
@@ -59,21 +62,19 @@ public class VmodelAppService : VengineAppService<Vmodel>, IVmodelAppService
/// <summary>
/// 获取多条 数据列表
/// </summary>
public override async Task<VmPagedOutput> GetListAsync(VmGetListInput input)
public override async Task<PagedOutput<dynamic>> GetListAsync(VmGetListInput input)
{
return await ListAsync(input.ToQueryInput());
}
[NonAction]
public override async Task<PagedOutput<dynamic>> ListAsync(VmQueryInput input)
{
VmPagedOutput ret = new();
var q = _db.Queryable<Vmodel>().WhereIF(!string.IsNullOrEmpty(input.k), a => a.vmCode.Contains(input.k!) || a.vmName.Contains(input.k!));
RefAsync<int> total = 0;
var data = await q.OrderBy(input.sort).ToPageListAsync((input.pnum - 1) * input.psize, input.psize, total);
ret.total = total;
ret.items = data.Adapt<List<DObject>>();
return ret;
}
[NonAction]
public override Task<VmPagedOutput> ListAsync(VmQueryInput input)
{
return base.ListAsync(input);
return PagedOutput.Create(total, data.Adapt<List<dynamic>>());
}
/// <summary>
@@ -102,6 +103,7 @@ public class VmodelAppService : VengineAppService<Vmodel>, IVmodelAppService
vm.vmCode = vm.vmCode.ToKebab();
vm.navProps.ForEach(a => a.naviModel = null);
await _db.Updateable(vm).WhereColumns(a => a.id).ExecuteCommandAsync();
await _cache.DelAsync(_dataAccess.GetVmodelCacheKey(vm.id));
return input;
}
@@ -111,6 +113,7 @@ public class VmodelAppService : VengineAppService<Vmodel>, IVmodelAppService
public override async Task<dynamic> DeleteAsync(VmDeleteInput input)
{
var ret = await _db.Deleteable<Vmodel>(input.id).ExecuteCommandAsync();
await _cache.DelAsync(_dataAccess.GetVmodelCacheKey(input.id!));
return ret;
}

View File

@@ -42,15 +42,13 @@ public class VmodelPageAppService : VengineAppService<VmodelPage>, IVmodelPageAp
/// <summary>
/// 获取多条 数据列表
/// </summary>
public override async Task<VmPagedOutput> GetListAsync(VmGetListInput input)
public override async Task<PagedOutput<dynamic>> ListAsync(VmQueryInput input)
{
VmPagedOutput ret = new();
var q = _db.Queryable<VmodelPage>().WhereIF(!string.IsNullOrEmpty(input.k), a => a.code.Contains(input.k!) || a.name.Contains(input.k!));
RefAsync<int> total = 0;
var data = await q.OrderBy(input.sort).ToPageListAsync((input.pnum - 1) * input.psize, input.psize, total);
ret.total = total;
ret.items = data.Adapt<List<DObject>>();
return ret;
return PagedOutput.Create(total, data.Adapt<List<dynamic>>());
}
/// <summary>

View File

@@ -101,6 +101,14 @@ public class DataAccess : IDataAccess, ITransient, IDisposable
return model;
}
/// <summary>
/// 获取 Vmodel 的缓存键
/// </summary>
public string GetVmodelCacheKey(string id)
{
return $"tnb.vmodel:{id}";
}
/// <summary>
/// 获取 Vmodel, 为空时不抛异常
/// </summary>
@@ -119,7 +127,7 @@ public class DataAccess : IDataAccess, ITransient, IDisposable
/// </summary>
public async Task<Vmodel> GetVmodelAsync(string id, bool loadNavigate = false)
{
var key = $"tnb.vmodel:{id}";
var key = GetVmodelCacheKey(id);
var vm = await _cache.GetAsync<Vmodel>(key);
if (vm == null)
{

View File

@@ -28,6 +28,11 @@ public interface IDataAccess : ITransient
/// </summary>
Task<Vmodel?> TryGetVmodelAsync(string id, bool loadNavigate = false);
/// <summary>
/// 获取 Vmodel 的缓存键
/// </summary>
string GetVmodelCacheKey(string id);
/// <summary>
/// 获取 Vmodel, 为空时抛异常
/// </summary>

View File

@@ -11,10 +11,10 @@ namespace Tnb.Vengine.Domain;
public class VmBaseInput
{
///// <summary>
///// 视图模型id
///// </summary>
//public string vmid { get; set; } = string.Empty;
/// <summary>
/// 附加参数
/// </summary>
public object? extra { get; set; }
}
public class VmGetInput : VmBaseInput
@@ -33,6 +33,23 @@ public class VmGetInput : VmBaseInput
/// 输出字段
/// </summary>
public string o { get; set; } = "*";
/// <summary>
/// 转换为QueryInput
/// </summary>
/// <param name="primaryKey"></param>
/// <returns></returns>
public VmQueryInput ToQueryInput(string primaryKey)
{
VmQueryInput arg = this.Adapt<VmQueryInput>();
if (!string.IsNullOrEmpty(id))
{
if (arg.q == null) arg.q = new DObject();
arg.q.Add(primaryKey, id);
}
return arg;
}
}
public class VmGetListInput : VmBaseInput
@@ -67,7 +84,11 @@ public class VmGetListInput : VmBaseInput
/// </summary>
public string o { get; set; } = "*";
public VmQueryInput ToListInput()
/// <summary>
/// 转换为QueryInput
/// </summary>
/// <returns></returns>
public VmQueryInput ToQueryInput()
{
VmQueryInput arg = this.Adapt<VmQueryInput>();
@@ -89,11 +110,6 @@ public class VmQueryInput : VmGetListInput
/// 查询条件
/// </summary>
public new DObject? q { get; set; }
/// <summary>
/// 高级查询
/// </summary>
public DObject? adv { get; set; }
}
/// <summary>
@@ -139,14 +155,32 @@ public class VmDeleteInput : VmBaseInput
public List<string>? ids { get; set; }
}
public class PagedOutput
{
public int total { get; set; }
public object? extra { get; set; }
public static PagedOutput<T> Create<T>(int totalNum, List<T> ls)
{
return new PagedOutput<T>(totalNum, ls);
}
}
/// <summary>
/// 分页列表输出对象
/// </summary>
/// <typeparam name="T"></typeparam>
public class PagedOutput<T>
public class PagedOutput<T> : PagedOutput
{
public int total { get; set; }
public List<T> items { get; set; } = new List<T>();
public PagedOutput()
{
}
public PagedOutput(int totalNum, List<T> ls)
{
total = totalNum;
items = ls;
}
}
/// <summary>
@@ -154,18 +188,12 @@ public class PagedOutput<T>
/// </summary>
public class VmPagedOutput : PagedOutput<DObject>
{
public PagedOutput<T> ToPagedOutput<T>()
{
return new PagedOutput<T>()
{
total = total,
items = items.Adapt<List<T>>()
};
}
}
///// <summary>
///// 查询属性信息
///// </summary>
//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; }
//}

View File

@@ -56,9 +56,9 @@ internal class VmQueryParser
{
// t1.t2.id
List<string> outputs = output.Split(',').Distinct().ToList();
foreach (string? outStr in outputs)
foreach (string? s in outputs)
{
_ = ClearStr(outStr);
var outStr = ClearStr(s);
if (string.IsNullOrWhiteSpace(outStr))
{
continue;
@@ -126,15 +126,15 @@ internal class VmQueryParser
}
string[] orders = sort.Split(',');
foreach (string orderStr in orders)
foreach (string s in orders)
{
_ = ClearStr(orderStr);
var orderStr = ClearStr(s);
if (string.IsNullOrWhiteSpace(orderStr))
{
continue;
}
// 拆分 m.code desc
string[] codes = orderStr.Split(' ', 1);
string[] codes = orderStr.Split(' ', 2);
// 拆分 m.code
(string?, string) orderPath = codes[0].GetParent(NAVI_SEPERATE);
orderPath.Item1 ??= MAIN_ALIES;

View File

@@ -7,10 +7,10 @@ using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using JNPF.Common.Contracts;
using JNPF.Common.Core.Manager;
using JNPF.Common.Extension;
using Mapster;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Tnb.Core;
using Yitter.IdGenerator;
@@ -65,19 +65,19 @@ public partial class Vmodel : Entity
/// 表字段属性
/// </summary>
[SugarColumn(ColumnName = "db_props", IsNullable = false, IsJson = true)]
public List<VmDbProp> dbProps { get; set; } = new List<VmDbProp>();
public List<VmDbProp> dbProps { get; set; } = [];
/// <summary>
/// 导航属性
/// </summary>
[SugarColumn(ColumnName = "nav_props", IsNullable = true, IsJson = true)]
public List<VmNavProp> navProps { get; set; } = new List<VmNavProp>();
public List<VmNavProp> navProps { get; set; } = [];
/// <summary>
/// 计算属性
/// </summary>
[SugarColumn(ColumnName = "cal_props", IsNullable = true, IsJson = true)]
public List<VmCalProp> calProps { get; set; } = new List<VmCalProp>();
public List<VmCalProp> calProps { get; set; } = [];
/// <summary>
/// 排序
@@ -134,8 +134,7 @@ public partial class Vmodel : Entity
public string? modifyId { get; set; }
[SugarColumn(IsIgnore = true)]
public string fullCode
{ get { return areaCode + "/" + vmCode; } }
public string fullCode => areaCode + "/" + vmCode;
/// <summary>
/// 主键
@@ -157,7 +156,7 @@ public partial class Vmodel : Entity
public static Vmodel CreateByEntity(Type tpEntity, string? dbCode = null)
{
Vmodel model = new() { dbCode = dbCode, vmCode = tpEntity.Name };
var sugarTableAttr = tpEntity.GetCustomAttribute<SugarTable>();
SugarTable? sugarTableAttr = tpEntity.GetCustomAttribute<SugarTable>();
if (sugarTableAttr != null)
{
model.tableName = sugarTableAttr.TableName;
@@ -171,12 +170,12 @@ public partial class Vmodel : Entity
{
model.vmName = tpEntity.GetCustomAttribute<DisplayAttribute>()?.Name ?? tpEntity.GetCustomAttribute<DescriptionAttribute>()?.Description ?? model.vmCode;
}
var props = tpEntity.GetProperties(BindingFlags.Public);
PropertyInfo[] props = tpEntity.GetProperties(BindingFlags.Public);
int n = 1;
foreach (var p in props)
foreach (PropertyInfo p in props)
{
VmDbProp prop = new();
var sugarColumn = p.GetCustomAttribute<SugarColumn>();
SugarColumn? sugarColumn = p.GetCustomAttribute<SugarColumn>();
if (sugarColumn != null)
{
prop = sugarColumn.Adapt<VmDbProp>();
@@ -204,10 +203,7 @@ public partial class Vmodel : Entity
/// <returns></returns>
public string? PropToFieldCode(string propCode)
{
if(_mapProps == null)
{
_mapProps = dbProps.ToDictionary(a=>a.code);
}
_mapProps ??= dbProps.ToDictionary(a => a.code);
return _mapProps.GetOrDefault(propCode)?.field;
}
@@ -219,10 +215,7 @@ public partial class Vmodel : Entity
/// <returns></returns>
public VmDbProp? GetDbProp(string propCode)
{
if (_mapProps == null)
{
_mapProps = dbProps.ToDictionary(a => a.code);
}
_mapProps ??= dbProps.ToDictionary(a => a.code);
return _mapProps.GetOrDefault(propCode);
}
@@ -232,8 +225,8 @@ public partial class Vmodel : Entity
/// <returns></returns>
public DObject GetDefaultDObject()
{
DObject obj = new();
foreach (var p in dbProps)
DObject obj = [];
foreach (VmDbProp p in dbProps)
{
obj.Add(p.code, p.GetDefaultValue()!);
}
@@ -246,8 +239,8 @@ public partial class Vmodel : Entity
/// <returns></returns>
public DObject ToCreateEntity(DObject input, IUserManager user)
{
DObject obj = new();
foreach (var p in dbProps)
DObject obj = [];
foreach (VmDbProp p in dbProps)
{
if (input.ContainsKey(p.code))
{
@@ -280,8 +273,8 @@ public partial class Vmodel : Entity
/// <returns></returns>
public DObject ToUpdateEntity(DObject input, IUserManager user)
{
DObject obj = new();
foreach (var p in dbProps)
DObject obj = [];
foreach (VmDbProp p in dbProps)
{
if (input.ContainsKey(p.code))
{

View File

@@ -3,6 +3,7 @@
// https://git.tuotong-tech.com/tnb/tnb.server //
/////////////////////////////////////////////////////////////////////////////////
using JNPF.Common.Contracts;
using SqlSugar;
using Yitter.IdGenerator;

View File

@@ -3,6 +3,7 @@
// https://git.tuotong-tech.com/tnb/tnb.server //
/////////////////////////////////////////////////////////////////////////////////
using JNPF.Common.Contracts;
using Newtonsoft.Json.Linq;
using SqlSugar;
using Yitter.IdGenerator;