using System.Data;
using System.Text.RegularExpressions;
using Common;
using Infrastructure;
using LitJson;
using Model;
using Model.Custom;
using Model.Source;
namespace Util.Logic
{
public class LogicCacheNodeHelper
{
///
/// 设置Redis对象节点
///
/// 节点数据
/// 参数列表
/// 父节点参数列表
public static void setRedisObject(NodeList node, List paramList, List logDic)
{
Dictionary condi = new Dictionary(); // 条件参数字典
JsonData nodeData = node.nodeData;
JsonData redisObjectFields = nodeData["data"]["formData"]["output"]["value"]; // Redis对象参数字段
string paramAttribute = "";
if (nodeData.ToJson().Contains("\"parentNode\""))
{
paramAttribute = "iterationInputField";
}
foreach (JsonData item in redisObjectFields)
{
string paramName = item["label"].ToString(); // Redis对象参数字段名
MatchCollection matches = Regex.Matches(paramName, "\\$\\{.*?\\}");
if(matches.Count > 0)
{
foreach(Match match in matches)
{
string key = match.Value.Replace("${", "").Replace("}", "");
string value = paramList.FirstOrDefault(x => x.paramName == key && x.paramAttribute == paramAttribute).paramValue;
paramName = paramName.Replace(match.Value, value);
}
}
string paramValue = item["value"].ToString(); // Redis对象参数字段值
if(paramValue.Contains("."))
{
paramValue = paramValue.Substring(paramValue.LastIndexOf(".") + 1);
var param = paramList.FirstOrDefault(x => x.paramName == paramValue && x.paramAttribute == paramAttribute) ?? paramList.FirstOrDefault(x => x.paramName == paramValue) ?? new ParamList();
paramValue = param.paramValue;
}// 聚合字段类型
RedisServer.Cache.Set("redisObject:" + paramName, paramValue);
}
logDic.Add(new LogItem()
{
title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
param = condi
});
}
///
/// 获取Redis对象节点
///
/// 节点数据
/// 参数列表
/// 父节点参数列表
public static void getRedisObject(NodeList node, List paramList, List logDic)
{
Dictionary condi = new Dictionary(); // 条件参数字典
JsonData nodeData = node.nodeData;
JsonData redisObjectFields = nodeData["data"]["formData"]["output"]["value"]; // Redis对象参数字段
string paramAttribute = "";
if (nodeData.ToJson().Contains("\"parentNode\""))
{
paramAttribute = "iterationInputField";
}
foreach (JsonData item in redisObjectFields)
{
string paramName = item["label"].ToString(); // Redis对象参数字段名
string paramFieldName = paramName; // 字段名
MatchCollection matches = Regex.Matches(paramName, "\\$\\{.*?\\}");
if(matches.Count > 0)
{
foreach(Match match in matches)
{
string key = match.Value.Replace("${", "").Replace("}", "");
var paramObj = paramList.FirstOrDefault(x => x.paramName == key && x.paramAttribute == paramAttribute);
string value = paramObj.paramValue;
paramName = paramName.Replace(match.Value, value);
paramFieldName = paramFieldName.Replace(match.Value, key);
}
}
string paramType = item["type"][0].ToString(); // 字段类型
string getValue = RedisServer.Cache.Get("redisObject:" + paramName);
if(paramType == "number") getValue = Function.CheckNum(getValue);
var param = paramList.FirstOrDefault(x => x.paramName == paramName && x.paramAttribute == paramAttribute);
if(param == null)
{
paramList.Add(new ParamList()
{
nodeId = node.nodeId,
paramAttribute = paramAttribute,
paramName = paramFieldName,
paramValue = getValue,
paramType = paramType
});
}
else
{
param.paramValue = getValue;
}
condi.Add(paramFieldName, getValue);
}
logDic.Add(new LogItem()
{
title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
param = condi
});
}
}
}