<template>
	<div class="app-container">
		<el-header>
			<div class="left-panel">
				<el-date-picker
					v-model="query.year_s"
					type="year"
					value-format="YYYY"
					format="YYYY"
					placeholder="查询年份"
					class="headerSearch"
				/>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></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="1900"
						cellspacing="0"
						:key="timeStamp"
						id="myTable"
						class="myTable"
					>
						<thead class="myTableHead">
							<tr>
								<th colspan="19">水泥磨工段班组月度对比分析</th>
							</tr>
							<tr>
								<th rowspan="2">月份</th>
								<th rowspan="2">班组</th>
								<th colspan="2">总产量（t）</th>
								<th>运转率</th>
								<th>成本</th>
								<th colspan="3">质量</th>
								<th colspan="9">单位产品电耗</th>
								<th rowspan="2">得分</th>
							</tr>
							<tr>
								<th>总产量（t）</th>
								<th>台时产量（t/h）</th>
								<th>运转率（%）</th>
								<th>单位产品成本（元/吨）</th>
								<th>比表面积（%）</th>
								<th>SO3（%）</th>
								<th>掺量（%）</th>
								<th>循环风机1906（KW·h/t）</th>
								<th>系统风机（KW·h/t）</th>
								<th>目标值（kW·h/t）</th>
								<th>当期值（kW·h/t）</th>
								<th>当期与目标值差值（kW·h/t）</th>
								<th>环期值（kW·h/t）</th>
								<th>当期与环期差值（kW·h/t）</th>
								<th>环比增长率（%）</th>
								<th>同比增长率（%）</th>
							</tr>
						</thead>
						<tr v-for="(item, index) in tableDatas" :key="index">
							<td
								v-for="(item0, index0) in item"
								:key="index0"
								class="numCell"
							>
								<span v-if="item0">{{ item0 }}</span>
								<span v-else>0</span>
							</td>
						</tr>
					</table>
				</div>
			</div>
		</el-card>
	</div>
</template>
<script>
export default {
	data() {
		return {
			timeStamp: 0,
			query: {
				year_s: "",
				page: 0,
				type: "month_st",
				mgroup: "3347217651339837440",
			},
			tableDatas: [],
			exportLoading: false,
		};
	},
	mounted() {
		let that = this;
		var myDate = new Date();
		let year = myDate.getFullYear();
		this.timeStamp = myDate.getTime();
		that.query.year_s = year;
		this.$API.mtm.mgroup.list
			.req({ page: 0, search: "水泥磨" })
			.then((res) => {
				that.query.mgroup = res[0].id;
				let params = {};
				params.page = 0;
				params.year = year;
				params.mgroup = that.query.mgroup;
				this.$API.mtm.goal.list.req(params).then((res) => {
				that.getData(res);
			});
			});
	},
	methods: {
		getGoalData() {
			const self = this;
			let params = {};
			params.page = 0;
			params.year = self.query.year_s;
			params.mgroup = self.query.mgroup;
			this.$API.mtm.goal.list.req(params).then((res) => {
				if (res.length > 0) {
						res.forEach((item) => {
						    if (item.goal_cate_name == "单位产品分布电耗（KW·h/t）") {
						        this.getData(item);
						    }
						})
					}
			})
			.catch((error) => {
            console.error('获取目标数据失败：', error);
        });
		},
		getData(goalData) {
			let that = this;
			let query0 = {};
			query0.page = 0;
			query0.type = "month_st";
			query0.year_s = that.query.year_s - 1;
			query0.mgroup = that.query.mgroup;
			let wrapArr = [],
				wrapArr0 = [],
				compareArr0 = [],
				month_obj = {};
			this.$API.enm.enstat.req(query0).then((res0) => {
				let data0 = []; //去年的值
				data0 = res0;
				if (data0.length > 0) {
					data0.forEach((item0) => {
						//先按月份排序，再按班组排序
						let ind0 = 0;
						wrapArr0[ind0] = item0;
					});
				} else {
				}
				this.$API.enm.enstat.req(that.query).then((res) => {
					//今年的值
					let data = res;
					if (data.length > 0) {
						data.forEach((item) => {
							//先按月份排序，再按班组排序
							let compareArr = [];
							compareArr.push(item.month_s);
							compareArr.push(item.team_name);
							compareArr.push(item.elec_consume_unit);
							compareArr0.push(compareArr);
							if (!month_obj[item.month_s]) {
								month_obj[item.month_s] = [];
							};
							month_obj[item.month_s].push(
								{
								team:item.team_name, 
								production_hour: item.production_hour,
								run_rate:item.run_rate,
								production_cost_unit:item.production_cost_unit,
								surface_area :item.出磨水泥_比表面积_rate_pass,
								SO3:item.出磨水泥_SO3_rate_pass,
								chanliang:item.出磨水泥_掺量_rate_pass,
								elec:item.elec_consume_unit,
								}
							);
						});
						data.forEach((item) => {
							//先按月份排序，再按班组排序
							let n = item.month_s;
							let ind = 0;
							let arr = [];
							let time = "" + item.year_s + "." + item.month_s;
							arr.push(time);
							arr.push(item.team_name);
							arr.push(item.total_production);
							arr.push(item.production_hour);
							arr.push(item.run_rate);
							arr.push(item.production_cost_unit);
							//质量
							arr[6] =
								item.出磨水泥_比表面积_rate_pass != null
									? item.出磨水泥_比表面积_rate_pass
									: "/";
							arr[7] =
								item.出磨水泥_SO3_rate_pass != null
									? item.出磨水泥_SO3_rate_pass
									: "/";
							arr[8] =
								item.出磨水泥_掺量_rate_pass != null
									? item.出磨水泥_掺量_rate_pass
									: "/";
							//设备
							arr[9] =
								item.循环风机_consume_unit != null
									? item.循环风机_consume_unit
									: "/";
							arr[10] =
								item.系统风机_consume_unit != null
									? item.系统风机_consume_unit
									: "/";
							let keyVale = "goal_val_" + n;
							if (goalData.length > 0){
								goalData.forEach((item) => {
								if (item.goal_cate_name == "单位产品分布电耗（KW·h/t）"){
									arr[11] = item[keyVale]; //目标值（KW·h/t）//需要接口获取
								}
							});
						};	
							arr[12] = item.elec_consume_unit; //当期值（KW·h/t）
							arr[13] = (item.elec_consume_unit - arr[11]).toFixed(2); //当期与目标差值（KW·h/t）
							let ind_pre = 0,
								huanqi = 0,
								huanqicha = 0,
								tongqi = 0,
								tongqicha = 0;
							if (n == 1) {
								ind_pre = 12;
								huanqi = wrapArr0[ind_pre]
									? wrapArr0[ind_pre].elec_consume_unit
									: "/";
							} else {
								// 如果 wrapArr数组长度大于0，那么循环遍历wrapArr数组，找到与item.month_s相差1的元素 而 item.name相同，并赋值给huanbi
								compareArr0.forEach((item0) => {
									if (item0[0] == item.month_s - 1 && item0[1] == item.team_name) {
										huanqi = item0[2];
								}
							})
							}
							arr[14] = huanqi; //环期值（KW·h/t）上个月的值
							if (huanqi !== "/") {
								huanqicha = (item.elec_consume_unit - huanqi).toFixed(2);
							} else {
								huanqicha = item.elec_consume_unit;
							}
							arr[15] = huanqicha; //当期与环期差值（KW·h/t）
							arr[16] =
								huanqi != "/"  && huanqicha!=0 && huanqi!=0 ? ((huanqicha / huanqi) * 100).toFixed(2) :"/" ; //环比增长率（%）
							if (wrapArr0[n]) {
								tongqicha =
									item.celec_consume_unit -
									wrapArr0[n].celec_consume_unit;
								tongqi = tongqicha / tongqi;
							} else {
								tongqicha = item.celec_consume_unit;
								tongqi = 0;
							}

							arr[17] = tongqi; //同比增长率（%）:本月-去年本月/去年
							// arr[18] = 0.0; //同比增长率（%）
							arr[18] = 0;
							if (month_obj[n]) {
							    const monthData = month_obj[n];
								const itemValue = item.elec_consume_unit; // 单位产品电耗的值
								const sortedData = monthData.sort((a, b) => parseFloat(b.elec) - parseFloat(a.elec));
								const index = sortedData.findIndex(obj => parseFloat(obj.elec) === parseFloat(itemValue));	
								let elec_score = 0;							
								if (index === 0){
									elec_score= 10;
								} else if (index === 1) {
									elec_score= 7;
								} else if (index === 2) {
									elec_score= 3;
								} else if (index === 3) {
									elec_score= 0;
								}
								arr[18] += elec_score;

								const run_rate_itemValue = item.run_rate; // 运转率的值
								const run_rate_sortedData = monthData.sort((a, b) => parseFloat(b.run_rate) - parseFloat(a.run_rate));
								const run_rate_index = run_rate_sortedData.findIndex(obj => parseFloat(obj.run_rate) === parseFloat(run_rate_itemValue));
								let run_rate_score = 0;
								if (run_rate_index === 0){
									run_rate_score= 10;
								} else if (run_rate_index === 1) {
									run_rate_score= 7;
								} else if (run_rate_index === 2) {
									run_rate_score= 3;
								} else if (run_rate_index === 3) {
									run_rate_score= 0;
								}
								arr[18] += run_rate_score;

								const production_hour_itemValue = item.production_hour; // 台时产量的值
								const production_hour_sortedData = monthData.sort((a, b) => parseFloat(b.production_hour) - parseFloat(a.production_hour));
								const production_hour_index = production_hour_sortedData.findIndex(obj => parseFloat(obj.production_hour) === parseFloat(production_hour_itemValue));
								let production_hour_score = 0;
								if (production_hour_index === 0){
									production_hour_score= 20;
								} else if (production_hour_index === 1) {
									production_hour_score= 15;
								} else if (production_hour_index === 2) {
									production_hour_score= 5;
								} else if (production_hour_index === 3) {
									production_hour_score= 0;
								}
								arr[18] += production_hour_score;

								const production_cost_unit_itemValue = item.production_cost_unit; // 单位产品成本的值
								const production_cost_unit_sortedData = monthData.sort((a, b) => parseFloat(b.production_cost_unit) - parseFloat(a.production_cost_unit));
								const production_cost_unit_index = production_cost_unit_sortedData.findIndex(obj => parseFloat(obj.production_cost_unit) === parseFloat(production_cost_unit_itemValue));
								let production_cost_unit_score = 0;
								if (production_cost_unit_index === 0){
									production_cost_unit_score= 10;
								} else if (production_cost_unit_index === 1) {
									production_cost_unit_score= 7;
								} else if (production_cost_unit_index === 2) {
									production_cost_unit_score= 3;
								} else if (production_cost_unit_index === 3) {
									production_cost_unit_score= 0;
								}
								arr[18] += production_cost_unit_score;

								const surface_area_itemValue = item.出磨水泥_比表面积_rate_pass; // 比表面积的值
								const surface_area_sortedData = monthData.sort((a, b) => parseFloat(b.surface_area) - parseFloat(a.surface_area));
								const surface_area_index = surface_area_sortedData.findIndex(obj => parseFloat(obj.surface_area) === parseFloat(surface_area_itemValue));
								let surface_area_score = 0;
								if (surface_area_index === 0){
									surface_area_score= 20;
								} else if (surface_area_index === 1) {
									surface_area_score= 15;
								} else if (surface_area_index === 2) {
									surface_area_score= 5;
								} else if (surface_area_index === 3) {
									surface_area_score= 0;
								}
								arr[18] += surface_area_score;

								const SO3_itemValue = item.出磨水泥_SO3_rate_pass; // SO3的值
								const SO3_sortedData = monthData.sort((a, b) => parseFloat(b.SO3) - parseFloat(a.SO3));
								const SO3_index = SO3_sortedData.findIndex(obj => parseFloat(obj.SO3) === parseFloat(SO3_itemValue));
								let SO3_score = 0;
								if (SO3_index === 0){
									SO3_score= 20;
								} else if (SO3_index === 1) {
									SO3_score= 15;
								} else if (SO3_index === 2) {
									SO3_score= 5;
								} else if (SO3_index === 3) {
									SO3_score= 0;
								}
								arr[18] += SO3_score;

								const CLitemValue = item.出磨水泥_掺量_rate_pass; // SO3的值
								const CL_sortedData = monthData.sort((a, b) => parseFloat(b.chanliang) - parseFloat(a.chanliang));
								const CL_index = CL_sortedData.findIndex(obj => parseFloat(obj.chanliang) === parseFloat(CLitemValue));
								let CL_score = 0;
								if (CL_index === 0){
									CL_score= 10;
								} else if (CL_index === 1) {
									CL_score= 7;
								} else if (CL_index === 2) {
									CL_score= 3;
								} else if (CL_index === 3) {
									CL_score= 0;
								}
								arr[18] += CL_score;
							}
							wrapArr.push(arr);
						});
						let newArr = [];
						for (let i = 0; i < wrapArr.length; i++) {
							let item = wrapArr[i];
							if (item == undefined) {
							} else {
								newArr.push(wrapArr[i]);
							}
						}
						that.tableDatas = newArr;
					} else {
					}
				});
			});
		},
		handleQuery() {
			this.tableDatas = [];
			this.getGoalData();
		},
		itemClick(type, item) {
			this.type = type;
			this.asynDialog = true;
		},
		itemClick1(type, item) {
			this.chartShow = false;
			this.$API.bi.dataset.exec.req("3322567213885833216").then((res) => {
				this.myOption = JSON.parse(res.echart_options);
				// debugger;
				this.chartShow = true;
			});
		},
		handlePrint() {
			this.$PRINT("#myReport");
		},
		exportExcel() {
			this.exportLoading = true;
			this.$XLSX("#myTable", this.tableName);
			this.exportLoading = false;
		},
	},
};
</script>
<style scoped>
.printWrap {
	width: 100%;
	overflow-x: scroll;
}
</style>
