wujingjing
2024-07-08 ae78b3b194d399e3ba7bd3922f9f83ca0d7c762c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<template>
    <div class="h-full flex">
        <!-- <div class="h-full flex-grow-0 flex-shrink-0 w-1/5 bg-[#277ad3] p-8 !text-white"></div> -->
 
        <div class="h-full flex-auto flex" style="flex-direction: column">
            <div class="items-center flex-grow-0 flex-shrink-0 flex space-x-2 ml-auto mr-5 mt-2">
                <!-- <el-switch title="显示/隐藏值" v-model="isShowValue" inline-prompt :active-icon="Check" :inactive-icon="Close"> </el-switch> -->
                <el-icon title="适应窗口" class="cursor-pointer" size="30" @click="fitContentClick"><ScaleToOriginal /></el-icon>
            </div>
            <div class="flex-auto">
                <div v-loading="graphLoading" class="w-full h-full" ref="containerRef"></div>
            </div>
            <div
                class="z-40 fixed p-2 bg-[#ebebeb]"
                style="transform-origin: center top"
                :style="{
                    left: `${hoverState.position.x}px`,
                    top: `${hoverState.position.y}px`,
                    visibility: hoverState.isShow ? 'visible' : 'hidden',
                }"
            >
                <div class="font-bold mb-1">{{ hoverState.data?.title }}</div>
                <div class="w-full space-y-1">
                    <div v-if="hoverState.data?.originData" class="flex">
                        <div class="w-32">SCADA ID</div>
                        <div class="before:content-[':'] before:pr-0.5">{{ hoverState.data?.originData?.ScadaId }}</div>
                    </div>
                    <div class="flex">
                        <div class="w-32">模型对象ID</div>
                        <div class="before:content-[':'] before:pr-0.5">{{ hoverState.data?.modelId }}</div>
                    </div>
                    <template v-if="hoverState.data?.originData">
                        <div class="flex">
                            <div class="w-32">检测值</div>
                            <div class="before:content-[':'] before:pr-0.5">
                                {{
                                    hoverState.data?.originData?.Unit
                                        ? `${hoverState.data?.originData?.ScadaValue ?? '--'} ${hoverState.data?.originData?.Unit}`
                                        : hoverState.data?.originData?.ScadaValue ?? '--'
                                }}
                            </div>
                        </div>
                        <div class="flex">
                            <div class="w-32">计算值</div>
                            <div class="before:content-[':'] before:pr-0.5">
                                {{
                                    hoverState.data?.originData?.Unit
                                        ? `${hoverState.data?.originData?.ModelValue ?? '--'} ${hoverState.data?.originData?.Unit}`
                                        : hoverState.data?.originData?.ModelValue ?? '--'
                                }}
                            </div>
                        </div>
                        <div
                            class="flex"
                            v-if="
                                (hoverState.data?.originData?.ModelValue || hoverState.data?.originData?.ModelValue === 0) &&
                                (hoverState.data?.originData?.ScadaValue || hoverState.data?.originData?.ScadaValue === 0)
                            "
                        >
                            <div class="w-32">差值</div>
                            <div class="before:content-[':'] before:pr-0.5">
                                {{
                                    hoverState.data?.originData?.Unit
                                        ? `${hoverState.data.dValue} ${hoverState.data?.originData?.Unit}`
                                        : hoverState.data.dValue
                                }}
                            </div>
                        </div>
                    </template>
                </div>
            </div>
        </div>
        <div
            class="h-full flex-grow-0 flex-shrink-0 flex flex-col"
            style="background: linear-gradient(90deg, rgba(45, 129, 217, 0.6), rgba(6, 85, 180, 0.6)); width: 450px"
        >
            <el-table
                v-loading="tableLoading"
                border
                class="!h-full table-attr"
                row-key="id"
                :cellStyle="{ ...tableCellCenterExceptColumn(), background: 'revert' }"
                :cell-class-name="getCellClassName"
                :data="tableData"
                :header-cell-style="{
                    textAlign: 'center',
                    color: '#fff',
                }"
                style="background: linear-gradient(90deg, rgba(45, 129, 217, 0.6), rgba(6, 85, 180, 0.6)) !important"
            >
                <el-table-column prop="id" label="名称" fixed="left" show-overflow-tooltip> </el-table-column>
                <el-table-column prop="value" width="130" label="检测值" show-overflow-tooltip>
                    <template #default="scope">
                        <div class="flex">
                            <span class="overflow-hidden flex-auto text-ellipsis">
                                {{ scope.row.value ?? '--' }}
                            </span>
                            <span class="w-3 mr-3 flex-shrink-0">
                                {{ scope.row.unit }}
                            </span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <el-form
                class="mt-10"
                style="padding-left: 10px; padding-right: 10px"
                :model="dialogFormValue"
                ref="dialogFormRef"
                :rules="dialogFormRules"
                label-width="130"
            >
                <el-form-item class="w-full" label="1输水目标流量" prop="totalFlow1">
                    <el-input-number class="!w-full" :controls="false" v-model="dialogFormValue.totalFlow1"></el-input-number>
                </el-form-item>
                <el-form-item class="w-full" label="1输水目标压力" prop="totalPressure1">
                    <el-input-number class="!w-full" :controls="false" v-model="dialogFormValue.totalPressure1"></el-input-number>
                </el-form-item>
 
                <el-form-item class="w-full" label="2输水目标流量" prop="totalFlow2">
                    <el-input-number class="!w-full" :controls="false" v-model="dialogFormValue.totalFlow2"></el-input-number>
                </el-form-item>
                <el-form-item class="w-full" label="2输水目标压力" prop="totalPressure2">
                    <el-input-number class="!w-full" :controls="false" v-model="dialogFormValue.totalPressure2"></el-input-number>
                </el-form-item>
                <div class="" style="margin-bottom: 18px">
                    <div>
                        <el-button type="primary" color="#77a6ec" @click="submitFormValue" class="!text-white w-full">计算</el-button>
                    </div>
                </div>
            </el-form>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import './model/shape/index';
import type { Graph } from '@antv/x6';
import { ScaleToOriginal } from '@element-plus/icons-vue';
import { ElLoadingService, type FormInstance, type FormRules } from 'element-plus';
import { debounce } from 'lodash';
import { onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue';
import { REFRESH_INTERVAL } from './model/constants';
import { ModelGraph, type ScheduleValidationParams } from './model/graph';
import { HIGH_LIGHT_LINE_COLOR, LINE_COLOR } from './model/shape/common/baseConfig';
import { LINE_NODE_SHAPE, ModelShape } from './model/shape/common/types';
import type { ScadaItemRes, ScheduleValidationRes } from '/@/api/dispatch/hydraulicModel';
import { GetScadaList } from '/@/api/dispatch/hydraulicModel';
import { tableCellCenterExceptColumn } from '/@/utils/util';
 
//#region ====================== 调度计算 ======================
 
const dialogFormRef = ref<FormInstance>(null);
 
// const dialogFormValue = ref<ScheduleValidationParams>({
//     totalFlow1: 23394.0,
//     totalFlow2: 38997.2,
//     totalPressure1: 0.3355,
//     totalPressure2: 0.1745,
// });
const dialogFormValue = ref<ScheduleValidationParams>({
    totalFlow1: null,
    totalFlow2: null,
    totalPressure1: null,
    totalPressure2: null,
});
const dialogFormRules = ref<FormRules>({
    totalFlow1: [{ required: true, message: '请输入1输水目标流量', trigger: 'blur' }],
    totalFlow2: [{ required: true, message: '请输入2输水目标流量', trigger: 'blur' }],
    totalPressure1: [{ required: true, message: '请输入1输水目标压力', trigger: 'blur' }],
    totalPressure2: [{ required: true, message: '请输入2输水目标压力', trigger: 'blur' }],
});
 
const handleModelData = (data: ScheduleValidationRes) => {
    if (!data.ScheduleValues) return;
    const nodeData = data.ScheduleValues.map((item) => ({
        id: item.ModelId,
        value: item.ModelValue,
    }));
    if (isShowValue.value) {
        modelGraph.showNodesValue(nodeData);
    }
};
 
const handleScadaData = (data: ScheduleValidationRes) => {
    if (!data.ScheduleValues) return;
    for (const item of data.ScheduleValues) {
        const { ScadaId, ScadaValue, Unit } = item;
        const foundIndex = tableData.value.findIndex((item) => item.id === ScadaId);
        tableData.value[foundIndex].value = ScadaValue;
        tableData.value[foundIndex].unit = Unit;
    }
};
 
const submitFormValue = async () => {
    const valid = await dialogFormRef.value.validate().catch(() => {});
    if (!valid) return;
    const loading = ElLoadingService({
        text: '加载中...',
    });
    const data = await ModelGraph.scheduleValidation(dialogFormValue.value).finally(() => {
        loading.close();
    });
    modelGraph.loadData(data);
};
 
//#endregion
 
//#region ====================== 获取 ScadaList ======================
type ScadaItemLogic = {
    id: string;
    value: number;
    unit: string;
    model: ScadaItemRes;
};
const tableData = ref<ScadaItemLogic[]>([]);
const getScadaList = async () => {
    tableLoading.value = true;
    const res = await GetScadaList().finally(() => {
        tableLoading.value = false;
    });
    const resData = (res?.Data || []) as ScadaItemRes[];
    tableData.value = resData.map((item) => ({
        get id() {
            return this.model.Id;
        },
        get value() {
            return this.model.Value;
        },
 
        set value(val) {
            this.model.Value = val;
        },
        unit: '',
        model: item,
    }));
};
//#endregion
 
//#region ====================== 工具栏 ======================
const isShowValue = ref(true);
watch(
    () => isShowValue.value,
    (val) => {
        val ? modelGraph.showNodesValue() : modelGraph.hideNodesValue();
    }
);
//#endregion
 
//#region ====================== 设置表格颜色 ======================
 
const getCellClassName = ({ column, columnIndex }) => {
    if (columnIndex === 1) {
        // 第二列的索引为1
        return 'cursor-pointer';
    }
};
 
//#endregion
let modelGraph: ModelGraph = null;
const graphLoading = ref(false);
const tableLoading = ref(false);
const containerRef = ref<HTMLDivElement>();
let graph: Graph = null;
 
const hoverState = reactive({
    isShow: false,
    position: {
        x: 0,
        y: 0,
    },
    data: null,
});
 
const offsetHoverPanel = 10;
 
const listenMouseHover = () => {
    graph.on('cell:mouseenter', ({ cell, e }) => {
        if (!cell.data) return;
        const { clientX, clientY } = e;
        hoverState.isShow = true;
        hoverState.position.x = clientX + offsetHoverPanel;
        hoverState.position.y = clientY + offsetHoverPanel;
        hoverState.data = cell.data;
 
        if ([ModelShape.Pipe].includes(cell.shape as any)) {
            cell.attr('line/stroke', HIGH_LIGHT_LINE_COLOR);
        } else if (!LINE_NODE_SHAPE.includes(cell.shape as any)) {
            cell.attr('rect/style/visibility', 'visible');
        } else {
            if (LINE_NODE_SHAPE.includes(cell.shape as any)) {
                const children = cell.getChildren();
                for (const item of children) {
                    if (item.shape === ModelShape.LinkEdge) {
                        item.attr('line/stroke', HIGH_LIGHT_LINE_COLOR);
                    }
                }
            } else {
                const parent = cell.getParent();
                const children = parent.getChildren();
                for (const item of children) {
                    if (item.shape === ModelShape.LinkEdge) {
                        item.attr('line/stroke', HIGH_LIGHT_LINE_COLOR);
                    }
                }
            }
        }
    });
 
    graph.on('cell:mouseleave', ({ cell }) => {
        hoverState.isShow = false;
 
        if ([ModelShape.Pipe].includes(cell.shape as any)) {
            cell.attr('line/stroke', LINE_COLOR);
        } else if (!LINE_NODE_SHAPE.includes(cell.shape as any)) {
            cell.attr('rect/style/visibility', 'hidden');
        } else {
            if (LINE_NODE_SHAPE.includes(cell.shape as any)) {
                const children = cell.getChildren();
                for (const item of children) {
                    if (item.shape === ModelShape.LinkEdge) {
                        item.attr('line/stroke', LINE_COLOR);
                    }
                }
            } else {
                const parent = cell.getParent();
 
                const children = parent.getChildren();
                for (const item of children) {
                    if (item.shape === ModelShape.LinkEdge) {
                        item.attr('line/stroke', LINE_COLOR);
                    }
                }
            }
        }
    });
};
let refreshTimer = null;
 
const fitContentClick = () => {
    modelGraph.zoomToFit();
};
onMounted(async () => {
    getScadaList();
 
    modelGraph = new ModelGraph({
        container: containerRef.value!,
    });
 
    graph = modelGraph.graph;
 
    graphLoading.value = true;
 
    await modelGraph.loadNode().finally(() => {
        graphLoading.value = false;
    });
 
    // graph.on('cell:click', ({ cell }) => {
    // });
    listenMouseHover();
 
    // modelGraph.openDevTools();
 
    refreshTimer = setInterval(getScadaList, REFRESH_INTERVAL);
});
 
onBeforeUnmount(() => {
    clearInterval(refreshTimer);
});
</script>
 
 
<style scoped lang="css">
:deep(.el-input__wrapper) {
    background-color: #1667c4;
}
:deep(.el-input__inner) {
    color: white;
}
 
:deep(.el-form-item__label) {
    color: white;
}
.el-table{
    --el-table-text-color:white
}
 
.table-attr :deep(.el-icon svg) {
    color: white;
}
 
:deep(.el-form-item__error){
    color:#b50000
}
 
:deep(.el-table__row) {
    background: linear-gradient(0deg, rgba(45, 129, 217, 0.3), rgba(6, 85, 180, 0.3)) !important;
}
 
:deep(.el-table th) {
    background-color: #8db9e7 !important;
}
 
:deep(.el-table tbody tr) {
    pointer-events: none;
}
</style>