using JNPF.Common.Const;
using JNPF.Common.Extension;
namespace JNPF.VisualDev.Engine.Security;
///
/// 代码生成 统一处理帮助类.
///
public class CodeGenUnifiedHandlerHelper
{
///
/// 统一处理表单内控件.
///
///
public static List UnifiedHandlerFormDataModel(List formDataModel, ColumnDesignModel pcColumnDesignModel, ColumnDesignModel appColumnDesignModel, bool isMain = true, string tableControlsKey = "")
{
var template = new List();
// 循环表单内控件
foreach (var item in formDataModel)
{
var config = item.__config__;
switch (config.jnpfKey)
{
case JnpfKeyConst.TABLE:
item.__config__.children = UnifiedHandlerFormDataModel(item.__config__.children, pcColumnDesignModel, appColumnDesignModel, false, item.__vModel__);
template.Add(item);
break;
default:
{
if (isMain)
{
// 是否为PC端查询字段与移动端查询字段
bool pcSearch = (pcColumnDesignModel?.searchList?.Any(it => it.__vModel__.Equals(item.__vModel__))).ParseToBool();
bool appSearch = (appColumnDesignModel?.searchList?.Any(it => it.__vModel__.Equals(item.__vModel__))).ParseToBool();
if (pcSearch || appSearch)
item.isQueryField = true;
else
item.isQueryField = false;
bool pcColumn = (pcColumnDesignModel?.columnList?.Any(it => it.__vModel__.Equals(item.__vModel__))).ParseToBool();
bool appColumn = (appColumnDesignModel?.columnList?.Any(it => it.__vModel__.Equals(item.__vModel__))).ParseToBool();
if (pcColumn || appColumn)
item.isIndexShow = true;
else
item.isIndexShow = false;
}
else
{
bool pcSearch = (pcColumnDesignModel?.searchList?.Any(it => it.__vModel__.Equals(string.Format("{0}-{1}", tableControlsKey, item.__vModel__)))).ParseToBool();
bool appSearch = (appColumnDesignModel?.searchList?.Any(it => it.__vModel__.Equals(string.Format("{0}-{1}", tableControlsKey, item.__vModel__)))).ParseToBool();
if (pcSearch || appSearch)
item.isQueryField = true;
else
item.isQueryField = false;
bool pcColumn = (pcColumnDesignModel?.columnList?.Any(it => it.__vModel__.Equals(string.Format("{0}-{1}", tableControlsKey, item.__vModel__)))).ParseToBool();
bool appColumn = (appColumnDesignModel?.columnList?.Any(it => it.__vModel__.Equals(string.Format("{0}-{1}", tableControlsKey, item.__vModel__)))).ParseToBool();
if (pcColumn || appColumn)
item.isIndexShow = true;
else
item.isIndexShow = false;
}
template.Add(item);
}
break;
}
}
return template;
}
///
/// 统一处理表单内控件.
///
///
public static List UnifiedHandlerFormDataModel(List formDataModel, ColumnDesignModel columnDesignModel, bool isMain = true, string tableControlsKey = "")
{
var template = new List();
// 循环表单内控件
formDataModel.ForEach(item =>
{
var config = item.__config__;
switch (config.jnpfKey)
{
case JnpfKeyConst.TABLE:
item.__config__.children = UnifiedHandlerFormDataModel(item.__config__.children, columnDesignModel, false, item.__vModel__);
template.Add(item);
break;
default:
{
if (isMain)
{
// 是否为PC端查询字段与移动端查询字段
bool search = (bool)columnDesignModel?.searchList?.Any(it => it.__vModel__.Equals(item.__vModel__));
if (search)
item.isQueryField = true;
else
item.isQueryField = false;
bool column = (bool)columnDesignModel?.columnList?.Any(it => it.__vModel__.Equals(item.__vModel__));
if (column)
item.isIndexShow = true;
else
item.isIndexShow = false;
}
else
{
bool search = (bool)columnDesignModel?.searchList?.Any(it => it.__vModel__.Equals(string.Format("{0}-{1}", tableControlsKey, item.__vModel__)));
if (search)
{
item.isQueryField = true;
item.superiorVModel = tableControlsKey;
}
else
{
item.isQueryField = false;
}
bool column = (bool)columnDesignModel?.columnList?.Any(it => it.__vModel__.Equals(string.Format("{0}-{1}", tableControlsKey, item.__vModel__)));
if (column)
item.isIndexShow = true;
else
item.isIndexShow = false;
}
template.Add(item);
}
break;
}
});
return template;
}
///
/// 联动关系链判断.
///
///
///
///
///
///
public static List LinkageChainJudgment(List formDataModel, ColumnDesignModel columnDesignModel, bool isMain = true, string tableControlsKey = "")
{
var NewFormDataModel = formDataModel.Copy();
var childrenFormModel = new List();
if (!isMain)
{
formDataModel = NewFormDataModel.Find(it => it.__vModel__.Equals(tableControlsKey) && it.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE)).__config__.children;
childrenFormModel = formDataModel.Copy();
}
formDataModel.ForEach(item =>
{
var config = item.__config__;
switch (config.jnpfKey)
{
case JnpfKeyConst.TABLE:
{
NewFormDataModel = LinkageChainJudgment(NewFormDataModel, columnDesignModel, false, item.__vModel__);
}
break;
case JnpfKeyConst.RADIO:
case JnpfKeyConst.CHECKBOX:
case JnpfKeyConst.SELECT:
case JnpfKeyConst.CASCADER:
case JnpfKeyConst.TREESELECT:
switch (isMain)
{
case true:
// dataType = dynamic && templateJson属性有长度,则代表有远端联动
if (config.dataType == "dynamic" && config.templateJson?.Count() > 0)
{
config.templateJson.FindAll(it => it.relationField != null && it.relationField.Any()).ForEach(items =>
{
var fieldModel = NewFormDataModel.Where(it => it.__vModel__.Equals(items.relationField) && it.__config__.jnpfKey.Equals(items.jnpfKey)).FirstOrDefault();
fieldModel.IsLinked = true;
List linkageConfigs = new List
{
new LinkageConfig()
{
field = item.__vModel__,
fieldName = item.__vModel__.ToLowerCase(),
jnpfKey = config.jnpfKey,
IsMultiple = config.jnpfKey.Equals(JnpfKeyConst.CASCADER) ? item.props.props.multiple : config.jnpfKey.Equals(JnpfKeyConst.CHECKBOX)? true: item.multiple,
}
};
fieldModel.linkageReverseRelationship.AddRange(linkageConfigs);
});
}
break;
default:
if (config.dataType == "dynamic" && config.templateJson?.Count() > 0)
{
var childrenFieldModel = childrenFormModel.Where(it => item.__vModel__.Equals(it.__vModel__) && it.__config__.jnpfKey.Equals(config.jnpfKey)).FirstOrDefault();
childrenFieldModel.IsLinkage = true;
config.templateJson.FindAll(it => it.relationField != null && it.relationField.Any()).ForEach(items =>
{
var isTrigger = false;
var fieldModel = childrenFormModel.Where(it => items.relationField.Equals(string.Format("{0}-{1}", tableControlsKey, it.__vModel__)) && it.__config__.jnpfKey.Equals(items.jnpfKey)).FirstOrDefault();
if (fieldModel == null)
{
isTrigger = true;
fieldModel = NewFormDataModel.Where(it => it.__vModel__.Equals(items.relationField) && it.__config__.jnpfKey.Equals(items.jnpfKey)).FirstOrDefault();
}
fieldModel.IsLinked = true;
List linkageConfigs = new List
{
new LinkageConfig()
{
field = item.__vModel__,
fieldName = tableControlsKey,
jnpfKey = config.jnpfKey,
isChildren = isTrigger,
IsMultiple = config.jnpfKey.Equals(JnpfKeyConst.CASCADER) ? item.props.props.multiple : item.multiple,
}
};
fieldModel.linkageReverseRelationship.AddRange(linkageConfigs);
});
}
break;
}
break;
case JnpfKeyConst.POPUPTABLESELECT:
case JnpfKeyConst.POPUPSELECT:
switch (isMain)
{
case true:
var mainFieldModel = NewFormDataModel.Where(it => item.__vModel__.Equals(it.__vModel__) && it.__config__.jnpfKey.Equals(config.jnpfKey)).FirstOrDefault();
mainFieldModel.IsLinkage = true;
item.templateJson?.FindAll(it => it.relationField != null && it.relationField.Any()).ForEach(items =>
{
var fieldModel = NewFormDataModel.Where(it => it.__vModel__.Equals(items.relationField) && it.__config__.jnpfKey.Equals(items.jnpfKey)).FirstOrDefault();
if (fieldModel != null)
{
fieldModel.IsLinked = true;
List linkageConfigs = new List
{
new LinkageConfig()
{
field = item.__vModel__,
fieldName = item.__vModel__.ToLowerCase(),
jnpfKey = config.jnpfKey,
IsMultiple = config.jnpfKey.Equals(JnpfKeyConst.CASCADER) ? item.props.props.multiple : config.jnpfKey.Equals(JnpfKeyConst.CHECKBOX) ? true : item.multiple,
}
};
fieldModel.linkageReverseRelationship.AddRange(linkageConfigs);
}
});
break;
default:
var childrenFieldModel = childrenFormModel.Where(it => item.__vModel__.Equals(it.__vModel__) && it.__config__.jnpfKey.Equals(config.jnpfKey)).FirstOrDefault();
childrenFieldModel.IsLinkage = true;
item.templateJson?.FindAll(it => it.relationField != null && it.relationField.Any()).ForEach(items =>
{
var isTrigger = false;
var fieldModel = childrenFormModel.Where(it => items.relationField.Equals(string.Format("{0}-{1}", tableControlsKey, it.__vModel__)) && it.__config__.jnpfKey.Equals(items.jnpfKey)).FirstOrDefault();
if (fieldModel == null)
{
isTrigger = true;
fieldModel = NewFormDataModel.Where(it => it.__vModel__.Equals(items.relationField) && it.__config__.jnpfKey.Equals(items.jnpfKey)).FirstOrDefault();
}
fieldModel.IsLinked = true;
List linkageConfigs = new List
{
new LinkageConfig()
{
field = item.__vModel__,
fieldName = tableControlsKey,
jnpfKey = config.jnpfKey,
isChildren = isTrigger,
IsMultiple = config.jnpfKey.Equals(JnpfKeyConst.CASCADER) ? item.props.props.multiple : item.multiple,
}
};
fieldModel.linkageReverseRelationship.AddRange(linkageConfigs);
});
break;
}
break;
}
});
if (!isMain)
{
NewFormDataModel.Find(it => it.__vModel__.Equals(tableControlsKey) && it.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE)).__config__.children = childrenFormModel;
}
return NewFormDataModel;
}
///
/// 统一处理控件关系.
///
/// 控件列表.
///
public static List UnifiedHandlerControlRelationship(List formDataModel, bool isMain = true)
{
formDataModel.ForEach(item =>
{
switch (item.__config__.jnpfKey)
{
case JnpfKeyConst.RELATIONFORM:
{
var list = formDataModel.FindAll(it => it.__config__.jnpfKey.Equals(JnpfKeyConst.RELATIONFORMATTR) && it.relationField.Equals(string.Format("{0}_jnpfTable_{1}{2}", item.__vModel__, item.__config__.tableName, isMain ? 1 : 0)) && it.__config__.isStorage.Equals(1));
item.relational = string.Join(",", list.Select(it => it.showField).ToList());
}
break;
case JnpfKeyConst.TABLE:
{
item.__config__.children = UnifiedHandlerControlRelationship(item.__config__.children, false);
}
break;
case JnpfKeyConst.POPUPSELECT:
{
var list = formDataModel.FindAll(it => it.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPATTR) && it.relationField.Equals(string.Format("{0}_jnpfTable_{1}{2}", item.__vModel__, item.__config__.tableName, isMain ? 1 : 0)) && it.__config__.isStorage.Equals(1));
item.relational = string.Join(",", list.Select(it => it.showField).ToList());
}
break;
}
});
return formDataModel;
}
}