75 门诊出诊医生诊室设置的完善

This commit is contained in:
HuangXinQuan
2026-02-09 16:38:54 +08:00
parent 4c2b015210
commit 6fb5b5993a

View File

@@ -47,7 +47,7 @@
<el-table-column label="卫生机构" width="200" align="center" show-overflow-tooltip>
<template #default="scope">
<!-- ==忽略类型匹配string的"3"和number的3就能匹配上 -->
{{ tenantOptions.find(item => item.id == scope.row.orgName)?.tenantName || '未知机构' }}
{{ tenantOptions.find(item => item.id == scope.row.orgName)?.tenantName || scope.row.orgName || '未知机构' }}
</template>
</el-table-column>
<el-table-column prop="roomName" label="诊室名称" width="160" align="center" show-overflow-tooltip />
@@ -118,6 +118,7 @@
width="500px"
append-to-body
:close-on-click-modal="false"
destroy-on-close
>
<el-form
ref="formRef"
@@ -150,7 +151,7 @@
:disabled="dialogType === 'view'"
>
<el-option
v-for="item in tenantOptions"
v-for="item in addEditTenantOptions"
:key="item.id"
:label="item.tenantName"
:value="item.id"
@@ -250,7 +251,9 @@ const queryParams = reactive({
// 科室选项
const departmentOptions = ref([])
// 租户选项
const tenantOptions = ref([])
const tenantOptions = ref([]) // 用于查询表单
const rawTenantOptions = ref([]) // 存储从API获取的所有租户选项
const addEditTenantOptions = ref([]) // 用于新增/编辑弹窗的租户选项
// 弹窗相关
const dialogVisible = ref(false)
const dialogType = ref('') // add/edit/view
@@ -271,37 +274,105 @@ const form = reactive({
})
// 表单验证规则
const rules = {
roomName: [
{ required: true, message: '诊室名称不能为空', trigger: 'blur' },
{ max: 20, message: '诊室名称长度不能超过20个字符', trigger: 'blur' },
roomName: [
{ required: true, message: '诊室名称不能为空', trigger: 'blur' },
{ max: 20, message: '诊室名称长度不能超过20个字符', trigger: 'blur' },
{
validator: (rule, value, callback) => {
// console.log('Validating roomName:', { value, formId: form.id });
if (!value) {
return callback()
}
// 检查诊室名称是否已存在(全局唯一性)
const isExist = clinicRoomList.value.some(item => {
const checkRoomName = String(item.roomName) === String(value); // Ensure type consistency
const checkId = item.id !== form.id;
// console.log('Comparing item for roomName:', {
// itemId: item.id,
// itemRoomName: item.roomName,
// checkRoomName,
// checkId,
// allConditions: checkRoomName && checkId
// });
return checkRoomName && checkId
})
if (isExist) {
callback(new Error('诊室名称已存在'))
} else {
callback()
}
},
trigger: 'blur'
} ], department: [
{ required: true, message: '', trigger: 'change' },
{
validator: (rule, value, callback) => {
// console.log('Validating department:', { value, formOrgName: form.orgName, formId: form.id });
if (!value) {
return callback()
}
if (!form.orgName) {
// console.log('Department validation: form.orgName is missing, skipping uniqueness check.');
return callback()
}
// 检查当前租户下是否已存在相同的诊室名称
// 检查当前卫生机构下科室名称是否已存在
const isExist = clinicRoomList.value.some(item => {
return item.roomName === value &&
item.orgName === form.orgName &&
item.id !== form.id
const checkDepartment = String(item.department) === String(value);
const checkOrgName = String(item.orgName) === String(form.orgName); // Ensure type consistency
const checkId = item.id !== form.id; // Check against other items
// console.log('Comparing item:', {
// itemId: item.id,
// itemDepartment: item.department,
// itemOrgName: item.orgName,
// checkDepartment,
// checkOrgName,
// checkId,
// allConditions: checkDepartment && checkOrgName && checkId
// });
return checkDepartment && checkOrgName && checkId
})
if (isExist) {
callback(new Error('诊室名称已存在'))
// 不在此处直接报错而是依赖orgName的校验来显示错误
callback()
} else {
callback()
}
// 触发对orgName字段的重新验证以实现联动校验
proxy.formRef.validateField('orgName')
},
trigger: 'blur'
trigger: 'change'
}
],
department: [
{ required: true, message: '科室名称不能为空', trigger: 'change' }
],
orgName: [
{ required: true, message: '卫生机构不能为空', trigger: 'change' }
{ required: true, message: '卫生机构不能为空', trigger: 'change' },
{
validator: (rule, value, callback) => {
// 如果卫生机构为空或科室名称为空,则不进行组合唯一性校验
if (!value || !form.department) {
return callback()
}
// 检查当前卫生机构下科室名称是否已存在
const isExist = clinicRoomList.value.some(item => {
const checkDepartment = String(item.department) === String(form.department);
const checkOrgName = String(item.orgName) === String(value); // 这里使用当前的value (orgName)
const checkId = item.id !== form.id;
return checkDepartment && checkOrgName && checkId
})
if (isExist) {
callback(new Error('当前卫生机构下已存在该科室名称的诊室'))
} else {
callback()
}
// 触发对department字段的重新验证以实现联动校验
proxy.formRef.validateField('department')
},
trigger: 'change'
}
]
}
@@ -311,15 +382,22 @@ function getList() {
getClinicRoomList(queryParams)
.then(response => {
if (response.code === 200) {
clinicRoomList.value = response.data?.records || response.data || []
total.value = response.data?.total || clinicRoomList.value.length || 0
// 确保数据格式正确
const records = response.data?.records || response.data || []
clinicRoomList.value = records
total.value = response.data?.total || records.length || 0
// console.log('获取诊室列表成功,记录数:', records.length)
} else {
ElMessage.error(response.msg || '获取诊室列表失败')
clinicRoomList.value = []
total.value = 0
}
})
.catch(error => {
console.error('获取诊室列表失败:', error)
ElMessage.error('获取诊室列表失败')
clinicRoomList.value = []
total.value = 0
})
.finally(() => {
loading.value = false
@@ -341,44 +419,113 @@ function resetQuery() {
}
// 新增按钮操作
function handleAdd() {
async function handleAdd() {
// 首先清空表单
resetForm()
// 过滤租户选项,只保留 status 为 '0' 的卫生机构用于新增
addEditTenantOptions.value = rawTenantOptions.value.filter(item => item.status === '0');
// 设置对话框状态
dialogType.value = 'add'
dialogTitle.value = '新增诊室'
dialogVisible.value = true
// 为新增模式修改诊室名称验证规则:检查全局唯一性
rules.roomName = [
{ required: true, message: '诊室名称不能为空', trigger: 'blur' },
{ max: 20, message: '诊室名称长度不能超过20个字符', trigger: 'blur' },
{
validator: (rule, value, callback) => {
if (!value) {
return callback()
}
// 新增时检查所有诊室名称是否已存在
const isExist = clinicRoomList.value.some(item => {
return item.roomName === value
})
if (isExist) {
callback(new Error('诊室名称已存在'))
} else {
callback()
}
},
trigger: 'blur'
// 然后获取当前用户信息并设置默认卫生机构
try {
const userModule = await import('@/store/modules/user')
const useUserStore = userModule.default
const userStore = useUserStore()
// 从用户store获取租户ID和租户名称
const tenantId = userStore?.tenantId
const tenantName = userStore?.tenantName
console.log('当前用户信息:', userStore)
console.log('当前用户租户ID:', tenantId)
console.log('当前用户租户名称:', tenantName)
console.log('租户选项列表:', tenantOptions.value)
// 检查租户ID是否存在如果不存在则尝试从租户名称匹配
let matchedTenantId = null
if (tenantId) {
// 优先使用租户ID进行匹配
matchedTenantId = tenantId
} else if (tenantName && tenantOptions.value && tenantOptions.value.length > 0) {
// 如果没有租户ID但有租户名称则尝试通过名称匹配
const matchedTenant = tenantOptions.value.find(tenant =>
tenant.tenantName === tenantName
)
if (matchedTenant) {
matchedTenantId = matchedTenant.id
console.log('通过租户名称匹配到租户ID:', matchedTenantId)
}
}
]
// 检查匹配到的租户是否在租户选项列表中
if (matchedTenantId && tenantOptions.value && tenantOptions.value.length > 0) {
const userTenantExists = tenantOptions.value.some(tenant =>
tenant.id === matchedTenantId ||
Number(tenant.id) === Number(matchedTenantId)
)
console.log('用户租户是否存在:', userTenantExists)
if (userTenantExists) {
// 确保数据类型一致将租户ID转换为数字类型
form.orgName = Number(matchedTenantId)
console.log('已设置默认卫生机构ID:', form.orgName)
// 查找并显示卫生机构名称
const tenantInfo = tenantOptions.value.find(tenant =>
tenant.id === matchedTenantId ||
Number(tenant.id) === Number(matchedTenantId)
)
if (tenantInfo) {
console.log('已设置默认卫生机构名称:', tenantInfo.tenantName)
}
} else {
console.log('当前用户租户不在租户选项列表中')
}
} else {
console.log('用户租户ID和租户名称都为空或租户选项列表为空')
}
} catch (error) {
console.warn('无法获取当前用户信息:', error.message)
}
// 确保 form.orgName 是数字类型以便与下拉框选项匹配
if (form.orgName && typeof form.orgName === 'string') {
const orgNameAsNumber = Number(form.orgName)
if (!isNaN(orgNameAsNumber)) {
form.orgName = orgNameAsNumber
}
}
}
// 编辑按钮操作
function handleEdit(row) {
resetForm()
const id = row.id
// 编辑时不需要过滤租户状态,显示所有租户
addEditTenantOptions.value = rawTenantOptions.value;
getClinicRoomDetail(id)
.then(response => {
if (response.code === 200) {
Object.assign(form, response.data || row)
const clinicRoomData = response.data || row
// 处理 orgName 数据类型,确保与下拉框选项值类型一致
if (clinicRoomData.orgName) {
const orgNameAsNumber = Number(clinicRoomData.orgName) // 转换为数字类型
// 确保转换成功且不是一个 NaN 值
if (!isNaN(orgNameAsNumber)) {
clinicRoomData.orgName = orgNameAsNumber
}
}
Object.assign(form, clinicRoomData)
dialogType.value = 'edit'
dialogTitle.value = '编辑诊室'
dialogVisible.value = true
@@ -424,10 +571,23 @@ function handleEdit(row) {
function handleView(row) {
resetForm()
const id = row.id
// 查看时不需要过滤租户状态,显示所有租户
addEditTenantOptions.value = rawTenantOptions.value;
getClinicRoomDetail(id)
.then(response => {
if (response.code === 200) {
Object.assign(form, response.data || row)
const clinicRoomData = response.data || row
// 处理 orgName 数据类型,确保与下拉框选项值类型一致
if (clinicRoomData.orgName) {
const orgNameAsNumber = Number(clinicRoomData.orgName) // 转换为数字类型
// 确保转换成功且不是一个 NaN 值
if (!isNaN(orgNameAsNumber)) {
clinicRoomData.orgName = orgNameAsNumber
}
}
Object.assign(form, clinicRoomData)
dialogType.value = 'view'
dialogTitle.value = '查看诊室详情'
dialogVisible.value = true
@@ -547,23 +707,33 @@ function getDepartmentOptions() {
// 获取租户列表
function getTenantOptions() {
getTenantList()
return getTenantList()
.then(response => {
if (response.code === 200) {
// 处理不同的响应格式
let tenantList = [];
if (response.data?.records && Array.isArray(response.data.records)) {
tenantOptions.value = response.data.records
tenantList = response.data.records;
} else if (Array.isArray(response.data)) {
tenantOptions.value = response.data
tenantList = response.data;
} else {
tenantOptions.value = []
tenantList = [];
}
rawTenantOptions.value = tenantList; // 存储所有租户
tenantOptions.value = tenantList; // 查询表单使用所有租户
} else {
console.warn('获取租户列表失败:', response.msg)
rawTenantOptions.value = []
tenantOptions.value = []
}
return response; // 返回响应以支持链式调用
})
.catch(error => {
console.error('获取租户列表失败:', error)
rawTenantOptions.value = []
tenantOptions.value = []
return Promise.reject(error); // 返回拒绝的Promise
})
}