w-vue-middle
Version:
统一公共服务组件
257 lines (244 loc) • 8.18 kB
JavaScript
/*
* @Author: Jason Liu
* @Date: 2023-02-01 13:24:33
* @Desc:
*/
import draggable from "vuedraggable";
export default {
components: { draggable },
name: "unionDesign",
props: {
value: {
type: Boolean,
default: false,
},
fields: { //设置的字段
type: Array,
default: () => { return []; },
},
models: { //联合的模型
type: Array,
default: () => { return []; },
},
mainModel: { //主模型状态
type: Object,
default: {
tables: []
},
},
},
data() {
return {
loading: false,
show: this.value,
mapFields: this.fields, //字段映射
unionModels: [], //关联模型
tablekey: 0,
activeId: undefined, //激活的ID
moveRow: {
row: undefined,
i: undefined
},
showModel: []
};
},
methods: {
/**
* @Author: Jason Liu
* @description: 过滤参照模型
*/
filterModel() {
if (this.show) {
this.showModel = this.unionModels.filter(col => {
let has = true;
this.mapFields.findIndex(field => {
field.maps.forEach(item => {
if (item.id == col.id) {
has = false;
}
})
})
return has;
});
} else {
this.showModel = [];
}
},
/**
* @Author: Jason Liu
* @description: 确认事件
*/
fixed() {
this.$emit("change", this.mapFields);
this.show = false;
},
/**
* @Author: Jason Liu
* @description: 结束克隆事件
*/
endClone() {
this.activeId = undefined;
},
/**
* @Author: Jason Liu
* @description: 克隆事件
*/
cloneColumnsEntry(row) {
this.activeId = row.parentId;
return row;
},
/**
* @Author: Jason Liu
* @description: 内容拖入时间
*/
changeEntry(e, row, i) {
if (e.added) {
let newC = e.added.element;
if (this.moveRow.row) {
let refmap = row.maps[i]
this.moveRow.row.maps[i] = refmap;
}
row.maps[i] = newC;
this.filterModel();
this.moveRow = {
row: undefined,
i: undefined
};
}
//this.$refs.modelList.updateData();
},
/**
* @Author: Jason Liu
* @description: 移动
*/
moveData(e, row, i) {
let data = row.maps[i];
this.moveRow = {
row: row,
i: i
}
this.activeId = data.parentId;
return data;
},
/**
* @Author: Jason Liu
* @description: 删除设置
*/
deleteColumn(row, i) {
row.maps[i] = {
code: undefined,
columnId: undefined,
columnName: undefined,
columns: undefined,
id: undefined,
name: undefined,
parentId: row.maps[i].parentId,
tableId: undefined
}
this.filterModel();
this.$refs.fieldsList.updateData();
},
/**
* @Author: Jason Liu
* @description: 初始化映射字段
*/
initMapFields() {
//拆解关联模型映射信息
this.unionModels = [];
let mapModels = []; //映射类型
this.models.forEach(model => {
let newModel = { code: model.modelInfo.code, id: model.modelInfo.id, name: model.modelInfo.name };
mapModels.push({
...newModel,
parentId: model.modelInfo.id,
id: undefined,
tableId: undefined,
columnId: undefined,
code: undefined,
columnName: undefined,
name: undefined,
show: true
})
this.unionModels.push(newModel);
model.tables.forEach(table => {
table.columns.forEach(column => {
this.unionModels.push({
id: `${newModel.id}_${table.tableId}_${column.id}`,
tableId: table.tableId,
columnId: column.id,
parentId: newModel.id,
code: column.code,
columnName: column.columnName,
name: column.name,
show: true
});
})
});
});
//初始化映射字段
this.mapFields = [];
let oldfields = this.fields;
this.mainModel.tables.forEach(table => {
table.columns.forEach(column => {
let newField = {
id: `${table.tableId}_${column.id}`,
tableId: table.tableId,
columnId: column.id,
code: column.code,
columnName: column.columnName,
name: column.name,
maps: [...mapModels]
}
let findIndex = oldfields.findIndex(field => { return field.id == newField.id });
if (findIndex > -1) {
let tempmaps = oldfields[findIndex].maps; //清洗模型信息
newField.maps = newField.maps.map(model => {
let oldmodel = tempmaps.find(old => { return old.parentId == model.parentId }); //找到原始对象
if (oldmodel) {
let column = this.unionModels.find(col => {
return col.id == oldmodel.id;
}); //查找字段是否有效
if (column) {
model = column;
}
}
return model;
})
}
this.mapFields.push(newField);
});
});
this.filterModel();
},
/**
* @Author: Jason Liu
* @description: 智能解析
*/
intelligentAnalysis() {
this.mapFields.forEach(fields => {
fields.maps = fields.maps.map(map => {
let column = this.unionModels.find(col => {
return col.parentId == map.parentId && col.code == fields.code;
}); //查找字段是否有效
console.log(column)
if (column) {
map = column;
}
return map;
});
});
this.filterModel();
},
},
watch: {
value(val) {
this.show = val;
if (val) {
this.initMapFields();
}
},
show(val) {
this.$emit("input", val);
},
},
};