yxh
2026-10-18 1446994d504819c367e1ff667360eadb314f9ac6
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
<!--
  工作台顶部「工位切换 / 围观」下拉(主/围观模型的入口)
 
  为什么放在这里:医生在工作台里干活时,最自然的诉求是"顺便看一眼别的诊室在做什么"。
  下拉列出所有**已开放**工位,并标出相对我的身份:
 
    · 我的工位      → 切回自己(可操作)
    · 空闲          → 询问后入座(成为主人;服务端会自动释放我原来的工位)
    · 已入座:张三   → **围观**(只读,不能改别人的数据)
 
  围观只改"我在看哪个工位"(roomStore.room),**不动我入座的工位**(roomStore.currentSeat);
  两者不同时 `getCanOperate` 为 false → 页面只读。服务端另有硬校验(doc_id 必须是我)。
-->
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { RoomApi, RoomVO } from '@/api/ecg/room'
import { useRoomStore } from '@/store/modules/room'
 
defineOptions({ name: 'BedSwitchSelect' })
 
const router = useRouter()
const roomStore = useRoomStore()
 
/**
 * 【本次需求】工位切换的范围控制:
 *   · 叫号检查页:`opTypes=[0]` → 下拉只列**检查类**工位;
 *   · 设备领用 / 装机页:`enabled=false` → 工位固定(= 当前查看的工位),不提供切换。
 * 三个页面通过 DoctorPageLayout 透传。
 */
const props = withDefaults(
  defineProps<{
    /** 是否允许切换工位(false 时只显示"固定工位"标签) */
    enabled?: boolean
    /** 可切换工位的作业类型过滤(null/空 = 不过滤);0检查 1领用 2装机 */
    opTypes?: number[] | null
  }>(),
  { enabled: true, opTypes: null }
)
 
const beds = ref<RoomVO[]>([])
const loading = ref(false)
let timer: number | undefined
 
const canOperate = computed(() => roomStore.getCanOperate)
const dropdownText = computed(() => {
  if (!roomStore.getIsSetRoom || !roomStore.getViewText) return '选择工位'
  return canOperate.value
    ? roomStore.getViewText
    : `围观 ${roomStore.getViewText}`
})
 
const isSameBed = (a?: { roomId?: any; bedNo?: any } | null, b?: RoomVO | null) =>
  !!a && !!b && String(a.roomId) === String(b.roomId) && String(a.bedNo) === String(b.bedNo)
 
const isMySeat = (bed: RoomVO) => isSameBed(roomStore.getCurrentSeat, bed)
const isViewing = (bed: RoomVO) => isSameBed(roomStore.getRoom, bed)
 
/**
 * 按 `opTypes` 过滤工位(null/空 = 不过滤)。
 *
 * 【本次需求】叫号检查页只允许切到**检查类**工位(opType=0);
 * 领用/装机页不提供切换(`enabled=false`),此函数只服务于前者。
 */
const filterBeds = (all: RoomVO[]): RoomVO[] => {
  const types = props.opTypes
  if (!Array.isArray(types) || types.length === 0) return all
  return all.filter((b) => types.some((t) => Number(t) === Number(b.opType)))
}
 
/** 固定工位的只读文案(`enabled=false` 时显示) */
const fixedText = computed(() => {
  const text = roomStore.getViewText
  if (!text) return '未选择工位'
  return canOperate.value ? text : `围观 ${text}`
})
 
/** 每个工位的身份文案 */
const bedLabel = (bed: RoomVO) => {
  const name = `${bed.roomName || ''} · ${bed.bedNo || ''}`
  if (isMySeat(bed)) return `${name}(我的工位)`
  if (bed.docId == null) return `${name}(空闲,可入座)`
  return `${name}(已入座:${bed.docName || '其他医生'},只读围观)`
}
 
const loadBeds = async () => {
  try {
    loading.value = true
    const map: any = await RoomApi.getOpeningBedMap()
    const all: RoomVO[] = []
    for (const key in map || {}) {
      const arr = map[key]
      if (Array.isArray(arr)) all.push(...arr)
    }
    beds.value = all
    // 【本次需求】按作业类型过滤"可切换的工位"(叫号检查页只列检查类工位)
    const visible = filterBeds(all)
    beds.value = visible
    // 正在围观时顺便刷新"主人是否已离开"(只提示,不自动接管)
    const cur = roomStore.getRoom
    if (cur?.roomId != null && cur.bedNo) {
      // 注意:用**全量** all 去找当前工位(它可能不在过滤后的列表里)
      const latest = all.find((b) => isSameBed(cur, b))
      if (latest) {
        roomStore.refreshViewTargetState(latest)
      } else {
        // 正在看的工位已不在"已开放工位"列表里(被关闭/删除)→ 自动回到我自己的工位,
        // 否则页面会一直查一个不存在工位的数据(表现就是"患者列表一直是空的")。
        const seat = roomStore.getCurrentSeat
        if (seat?.seated && !isSameBed(cur, seat)) {
          console.warn('[BedSwitchSelect] 正在查看的工位已不可用,切回我的工位', cur)
          await roomStore.setRoomInfoAction({
            id: seat.id ?? null,
            roomId: seat.roomId ?? null,
            roomName: seat.roomName ?? null,
            bedNo: seat.bedNo ?? null,
            ip: seat.ip ?? null,
            status: seat.status ?? null,
            docId: seat.docId ?? null,
            docName: seat.docName ?? null,
            checkTypes: seat.checkTypes ?? null,
            opType: seat.opType ?? null
          } as RoomVO)
          ElMessage({
            message: `原查看的工位已不可用,已切回我的工位:${seat.roomName || ''} ${seat.bedNo || ''}`,
            type: 'warning',
            duration: 5000
          })
        }
      }
    }
  } catch (e) {
    console.warn('[BedSwitchSelect] 加载工位列表失败', e)
  } finally {
    loading.value = false
  }
}
 
/** 选中一个工位 */
const onPick = async (bed: RoomVO) => {
  // ① 我的工位 → 切回自己
  if (isMySeat(bed)) {
    await roomStore.setRoomInfoAction(bed)
    ElMessage.success(`已切回我的工位:${bed.roomName || ''} ${bed.bedNo || ''}`)
    return
  }
  // ② 空闲 → 询问后入座
  if (bed.docId == null) {
    const mine = roomStore.getSeatText
    try {
      await ElMessageBox.confirm(
        `入座到「${bed.roomName || ''} / ${bed.bedNo || ''}」?` +
          (mine ? `(会退出原来的「${mine}」)` : ''),
        '入座确认',
        { confirmButtonText: '入座', cancelButtonText: '取消', type: 'warning' }
      )
    } catch {
      return
    }
    const ok = await roomStore.seatRoom(bed)
    if (!ok) {
      ElMessage({ message: '入座失败:该工位可能已被他人占用,或状态不允许(已关闭)', type: 'warning', duration: 4000 })
    } else {
      await roomStore.fetchMySeat()
      ElMessage.success(`已入座:${bed.roomName || ''} ${bed.bedNo || ''}`)
    }
    return
  }
  // ③ 已被别人占用 → 围观(只读)
  await roomStore.setViewTargetAction(bed, bed.docName)
  ElMessage({
    message: `正在围观「${bed.roomName || ''} / ${bed.bedNo || ''}」(主人 ${bed.docName || '其他医生'}),只读查看`,
    type: 'info',
    duration: 4000
  })
}
 
onMounted(async () => {
  await loadBeds()
  // 30s 轮询:刷新占用状态,并发现"主人已离开"(只提示,不自动接管)
  timer = window.setInterval(loadBeds, 30000)
})
 
onBeforeUnmount(() => {
  if (timer) window.clearInterval(timer)
})
</script>
 
<template>
  <div class="bed-switch">
    <!--
      【本次需求】领用 / 装机页不提供切换:工位固定为"当前查看的工位",
      这里只显示一个只读标签;要换工位请到顶栏工位标签 → 选工位页。
    -->
    <el-tooltip v-if="!enabled" content="本工作台工位固定;如需查看其它工位,请点顶栏工位标签进入「选工位页」" placement="bottom">
      <el-tag :type="canOperate ? 'primary' : 'warning'" effect="plain" class="bed-switch__fixed">
        <Icon :icon="canOperate ? 'ep:office-building' : 'ep:view'" class="mr-4px" />
        工位:{{ fixedText }}
        <span class="bed-switch__fixed-tip">(固定)</span>
      </el-tag>
    </el-tooltip>
 
    <el-dropdown v-else trigger="click" :disabled="loading" @command="onPick">
      <el-button size="small" :type="canOperate ? 'primary' : 'warning'" plain>
        <Icon :icon="canOperate ? 'ep:office-building' : 'ep:view'" class="mr-4px" />
        {{ dropdownText }}
        <Icon icon="ep:arrow-down" class="ml-4px" />
      </el-button>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item
            v-for="bed in beds"
            :key="`${bed.roomId}-${bed.bedNo}`"
            :command="bed"
            :class="{ 'is-current': isViewing(bed) }"
          >
            {{ bedLabel(bed) }}
          </el-dropdown-item>
          <el-dropdown-item v-if="beds.length === 0" disabled>暂无已开放工位</el-dropdown-item>
          <el-dropdown-item divided @click="router.push({ path: '/roomselect' })">
            去「选工位页」…
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
 
    <!-- 主人已离开:只提示,不自动接管(按确认口径) -->
    <el-tag v-if="!canOperate && roomStore.viewTargetVacant" type="info" size="small" class="ml-8px">
      该工位现无人操作,如需操作请到选工位页入座
    </el-tag>
  </div>
</template>
 
<style scoped lang="scss">
.bed-switch {
  display: flex;
  align-items: center;
  flex: none;
}
 
/* 【本次需求】固定工位的只读标签(领用 / 装机页) */
.bed-switch__fixed {
  font-weight: 600;
}
 
.bed-switch__fixed-tip {
  margin-left: 2px;
  font-weight: 400;
  color: var(--el-text-color-secondary);
}
 
:deep(.el-dropdown-menu__item.is-current) {
  color: var(--el-color-primary);
  font-weight: 600;
}
</style>