diff --git a/Entitys/InstanceData.cs b/Entitys/InstanceData.cs index dcb80550c3ccd42aec4e8ee8f43206a49953c41b..323920b196819845a8e5bbfd3197be6496366ab1 100644 --- a/Entitys/InstanceData.cs +++ b/Entitys/InstanceData.cs @@ -165,6 +165,7 @@ namespace anydata.Entitys public string id { get; set; } public string code { get; set; } public string attr { get; set; } + public string queryRule { get; set; } public string valueType { get; set; } public bool? isChangeTarget { get; set; } public bool? isChangeSource { get; set; } diff --git a/Entitys/XAttribute.cs b/Entitys/XAttribute.cs index 97641d75f0e717d9d1f3c5ff1128ec337dcbb506..06123fdf34fc8e176ac354b5c0bfcfd82164f058 100644 --- a/Entitys/XAttribute.cs +++ b/Entitys/XAttribute.cs @@ -12,6 +12,11 @@ namespace anydata.Entitys get => this["rule"] as string; set => this["rule"] = value; } + public string queryRule + { + get => this["queryRule"] as string; + set => this["queryRule"] = value; + } public string propId { get => this["propId"] as string; @@ -37,6 +42,7 @@ namespace anydata.Entitys code = "T" + propId, attr = code, valueType = property.valueType, + queryRule = queryRule }; } } diff --git a/Extensions/DataProviderExtensions.cs b/Extensions/DataProviderExtensions.cs index 1c4279503bbe77c055e10ce65f5ce9ead1846098..561ddd23c5593d573a65cd12ff48f686dcc730ed 100644 --- a/Extensions/DataProviderExtensions.cs +++ b/Extensions/DataProviderExtensions.cs @@ -128,7 +128,7 @@ namespace anydata.Extensions } public static class FormProviderExtensions { - public const string FormCollName = "standard-form"; + public const string FormCollName = "standard-view"; public const string SpeciesItemCollName = "standard-species-item"; public static async Task findForm(this DataProvider provider, TokenModel token, string id) diff --git a/Providers/DataProvider.cs b/Providers/DataProvider.cs index 4921fb962056665f92f14d445fa2b9de37326acf..0a5c0cfecd692db789d2f77827941a316b1e81c4 100644 --- a/Providers/DataProvider.cs +++ b/Providers/DataProvider.cs @@ -117,8 +117,8 @@ namespace anydata.Providers var form = await this.findForm(token, options.formId); if (form is not null) { - var svc = services.GetRequiredService(); - results.data = svc.Converting(results.data, form); + results.data = services.GetRequiredService().Converting(results.data, form); + services.GetRequiredService().Converting(results.data, form); } } return OperateResult.Success(results); diff --git a/Services/RuleService.cs b/Services/RuleService.cs new file mode 100644 index 0000000000000000000000000000000000000000..ac665b76c80a04cd6bdb3da456f26e7dcf171be8 --- /dev/null +++ b/Services/RuleService.cs @@ -0,0 +1,49 @@ +using anydata.Entitys; +using Newtonsoft.Json.Linq; +using System.Collections; +using System.Text.RegularExpressions; +using Jint; +using Newtonsoft.Json; + +namespace anydata.Services +{ + public class RuleService : ITransient + { + public void Converting(IEnumerable datas, XForm? form) + { + foreach (EntityBase data in datas) + { + foreach (var field in form.fields) + { + var queryRule = field.queryRule; + if (string.IsNullOrEmpty(queryRule)) continue; + + var rule = JsonConvert.DeserializeObject>(queryRule); + if (rule?["formula"] is not string formula || rule["mappingData"] is not JArray mappingData) + { + continue; + } + + if (mappingData.Count > 0) + { + var codes = mappingData.Select(item => item["code"]?.ToString()) + .Where(code => !string.IsNullOrEmpty(code)).ToList(); + formula = ReplaceCodesInFormula(data, codes, formula); + } + + data[field.attr] = new Engine().SetValue("data", data).Evaluate(formula).ToObject(); + } + } + } + + private string ReplaceCodesInFormula(EntityBase data, List codes, string formula) + { + foreach (var code in codes) + { + formula = Regex.Replace(formula, $@"\b{Regex.Escape(code)}\b", data[code]?.ToString() ?? string.Empty); + } + + return formula; + } + } +} \ No newline at end of file diff --git a/Services/ThingConvertService.cs b/Services/ThingConvertService.cs index a8471adda8ed2dfc165028b00d68641384ea375c..a30d38d1d4054f4f92a072e71baeecb0ddc84adf 100644 --- a/Services/ThingConvertService.cs +++ b/Services/ThingConvertService.cs @@ -7,16 +7,16 @@ namespace anydata.Services { public class ThingConvertService : ITransient { - public List Converting(IEnumerable data, XForm form) + public List Converting(IEnumerable data, XForm form) { - var result = new List(); + var result = new List(); foreach (var raw in data) { if (raw is not EntityBase thing) { continue; } - var clone = new JObject + var clone = new EntityBase { ["id"] = thing.id, ["name"] = thing.name, diff --git a/anydata.csproj b/anydata.csproj index 985bc4cf6cb7a79d4ed5433a3156ec731f5edabe..c3ac14547589ca7f0255578b65a998ad8f5ee286 100644 --- a/anydata.csproj +++ b/anydata.csproj @@ -26,6 +26,7 @@ +