UNPKG

w-vue-middle

Version:

统一公共服务组件

510 lines (501 loc) 13.9 kB
/* * @Author: Jason Liu * @Date: 2023-05-10 16:35:00 * @Desc: */ const glSqlEditor = require('w-vue-middle/components/glSqlEditor').default; const { jobVariable } = require('w-vue-middle/jobProcessService'); export default { components: { glSqlEditor, jobVariable }, props: { value: { type: Object, default: () => { return { tables: [], joins: [], }; }, }, referData: { type: Object, default: () => { return { joins: [], tables: [], }; }, }, set: { type: Boolean, default: false, }, columns: { type: Array, default: () => { return []; }, }, }, data() { return { show: this.set, loading: false, nodeData: JSON.DeepCopy(this.value), joinTypes: [ { value: 'innerjoin', label: 'inner join' }, { value: 'leftjoin', label: 'left join' }, { value: 'rightjoin', label: 'right join' }, { value: 'fulljoin', label: 'full join' }, { value: 'crossjoin', label: 'cross join' }, ], relationType: [ { value: '=', label: '=' }, { value: '>', label: '>' }, { value: '<', label: '<' }, { value: '>=', label: '>=' }, { value: '<=', label: '<=' }, ], conditionList: [], //关联字段 viewTables: {}, compTables: [], referJoins: [], }; }, methods: { /** * @Author: Jason Liu * @description: 确定,提交数据变更 */ fixed() { let errMsg = this.validateJoins(); if (errMsg) { this.$message.warn(errMsg); return; } this.show = false; // 处理数据 this.nodeData.tables = this.compTables .filter((item) => { return item.tableCode; }) .map((item) => { return { ...item, referTable: undefined, }; }); this.nodeData.joins = this.nodeData.joins.map((item) => { return { ...item, isDiff: undefined, }; }); this.$emit('input', this.nodeData); this.$emit('change', this.nodeData); }, /** * @Author: Jason Liu * @description: 处理关联表格 */ parseJoinTable(join) { let refTable = this.nodeData.tables.find((table) => { return table.asName == join.asName; }); if (refTable) { join.tableCode = refTable.tableCode; join.tableId = refTable.tableId; join.tableName = refTable.tableName; join.nodeId = refTable.nodeId; this.compJoinList(); } }, /** * @Author: Jason Liu * @description: 修改设置主表信息 */ changeSetMainTable(row) { this.compTables = this.compTables.map((item) => { if (item.asName != row.asName) { item.isMain = false; } else { item.isMain = true; } // 对比数据 if (item.referTable && item.referTable.tableCode) { item.isDiff = !( item.tableCode == item.referTable.tableCode && item.isMain == item.referTable.isMain && item.asName == item.referTable.asName ); } return item; }); if ( !this.compTables.find((item) => { return item.isMain; }) ) { this.$message.warning($t('必须设置一条数据是主表。')); this.compTables[0].isMain = true; } //this.nodeData.joins = []; 取消清除joins配置的操作 }, /** * @Author: Jason Liu * @description: 查询关联表信息 */ filter(inputValue, path) { return path.some( (option) => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1, ); }, /** * @Author: Jason Liu * @description: 获取设置的节点内容信息 */ /** * @Author: Jason Liu * @description: 获取关联表信息 */ getConditionList() { let queue = {}; this.conditionList = this.nodeData.tables.map((table) => { queue[table.asName] = []; return { value: table.asName, label: table.asName, children: table.columns.map((column) => { queue[table.asName].push(column.columnName); return { value: column.columnName, label: `${ column.description ? `${column.description}(${column.columnName})` : column.columnName } [${column.columnTypeFlink || column.columnType}]`, }; }), }; }); this.viewTables = queue; }, /** * @Author: Jason Liu * @description: 修改关联 */ changeCondition(row, map, send, join) { let value = row[map]; this.nodeData.tables.forEach((condition) => { if (condition.asName == value[0]) { row[send] = { nodeId: condition.nodeId, tableCode: condition.tableCode, tableName: condition.tableName, asName: condition.asName, tableId: condition.tableId, column: condition.columns.find((item) => { return item.columnName == value[1]; }), }; } }); //自动补全左侧映射函数 if (send == 'left' && row.rightmap.length == 0 && join.right.asName) { //左侧内容设置,并且右侧没有数据 this.nodeData.tables.forEach((condition) => { if (condition.asName == join.right.asName) { //做数据对齐 let rightColumn = condition.columns.find((item) => { return item.columnName == value[1] || item.asName == value[1]; }); if (rightColumn) { row.right = { nodeId: condition.nodeId, tableCode: condition.tableCode, tableName: condition.tableName, asName: condition.asName, tableId: condition.tableId, column: rightColumn, }; row.rightmap = [join.right.asName, value[1]]; } } }); } this.compJoinList(); }, /** * @Author: Jason Liu * @description: 添加配置条件 */ addCondition(join, type = 'def') { join.conditions.push({ type: type, //通过关系的方式关联 script: undefined, //自定义脚本内容 leftmap: [], left: { nodeId: undefined, tableCode: undefined, tableName: undefined, tableId: undefined, column: {}, }, relation: '=', rightmap: [], right: { nodeId: undefined, tableCode: undefined, tableName: undefined, tableId: undefined, asName: undefined, column: {}, }, }); }, /** * @Author: Jason Liu * @description: 删除条件 */ deleteCondition(join, i) { join.conditions.splice(i, 1); this.compJoinList(); }, /** * @Author: Jason Liu * @description: 添加join */ addJoin() { this.nodeData.joins.push({ joinType: 'innerjoin', //关联方式 right: { //关联表 nodeId: undefined, tableCode: undefined, tableName: undefined, tableId: undefined, asName: undefined, }, conditions: [ //关联字段 { type: 'def', //通过关系的方式关联 script: undefined, leftmap: [], left: { nodeId: undefined, tableCode: undefined, tableName: undefined, tableId: undefined, asName: undefined, column: {}, }, relation: '=', rightmap: [], right: { nodeId: undefined, tableCode: undefined, tableName: undefined, tableId: undefined, asName: undefined, column: {}, }, }, ], }); }, /** * @Author: Jason Liu * @description: 删除表关联 */ deleteJoin(i) { this.nodeData.joins.splice(i, 1); this.compJoinList(); }, /** * @Author: y_zp * @description: 校验关联条件 */ validateJoins() { let msg = ''; for (let i = 0; i < this.nodeData.joins.length; i++) { let curItem = this.nodeData.joins[i]; if (!curItem.right.asName) { msg = $t('请完善关联条件'); return msg; } let curCondition = curItem.conditions.find((v) => { return ( (v.type == 'sql' && !v.script) || (v.type != 'sql' && (v.leftmap.length == 0 || v.rightmap.length == 0)) ); }); if (curCondition) { msg = $t('请完善关联条件'); return msg; } } return msg; }, /** * @Author: y_zp * @description: 行、列设置背景色 */ setCellClass({ row, column }) { if (column.field && column.field.indexOf('referTable') > -1) { return 'col-gray'; } }, setRowDataClass({ row }) { let result = ''; if (row.isDiff) { result = 'row-diff'; } return result; }, /** * @Author: y_zp * @description: 关联方式 */ formatJoinType(val) { let item = this.joinTypes.find((v) => { return v.value == val; }); return item.label; }, /** * @Author: y_zp * @description: 对比关联表 */ compTableList() { const compFun = (tableA, tableB) => { let newList = []; let commomTable = []; tableA.forEach((itemA) => { // 可能会有相同的表,但是别名不一样 let commonItem = tableB.find((itemB) => { return itemA.tableCode == itemB.tableCode && itemA.asName == itemB.asName; }); if (commonItem) { let diffFlag = itemA.isMain == commonItem.isMain; newList.push({ ...itemA, referTable: { ...commonItem, }, isDiff: !diffFlag, }); commomTable.push(itemA); } else { newList.push({ ...itemA, referTable: {}, }); } }); tableB.forEach((itemB) => { let index = commomTable.findIndex((v) => { return v.tableCode == itemB.tableCode && v.asName == itemB.asName; }); if (index == -1) { newList.push({ referTable: { ...itemB, }, }); } }); return newList; }; this.compTables = compFun(this.nodeData.tables, this.referData.tables); }, /** * @Author: y_zp * @description: 对比关联条件 */ compJoinList() { const compFun = (joinA, joinB) => { let newList = []; let commomJoin = []; joinA.forEach((itemA) => { let itemAStr = this.getStrJoin(itemA); let commonItem = joinB.find((itemB) => { let itemBStr = this.getStrJoin(itemB); return itemAStr == itemBStr; }); if (commonItem) { newList.push({ ...commonItem, }); commomJoin.push(itemAStr); itemA.isDiff = false; } else { itemA.isDiff = true; } }); joinB.forEach((itemB) => { let itemBStr = this.getStrJoin(itemB); let index = commomJoin.findIndex((v) => { return v == itemBStr; }); if (index == -1) { newList.push({ ...itemB, isDiff: true, }); } }); return newList; }; this.referJoins = compFun(this.nodeData.joins, this.referData.joins); }, getStrJoin(item) { let conditionList = item.conditions.map((v) => { if (v.type == 'sql') { return { type: v.type, script: v.script, }; } else if (v.type == 'where') { return { type: v.type, right: { column: { columnName: right.column.columnName, }, }, }; } else { return { leftmap: v.leftmap, rightmap: v.rightmap, relation: v.relation, }; } }); return JSON.stringify({ joinType: item.joinType, right: { asName: item.right.asName, tableCode: item.right.tableCode, }, conditions: conditionList, }); }, }, watch: { set(val) { this.show = val; if (val) { this.nodeData = JSON.DeepCopy(this.value); this.getConditionList(); // 对比表和映射条件 this.compTableList(); this.compJoinList(); } }, show(val) { this.$emit('show', val); }, }, };