Newer
Older
smartwell_front / src / views / home / rule / alarm / index.vue
lyg on 11 Sep 7 KB 数据报警
<!--
  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 { 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 list = ref([])
const total = ref(0)
// 初始展示列
const columns = ref<any>([
  { text: '报警类型', value: 'alarmTypeName', align: 'center' },
  { text: '报警名称', value: 'alarmName', align: 'center' },
  { text: '报警等级', value: 'alarmLevelName', align: 'center' },
  { text: '报警提示方式', value: 'alarmNoteMethodName', align: 'center' },
  { text: '状态', value: 'stateName', align: 'center' },
  { text: '创建时间', value: 'ts', align: 'center' },
])
// 最终展示列
const columnsConfig = ref([])
// 修改列
const editColumns = (data: any) => {
  columnsConfig.value = data
}
const loadingTable = ref(true)
//  查询条件
const listQuery = ref({
  limit: 20,
  offset: 1,
  alarmLevelId: '',
  alarmName: '',
})
// 查询数据
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: '',
  }
  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(res.data.rows.map((item: any) => ({
    name: item.alarmType,
    value: item.id,
    id: item.id,
  })), 'name')
}

onMounted(async () => {
  await fetchDict()
  fetchData()
})
</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.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-area>
    <!-- 表头标题 -->
    <table-container
      :is-config="true" config-title="alarm-rule" :columns="columns" :config-columns="columnsConfig"
      :edit="editColumns"
    >
      <template #btns-right>
        <!-- 操作 -->
        <div>
          <el-button type="primary" @click="showAlarmType">
            报警类型管理
          </el-button>
          <el-button type="primary" @click="showAlarmLevel">
            报警等级管理
          </el-button>
          <el-button 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 #columns>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button type="primary" link size="small" @click="switchStatusRow(scope.row)">
                {{ scope.row.state === '1' ? '停用' : '启用' }}
              </el-button>
              <el-button type="primary" link size="small" @click="detailRow('edit', scope.row)">
                编辑
              </el-button>
              <el-button type="danger" link size="small" @click="removeRow(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>