<!-- Description: 规则引擎-报警规则管理 Author: 李亚光 Date: 2024-09-04 --> <script lang="ts" setup name="RuleAlarmManage"> import { ElLoading, ElMessage, ElMessageBox } from 'element-plus' import alarmType from './components/alarmTypeList.vue' import alarmLevel from './components/alarmLevelList.vue' import editDialog from './components/editDialog.vue' import { getDictByCode } from '@/api/system/dict' import { getAlarmLevelListPage, getAlarmRuleListPage, getAlarmTypeListPage, removeAlarmRule, switchStatus } from '@/api/home/rule/alarm' import { getDeviceTypeListPage } from '@/api/home/device/type' import { getManufacturerListPage, getProductListPage } from '@/api/home/device/product' import { uniqueMultiArray } from '@/utils/Array' const alarmCategoryList = ref<{ id: string; name: string; value: string }[]>([]) // 报警类别 const alarmLevelList = ref<{ id: string; name: string; value: string }[]>([]) // 报警等级 const alarmTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 报警类型 const deviceTypeList = ref<{ id: string; name: string; value: string }[]>([]) const manufacturerList = ref<{ id: string; name: string; value: string }[]>([]) const productList = ref<{ id: string; name: string; value: string }[]>([]) // 表格数据 const list = ref([]) const total = ref(0) // 初始展示列 const columns = ref<any>([ { text: '报警名称', value: 'alarmName', align: 'center' }, { text: '报警类型', value: 'alarmTypeName', align: 'center' }, { text: '设备类型', value: 'typeName', align: 'center' }, { text: '产品', value: 'productName', align: 'center' }, { text: '厂商', value: 'manufacturerName', align: 'center' }, { text: '报警等级', value: 'alarmLevelName', align: 'center', width: '100' }, { text: '报警提示方式', value: 'alarmNoteMethodName', align: 'center', width: '130', isCustom: true }, { text: '状态', value: 'stateName', align: 'center', width: '100', isCustom: true }, { text: '创建时间', value: 'ts', align: 'center', width: '200' }, ]) // 最终展示列 const columnsConfig = ref([]) // 修改列 const editColumns = (data: any) => { columnsConfig.value = data } const loadingTable = ref(true) // 查询条件 const listQuery = ref({ limit: 20, offset: 1, alarmLevelId: '', alarmName: '', alarmTypeId: '', typeId: '', manufacturerId: '', productId: '', }) // 查询数据 const fetchData = () => { loadingTable.value = true getAlarmRuleListPage(listQuery.value).then((res) => { list.value = res.data.rows.map((item: any) => ({ ...item, alarmTypeName: alarmTypeList.value[alarmTypeList.value.findIndex(citem => item.alarmTypeId === citem.id)].name || '', alarmLevelName: alarmLevelList.value[alarmLevelList.value.findIndex(citem => item.alarmLevelId === citem.id)].name || '', stateName: item.state === '1' ? '启用' : '未启用', alarmNoteMethodName: item.alarmNoteMethod === '1' ? '弹窗' : '消息提醒', })) total.value = res.data.total loadingTable.value = false }).catch(() => { loadingTable.value = false }) } // 重置查询条件f const reset = () => { listQuery.value = { limit: 20, offset: 1, alarmLevelId: '', alarmName: '', alarmTypeId: '', typeId: '', manufacturerId: '', productId: '', } fetchData() } // 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写 const changePage = (val: { size: number; page: number }) => { if (val && val.size) { listQuery.value.limit = val.size } if (val && val.page) { listQuery.value.offset = val.page } fetchData() } // 报警类型管理 const typeRef = ref() const showAlarmType = () => { typeRef.value.initDialog() } // 报警等级管理 const levelRef = ref() const showAlarmLevel = () => { levelRef.value.initDialog() } // 报警等级/类型确认修改 const confirmAlarm = async () => { await fetchDict() fetchData() } // 删除行 const removeRow = (row: any) => { ElMessageBox.confirm( '确定要删除该数据吗?', '确认操作', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', }, ).then(() => { removeAlarmRule(row.id).then(() => { ElMessage.success('操作成功') fetchData() }) }) } // 新建或编辑 const editRef = ref() const detailRow = (type: string, row: any) => { editRef.value.initDialog(type, row) } // 停用或者启用 const switchStatusRow = (row: any) => { ElMessageBox.confirm( `确认要${row.state === '1' ? '停用' : '启用'}该报警吗?`, '确认操作', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', }, ).then(() => { switchStatus({ ids: [row.id], state: row.state === '1' ? '0' : '1', }).then(() => { ElMessage.success('操作成功') fetchData() }) }) } // 获取字典 async function fetchDict() { const res = await getDictByCode('alarmCategory') alarmCategoryList.value = res.data const res1 = await getAlarmLevelListPage({ offset: 1, limit: 9999 }) alarmLevelList.value = res1.data.rows.map((item: any) => ({ name: item.alarmLevel, value: item.id, id: item.id, })) const res2 = await getAlarmTypeListPage({ offset: 1, limit: 9999 }) alarmTypeList.value = uniqueMultiArray(res2.data.rows.map((item: any) => ({ name: item.alarmType, value: item.id, id: item.id, })), 'name') getDeviceTypeListPage({ offset: 1, limit: 9999 }).then((res) => { deviceTypeList.value = res.data.rows.map((item: any) => ({ name: item.typeName, value: item.id, id: item.id, })) }) getManufacturerListPage().then((res) => { manufacturerList.value = res.data.rows.map((item: any) => ({ name: item.name, value: item.id, id: item.id, })) }) // productList getProductListPage({ offset: 1, limit: 99999 }).then((res) => { productList.value = res.data.rows.map((item: any) => ({ name: item.productName || '', id: item.id, value: item.id, })) }) } onMounted(async () => { await fetchDict() fetchData() }) const { proxy } = getCurrentInstance() as any </script> <template> <!-- 布局 --> <app-container> <!-- 报警类型弹窗 --> <alarm-type ref="typeRef" @refresh="confirmAlarm" /> <!-- 报警等级弹窗 --> <alarm-level ref="levelRef" @refresh="confirmAlarm" /> <!-- 新建或编辑弹窗 --> <edit-dialog ref="editRef" @refresh="fetchData" /> <!-- 筛选条件 --> <search-area :need-clear="true" @search="fetchData" @clear="reset"> <search-item> <el-input v-model="listQuery.alarmName" placeholder="报警名称" clearable /> </search-item> <search-item> <el-select v-model="listQuery.alarmTypeId" placeholder="报警类型" clearable filterable class="select" style="width: 192px;" > <el-option v-for="item in alarmTypeList" :key="item.id" :label="item.name" :value="item.id" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.alarmLevelId" placeholder="报警等级" clearable filterable class="select" style="width: 192px;" > <el-option v-for="item in alarmLevelList" :key="item.id" :label="item.name" :value="item.id" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.typeId" placeholder="设备类型" clearable filterable class="select" style="width: 192px;" > <el-option v-for="item in deviceTypeList" :key="item.id" :label="item.name" :value="item.id" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.productId" placeholder="产品" clearable filterable class="select" style="width: 192px;" > <el-option v-for="item in productList" :key="item.id" :label="item.name" :value="item.value" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.manufacturerId" placeholder="厂商" clearable filterable class="select" style="width: 192px;" > <el-option v-for="item in manufacturerList" :key="item.id" :label="item.name" :value="item.id" /> </el-select> </search-item> </search-area> <!-- 表头标题 --> <table-container :is-config="true" config-title="alarm-rule" :columns="columns" :config-columns="columnsConfig" :edit="editColumns" > <template #btns-right> <!-- 操作 --> <div> <el-button v-if="proxy.hasPerm('/rule/alarm/type')" type="primary" @click="showAlarmType"> 报警类型管理 </el-button> <el-button v-if="proxy.hasPerm('/rule/alarm/level')" type="primary" @click="showAlarmLevel"> 报警等级管理 </el-button> <el-button v-if="proxy.hasPerm('/rule/alarm/add')" type="primary" @click="detailRow('create', {})"> 新增 </el-button> </div> </template> <!-- 查询结果Table显示 --> <normal-table :data="list" :total="total" :columns="columnsConfig" :query="listQuery" :list-loading="loadingTable" @change="changePage" > <template #preColumns> <el-table-column label="序号" width="55" align="center"> <template #default="scope"> {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }} </template> </el-table-column> </template> <template #isCustom="{ scope, column }"> <!-- 报警提示方式 --> <span v-if="column.text === '报警提示方式'"> <el-tag v-if="scope.row.alarmNoteMethodName === '消息提醒'" type="primary">消息提醒</el-tag> <el-tag v-if="scope.row.alarmNoteMethodName === '弹窗'" type="danger">弹窗提醒</el-tag> </span> <!-- 报警提示方式 --> <span v-if="column.text === '状态'"> <el-tag v-if="scope.row.stateName === '启用'" type="success">启用</el-tag> <el-tag v-if="scope.row.stateName === '未启用'" type="info">未启用</el-tag> </span> </template> <template #columns> <el-table-column label="操作" width="140" align="center"> <template #default="scope"> <el-button v-if="proxy.hasPerm('/rule/alarm/handler')" type="primary" link size="small" @click="switchStatusRow(scope.row)" > {{ scope.row.state === '1' ? '停用' : '启用' }} </el-button> <el-button v-if="proxy.hasPerm('/rule/alarm/update')" type="primary" link size="small" @click="detailRow('edit', scope.row)" > 编辑 </el-button> <el-button v-if="proxy.hasPerm('/rule/alarm/delete')" type="danger" link size="small" @click="removeRow(scope.row)" > 删除 </el-button> </template> </el-table-column> </template> </normal-table> </table-container> </app-container> </template>