123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- <template>
- <div class="container">
- <el-row :gutter="20" class="equal-height-row">
- <!-- 左侧设备列表 -->
- <el-col :span="12">
- <div class="card">
- <h3>{{ t('configPerson.deviceList') }}</h3>
- <div class="dept-select">
- <el-tree-select
- clearable
- v-model="formData.deptId1"
- :data="deptList"
- :props="defaultProps"
- check-strictly
- node-key="id"
- filterable
- :placeholder="t('configPerson.deviceListHolder')"
- @node-click="handleDeptDeviceTreeNodeClick"
- />
- </div>
- <!-- 设备搜索框 -->
- <div class="filter-input">
- <el-input
- v-model="deviceFilterText"
- :placeholder="t('devicePerson.filterDevicePlaceholder')"
- clearable
- prefix-icon="Search"
- />
- </div>
- <el-scrollbar height="400px">
- <el-checkbox-group v-model="selectedDevices">
- <div
- v-for="device in filteredDevices"
- :key="device.id"
- class="radio-item"
- >
- <el-checkbox :label="device.id">
- {{ device.deviceCode }} ({{ device.deviceName }}) - {{ device.devicePersons }}
- </el-checkbox>
- </div>
- </el-checkbox-group>
- </el-scrollbar>
- </div>
- </el-col>
- <!-- 右侧责任人选择 -->
- <el-col :span="12">
- <div class="card">
- <h3>{{ t('configPerson.rp') }}</h3>
- <div class="dept-select">
- <el-tree-select
- clearable
- v-model="formData.deptId"
- :data="deptList"
- :props="defaultProps"
- check-strictly
- node-key="id"
- filterable
- :placeholder="t('configPerson.rpHolder')"
- @node-click="handleDeptUserTreeNodeClick"
- />
- </div>
- <el-scrollbar height="450px">
- <el-checkbox-group v-model="selectedUsers" @change="handleUserSelectionChange">
- <div
- v-for="user in simpleUsers"
- :key="user.id"
- class="list-item"
- >
- <el-checkbox :label="user.id">
- {{ user.nickname }} ({{ user.deptName }})
- </el-checkbox>
- </div>
- </el-checkbox-group>
- </el-scrollbar>
- </div>
- </el-col>
- </el-row>
- <!-- 暂存关联列表 -->
- <div class="submit-area">
- <div class="card">
- <h3>{{ t('configPerson.reasonForAdjustment') }}<span class="required-star">*</span></h3>
- <el-input
- v-model="formData.reason"
- :placeholder="t('configPerson.rfaHolder')"
- class="reason-input"
- type="textarea"
- :rows="3"
- @input="handleReasonInput"
- :disabled="selectedDevices.length === 0 || selectedUsers.length === 0"
- />
- </div>
- <div class="temp-list card">
- <h3>{{ t('configPerson.adjustmentRecords') }}</h3>
- <el-table :data="tempRelations" style="width: 100%">
- <el-table-column prop="deviceNames" :label="t('devicePerson.deviceName')" width="200" />
- <el-table-column prop="userNames" :label="t('devicePerson.rp')" />
- <el-table-column prop="reason" :label="t('configPerson.reasonForAdjustment')" />
- <el-table-column :label="t('devicePerson.operation')" width="120">
- <template #default="{ row }">
- <el-button
- type="danger"
- size="small"
- @click="removeTempRelation(row.deviceIds)"
- >
- {{ t('fault.del') }}
- </el-button>
- </template>
- </el-table-column>
- </el-table>
- <div class="submit-area">
- <el-button
- type="primary"
- size="large"
- @click="submitRelations"
- :disabled="isSaveDisabled"
- >
- {{ t('iotMaintain.save') }}
- </el-button>
- </div>
- </div>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { ref, computed } from 'vue'
- import { ElMessage } from 'element-plus'
- import {defaultProps, handleTree} from "@/utils/tree";
- import * as DeptApi from "@/api/system/dept";
- import {IotDeviceApi, IotDeviceVO} from "@/api/pms/device";
- import {IotDevicePersonApi, IotDevicePersonVO} from "@/api/pms/iotdeviceperson";
- import * as UserApi from "@/api/system/user";
- import {simpleUserList, UserVO} from "@/api/system/user";
- import { useRouter } from 'vue-router'
- const router = useRouter()
- const { t } = useI18n() // 国际化
- import { useTagsViewStore } from "@/store/modules/tagsView";
- defineOptions({ name: 'ConfigDevicePerson' })
- const simpleDevices = ref<IotDeviceVO[]>([])
- const simpleUsers = ref<UserVO[]>([])
- const deptList = ref<Tree[]>([]) // 树形结构
- const { delView } = useTagsViewStore() // 视图操作
- const formData = ref({
- id: undefined,
- deviceCode: undefined,
- deviceName: undefined,
- brand: undefined,
- model: undefined,
- deptId: undefined as string | undefined,
- deptId1: undefined as string | undefined,
- deviceStatus: undefined,
- assetProperty: undefined,
- reason: '',
- picUrl: undefined,
- })
- const queryParams = reactive({
- deptId1: formData.value.deptId1,
- deptId: formData.value.deptId,
- })
- const emit = defineEmits(['success', 'node-click']) // 定义 success 树点击 事件,用于操作成功后的回调
- // 响应式数据
- const selectedDevice = ref<number>(0)
- const selectedDevices = ref<number[]>([])
- const selectedDept = ref('')
- const selectedUsers = ref<number[]>([])
- const tempRelations = ref<Array<{
- deviceIds: number[]
- deviceNames: string
- userIds: number[]
- userNames: string
- reason: string
- }>>([])
- watch(selectedDevices, (newVal, oldVal) => {
- // 找出新增的设备
- const addedDevices = newVal.filter(id => !oldVal.includes(id));
- // 找出取消选择的设备
- const removedDevices = oldVal.filter(id => !newVal.includes(id))
- // 移除对应设备的关联记录
- tempRelations.value = tempRelations.value.filter(
- r => !removedDevices.includes(r.deviceIds[0])
- )
- // 为新增的设备创建关联记录
- if (addedDevices.length > 0) {
- const addedDeviceObjects = simpleDevices.value.filter(d =>
- addedDevices.includes(d.id)
- );
- addedDeviceObjects.forEach(device => {
- // 使用当前已选的责任人和调整原因
- updateDeviceRelation(device, selectedUsers.value);
- });
- }
- // 当没有选中设备时清空其他选项
- if (newVal.length === 0) {
- selectedUsers.value = []
- formData.value.reason = ''
- }
- })
- const handleUserSelectionChange = (userIds: number[]) => {
- if (selectedDevices.value.length === 0) {
- ElMessage.warning('请先选择设备')
- selectedUsers.value = []
- return
- }
- // 获取所有选中设备
- const devices = simpleDevices.value.filter(d =>
- selectedDevices.value.includes(d.id)
- )
- // 更新所有设备的关联关系
- devices.forEach(device => {
- updateDeviceRelation(device, userIds)
- })
- }
- /** 处理 部门-设备 树 被点击 */
- const handleDeptDeviceTreeNodeClick = async (row: { [key: string]: any }) => {
- emit('node-click', row)
- formData.value.deptId1 = row.id
- await getDeviceList()
- }
- // 新增计算属性:判断保存按钮是否禁用
- const isSaveDisabled = computed(() => {
- // 当没有调整记录或调整原因为空时禁用按钮
- return tempRelations.value.length === 0 || !formData.value.reason.trim();
- });
- /** 获得 部门下的设备 列表 */
- const getDeviceList = async () => {
- try {
- const params = { deptId: formData.value.deptId1 }
- const data = await IotDeviceApi.simpleDevices(params)
- simpleDevices.value = data || []
- } catch (error) {
- simpleDevices.value = []
- console.error('获取设备列表失败:', error)
- }
- }
- /** 处理 部门-人员 树 被点击 */
- const handleDeptUserTreeNodeClick = async (row: { [key: string]: any }) => {
- emit('node-click', row)
- formData.value.deptId = row.id
- await getUserList()
- }
- /** 获得 部门下的人员 列表 */
- const getUserList = async () => {
- try {
- const params = {
- deptId: formData.value.deptId,
- pageNo: 1,
- pageSize: 10 }
- const data = await UserApi.simpleUserList(params)
- simpleUsers.value = data || []
- } catch (error) {
- simpleUsers.value = []
- console.error('获取人员列表失败:', error)
- }
- }
- // 设备过滤文本
- const deviceFilterText = ref('')
- // 计算属性:过滤设备列表
- const filteredDevices = computed(() => {
- const searchText = deviceFilterText.value.toLowerCase().trim()
- if (!searchText) return simpleDevices.value
- return simpleDevices.value.filter(device => {
- return (
- (device.deviceCode || '').toLowerCase().includes(searchText) ||
- (device.deviceName || '').toLowerCase().includes(searchText)
- )
- })
- })
- // 新增输入处理方法
- const handleReasonInput = (value: string) => {
- formData.value.reason = value
- // 同步到所有已选设备的记录
- selectedDevices.value.forEach(deviceId => {
- const relation = tempRelations.value.find(
- r => r.deviceIds[0] === deviceId
- )
- if (relation) {
- relation.reason = value
- }
- })
- }
- // 更新设备关联关系
- const updateDeviceRelation = (device: IotDeviceVO, userIds: number[]) => {
- // 获取当前选择的人员
- const users = simpleUsers.value.filter(u => userIds.includes(u.id))
- const newRelation = {
- deviceIds: [device.id],
- deviceNames: `${device.deviceCode} (${device.deviceName})`,
- userIds: users.map(u => u.id),
- userNames: users.map(u => u.nickname).join(', '),
- reason: formData.value.reason
- }
- const existIndex = tempRelations.value.findIndex(
- // r => r.deviceIds[0] === device.id
- r => r.deviceIds.includes(device.id)
- )
- if (existIndex > -1) {
- tempRelations.value[existIndex] = newRelation
- } else {
- tempRelations.value.push(newRelation)
- }
- }
- const clearSelection = () => {
- selectedDevice.value = ''
- selectedUsers.value = []
- selectedDept.value = ''
- }
- const removeTempRelation = (deviceIds: number[]) => {
- tempRelations.value = tempRelations.value.filter(
- r => r.deviceIds.join() !== deviceIds.join()
- )
- // 同步取消勾选设备
- selectedDevices.value = selectedDevices.value.filter(
- id => !deviceIds.includes(id)
- )
- }
- const submitRelations = async () => {
- try {
- // 校验所有调整原因
- const hasEmptyReason = tempRelations.value.some(
- item => !item.reason?.trim()
- )
- if (hasEmptyReason) {
- ElMessage.error('请填写调整原因')
- return
- }
- // 转换为后端需要的格式
- const submitData = tempRelations.value.flatMap(relation => {
- return relation.deviceIds.map(deviceId => ({
- deviceId,
- userIds: relation.userIds,
- reason: relation.reason
- }))
- })
- await IotDevicePersonApi.saveDevicePersonRelation(submitData)
- // 模拟API调用
- ElMessage.success('提交成功')
- tempRelations.value = []
- delView(unref(router.currentRoute.value))
- router.back()
- } catch (error) {
- ElMessage.error('提交失败,请重试')
- }
- }
- /** 初始化 */
- onMounted(async () => {
- // 初始化部门树 根据选择的部门 查询设备列表 部门人员列表
- deptList.value = handleTree(await DeptApi.getSimpleDeptList())
- })
- </script>
- <style scoped>
- .equal-height-row {
- display: flex;
- align-items: stretch;
- }
- .container {
- padding: 20px;
- }
- .card {
- border: 1px solid #ebeef5;
- border-radius: 4px;
- padding: 20px;
- margin-bottom: 20px;
- background: white;
- box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
- transition: box-shadow 0.2s;
- }
- .list-item {
- padding: 8px 12px;
- border-bottom: 1px solid #f0f0f0;
- }
- .dept-select {
- margin-bottom: 20px;
- }
- .action-bar {
- text-align: right;
- }
- .temp-list {
- margin-top: 20px;
- }
- .submit-area {
- margin-top: 20px;
- text-align: center;
- }
- h3 {
- margin: 0 0 15px 0;
- color: #303133;
- }
- .radio-item {
- width: 100%;
- padding: 8px 12px;
- border-bottom: 1px solid #f0f0f0;
- }
- .radio-item .el-radio {
- width: 100%;
- height: 100%;
- }
- .radio-item .el-radio__label {
- display: block;
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .required-star {
- color: #ff4d4f;
- margin-left: 3px;
- vertical-align: middle;
- }
- .filter-input {
- margin-bottom: 15px;
- }
- .no-data {
- padding: 20px;
- text-align: center;
- color: #999;
- }
- </style>
|