<template>
	<el-container>
		<el-main>
			<el-container>
				<el-header style="height: 40%">
					<el-container>
						<el-header>
							<h2>棒数据</h2>
							<div>
								<el-date-picker
									v-model="query.date"
									type="date"
									placeholder="查询日期"
									value-format="YYYY-MM-DD"
									style="width: 150px; margin-right: 10px"
								>
								</el-date-picker>
								<el-button
									type="primary"
									icon="el-icon-search"
									@click="handleQuery"
								></el-button>
							</div>
						</el-header>
						<el-main id="bangTable">
							<el-table
								id="hourBaseTable"
								:data="tableData11"
								style="width: 1200px"
								:height="bangHeight"
								border
							>
								<el-table-column label="版型" prop="版型">
								</el-table-column>
								<el-table-column label="7号">
									<el-table-column
										label="合格数"
										prop="合格数"
									>
									</el-table-column>
								</el-table-column>
								<el-table-column label="8号">
									<el-table-column
										label="仓库库存"
										prop="仓库库存"
									>
									</el-table-column>
								</el-table-column>
								<el-table-column label="6号">
									<el-table-column label="库存" prop="库存">
									</el-table-column>
									<el-table-column label="平头" prop="平头">
									</el-table-column>
									<el-table-column label="粘头" prop="粘头">
									</el-table-column>
									<el-table-column
										label="粗中细"
										prop="粗中细"
									>
									</el-table-column>
									<el-table-column label="抛光" prop="抛光">
									</el-table-column>
									<el-table-column label="开槽" prop="开槽">
									</el-table-column>
									<el-table-column label="配管" prop="配管">
									</el-table-column>
								</el-table-column>
								<el-table-column label="成品库" prop="成品库">
								</el-table-column>
								<!-- <el-table-column label="中检合格数" prop="中检合格数">
								</el-table-column> -->
								<el-table-column label="合计" prop="合计">
								</el-table-column>
								<el-table-column label="发货合格数" prop="检验合格数">
								</el-table-column>
							</el-table>
						</el-main>
					</el-container>
				</el-header>
				<el-main style="padding: 8px 0 0 0">
					<el-container>
						<el-header> <h2>管数据</h2> </el-header>
						<el-main id="guanTable" style="background: #ffffff;padding-left:23px;">
							<el-table
								:data="tableData22"
								style="width: 1200px"
								border
								id="hourBaseTable"
								:height="guanHeight"
							>
								<el-table-column label="版型" prop="版型">
								</el-table-column>
								<el-table-column label="规格" prop="规格">
								</el-table-column>
								<el-table-column label="10号">
									<el-table-column
										label="合格数"
										prop="合格数"
									>
									</el-table-column>
								</el-table-column>
								<el-table-column label="8号">
									<!-- 仓库库存加车间未退火数 -->
									<el-table-column
										label="库存"
										prop="仓库库存"
									>
									</el-table-column>
									<el-table-column label="退火" prop="退火">
									</el-table-column>
								</el-table-column>
								<el-table-column label="6号">
									<!-- 6车间 管成型退火 -->
									<el-table-column label="库存" prop="库存">
									</el-table-column>
									<el-table-column label="开槽" prop="开槽">
									</el-table-column>
									<el-table-column label="倒角" prop="倒角">
									</el-table-column>
									<el-table-column label="配棒" prop="配棒">
									</el-table-column>
								</el-table-column>
								<el-table-column label="成品库" prop="成品库">
								</el-table-column>
								<!-- <el-table-column label="中检合格数" prop="中检合格数">
								</el-table-column> -->
								<el-table-column label="合计" prop="合计">
								</el-table-column>
								<el-table-column label="发货合格数" prop="检验合格数">
								</el-table-column>
							</el-table>
						</el-main>
					</el-container>
				</el-main>
			</el-container>
		</el-main>
	</el-container>
</template>
<script>
// import mockData from "./mock.json";
export default {
	name: "total_statistics",
	data() {
		return {
			// mockData: mockData,
			tableData11: [],
			tableData22: [],
			bangHeight: 300,
			guanHeight: 500,
			query: {
				date: "",
			},
			currentDate: "",
			models_b:[],
			models_g:[],
		};
	},
	mounted() {
		let date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
		this.year = year;
		this.month = month;
		month = month < 10 ? "0" + month : month;
		day = day < 10 ? "0" + day : day;
		this.day = day;
		this.query.date = this.currentDate = `${year}-${month}-${day}`;
		this.getSourceDataNow();
		this.bangHeight = document.getElementById("bangTable").offsetHeight - 20;
		this.guanHeight = document.getElementById("guanTable").offsetHeight - 20;
	},
	created() {},
	methods: {
		getCountOk(type,models) {
			let that = this;
			let params = {
				query: {
					start_date: that.query.date,
					end_date: that.query.date,
					fw_type: "process",
				},
			};
			that.$API.bi.dataset.exec.req("count_ok_day", params).then((res) => {
				let datas = res.data2.ds0;
				if(datas.length>0){
					if(type=="b"){
						datas.forEach((item) => {
							let b_model = "";
							if (item.型号.indexOf("+") > -1) {
								b_model = item.型号.split("+")[0];
							} else {
								b_model = item.型号;
							}
							let index = models.indexOf(b_model);
							if(index>-1){
								that.tableData11[index].中检合格数 += item.合格数量;
							}
						});
						that.saleDatas.forEach((item1) => {
							let b_model = "";
							if (item1.型号.indexOf("+") > -1) {
								b_model = item1.型号.split("+")[0];
							} else {
								b_model = item1.型号;
							}
							let index = models.indexOf(b_model);
							if(index>-1){
								that.tableData11[index].检验合格数 += item1.数量;
							}
						})
					}else if(type=="g"){
						datas.forEach((item) => {
							let g_model = "",g_spec = "";
							if (item.型号.indexOf("+") > -1) {
								g_model = item.型号.split("+")[1];
							} else {
								g_model = item.型号;
							}
							if (item.规格.indexOf("+") > -1) {
								g_spec = item.规格.split("+")[1];
							} else {
								g_spec = item.规格;
							}
							let msitem = g_model+g_spec;
							let index = models.indexOf(msitem);
							if(index>-1){
								that.tableData22[index].中检合格数 += item.合格数量;
							}
						});
						that.saleDatas.forEach((item1) => {
						    let g_model = "",g_spec = "";
							if (item1.型号.indexOf("+") > -1) {
								g_model = item1.型号.split("+")[1];
							} else {
								g_model = item1.型号;
							}
							if (item1.规格.indexOf("+") > -1) {
								g_spec = item1.规格.split("+")[1];
							} else {
								g_spec = item1.规格;
							}
							let msitem = g_model+g_spec;
							let index = models.indexOf(msitem);
							if(index>-1){
								that.tableData22[index].检验合格数 += item1.数量;
							}
						})
					}
				}
			});
		},
		getSourceDataNow(){
			let that = this;
			that.$API.bi.dataset.exec.req("gz_zh").then((res) => {
				let datas = res.data2;
				let data_b = [...datas.ds0, ...datas.ds2];
				let data_g = [...datas.ds1, ...datas.ds3];
				that.data_b = data_b;
				that.data_g = data_g;
				that.getSaleOutData(data_b, data_g);
			});
		},
		//获取表格原始数据
		getSourceData() {
			let that = this;
			let obj = {};
			obj.timex__year = that.year;
			obj.timex__month = that.month;
			obj.timex__day = that.day;
			obj.page = 0;
			that.$API.bi.dataset.record.req(obj).then((res) => {
				let datas = res[0].result.data2;
				let data_b = [...datas.ds0, ...datas.ds2];
				let data_g = [...datas.ds1, ...datas.ds3];
				that.data_b = data_b;
				that.data_g = data_g;
				that.getSaleOutData(data_b, data_g);
			});
		},
		//棒数据
		getDataB(data,saleData) {
			let that = this;
			let models = [],dataArr = [];
			//按版型（型号）分组
			data.forEach((item) => {
				let index = 0;
				if (item.型号.indexOf("+") > -1) {
					let model = item.型号.split("+")[0];
					index = models.indexOf(model);
					item.型号 = model;
				} else {
					index = models.indexOf(item.型号);
				}
				if (index > -1) {
					dataArr[index].push(item);
				} else {
					models.push(item.型号);
					let arr = [];
					arr.push(item);
					dataArr.push(arr);
				}
			});
			//将数组组成table要用的格式
			dataArr.forEach((itemArr, index) => {
				//itemArr 是型号相通数据的一个对象数组
				let obj = {
					版型: "",
					合格数: 0,
					仓库库存: 0,
					库存: 0,
					平头: 0,
					粘头: 0,
					粗中细: 0,
					抛光: 0,
					开槽: 0,
					配管: 0,
					成品库:0,
					合计: 0,
					中检合格数: 0,
					检验合格数: 0,
				};
				itemArr.forEach((item1) => {
					obj.版型 = item1.型号;
					let key = "";
					if (item1.车间 == "7车间") {
						key = "合格数";
					} else if (item1.车间 == "6车间" &&item1.工序 == "棒料成型") {
						key = "库存";
					} else if (item1.车间 == "6车间" && item1.工序 == "平头") {
						key = "平头";
					} else if (item1.车间 == "6车间" && item1.工序 == "粘头") {
						key = "粘头";
					} else if (item1.车间 == "6车间" &&(item1.工序 == "粗磨"||item1.工序 == "粗中磨"|| item1.工序 == "粗中细磨")) {
						key = "粗中细";
					} else if (item1.车间 == "6车间" && item1.工序 == "抛光") {
						key = "抛光";
					} else if (item1.车间 == "6车间" && item1.工序 == "开槽") {
						key = "开槽";
					}
					if (item1.车间==null||item1.车间==undefined) {
						if (item1.工序 == "配管") {
							key = "配管";
						}else if(item1.工序 !== "配管"&&item1.类型==10){
							key = "成品库";
						}else if(item1.工序 !== "配管"&&item1.类型==20){
							key = "仓库库存";
						}
					} 
					obj[key] += item1.数量;
				});
				obj.合计 =
					obj.合格数 +
					obj.仓库库存 +
					obj.库存 +
					obj.平头 +
					obj.粘头 +
					obj.粗中细 +
					obj.抛光 +
					obj.开槽 +
					obj.配管+
					obj.成品库;
				that.tableData11[index] = obj;
			});
			that.models_b = models;
			that.getCountOk('b',models);
		},
		//管数据
		getDataG(data,saleData) {
			let that = this;
			let models = [],
				modelArr = [];
			//modelArr按型号分组
			data.forEach((item) => {
				let index = 0;
				if (item.型号.indexOf("+") > -1) {
					let model = item.型号.split("+")[1];
					index = models.indexOf(model);
					item.型号 = model;
				} else {
					index = models.indexOf(item.型号);
				}

				if (index > -1) {
					modelArr[index].push(item);
				} else {
					models.push(item.型号);
					let arr = [];
					arr.push(item);
					modelArr.push(arr);
				}
			});
			//specArr按规格的分组
			let specArr = [];
			modelArr.forEach((itemArr, index0) => {
				specArr[index0] = [];
				let specifica = []; //规格数组
				let ind = 0;
				//itemArr是同一规格的对象数组[{},{}]
				itemArr.forEach((item1, index1) => {
					//item1==>对象{}
					if (item1.规格.indexOf("+") > -1) {
						let spec = item1.规格.split("+")[1];
						ind = specifica.indexOf(spec);
					} else {
						ind = specifica.indexOf(item1.规格);
					}
					if (ind > -1) {
						specArr[index0][ind].push(item1);
					} else {
						specifica.push(item1.规格);
						let arr = [];
						arr.push(item1);
						specArr[index0].push(arr);
					}
				});
			});
			//specArr已将同型号的数组中按规格分组
			specArr.forEach((item3, index3) => {
				let modelsspec = [];
				item3.forEach((item4) => {
					let obj = {
						版型: "",
						规格: "",
						合格数: 0,
						仓库库存: 0,
						退火: 0,
						库存: 0,
						开槽: 0,
						倒角: 0,
						配棒: 0,
						成品库:0,
						合计: 0,
						中检合格数: 0,
						检验合格数: 0,
					};
					item4.forEach((item5) => {
						let key = "";
						obj.版型 = item5.型号;
						if (item5.规格.indexOf("+") > -1) {
							let spec = item5.规格.split("+")[1];
							obj.规格 = spec;
						}else{
							obj.规格 = item5.规格;
						}
						if (item5.车间 == "10车间") {
							key = "合格数";
						}else if (item5.车间 == "8车间" &&item5.工序 == "管料成型") {
							key = "仓库库存";
						} else if (item5.车间 == "8车间" &&item5.工序 == "管料退火") {
							key = "退火";
						} else if (item5.车间 == "6车间" &&item5.工序 == "管料退火") {
							key = "库存";
						} else if (item5.车间 == "6车间" &&item5.工序 == "开槽") {
							key = "开槽";
						} else if (item5.车间 == "6车间" &&item5.工序 == "倒角") {
							key = "倒角";
						}
						if (item5.车间==null||item5.车间==undefined) {
							if (item5.工序 == "配管") {
								key = "配棒";
							}else if(item5.工序 !== "配管"&&item5.类型==10){
								key = "成品库";
							}else if(item5.工序 !== "配管"&&item5.类型==20){
								key = "仓库库存";
							}
						} 
						obj[key] = obj[key] + item5.数量;
					});
					obj.合计 =
						obj.合格数 +
						obj.仓库库存 +
						obj.退火 +
						obj.库存 +
						obj.开槽 +
						obj.倒角 +
						obj.配棒+
						obj.成品库;
					that.tableData22.push(obj);
					let modelsspecitem = obj.版型+obj.规格;
					modelsspec.push(modelsspecitem);
				});
			});
			saleData.forEach((item) => {
				let index = 0;
				if (item.型号.indexOf("+") > -1) {
					let model = item.型号.split("+")[1];
					index = models.indexOf(model);
				} else {
					index = models.indexOf(item.型号);
				}
				if (index > -1) {
					that.tableData22[index].检验合格数=item.数量;
				}
			})
			that.models_g = models;
			that.getCountOk('g',models);
		},
		getSaleOutData(data_b, data_g){
			let that = this;
			let obj = {
				query: {
					start_date: that.query.date,
					end_date: that.query.date,
					mio_type: "sale_out",
					material_id: "",
				},
			};
			that.$API.bi.dataset.exec.req("saleOutDay", obj).then((res) => {
				let saleData = that.saleDatas = res.data2.ds0;
				that.getDataB(data_b,saleData);
				that.getDataG(data_g,saleData);
			})
		},
		handleQuery() {
			let that = this;
			if (that.query.date !== null && that.query.date !== "") {
				if (that.query.date !== "") {
					that.year = that.query.date.split("-")[0];
					that.month = that.query.date.split("-")[1];
					that.day = that.query.date.split("-")[2];
				} else {
					that.year = new Date().getFullYear();
					that.month = new Date().getMonth() + 1;
					that.day = new Date().getDate();
					that.query.date = that.currentDate;
				}
			}
			if(that.query.date == that.currentDate){
				that.getSourceDataNow();
			}else{
				that.getSourceData();
			}
			
		},
	},
};
</script>

<style scoped></style>
