index.vue 11.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
<template>
    <div class="warning-baseline-container">
        <!-- 头部:标题 + 新增按钮 -->
        <div class="header">
            <h2 class="title">
                预警基线管理
            </h2>
        </div>
        <div class="table-container">
            <div class="flex">
                <div class="table-labal">
                    <span>*</span>
                    预警基线:
                </div>
                <el-button icon="el-icon-plus"
                           plain
                           type="primary"
                           @click="handleAdd">
                    新增配置
                </el-button>
            </div>

        </div>
        <!-- 表格 - 使用el-table -->
        <el-table border
                  class="warning-table"
                  :data="flattenTableData"
                  :header-cell-style="headerCellStyle"
                  :span-method="objectSpanMethod">
            <!-- 序号列 -->
            <el-table-column align="center"
                             label="序号"
                             prop="indexType"
                             width="60">
            </el-table-column>

            <!-- 指标列 -->
            <el-table-column align="center"
                             label="指标"
                             width="160">
                <template #default="scope">
                    <div v-if="scope.row.isFirstRow" class="indicator-info">
                        <span class="indicator-name">{{ scope.row.indicatorName }}</span>
                    </div>
                </template>
            </el-table-column>

            <!-- 时间维度列 -->
            <el-table-column align="center"
                             label="时间维度"
                             width="160">
                <template #default="scope">
                    <span v-if="scope.row.isFirstRow">{{ formatTimeDimension(scope.row.timeDimension) }}</span>
                </template>
            </el-table-column>

            <!-- 预警规则列 -->
            <el-table-column align="center"
                             label="预警规则"
                             min-width="320">
                <template #default="scope">
                    <div class="warning-rule">
                        <span class="warning-level">
                            {{ getLevelText(scope.row.warningLevel) }}
                            <span class="level" :class="[getLevelClass(scope.row.warningLevel)]"></span>
                        </span>
                        <div class="warning-rule-info">
                            数值区间
                            <el-input v-model="scope.row.minValue"
                                      disabled
                                      style="width: 140px;margin:0 8px;">
                                <template v-if="scope.row.indicatorCode === 'A085'" #append>
                                    %
                                </template>
                            </el-input>
                            <span>-</span>
                            <el-input v-model="scope.row.maxValue"
                                      disabled
                                      style="width: 140px;margin:0 8px;">
                                <template v-if="scope.row.indicatorCode === 'A085'" #append>
                                    %
                                </template>
                            </el-input>
                        </div>
                    </div>
                </template>
            </el-table-column>

            <!-- 操作列 -->
            <el-table-column fixed="right"
                             label="操作"
                             width="99">
                <template #default="scope">
                    <el-button type="text"
                               @click="handleConfig(scope.row)">
                        配置
                    </el-button>
                    <el-button type="text"
                               @click="handleDelete(scope.row)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <AddConfigDialog v-if="addConfigDialogVisible"
                         ref="addConfigDialog"
                         :record-info="recordInfo"
                         :visible.sync="addConfigDialogVisible"
                         @handleAddConfirm="handleAddConfirm"
                         @handleClose="handleClose">
        </AddConfigDialog>
    </div>
</template>

<script>
import { deleteBaselineConfig, listAllBaselineConfig } from "@/api/resident";
import AddConfigDialog from "./addConfig.vue";

export default {

	name: "WarningBaseline",
	components: {
		AddConfigDialog,
	},
	data() {
		return {
			recordInfo: {},
			addConfigDialogVisible: false,
			// 原始数据(你提供的格式)
			originalData: [
			],
		};
	},
	computed: {
		// 将原始数据扁平化,便于el-table渲染
		flattenTableData() {
			const flatData = [];

			this.originalData.forEach((indicator, index) => {
				const configs = indicator.configList || [];

				// 遍历每个指标的配置列表
				configs.forEach((config, configIndex) => {
					flatData.push({
						// 合并后的字段
						...config,
						maxValue: this.formatRuleValue(config),
						// 添加一些辅助字段用于合并单元格
						isFirstRow: configIndex === 0, // 是否是当前指标的第一行
						indicatorGroup: index, // 指标组索引
						indexType: index + 1,
						rowIndex: configIndex, // 组内行索引
						// 复制指标层字段(方便第一行显示)
						indicatorCode: indicator.indicatorCode,
						indicatorName: indicator.indicatorName,
						timeDimension: indicator.timeDimension,
						configList: configs,
					});
				});
			});

			return flatData;
		},
	},
	mounted() {
		this.getBaseLineConfigList();
	},
	methods: {
		async getBaseLineConfigList() {
			try {
				let result = await listAllBaselineConfig();
				console.log("获取预警基线配置列表成功:", result);
				if (result) {
					this.originalData = result || [];
				}
			} catch (error) {
				console.error("获取预警基线配置列表失败:", error);
			}
		},
		handleAdd() {
			this.addConfigDialogVisible = true;
			this.recordInfo = {};
		},
		// 合并单元格逻辑
		objectSpanMethod({ row, column, rowIndex, columnIndex }) {
			// 合并指标列(第1列)和时间维度列(第2列)
			if (columnIndex !== 3) {
				// 获取当前指标组的起始索引
				let startIndex = 0;
				for (let i = 0; i < this.originalData.length; i++) {
					const groupSize = this.originalData[i].configList.length;
					if (rowIndex >= startIndex && rowIndex < startIndex + groupSize) {
						// 当前行属于第 i 组
						if (rowIndex === startIndex) {
							// 组的第一个行,向下合并 groupSize 行
							return [groupSize, 1];
						} else {
							// 组的其他行,被合并(不显示)
							return [0, 1];
						}
					}
					startIndex += groupSize;
				}
			}

			// 预警规则列和操作列正常显示
			return [1, 1];
		},

		// 表头样式
		headerCellStyle({ row, column, rowIndex, columnIndex }) {
			return {
				backgroundColor: "#f8fafd",
				color: "#475569",
				fontWeight: "600",
				fontSize: "14px",
			};
		},

		// 时间维度格式化
		formatTimeDimension(dim) {
			const map = {
				1: "日",
				2: "周",
				3: "月",
			};
			return map[dim] || "--";
		},

		// 预警级别文字
		getLevelText(level) {
			const map = {
				0: "红灯",
				1: "黄灯",
				2: "绿灯",
			};
			return map[level] || "--";
		},

		// 预警级别样式类
		getLevelClass(level) {
			const map = {
				0: "level-red",
				1: "level-yellow",
				2: "level-green",
			};
			return map[level] || "";
		},

		// 格式化规则数值区间
		formatRuleValue(rule) {
			// 处理最大值:如果是正无穷 (maxValueInf === 1) 或者 maxValue 为 null,显示 +∞
			if (rule.maxValueInf === 1 || rule.maxValue === null) {
				return `+∞`;
			}
			return `${rule.maxValue}`;
		},

		// 删除操作
		handleDelete(row) {
			console.log(row);
			this.$confirm(`确定删除该预警配置吗?`, "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			}).then(() => {
				let params = {
					indicatorCode: row.indicatorCode,
					timeDimension: row.timeDimension,
				};
				deleteBaselineConfig(params).then((res) => {
					if (res) {
						this.$message({
							type: "success",
							message: "删除成功!",
						});
						this.getBaseLineConfigList();
					}
				});
			}).catch(() => {

			});
		},
		handleConfig(row) {
			this.recordInfo = row;
			this.addConfigDialogVisible = true;
		},
		handleClose() {
			this.addConfigDialogVisible = false;
		},
		handleAddConfirm() {
			this.addConfigDialogVisible = false;
			this.getBaseLineConfigList();
		},
	},
};
</script>

<style scoped lang="scss">
.warning-baseline-container {
	height: calc(100vh - 200px);
	overflow: auto;
	background: #ffffff;
	box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
	margin: 0 20px;
	padding-bottom: 20px;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.flex {
	display: flex;
	align-items: center;
}
.table-container {
	padding: 20px;
	.table-labal {
		dispaly: flex;
		align-items: center;
		width: 150px;
		text-align: right;
		color: #333333;
		font-size: 14px;
		span {
			color: #eb4e4e;
		}
	}
}
/* 头部 */
.header {
	padding: 13.5px 20px;
	color: #333333;
	font-size: 16px;
	font-weight: 600;
	border-bottom: 1px solid #e5e5e5;
}

.title {
	font-size: 20px;
	font-weight: 600;
	color: #1e293b;
	margin: 0;
	position: relative;
	padding-left: 12px;
}

.warning-table {
	margin-left: 170px !important;
	// border: 1px solid #e9edf4;
	width: calc(100% - 190px) !important;
}

/* 指标信息 */
.indicator-info {
	display: flex;
	flex-direction: column;
	gap: 4px;
}

.indicator-name {
	font-weight: 600;
	color: #0f1829;
}

.indicator-code {
	font-size: 12px;
	color: #94a3b8;
}

/* 预警规则 */
.warning-rule {
	display: flex;
	align-items: center;
	gap: 8px;
	flex-wrap: wrap;
}

.warning-level {
	display: flex;
	align-items: center;
	min-width: 48px;
	font-weight: 500;
	padding: 2px 0;
	margin-right: 70px;
}
.level {
	display: inline-block;
	width: 6px;
	height: 6px;
	border-radius: 6px;
	margin-left: 4px;
}
.warning-rule-info {
	display: flex;
	align-items: center;
}

.level-red {
	background: #f24724;
}

.level-yellow {
	background: #ff9d0a;
}

.level-green {
	background: #35c376;
}

.rule-value {
	font-family: "JetBrains Mono", "SF Mono", Monaco, "Cascadia Code", monospace;
	color: #334155;
	background: #f1f5f9;
	padding: 4px 10px;
	border-radius: 16px;
	font-size: 13px;
	letter-spacing: 0.3px;
}

/* 删除按钮 */
.btn-delete {
	color: #dc2626;
	padding: 6px 0;
	font-size: 13px;
}

.btn-delete:hover {
	color: #b91c1c;
	background: none;
}

/* 覆盖Element表格内边距 */
::v-deep .el-table .cell {
	padding-left: 12px;
	padding-right: 12px;
}

::v-deep .el-table td {
	padding: 8px 0;
}

/* 让合并后的单元格内容垂直居中 */
::v-deep .el-table .warning-table td {
	vertical-align: middle;
}
</style>