GasThreshold.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <div class="app-container">
  3. <!-- 搜索区 -->
  4. <el-card class="search-card">
  5. <el-form :model="queryParams" ref="queryRef" :inline="true" class="search-form">
  6. <el-form-item label="设备编码">
  7. <el-input v-model="queryParams.deviceCode" placeholder="请输入设备编码" clearable style="width: 180px" />
  8. </el-form-item>
  9. <el-form-item label="预警类型">
  10. <el-select v-model="queryParams.warningType" placeholder="请选择" clearable style="width: 160px">
  11. <el-option v-for="opt in WARNING_TYPE_OPTIONS" :key="opt.warningType" :label="opt.warningType" :value="opt.warningType" />
  12. </el-select>
  13. </el-form-item>
  14. <el-form-item label="预警编码">
  15. <el-input v-model="queryParams.warningCode" placeholder="请输入预警编码" clearable style="width: 180px" />
  16. </el-form-item>
  17. <el-form-item>
  18. <el-button type="primary" @click="handleQuery">查询</el-button>
  19. <el-button @click="resetQuery">重置</el-button>
  20. <el-button type="success" @click="handleAdd">新增阈值</el-button>
  21. </el-form-item>
  22. </el-form>
  23. </el-card>
  24. <!-- 表格 -->
  25. <el-card class="table-card">
  26. <el-table :data="tableData" v-loading="loading" border style="width: 100%">
  27. <el-table-column label="设备编码" prop="deviceCode" min-width="140" show-overflow-tooltip />
  28. <el-table-column label="设备名称" min-width="140" show-overflow-tooltip>
  29. <template #default="{ row }">
  30. {{ getDeviceName(row.deviceCode) }}
  31. </template>
  32. </el-table-column>
  33. <el-table-column label="设备类型" min-width="120" show-overflow-tooltip>
  34. <template #default="{ row }">
  35. {{ getDeviceType(row.deviceCode) }}
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="预警类型" prop="warningType" min-width="120" />
  39. <el-table-column label="预警编码" prop="warningCode" min-width="140" />
  40. <el-table-column label="最小值" prop="minValue" width="90" align="center" />
  41. <el-table-column label="最大值" prop="maxValue" width="90" align="center" />
  42. <el-table-column label="备注" prop="remark" min-width="120" show-overflow-tooltip />
  43. <el-table-column label="创建时间" prop="createTime" width="170" />
  44. <el-table-column label="操作" width="160" align="center" fixed="right">
  45. <template #default="{ row }">
  46. <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
  47. <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
  48. </template>
  49. </el-table-column>
  50. </el-table>
  51. <el-pagination
  52. v-model:current-page="pageNum" v-model:page-size="pageSize" :total="total"
  53. layout="total, prev, pager, next, sizes" :page-sizes="[10, 20, 50]"
  54. @size-change="handleSizeChange" @current-change="handlePageChange"
  55. style="margin-top: 16px; justify-content: flex-end"
  56. />
  57. </el-card>
  58. <!-- 新增/编辑对话框 -->
  59. <el-dialog :title="dialogTitle" v-model="dialogVisible" width="600px" destroy-on-close>
  60. <el-form :model="formData" :rules="rules" ref="formRef" label-width="100px">
  61. <el-form-item label="选择设备" prop="deviceCode">
  62. <el-select v-model="formData.deviceCode" placeholder="请选择设备" filterable clearable style="width: 100%"
  63. @click="loadGasDeviceOptions">
  64. <el-option v-for="d in deviceOptions" :key="d.equipmentCode" :label="d.equipmentCode + ' - ' + d.equipmentName" :value="d.equipmentCode" />
  65. </el-select>
  66. </el-form-item>
  67. <el-form-item label="预警类型" prop="warningType">
  68. <el-select v-model="formData.warningType" placeholder="请选择预警类型" style="width: 100%" @change="onWarningTypeChange">
  69. <el-option v-for="opt in WARNING_TYPE_OPTIONS" :key="opt.warningType" :label="opt.warningType" :value="opt.warningType" />
  70. </el-select>
  71. </el-form-item>
  72. <el-form-item label="预警编码">
  73. <el-input v-model="formData.warningCode" placeholder="选择预警类型后自动带出" />
  74. </el-form-item>
  75. <el-row :gutter="20">
  76. <el-col :span="12">
  77. <el-form-item label="最小值" prop="minValue">
  78. <el-input-number v-model="formData.minValue" :precision="2" :min="0" style="width: 100%" placeholder="阈值最小值" />
  79. </el-form-item>
  80. </el-col>
  81. <el-col :span="12">
  82. <el-form-item label="最大值" prop="maxValue">
  83. <el-input-number v-model="formData.maxValue" :precision="2" :min="0" style="width: 100%" placeholder="阈值最大值" />
  84. </el-form-item>
  85. </el-col>
  86. </el-row>
  87. <el-form-item label="备注">
  88. <el-input v-model="formData.remark" type="textarea" :rows="3" placeholder="备注信息" />
  89. </el-form-item>
  90. </el-form>
  91. <template #footer>
  92. <el-button @click="dialogVisible = false">取消</el-button>
  93. <el-button type="primary" @click="handleSave" :loading="submitLoading">确定</el-button>
  94. </template>
  95. </el-dialog>
  96. </div>
  97. </template>
  98. <script setup name="GasThreshold">
  99. import { ref, reactive, onMounted } from 'vue'
  100. import { ElMessage, ElMessageBox } from 'element-plus'
  101. import { getWarningThresholdModulePage, saveWarningThreshold, updateWarningThreshold, deleteWarningThreshold } from '@/api/pipeNetwork/basic'
  102. import request from '@/utils/request'
  103. // ==================== 燃气模块预警类型与预警编码参考 ====================
  104. const WARNING_TYPE_OPTIONS = [
  105. { warningType: '可燃气体浓度预警', warningCode: 'WARN-GAS-CONCENTRATION' },
  106. { warningType: '可燃气体温度预警', warningCode: 'WARN-GAS-TEMPERATURE' },
  107. { warningType: '可燃气体湿度预警', warningCode: 'WARN-GAS-HUMIDITY' },
  108. { warningType: '压力预警', warningCode: 'WARN-PRESSURE' },
  109. { warningType: '温度预警', warningCode: 'WARN-TEMPERATURE' },
  110. { warningType: '湿度预警', warningCode: 'WARN-HUMIDITY' }
  111. ]
  112. // ==================== 搜索参数 ====================
  113. const queryParams = reactive({
  114. deviceCode: '',
  115. warningType: '',
  116. warningCode: ''
  117. })
  118. const queryRef = ref(null)
  119. function handleQuery() {
  120. pageNum.value = 1
  121. loadData()
  122. }
  123. function resetQuery() {
  124. queryParams.deviceCode = ''
  125. queryParams.warningType = ''
  126. queryParams.warningCode = ''
  127. pageNum.value = 1
  128. loadData()
  129. }
  130. // ==================== 表格数据 ====================
  131. const tableData = ref([])
  132. const total = ref(0)
  133. const loading = ref(false)
  134. const pageNum = ref(1)
  135. const pageSize = ref(10)
  136. async function loadData() {
  137. loading.value = true
  138. try {
  139. const params = {}
  140. if (queryParams.deviceCode) params.deviceCode = queryParams.deviceCode
  141. if (queryParams.warningType) params.warningType = queryParams.warningType
  142. if (queryParams.warningCode) params.warningCode = queryParams.warningCode
  143. const res = await getWarningThresholdModulePage(pageNum.value, pageSize.value, '燃气', params)
  144. const pageData = res.data || res
  145. tableData.value = pageData.records || []
  146. total.value = pageData.total || 0
  147. } catch (e) {
  148. console.error('加载阈值列表失败', e)
  149. ElMessage.error('加载阈值列表失败')
  150. } finally {
  151. loading.value = false
  152. }
  153. }
  154. function handlePageChange() { loadData() }
  155. function handleSizeChange() { pageNum.value = 1; loadData() }
  156. // ==================== 设备名称/类型映射 ====================
  157. const deviceInfoMap = ref({})
  158. async function ensureDeviceMap() {
  159. if (Object.keys(deviceInfoMap.value).length > 0) return
  160. try {
  161. const res = await request({ url: '/EquipmentBase/findByTopLevelType', method: 'get', params: { typeName: '燃气' } })
  162. const list = res.data || []
  163. const map = {}
  164. list.forEach(d => { map[d.equipmentCode] = d })
  165. deviceInfoMap.value = map
  166. } catch (e) { console.error('加载燃气设备列表失败', e) }
  167. }
  168. function getDeviceName(deviceCode) {
  169. const d = deviceInfoMap.value[deviceCode]
  170. return d ? d.equipmentName : deviceCode
  171. }
  172. function getDeviceType(deviceCode) {
  173. const d = deviceInfoMap.value[deviceCode]
  174. return d ? (d.typeName || '-') : '-'
  175. }
  176. // ==================== 新增/编辑 ====================
  177. const dialogVisible = ref(false)
  178. const dialogTitle = ref('新增阈值')
  179. const formRef = ref(null)
  180. const submitLoading = ref(false)
  181. const isEdit = ref(false)
  182. const rules = {
  183. deviceCode: [{ required: true, message: '请选择设备', trigger: 'change' }],
  184. warningType: [{ required: true, message: '请选择预警类型', trigger: 'change' }]
  185. }
  186. const formData = reactive({
  187. id: '',
  188. deviceCode: '',
  189. warningType: '',
  190. warningCode: '',
  191. minValue: null,
  192. maxValue: null,
  193. remark: ''
  194. })
  195. const deviceOptions = ref([])
  196. async function loadGasDeviceOptions() {
  197. if (deviceOptions.value.length > 0) return
  198. try {
  199. const res = await request({ url: '/EquipmentBase/findByTopLevelType', method: 'get', params: { typeName: '燃气' } })
  200. deviceOptions.value = res.data || []
  201. } catch (e) {
  202. console.error('加载燃气设备列表失败', e)
  203. }
  204. }
  205. function resetForm() {
  206. formData.id = ''
  207. formData.deviceCode = ''
  208. formData.warningType = ''
  209. formData.warningCode = ''
  210. formData.minValue = null
  211. formData.maxValue = null
  212. formData.remark = ''
  213. isEdit.value = false
  214. dialogTitle.value = '新增阈值'
  215. }
  216. // 选择预警类型后自动带出预警编码
  217. function onWarningTypeChange(val) {
  218. const opt = WARNING_TYPE_OPTIONS.find(o => o.warningType === val)
  219. formData.warningCode = opt ? opt.warningCode : ''
  220. }
  221. async function handleAdd() {
  222. resetForm()
  223. await loadGasDeviceOptions()
  224. dialogVisible.value = true
  225. }
  226. function handleEdit(row) {
  227. isEdit.value = true
  228. dialogTitle.value = '编辑阈值'
  229. formData.id = row.id || ''
  230. formData.deviceCode = row.deviceCode
  231. formData.warningType = row.warningType
  232. formData.warningCode = row.warningCode || ''
  233. formData.minValue = row.minValue
  234. formData.maxValue = row.maxValue
  235. formData.remark = row.remark || ''
  236. dialogVisible.value = true
  237. }
  238. async function handleSave() {
  239. const valid = await formRef.value.validate().catch(() => false)
  240. if (!valid) return
  241. submitLoading.value = true
  242. try {
  243. const data = {
  244. deviceCode: formData.deviceCode,
  245. warningType: formData.warningType,
  246. warningCode: formData.warningCode || undefined,
  247. minValue: formData.minValue,
  248. maxValue: formData.maxValue,
  249. remark: formData.remark || undefined
  250. }
  251. if (isEdit.value) {
  252. data.id = formData.id
  253. await updateWarningThreshold(data)
  254. ElMessage.success('修改成功')
  255. } else {
  256. await saveWarningThreshold(data)
  257. ElMessage.success('新增成功')
  258. }
  259. dialogVisible.value = false
  260. loadData()
  261. // 刷新设备映射
  262. deviceInfoMap.value = {}
  263. await ensureDeviceMap()
  264. } catch (e) {
  265. const msg = e?.response?.data?.msg || e?.message || '操作失败'
  266. ElMessage.error(msg)
  267. } finally {
  268. submitLoading.value = false
  269. }
  270. }
  271. // ==================== 删除 ====================
  272. function handleDelete(row) {
  273. ElMessageBox.confirm('确认删除该阈值配置?', '提示', { type: 'warning' }).then(async () => {
  274. try {
  275. await deleteWarningThreshold([row.id])
  276. ElMessage.success('删除成功')
  277. loadData()
  278. } catch (e) {
  279. ElMessage.error(e?.response?.data?.msg || '删除失败')
  280. }
  281. }).catch(() => {})
  282. }
  283. // ==================== 生命周期 ====================
  284. onMounted(async () => {
  285. await ensureDeviceMap()
  286. loadData()
  287. })
  288. </script>
  289. <style scoped>
  290. .app-container {
  291. padding: 16px;
  292. }
  293. .search-card {
  294. margin-bottom: 16px;
  295. }
  296. .search-form {
  297. display: flex;
  298. flex-wrap: wrap;
  299. }
  300. .table-card {
  301. min-height: 400px;
  302. }
  303. </style>