<!-- 全厂电量统计 -->
<template>
	<div class="app-container">
		<el-header>
			<div class="left-panel">
				<el-date-picker
					v-model="search_date"
					type="year"
					value-format="YYYY"
					format="YYYY"
					placeholder="年"
					style="margin-right: 6px"
					@change="dateChange"
				/>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="dateChange"
				></el-button>
				<el-button
					type="primary"
					@click="exportExcel()"
					:loading="exportLoading"
					>导出xlsx
				</el-button>
				<el-button type="primary" @click="handlePrint">打印 </el-button>
			</div>
		</el-header>
		<el-card style="margin-top: 5px">
			<div class="printWrap">
				<div ref="print" id="myReport" class="printContainer">
					<table
						border="1"
						width="1600"
						cellspacing="0"
						:key="timeStamp"
						id="myTable"
						class="myTable"
					>
						<thead class="myTableHead">
							<tr>
								<th colspan="16">电量消耗月报表</th>
							</tr>
							<tr>
								<th>工段</th>
								<th>设备</th>
								<th>单位</th>
								<th v-for="item in 12" :key="item">
									{{ item }}月
								</th>
								<th>本年合计</th>
							</tr>
						</thead>
						<tr v-for="(item, index) in tableDatas" :key="index">
							<td class="numCell" v-if="index == 0" rowspan="4">
								{{ item[0] }}
							</td>
							<td
								class="numCell"
								v-else-if="index == 4"
								rowspan="5"
							>
								{{ item[0] }}
							</td>
							<td
								class="numCell"
								v-else-if="index == 9"
								rowspan="5"
							>
								{{ item[0] }}
							</td>
							<td
								class="numCell"
								v-else-if="index == 14"
								rowspan="4"
							>
								{{ item[0] }}
							</td>
							<td
								class="numCell"
								v-else-if="index == 18"
								rowspan="3"
							>
								{{ item[0] }}
							</td>
							<td
								class="numCell"
								v-else-if="index == 21"
								rowspan="5"
							>
								{{ item[0] }}
							</td>
							<td
								class="numCell"
								v-else-if="
									index == 26 || index == 27 || index == 28
								"
							>
								{{ item[0] }}
							</td>
							<td class="numCell">{{ item[1] }}</td>
							<td class="numCell">{{ item[2] }}</td>
							<td
								class="numCell"
								v-for="month_item in months"
								:key="month_item"
								@click="itemClick(item, index)"
							>
								{{ item[2 + month_item] }}
							</td>
							<td class="numCell">{{ item[15] }}</td>
						</tr>
					</table>
				</div>
			</div>
		</el-card>
		<el-dialog v-model="itemVisible" :title="tableName">
			<scEcharts
				height="400px"
				width="1033px"
				:option="option"
			></scEcharts>
			<template #footer>
				<slot name="footer"></slot>
			</template>
		</el-dialog>
	</div>
</template>
<script>
import scEcharts from "@/components/scEcharts";
export default {
	components: {
		scEcharts,
	},
	data() {
		return {
			year: "",
			month: "",
			day: "",
			search_date: "",
			tableDatas: [
				["石灰石破碎", "破碎机", "kw.h"], //0
				["石灰石破碎", "堆取料机", "kw.h"],
				["石灰石破碎", "低压变压器柜", "kw.h"],
				["石灰石破碎", "石灰石破碎合计", "kw.h"],
				["原料磨", "循环风机", "kw.h"],
				["原料磨", "辊压机", "kw.h"],
				["原料磨", "调配变压器", "kw.h"],
				["原料磨", "低压变压器柜器", "kw.h"],
				["原料磨", "原料磨系统合计", "kw.h"],
				["回转窑", "尾排风机", "kw.h"],
				["回转窑", "高温风机", "kw.h"],
				["回转窑", "头排风机", "kw.h"],
				["回转窑", "低压变压器柜", "kw.h"],
				["回转窑", "回转窑烧成系统合计", "kw.h"],
				["煤磨", "煤磨主电机", "kw.h"],
				["煤磨", "煤磨排风机", "kw.h"],
				["煤磨", "低压变压器柜", "kw.h"],
				["煤磨", "煤磨系统合计", "kw.h"],
				["余热发电", "余热发电量", "kw.h"],
				["余热发电", "余热发电自用电量", "kw.h"],
				["余热发电", "余热供电量", "kw.h"],
				["空压机", "1#空压机", "kw.h"],
				["空压机", "2#空压机", "kw.h"],
				["空压机", "3#空压机", "kw.h"],
				["空压机", "4#空压机", "kw.h"],
				["空压机", "空压机合计", "kw.h"],
				["富氧燃烧系统", "富氧燃烧耗电设备", "kw.h"],
				["生产总耗电量", "生产部分总耗电", "kw.h"],
				["全厂进线", "全厂耗电量", "kw.h"],
			],
			timeStamp: null,
			itemVisible: false,
			months: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
			tableName: "全厂电量月统计",
			sourceData: {},
			xAxisData: [],
			option: {
				title: {
					text: "全厂电量月统计",
					x: "center",
				},
				grid: {
					top: "20%",
					left: "3%",
					right: "10%",
					bottom: "5%",
					containLabel: true,
				},
				legend: {
					orient: "horizontal",
					x: "right",
					y: "10",
					data: [],
				},
				xAxis: {
					type: "category",
					data: [
						"1月",
						"2月",
						"3月",
						"4月",
						"5月",
						"6月",
						"7月",
						"8月",
						"9月",
						"10月",
						"11月",
						"12月",
					],
					name: "时间",
					nameTextStyle: {
						fontWeight: 600,
						fontSize: 14,
					},
					axisLine: {
						lineStyle: {
							color: "#3366CC",
						},
					},
					axisLabel: {
						rotate: 45,
						interval: 0,
					},
					boundaryGap: false,
				},
				yAxis: {
					type: "value",
					name: "对象值",
					nameTextStyle: {
						fontWeight: 500,
						fontSize: 14,
					},

					axisLine: {
						lineStyle: {
							color: "#3366CC",
						},
					},
				},
				tooltip: {
					show: true,
					trigger: "axis",
					axisPointer: {
						type: "cross",
						axis: "auto",
						snap: true,
					},
					showContent: true,
				},
				series: [
					{
						name: "",
						data: [4, 1, 9, 2, 11, 3, 5, 7, 8, 10, 6, 12],
						type: "line",
						symbolSize: 8,
						symbol: "circle",
						smooth: 0.5,
						itemStyle: {
							normal: {
								label: {
									show: true,
								},
								lineStyle: {
									width: 1,
									type: "dotted",
									color: "#3366CC",
								},
							},
						},
					},
				],
			},
		};
	},
	mounted() {
		let that = this;
		var nowDate = new Date();
		that.year = nowDate.getFullYear();
		that.search_date = that.year + "";
		that.timeStamp = nowDate.getTime();
		let tableDatas = that.tableDatas;
		for (let i = 0; i < tableDatas.length; i++) {
			for (let j = 0; j < 12; j++) {
				let k = 3 + j;
				that.tableDatas[i][k] = 0;
				this.$forceUpdate();
			}
		}
		this.getData();
	},
	methods: {
		dateChange() {
			let that = this;
			for (let i = 0; i < that.tableDatas.length; i++) {
				for (let j = 0; j < 13; j++) {
					let k = 3 + j;
					that.tableDatas[i][k] = 0;
					this.$forceUpdate();
				}
			}
			let nowDate = new Date();
			that.timeStamp = nowDate.getTime();
			this.getData();
		},
		getData() {
			let that = this;
			function dataPush(name) {
				let index = 0;
				if (name == "破碎机") {
					index = 0;
				} else if (name == "堆取料机") {
					index = 1;
				} else if (name == "低压变压器柜") {
					index = 2;
				} else if (name == "循环风机") {
					index = 4;
				} else if (name == "调配变压器") {
					index = 6;
				} else if (name == "低压变压器柜器") {
					index = 7;
				} else if (name == "尾排风机") {
					index = 9;
				} else if (name == "高温风机") {
					index = 10;
				} else if (name == "头排风机") {
					index = 11;
				} else if (name == "煤磨主电机") {
					index = 14;
				} else if (name == "煤磨排风机") {
					index = 15;
				} else if (name == "低压变压器柜") {
					index = 16;
				} else if (name == "余热发电量") {
					index = 18;
				} else if (name == "余热发电自用电量") {
					index = 19;
				} else if (name == "1#空压机") {
					index = 21;
				} else if (name == "2#空压机") {
					index = 22;
				} else if (name == "3#空压机") {
					index = 23;
				} else if (name == "4#空压机") {
					index = 24;
				} else if (name == "富氧燃烧耗电设备") {
					index = 26;
				}
				return index;
			}
			function sumNum(a, b, c, d) {
				let sum = 0;
				if (typeof a !== "undefined" && a !== "NaN") {
					sum = sum + Number(a);
				}
				if (typeof b !== "undefined" && a !== "NaN") {
					sum = sum + Number(b);
				}
				if (typeof c !== "undefined" && a !== "NaN") {
					sum = sum + Number(c);
				}
				if (typeof d !== "undefined" && a !== "NaN") {
					sum = sum + Number(d);
				}
				return sum;
			}
			let obj = {};

			obj.type = "month_s";
			obj.year_s = that.year;
			obj.page = 0;
			obj.mpoint__ep_monitored__power_kw__gte = 100;
			this.$API.enm.mpoint.stat
				.req(obj)
				.then((res) => {
					let wrapArr = [],
						innerArr = [];
					res.forEach((item) => {
						let month = item.month;
						if (wrapArr[month]) {
							wrapArr[month].push(item);
						} else {
							wrapArr[month] = [];
							wrapArr[month].push(item);
						}
					});
					wrapArr.forEach((inner, inde) => {
						inner.forEach((item) => {
							let i = dataPush(item.mpoint_name);
							let k = 0;
							if (
								i == 0 ||
								i == 4 ||
								i == 9 ||
								i == 14 ||
								i == 18 ||
								i == 21 ||
								i == 26 ||
								i == 27 ||
								i == 28
							) {
								k = 3 + inde;
							} else {
								k = 2 + inde;
							}
							that.tableDatas[i][k] = item.val;
						});
					});
					for (let k = 0; k < 24; k++) {
						let m = k + 3;
						let n = k + 1;
						//石灰石破碎合计
						that.tableDatas[3][m] = sumNum(
							that.tableDatas[0][m],
							that.tableDatas[1][m],
							that.tableDatas[2][m]
						);
						//辊压机
						// that.tableDatas[5][m] = sumNum();
						//原料磨系统合计
						that.tableDatas[8][m] =
							sumNum(
								that.tableDatas[4][m],
								// that.tableDatas[5][m],
								that.tableDatas[6][m],
								that.tableDatas[7][m]
							) -
							sumNum(
								that.tableDatas[1][m],
								that.tableDatas[2][m]
							);
						//低压变压器柜
						// that.tableDatas[12][m] = sumNum(
						// 	that.tableDatas[9][m],
						// 	that.tableDatas[10][m],
						// 	that.tableDatas[11][m]
						// );
						//回转窑烧成系统合计
						that.tableDatas[13][m] =
							typeof that.tableDatas[12][m] !== "undefined"
								? sumNum(
										that.tableDatas[9][m],
										that.tableDatas[10][m],
										that.tableDatas[11][m],
										that.tableDatas[12][m]
								  )
								: sumNum(
										that.tableDatas[9][m],
										that.tableDatas[10][m],
										that.tableDatas[11][m]
								  );
						//煤磨系统合计
						let sum17 = sumNum(
							that.tableDatas[14][m],
							that.tableDatas[15][m],
							that.tableDatas[16][m]
						);
						that.tableDatas[17][m] =
							typeof that.tableDatas[21][m] !== "undefined"
								? sum17 - that.tableDatas[21][m]
								: sum17;
						//余热供电量
						that.tableDatas[20][m] =
							typeof that.tableDatas[19][m] !== "undefined"
								? that.tableDatas[18][m] -
								  that.tableDatas[19][m]
								: that.tableDatas[18][m];
						//空压机合计
						that.tableDatas[25][m] = sumNum(
							that.tableDatas[21][m],
							that.tableDatas[22][m],
							that.tableDatas[23][m],
							that.tableDatas[24][m]
						);
						//生产部分总耗电
						that.tableDatas[27][m] = sumNum(
							that.tableDatas[3][m],
							that.tableDatas[8][m],
							that.tableDatas[13][m],
							that.tableDatas[17][m],
							that.tableDatas[18][m],
							that.tableDatas[25][m],
							that.tableDatas[26][m]
						);
						//全厂耗电量
						// that.tableDatas[28][m] = sumNum(
						// 	that.tableDatas[27][m],
						// 	that.tableDatas[26][m]
						// );
					}
					that.tableDatas.forEach((inner, inde) => {
						let sum15 = 0;
						inner.forEach((num, ind) => {
							if (ind > 2 && ind < 15) {
								sum15 = sum15 + Number(num);
							}
						});
						inner[15] = sum15;
					});
				})
				.then(() => {});
		},
		itemClick(item, index) {
			let that = this;
			let lineData = that.tableDatas[index].slice(3, that.headerLength);
			that.option.xAxis.data = that.xAxisData;
			that.option.series[0].data = lineData;
			this.itemVisible = true;
		},
		exportExcel() {
			this.exportLoading = true;
			this.$XLSX("#myTable", this.tableName);
			this.exportLoading = false;
		},
		handlePrint() {
			this.$PRINT("#myReport");
		},
	},
};
</script>
<style scoped>
.printWrap {
	width: 100%;
	overflow-x: scroll;
}
</style>
