//@@@这一行不要删除
var data=data; //JSON对象
var typedefs = new Map();
var roomMap = new Map();
var 当前订单 = null;
var 当前柜体对象 = null;
var ROOT = {
propRefs: "designData.designName->终端客户",
props: [{
name: "产品表",
type: "Array",
propRef: "paramModel",
subPropType: "GT",
onAdd: function (本节点对象, 子节点对象, 原始子节点对象) {
当前订单 = 本节点对象;
当前订单.产品表.push(子节点对象);
//将房间信息写入到订单对象
子节点对象.房间 = roomMap.get(原始子节点对象.roomId);
}
}],
afterSetPropsVal: function (本节点对象, 源对象) {
本节点对象.设计师 = "张三";
本节点对象.经销商 = "上海伯爵";
}
}
var GT = {
typename: "GT",
propRefs: `id,modelName->产品名称,modelProductNumber->产品型号,modelProductNumber->对方货号,
baseTexture->基材,textureName->颜色,
size.x->宽,size.y->高,size.z->厚`,
props: [
{
name: "部件列表",
type: "Array",
propRef: "subModels",
subPropType: "DTL",
onAdd: function (本节点对象, 子节点对象, 原始子节点对象) {
//可判断是否加入子节点
当前柜体对象 = 本节点对象;
当前柜体对象.部件列表.push(子节点对象);
}
},
{
name: "深1",
type: "Object",
propRef: "parameters",
//循环关联属性列表,返回对应的值
getValFun: function (bean) {
if (bean.name == "D") {
return bean.value;
}
return "-";
},
}
],
afterSetPropsVal: function (本节点对象, 源对象) {
本节点对象.对方货号 = "GT"
}
}
var DTL = {
typename: "DTL",
propRefs: `id,roomId,modelName->产品名称,modelNumber->产品型号,
baseTexture->基材,textureName->颜色,
size.x->宽,size.y->高,size.z->厚`,
props: [
{
name: "部件列表",
type: "Array",
propRef: "subModels",
subPropType: "DTL",
onAdd: function (本节点对象, 子节点对象, 原始子节点对象) {
//因板件有多层的结构,需要判断哪些是要导入到对应柜体子表的,哪些是要导入到订单明细表的
// if(原始子节点对象xxxx xxxx){
// 当前订单.产品表.push(子节点对象);
// }
if (原始子节点对象.modelNumber != null && 原始子节点对象.modelNumber != "") {
当前柜体对象.部件列表.push(子节点对象);
}
}
},
{
name: "深1",
type: "Object",
propRef: "parameters",
getValFun: function (bean) {
if (bean.name == "D") {
return bean.value;
}
return "-";
},
}
],
afterSetPropsVal: function (本节点对象, 源对象) {
}
}
//将类型加入至缓存
typedefs.set(GT.typename, GT);
typedefs.set(DTL.typename, DTL);
//定义结果
var res = {};
//按配置设置对象的属性值
function setBeanProp(source, target, propRefs) {
function getDeepVal(fsource, sprop) {
var fsprop = sprop.split(".");
if (fsprop.length == 1) {
return fsource[sprop];
} else {
var prop = fsprop[0];
var f = fsource[prop];
fsprop = sprop.substr(prop.length + 1);
return getDeepVal(fsource[prop], fsprop)
}
}
propRefs = propRefs.replace(/\r\n/g, "");
propRefs = propRefs.replace(/\s+/g, "");
var props = propRefs.split(",");
for (var i = 0; i < props.length; i++) {
prop = props[i];
var sprop = prop;
var tprop = prop;
if (prop.indexOf("->") > -1) {
var al = prop.split("->");
tprop = al[1];
sprop = al[0];
}
if (sprop.indexOf(".") == -1) {
target[tprop] = source[sprop];
} else {
target[tprop] = getDeepVal(source, sprop);
}
//console.log(sprop+"->"+tprop);
}
}
//设置对象的数组
function dealWithBeanPropArrayData(sourceBean, targetBean, propdef) {
try {
//先将原始对象的子节点赋值空数组
targetBean[propdef.name] = [];
sourceBean[propdef.propRef] = sourceBean[propdef.propRef] == null ? [] : sourceBean[propdef.propRef];
//循环源对象的数组
for (var i = 0; i < sourceBean[propdef.propRef].length; i++) {
//源对象-数组-单对象
var oldSourceBean = sourceBean[propdef.propRef][i];
//定义新的子节点 -
var newTargetBean = {};
//检查是否需要添加至目标节点
if (propdef.onAdd == null) {
targetBean[propdef.name].push(newTargetBean);
} else {
propdef.onAdd(targetBean, newTargetBean, oldSourceBean);
}
//获取子节点对象类型
var newTypeDef = typedefs.get(propdef.subPropType);
//对新对象赋值
if (oldSourceBean != null) {
setBeanPropsVal(oldSourceBean, newTargetBean, newTypeDef);
}
}
} catch (e) {
throw "在处理对象Id:" + sourceBean.id + "产品名称:" +
sourceBean.modelName + "时错误,dealWithBeanArrayData;" + e;
}
}
//设置对象的特殊属性
function dealWithBeanPropObjectData(sourceBean, targetBean, propdef) {
//将ref对象类型的循环并带入,执行函数并返回值
for (var i = 0; i < sourceBean[propdef.propRef].length; i++) {
//关联的属性对象
var refBean = sourceBean[propdef.propRef][i];
if (propdef.getValFun == null) {
throw propdef.name + "属性,未定义getValFun";
}
var value = propdef.getValFun(refBean);
if (value != "-") {
//如果有值直接返回
targetBean[propdef.name] = value;
return;
}
}
targetBean[propdef.name] = null;
}
//设置对象的属性
function setBeanPropsVal(sourceBean, targetBean, typedef) {
//如果定义的关联属性存在
if (typedef.propRefs != null && typedef.propRefs != "") {
setBeanProp(sourceBean, targetBean, typedef.propRefs);
}
typedef.props != typedef.props == null ? [] : typedef.props;
for (var i = 0; i < typedef.props.length; i++) {
var propdef = typedef.props[i];
if (propdef.type == "Array") {
dealWithBeanPropArrayData(sourceBean, targetBean, propdef);
} else if (propdef.type == "Object") {
dealWithBeanPropObjectData(sourceBean, targetBean, propdef);
}
}
//在设置属性之后
if (typedef.afterSetPropsVal) {
typedef.afterSetPropsVal(targetBean, sourceBean);
}
}
//先处理房间列表
var roomList = data.designData.roomDataList;
roomList.forEach(room => {
roomMap.set(room.roomId, room.name);
})
//开始设置结果的属性
setBeanPropsVal(data, res, ROOT);
console.log(res);
//设置root的其他属性
console