吉安感知网项目-前端
shuishen
2026-02-03 89380e6260a75d1d3b94de687ebcc2f50d50659d
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
<template>
    <el-dialog class="command-page-map-view-dialog" v-model="visible" :show-close="false" :close-on-click-modal="false">
        <div class="dialog-container">
            <div class="right-container" style="width: 100%">
                <div class="header">
                    <span>设备共享</span>
                    <el-icon class="close-btn" @click.stop="visible = false"><Close /></el-icon>
                </div>
 
                <div class="content">
                    <div class="command-table-container" v-loading="loading" element-loading-background="rgba(5, 5, 15, 0.6)">
                        <div class="command-table-content">
                            <el-table
                                ref="tableRef"
                                class="command-table"
                                :data="deviceList"
                                row-key="id"
                                @selection-change="handleSelectionChange"
                            >
                                <el-table-column type="selection" width="55" :reserve-selection="true" />
                                <el-table-column type="index" show-overflow-tooltip width="64" label="序号" />
                                <el-table-column prop="deviceName" show-overflow-tooltip label="设备名称" />
                                <el-table-column prop="deviceSn" show-overflow-tooltip label="设备SN" />
                                <el-table-column prop="deviceType" show-overflow-tooltip label="设备类型">
                                    <template v-slot="{ row }">
                                        {{ getDictLabel(row.deviceType, dictObj.deviceType) }}
                                    </template>
                                </el-table-column>
                                <el-table-column prop="belongDeptName" show-overflow-tooltip label="所属部门" />
                            </el-table>
                        </div>
                    </div>
                </div>
 
                <div class="footer">
                    <el-button color="#2B2B4C" @click="handleCancel">取消</el-button>
                    <el-button color="#284FE3" type="primary" :loading="submitting" :disabled="submitting" @click="handleSubmit">
                        确定
                    </el-button>
                </div>
            </div>
        </div>
    </el-dialog>
</template>
 
<script setup>
import { Close } from '@element-plus/icons-vue'
import { ref, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import { nonDeptAreaDevicesApi, fwDevicePerShareSaveApi, fwDevicePerShareRemoveApi } from './fwDevicePerShareApi'
import { getDictionaryByCode } from '@/api/system/dictbiz'
import { getDictLabel } from '@ztzf/utils'
 
const emit = defineEmits(['success'])
const visible = defineModel() // 弹框显隐
const loading = ref(false) // 加载中
const submitting = ref(false) // 提交中
const deviceList = ref([]) // 设备列表
const tableRef = ref(null) // 表格实例
const selectedRows = ref([]) // 当前选中的行
const originalSharedIds = ref([]) // 原始已共享的设备ID集合
const originalSharedMap = ref({}) // 原始已共享的设备ID与shareId的映射
const currentDeptId = ref(null) // 当前操作的部门ID
const regionCode = ref(null) // 当前操作的部门code
const dictObj = ref({
    deviceType: [], // 设备类型
})
 
// 获取字典
function getDictList() {
    getDictionaryByCode('deviceType').then(res => {
        dictObj.value = res.data.data
    })
}
 
// 选中变化
function handleSelectionChange(rows) {
    selectedRows.value = rows
}
 
// 关闭弹框
function handleCancel() {
    visible.value = false
}
 
// 获取设备列表
async function getDeviceList() {
    loading.value = true
    try {
        const res = await nonDeptAreaDevicesApi({ deptId: currentDeptId.value,regionCode: regionCode.value })
        deviceList.value = res?.data?.data ?? []
        // 记录原始已共享的设备ID和shareId映射
        const sharedDevices = deviceList.value.filter(item => item.isShared)
        originalSharedIds.value = sharedDevices.map(item => item.id)
        originalSharedMap.value = sharedDevices.reduce((map, item) => {
            map[item.id] = item.shareId
            return map
        }, {})
        // 回显已共享的设备勾选状态
        nextTick(() => {
            deviceList.value.forEach(item => {
                if (item.isShared) {
                    tableRef.value?.toggleRowSelection(item, true)
                }
            })
        })
    } finally {
        loading.value = false
    }
}
 
// 提交
async function handleSubmit() {
    submitting.value = true
    try {
        const currentSelectedIds = selectedRows.value.map(item => item.id)
 
        // 计算需要新增的设备(当前选中但原来未共享)
        const toAddDevices = selectedRows.value.filter(item => !originalSharedIds.value.includes(item.id))
 
        // 计算需要删除的设备(原来共享但当前未选中),使用shareId
        const toRemoveShareIds = originalSharedIds.value
            .filter(id => !currentSelectedIds.includes(id))
            .map(id => originalSharedMap.value[id])
            .filter(Boolean)
 
        const promises = []
 
        // 调用新增接口
        if (toAddDevices.length > 0) {
            const addData = toAddDevices.map(item => ({
                deviceId: item.id,
                deviceSn: item.deviceSn,
                loanFromDeptId: item.belongDept,
                loanToDeptId: currentDeptId.value,
            }))
            promises.push(fwDevicePerShareSaveApi(addData))
        }
 
        // 调用删除接口
        if (toRemoveShareIds.length > 0) {
            promises.push(fwDevicePerShareRemoveApi({ ids: toRemoveShareIds.join(',') }))
        }
 
        if (promises.length > 0) {
            await Promise.all(promises)
            ElMessage.success('保存成功')
        } else {
            ElMessage.info('未做任何修改')
        }
 
        visible.value = false
        emit('success')
    } finally {
        submitting.value = false
    }
}
 
// 打开弹框
async function open({ row } = {}) {
    currentDeptId.value = row?.id
    regionCode.value = row?.areaCode
    selectedRows.value = []
    getDictList()
    await getDeviceList()
}
 
defineExpose({ open })
</script>
 
<style scoped lang="scss"></style>