gaosp
2024-01-12 de49d40ab302b48a5d98201ba80105dadb2b2ac4
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<!--
 * @Date: 2024-01-05 23:47:53
 * @LastEditors: Sneed
 * @LastEditTime: 2024-01-12 00:35:17
 * @FilePath: /belleson-frontend/Users/mache/Documents/demo/mdc/src/container/Map/Map.vue
-->
<template>
    <div class="map">
        <div class="map-container" v-if="!addStatus">
            <div class="table-tool" v-if="status !== 2">
                <el-button style="width: 150px;" type="primary" size="mini" @click="save">保存</el-button>
                <el-button style="width: 150px;" size="mini" @click="out">退出</el-button>
                <el-input v-show="status !==2" v-model="rows" />
                <el-input v-show="status !==2" v-model="cols" />
                <el-button size="mini" v-show="status !==2" @click="createMap">生成地图</el-button>
                <div class="plant-name" v-if="status===2">{{plantName}}</div>
                <el-input class="plant-name" v-if="status!==2" v-model="plantName" placeholder="请输入厂名"/>
            </div>
            <div class="table" >
                <div class="table-action" :style="position" v-show="showAction && status !== 2">
                    <div @click="merge">合并</div>
                    <div @click="split">拆分</div>
                    <div @click="sign(1)">过道</div>
                    <div @click="sign(2)">机床</div>
                    <div @click="sign(0)">取消</div>
                    <!-- <div>增加行</div> -->
                    <!-- <div>删除行</div> -->
                    <!-- <div>增加列</div> -->
                    <!-- <div>删除列</div> -->
                </div>
                <table>
                    <tr v-for="(item, index) in map" :key="index">
                        <td v-if="v.rowspan !==0 && v.colspan!==0"
                        :width="v.type ===1 ? '20px' : v.colspan * 400 + 'px'"
                        :height="v.type ===1 ? '20px' : v.rowspan * 100+ 'px'"
                        :class="{'active': (range.x !=='' && v.rowIndex >=range.x && v.rowIndex<=range.x1 && v.colIndex>=range.y && v.colIndex<=range.y1),aisle: v.type ===1}" @mousedown="e => onMousedown(e, v)" @mousemove="e => onMouseMove(e, v)" v-for="v in item"
                            :rowspan="v.rowspan" :colspan="v.colspan" :key="v.rowIndex + '-' + v.colIndex">
                            <img @click="addDevice($event,v)" @mousedown="e => e.stopPropagation()" v-if="v.type ===2 && status!==2" src="./img/+.png" alt="">
                        </td>
                        <!-- <td></td> -->
                    </tr>
                    <!-- <tr>
                        <td></td>
                    </tr> -->
                </table>
            </div>
        </div>
        <div class="map-add" v-else>
            <div class="map-add-item">
                <span>统一编号</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>设备名称</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>设备型号</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>设备规格</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>使用单位</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>保养者</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>保养周期</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>保养类型</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>是否合格</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>计划开始时间</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>计划完成时间</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-item">
                <span>保养内容与存在问题</span>
                <el-input class="value"></el-input>
            </div>
            <div class="map-add-footer">
                <el-button size="mini" @click="close">关闭</el-button>
                <el-button type="primary"  size="mini" @click="saveDevice">保存</el-button>
            </div>
        </div>
    </div>
    
</template>
<script>
const getParent = (v, layout) => {
    if (v.rowspan === 0) {
        return getParent(layout[v.parent[0]][v.parent[1]], layout)
    } else {
        return v
    }
}
import mixins  from '@/mixins/index'
import AddMachine from './addMachine.vue'
import  {mapGetters} from 'vuex'
export default {
    mixins: [mixins],
    components: {
        AddMachine
    },
    props: {
        status: {
            type: Number,
            default: 0
        },
        currentMap: {
            default: []
        },
        name: {
            type: String,
        },
        id: {
            type: Number
        }
    },
    data() {
        return {
            plantName: '',
            rows: 10,
            cols: 50,
            map: null, // 地图数据
            addStatus: false,
            position: {},
            selection:[],
            showAction: false,
            tdMounseDown: false,
            range: {x: '',x1: '',y: '',y1: ''},
            currentAdd: {},
            colgroups:[]
        }
    },
    watch: {
        selection(val) {
            let select = this.selection
            let range = {}
            if (val.length === 0) {
                range = {}
            } else if (val.length === 1) {
                let x = select[0].rowIndex
                let y = select[0].colIndex
                let x1 = select[0].rowIndex + select[0].rowspan - 1
                let y1 = select[0].colIndex + select[0].colspan - 1
                range = { x, y, x1, y1 }
            } else {
                // 起始行号 不包含合并
                let x = Math.min(select[0].rowIndex, select[1].rowIndex)
                let x1 = Math.max(select[0].rowIndex + select[0].rowspan - 1, select[1].rowIndex + select[1].rowspan - 1)
                let y = Math.min(select[0].colIndex, select[1].colIndex)
                let y1 = Math.max(select[0].colIndex + select[0].colspan - 1, select[1].colIndex + select[1].colspan - 1)
                // 范围内碰到合并格
                for (var i = x; i <= x1; i++) {
                    for (var j = y; j <= y1; j++) {
                        let v = getParent(this.map[i][j], this.map)
                        x = Math.min(x, v.rowIndex)
                        y = Math.min(y, v.colIndex)
                        x1 = Math.max(x1, v.rowIndex + v.rowspan - 1)
                        y1 = Math.max(y1, v.colIndex + v.colspan - 1)
                    }
                }
                range = { x, y, x1, y1 }
            }
            this.$set(this.range,'x',range.x)
            this.$set(this.range,'x1',range.x1)
            this.$set(this.range,'y',range.y)
            this.$set(this.range,'y1',range.y1)
        },
        name: {
            handler () {
                this.plantName = this.name
            },
            immediate: true
        },
        currentMap: {
            handler () {
                if (this.currentMap.length > 0) this.map = this.currentMap
 
            },
            immediate: true
        },
    },
    mounted() {
        this.$el.querySelector('table').addEventListener('contextmenu', this.contextmenu)
        this.$el.addEventListener('mouseup',e => {
            if(this.status ===2) return 
            this.tdMounseDown = false
        })
        if (this.id) {
            // this.getmachineList()
            this.rows = this.currentMap.length
            this.cols = this.currentMap[0].length
        }
    },
    beforeUpdate() {
        try {
            this.$el.querySelector('table').removeEventListener('contextmenu', this.contextmenu)
        } catch (error) {
            
        }
        
    },
    updated() {
        try {
            this.$el.querySelector('table').addEventListener('contextmenu', this.contextmenu)
        } catch (error) {
            
        }
    },
    methods: {
        createMap() {
            let data = []
            for (var i = 0; i < this.rows; i++) {
                let arr = []
                for (var j = 0; j < this.cols; j++) {
                    arr.push({
                        rowspan: 1,
                        colspan: 1,
                        rowIndex: i,
                        colIndex: j,
                        type: ''
                    })
                }
                data.push(arr)
            }
            this.map = data
        },
        contextmenu(e) {
            console.log(e)
            if(this.status ===2) return 
            // if (this.range.x !=='') return 
            e.preventDefault()
            e.stopPropagation()
            let { pageX: left, pageY: top } = e
            this.showAction = true
            let { top: t } = document.querySelector('.table').getBoundingClientRect()
            if (window.innerWidth - left < 100) {
                this.position = {
                    right: 0,
                    top: top - t + 10 + 'px'
                }
            } else {
                this.position = {
                    left: left + 'px',
                    top: top -t + 10 + 'px'
                }
            }
            
        },
        onMousedown(e, v) {
            if(this.status ===2) return 
            if (e.button !== 0) return
            this.tdMounseDown = true
            this.showAction = false
            this.selection = [v]
        },
        onMouseMove(e,v) {
            if(this.status ===2) return 
            if(!this.tdMounseDown) return
            this.$set(this.selection,1,v)
        },
        merge(e) {
            const { x, x1, y, y1 } = this.range
            for (var i = x; i <= x1; i++) {
                for (var j = y; j <= y1; j++) {
                    if (i == x && j == y) {
                        let arr = [...(this.map[i])]
                        arr[j].rowspan = x1 - x + 1
                        arr[j].colspan = y1 - y + 1
                        this.$set(this.map,i,arr)
                    } else {
                        let arr = [...(this.map[i])]
                        arr[j].rowspan = 0
                        arr[j].colspan = 0
                        arr[j].parent = [x, y]
                        this.$set(this.map,i,arr)
                    }
                }
            }
            this.$set(this.range,'x',this.range.x)
            this.$set(this.range,'x1',this.range.x)
            this.$set(this.range,'y',this.range.y)
            this.$set(this.range,'y1',this.range.y)
            this.showAction = false
        },
        split () {
            const { x, x1, y, y1 } = this.range
            for(var i=x;i<=x1;i++) {
                for(var j=y;j<=y1;j++) {
                    if (i==x && j==y) {
                        let arr = [...(this.map[i])]
                        arr[j].rowspan = 1
                        arr[j].colspan = 1
                        this.$set(this.map,i,arr)
                    } else {
                        let arr = [...(this.map[i])]
                        arr[j].rowspan = 1
                        arr[j].colspan = 1
                        arr[j].parent = ''
                        this.$set(this.map,i,arr)
                    }
                }
            }
            this.showAction = false
            // setData(layoutNew)
            // setRange({x,y,x1:x,y1: y})
            // setShowMenu(false)
        },
        sign (type) {
            const { x, x1, y, y1 } = this.range
            for(var i=x;i<=x1;i++) {
                for(var j=y;j<=y1;j++) {
                    let arr = [...(this.map[i])]
                    arr[j].type = type
                    this.$set(this.map,i,arr)
                }
            }
            this.showAction = false
            this.$forceUpdate()
        },
        addDevice (e,v) {
            if(this.status ===2) return 
            e.stopPropagation()
            this.addStatus = true
            this.currentAdd =  v
        },
        save () {
            if (this.status === 0) {
                this.$http.postJson('/plant/save',{
                    name: this.plantName,
                    gridSetting: JSON.stringify(this.map)
                }).then(res => {
                    this.$emit('out')
                    console.log(res)
                })
            } else if(this.status === 1) {
                this.$http.postJson('/plant/modify',{
                    name: this.plantName,
                    gridSetting: JSON.stringify(this.map),
                    id: this.id
                }).then(res => {
                    this.$emit('out')
                    console.log(res)
                })
            }
            
        },
        out () {
            this.$emit('out')
        },
        close () {
            this.addStatus = false
        },
        setmachineId (id) {
            console.log(id)
            if (!id) return
            let i = this.currentAdd.rowIndex
            let j = this.currentAdd.colIndex
            let arr = [...(this.map[i])];
            arr[j].id = id
            this.$set(this.map,i,arr)
            this.close()
        }
    },
}
</script>
<style lang="scss" scoped>
.map {
    width: 100%;
    height: 100%;
    position: relative;
    .map-container {
        width: 100%;
        height: 100%;
        overflow: auto;
        position: relative;
        display: flex;
        flex-direction: column;
    }
}
 
.table-tool {
    padding: 10px 0;
    display: flex;
    justify-content: flex-start;
 
    &>div {
        width: 80px;
        margin-inline: 10px;
    }
    .plant-name {
        width: 120px;
        margin-left: auto;
        margin-right: 30px;
        text-align: center;
        line-height: 28px;
        background: rgba(23,38,67,0.6);
        border-radius: 14px;
        overflow: hidden;
        color: #F7F8FA;
        font-size: 12px;
        border: 1px solid;
        border-image: linear-gradient(180deg, rgba(154, 254, 254, 1), rgba(75, 120, 205, 1)) 1 1;
    }
}
 
.table {
    // margin-top: 10px;
    display: flex;
    justify-content: flex-start;
    overflow-y: scroll;
    width: 100%;
    padding: 20px;
    height: 100%;
    position: relative;
    .table-action {
        position: absolute;
        z-index: 999;
        color: #fff;
        width: 100px;
        div {
            margin-top: 4px;
            cursor: pointer;
        }
        div:hover {
            color: #68D9FF;
        }
    }
 
    table {
        width: 2000px;
        height: 100%;
        
        border-collapse: collapse;
        // table-layout: fixed;
        word-wrap: break-word;
        overflow: hidden;
        
        td {
            // width: 50px;
            // height: 50px;
            box-sizing: border-box;
            border: 1px solid #1662DB;
            vertical-align: middle;
            text-align: center;
            img {
                width: 20px;
                height: 20px;
                cursor: pointer;
            }
        }
        td.active {
            background: #68D9FF!important;
        }
        td.aisle {
            // width: 20px;
            // height: 20px;
            background: #1662DB;
            div {
                // width: 20px;
                // height: 20px;
            }
        }
        td.device {
            // width: 400px;
            // height: 400px;
            overflow: hidden;
            div {
                // width: 400px;
            }
        }
    }
}
.grid {
    width: 100%;
    height: 100%;
    div {
        border: 1px solid red;
    }
}
</style>