示例#1
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <PeekListActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list    = JsonHelper.GetArray(listJson, setting.Source);
                var results = new List <JToken>();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("sourceItem");
                    tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                    if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                    {
                        string targetValue = tmpArgument.Format(setting.Target, true);
                        if (setting.AsString)
                        {
                            results.Add(targetValue);
                        }
                        else
                        {
                            var valueObj = JsonHelper.TryGetObject(targetValue, setting.Target);
                            if (valueObj == null)
                            {
                                results.Add(targetValue);
                            }
                            else
                            {
                                targetValue = JsonConvert.SerializeObject(valueObj);
                                results.Add(valueObj);
                            }
                        }
                    }
                }

                var resultStr = JsonConvert.SerializeObject(results);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
示例#2
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <MapListActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list    = JsonHelper.GetArray(listJson, setting.Source);
                var results = new List <Dictionary <string, string> >();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("mapItem");
                    tmpArgument.PutPrivate("mapItem", listItem.ToString());
                    if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                    {
                        Dictionary <string, string> result = new Dictionary <string, string>();
                        foreach (var rule in setting.Output)
                        {
                            result.Add(rule.Key, tmpArgument.Format(rule.Value, true));
                        }
                        results.Add(result);
                    }
                }

                var resultStr = JsonConvert.SerializeObject(results);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
示例#3
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <GroupListActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list   = JsonHelper.GetArray(listJson, setting.Source);
                var result = new List <Dictionary <string, string> >();
                Dictionary <string, JArray> groupData = new Dictionary <string, JArray>();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("loopItem");
                    tmpArgument.PutPrivate("loopItem", listItem.ToString());
                    var key = tmpArgument.Format(setting.Key, true);
                    if (groupData.ContainsKey(key))
                    {
                        groupData[key].Add(listItem);
                    }
                    else
                    {
                        groupData[key] = new JArray {
                            listItem
                        };
                    }
                }
                foreach (var kvp in groupData)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("groupKey");
                    tmpArgument.ClearKey("groupItems");
                    tmpArgument.PutPrivate("groupKey", kvp.Key.ToString());
                    tmpArgument.PutPrivate("groupItems", kvp.Value.ToString());
                    Dictionary <string, string> results = new Dictionary <string, string>();
                    foreach (var rule in setting.Output)
                    {
                        results.Add(rule.Key, tmpArgument.Format(rule.Value, true));
                    }
                    result.Add(results);
                }

                var resultStr = JsonConvert.SerializeObject(result);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <CompareListActionSetting>(actionSetting);

            try
            {
                var    listStr       = argument.Format(setting.Source);
                var    list          = JsonHelper.GetArray(listStr, setting.Source);
                string methodSetting = JsonConvert.SerializeObject(setting.ActionSetting);

                bool result = false;
                switch (setting.Comparer)
                {
                case "any":
                    result = list.Any(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                case "all":
                    result = list.All(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                case "notany":
                    result = !list.Any(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                case "notall":
                    result = !list.All(x =>
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", x.ToString());

                        return(ExecuteAction(setting.Action, methodSetting, tmpArgument).Result?.Success == true);
                    });
                    break;

                default:
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Fail = true,
                        Message = $"Comparer ({setting.Comparer}) is not known"
                    }));
                }
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", result.ToString() }
                    },
                    Data = result.ToString()
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message
                }));
            }
        }
示例#5
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <ExpandActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list   = JsonHelper.GetArray(listJson, setting.Source);;
                var result = new List <Dictionary <string, JToken> >();
                foreach (var listItem in list)
                {
                    var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                    tmpArgument.ClearKey("sourceItem");
                    tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                    var target = setting.Target;
                    if (target.StartsWith("{{") && target.EndsWith("}}"))
                    {
                        target = target.Substring(2, target.Length - 4);
                    }

                    var targetJson = tmpArgument.Format($"{{{{sourceItem.{target}}}}}", true);
                    var targetList = JsonHelper.GetArray(targetJson, target);
                    foreach (var targetItem in targetList)
                    {
                        var tmpArgument2 = new ArgumentProvider(tmpArgument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("targetItem");
                        tmpArgument2.PutPrivate("targetItem", targetItem.ToString());

                        if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument2) ?? false) == true)
                        {
                            Dictionary <string, JToken> results = new Dictionary <string, JToken>();
                            foreach (var rule in setting.Output)
                            {
                                string targetValue = tmpArgument2.Format(rule.Value, true);
                                if (JsonHelper.IsObject(targetValue))
                                {
                                    results.Add(rule.Key, JsonHelper.TryGetObject(targetValue, rule.Value));
                                }
                                else if (JsonHelper.IsArray(targetValue))
                                {
                                    results.Add(rule.Key, JsonHelper.TryGetArray(targetValue, rule.Value));
                                }
                                else
                                {
                                    results.Add(rule.Key, targetValue);
                                }
                            }
                            result.Add(results);
                        }
                    }
                }

                var resultStr = JsonConvert.SerializeObject(result);
                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = true,
                    Output = new Dictionary <string, string> {
                        { "result", resultStr }
                    },
                    Data = resultStr
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }
示例#6
0
        /// <summary>
        /// execute
        /// </summary>
        /// <param name="actionSetting"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        public Task <ActionExecuteResult> Execute(string actionSetting, ArgumentProvider argument)
        {
            var setting = JsonConvert.DeserializeObject <AggregateActionSetting>(actionSetting);

            var listJson = argument.Format(setting.Source);

            try
            {
                var list = JsonHelper.GetArray(listJson, setting.Source);
                if (string.Equals("sum", setting?.Action, StringComparison.CurrentCultureIgnoreCase))
                {
                    // sum
                    var sum = 0;
                    foreach (var listItem in list)
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                        if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                        {
                            var targetJson = tmpArgument.Format(setting.Target, true);
                            sum += targetJson.ToNullableInt() ?? 0;
                        }
                    }
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Success = true,
                        Output = new Dictionary <string, string> {
                            { "result", $"{sum}" }
                        },
                        Data = $"{sum}"
                    }));
                }
                else if (string.Equals("count", setting?.Action, StringComparison.CurrentCultureIgnoreCase))
                {
                    // count
                    var count = 0;
                    foreach (var listItem in list)
                    {
                        var tmpArgument = new ArgumentProvider(argument.WorkingArguments.Copy());
                        tmpArgument.ClearKey("sourceItem");
                        tmpArgument.PutPrivate("sourceItem", listItem.ToString());
                        if (setting?.Where?.Any() != true || setting?.Where?.Any(x => x.Indicate(tmpArgument) ?? false) == true)
                        {
                            count++;
                        }
                    }
                    return(Task.FromResult(new ActionExecuteResult
                    {
                        Success = true,
                        Output = new Dictionary <string, string> {
                            { "result", $"{count}" }
                        },
                        Data = $"{count}"
                    }));
                }

                return(Task.FromResult(new ActionExecuteResult
                {
                    Success = false,
                    Output = new Dictionary <string, string> {
                        { "result", "Unknow action" }
                    },
                    Data = "Unknow action"
                }));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new ActionExecuteResult
                {
                    Fail = true,
                    Message = ex.Message,
                    Output = new Dictionary <string, string> {
                    },
                    Data = string.Empty
                }));
            }
        }