优化宿舍分配
This commit is contained in:
1 parent
92e9f7c8f1
commit
e15f81a9e4
4 files changed
+268
-40
No files matched your search
@@ -0,0 +1,146 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import http from '../api/http'
|
||||
|
||||
// 服务端搜索空床位:楼栋/楼层/房间筛选 + 关键字远程搜索 + 分页加载,避免一次性渲染全部床位。
|
||||
type BedOption = { id: string; label: string; buildingName: string; roomNumber: string; bedNumber: string }
|
||||
type RoomOption = { id: string; roomNumber: string; floor: number; isEnabled: boolean }
|
||||
type BuildingOption = { id: string; name: string; isEnabled: boolean; rooms: RoomOption[] }
|
||||
|
||||
const props = defineProps<{ modelValue?: string; buildings?: BuildingOption[]; excludeIds?: string[] }>()
|
||||
const emit = defineEmits<{ (e: 'update:modelValue', v: string): void; (e: 'change', bed: BedOption | null): void }>()
|
||||
|
||||
const buildingId = ref<string>()
|
||||
const floor = ref<number>()
|
||||
const roomId = ref<string>()
|
||||
const keyword = ref('')
|
||||
const options = ref<BedOption[]>([])
|
||||
const total = ref(0)
|
||||
const page = ref(1)
|
||||
const pageSize = 50
|
||||
const loading = ref(false)
|
||||
|
||||
const buildingOptions = computed(() => (props.buildings ?? []).filter(b => b.isEnabled !== false))
|
||||
const floorOptions = computed(() => {
|
||||
const list = (props.buildings ?? [])
|
||||
.filter(b => (buildingId.value ? b.id === buildingId.value : true))
|
||||
.flatMap(b => b.rooms ?? [])
|
||||
.filter(r => r.isEnabled !== false)
|
||||
.map(r => r.floor)
|
||||
return [...new Set(list)].sort((a, b) => a - b)
|
||||
})
|
||||
const roomOptions = computed(() => (props.buildings ?? [])
|
||||
.filter(b => (buildingId.value ? b.id === buildingId.value : true))
|
||||
.flatMap(b => b.rooms ?? [])
|
||||
.filter(r => r.isEnabled !== false && (floor.value == null || r.floor === floor.value))
|
||||
.sort((a, b) => a.roomNumber.localeCompare(b.roomNumber, 'zh-CN', { numeric: true })))
|
||||
const excludeSet = computed(() => new Set(props.excludeIds ?? []))
|
||||
|
||||
function query() {
|
||||
const p = new URLSearchParams()
|
||||
if (buildingId.value) p.set('buildingId', buildingId.value)
|
||||
if (floor.value != null) p.set('floor', String(floor.value))
|
||||
if (roomId.value) p.set('roomId', roomId.value)
|
||||
if (keyword.value) p.set('keyword', keyword.value)
|
||||
p.set('page', String(page.value))
|
||||
p.set('pageSize', String(pageSize))
|
||||
return p
|
||||
}
|
||||
|
||||
async function load(reset = false) {
|
||||
if (reset) page.value = 1
|
||||
loading.value = true
|
||||
try {
|
||||
const { data } = await http.get('/dormitory-services/vacant-beds', { params: query() })
|
||||
const items = (data.items ?? []) as BedOption[]
|
||||
options.value = reset ? items : [...options.value, ...items]
|
||||
total.value = data.total ?? 0
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function onSearch(value: string) {
|
||||
keyword.value = value.trim()
|
||||
load(true)
|
||||
}
|
||||
|
||||
function onVisible(visible: boolean) {
|
||||
if (visible && options.value.length === 0) load(true)
|
||||
}
|
||||
|
||||
function loadMore() {
|
||||
page.value += 1
|
||||
load(false)
|
||||
}
|
||||
|
||||
function onBuildingChange() {
|
||||
floor.value = undefined
|
||||
roomId.value = undefined
|
||||
load(true)
|
||||
}
|
||||
|
||||
function onFloorChange() {
|
||||
roomId.value = undefined
|
||||
load(true)
|
||||
}
|
||||
|
||||
function onRoomChange() {
|
||||
load(true)
|
||||
}
|
||||
|
||||
function onSelect(value: string) {
|
||||
emit('update:modelValue', value)
|
||||
emit('change', options.value.find(x => x.id === value) ?? null)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="vacant-bed-picker">
|
||||
<div class="picker-filters">
|
||||
<el-select v-model="buildingId" clearable filterable placeholder="楼栋" @change="onBuildingChange">
|
||||
<el-option v-for="b in buildingOptions" :key="b.id" :label="b.name" :value="b.id" />
|
||||
</el-select>
|
||||
<el-select v-model="floor" clearable placeholder="楼层" @change="onFloorChange">
|
||||
<el-option v-for="f in floorOptions" :key="f" :label="`${f} 层`" :value="f" />
|
||||
</el-select>
|
||||
<el-select v-model="roomId" clearable filterable placeholder="房间" @change="onRoomChange">
|
||||
<el-option v-for="r in roomOptions" :key="r.id" :label="`${r.roomNumber}(${r.floor} 层)`" :value="r.id" />
|
||||
</el-select>
|
||||
</div>
|
||||
<el-select
|
||||
:model-value="modelValue"
|
||||
filterable
|
||||
remote
|
||||
:remote-method="onSearch"
|
||||
:loading="loading"
|
||||
clearable
|
||||
placeholder="输入楼栋/房间/床位号搜索空床位"
|
||||
class="bed-select"
|
||||
@update:model-value="onSelect"
|
||||
@visible-change="onVisible"
|
||||
>
|
||||
<el-option
|
||||
v-for="b in options"
|
||||
:key="b.id"
|
||||
:label="b.label"
|
||||
:value="b.id"
|
||||
:disabled="excludeSet.has(b.id)"
|
||||
/>
|
||||
<template #footer>
|
||||
<div class="picker-footer">
|
||||
<span>{{ options.length >= total ? `共 ${total} 个空床位` : `已加载 ${options.length} / ${total}` }}</span>
|
||||
<el-button v-if="options.length < total" link type="primary" @click.stop="loadMore">加载更多</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-select>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.vacant-bed-picker { display: flex; flex-direction: column; gap: 8px; width: 100%; }
|
||||
.picker-filters { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
|
||||
.bed-select { width: 100%; }
|
||||
.picker-footer { display: flex; align-items: center; justify-content: space-between; padding: 0 12px 4px; color: #798780; font-size: 12px; }
|
||||
@media (max-width: 720px) { .picker-filters { grid-template-columns: 1fr; } }
|
||||
</style>
|
||||
Reference in new issue
Block a user