<template>
    <div class="app-container">
        <el-header>
            <div class="left-panel">
                <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 ref="print" id="myReport" class="printContainer">
                <table border="1"  cellspacing="0" :key="timeStamp" id="numTable" class="myTable">
                    <thead class="myTableHead">
                        <tr>
                            <th colspan="7">全厂电量统计</th>
                        </tr>
                        <tr>
                            <th>车间</th>
                            <th>工段</th>
                            <th>设备</th>
                            <th>上一个小时（kW·h）</th>
                            <th>昨日电量累计（kW·h）</th>
                            <th>本月电量累计（kW·h）</th>
                            <th>本年电量累计（kW·h）</th>
                        </tr>
                    </thead>
                    <tr v-for="(item,index) in tableData" :key="index">
                        <td class="numCell" v-if="index==0" rowspan="9">{{item.dept}}</td>
                        <td class="numCell" v-else-if="index==9" rowspan="18">{{item.dept}}</td>
                        <td class="numCell" v-else-if="index==27" rowspan="12">{{item.dept}}</td>
                        <td class="numCell" v-else-if="index==39" rowspan="10" colspan="2">{{item.dept}}</td>
                        <td class="numCell" v-else-if="index==49" colspan="3">{{item.dept}}</td>
                        
                        <td class="numCell" v-if="index==8||index==26||index==37||index==38" colspan="2">{{item.mgroup}}</td>
                        <td class="numCell" v-else-if="index==0" rowspan="2">{{item.mgroup}}</td>
                        <td class="numCell" v-else-if="index==2" rowspan="6">{{item.mgroup}}</td>
                        <td class="numCell" v-else-if="index==9" rowspan="14">{{item.mgroup}}</td>
                        <td class="numCell" v-else-if="index==23" rowspan="3">{{item.mgroup}}</td>
                        <td class="numCell" v-else-if="index==27" rowspan="10">{{item.mgroup}}</td>
                        
                        <td class="numCell" v-if="item.mpointName!==''">{{item.mpointName}}</td>
                        <td class="numCell" @click="chartShow('hour_s',item)">{{item.hour_data}}</td>
                        <td class="numCell" @click="chartShow('day_s',item)">{{item.day_data}}</td>
                        <td class="numCell" @click="chartShow('month_s',item)">{{item.month_data}}</td>
                        <td class="numCell">{{item.year_data}}</td>
                    </tr>
                </table>
            </div>
            <charts 
                v-if="asynDialog"
                :cate="cate"
                :type="type" 
                :title="title"
                :apiObj = "apiObj"
                :mpoint="mpoint"
                :modelValue="modelValue"
                :showClose="showClose"
                :echartType='echartType'
                @closed="asynDialog = false"
            ></charts>
        </el-card>
    </div>
  </template>
<script>
import { defineAsyncComponent } from 'vue';
    export default {
        components: {
            charts: defineAsyncComponent(() => import("@/components/scEnm/lineChartsdialog.vue")),
        },
        data() {
            return { 
                query:{
                    year:'',
                    month:'',
                    days:'',
                },
                cate:'设备名',
                type:'hour_s',//时间类型：年、月、日、小时
                showClose:true,
                modelValue:true,
                echartType:'line',
                apiObj:this.$API.enm.mpoint.stat,
                title:'全场电量小时统计',
                asynDialog:false,//趋势图显示
                tableData:[
                    {dept:'原料车间',mgroup:'电石渣',mpointName:'烘干破主电机',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},//0
                    {dept:'原料车间',mgroup:'电石渣',mpointName:'电石渣合计',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},
                    {dept:'原料车间',mgroup:'原料磨',mpointName:'反击式破碎机',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},
                    {dept:'原料车间',mgroup:'原料磨',mpointName:'立磨主电机',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},
                    {dept:'原料车间',mgroup:'原料磨',mpointName:'循环风机',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},
                    {dept:'原料车间',mgroup:'原料磨',mpointName:'废气风机',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},
                    {dept:'原料车间',mgroup:'原料磨',mpointName:'尾排风机',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},
                    {dept:'原料车间',mgroup:'原料磨',mpointName:'原料磨合计',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},
                    {dept:'原料车间',mgroup:'原料车间合计',mpointName:'',hour_data:0,day_data:0,month_data:0,year_data:0,mpoint:null},//8

                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'高温风机',hour_data:0,day_data:0,month_data:0,year_data:0},//9
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'窑主电机',hour_data:0,day_data:0,month_data:0,year_data:0},//10
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'篦冷机一室风机电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'篦冷机三室风机电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'篦冷机二室风机电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'篦冷机一室充气梁风机（左）',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'篦冷机一室充气梁风机（右）',hour_data:0,day_data:0,month_data:0,year_data:0},//15
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'窑头排风机主电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'头煤粉罗茨电机（东、备用）',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'尾煤粉罗茨电机（西）',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'空压机3301',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'空压机3308',hour_data:0,day_data:0,month_data:0,year_data:0},//20
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'空压机3318',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'回转窑',mpointName:'回转窑合计',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'煤磨',mpointName:'煤磨主电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'煤磨',mpointName:'煤磨排风机主电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'煤磨',mpointName:'煤磨合计',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'烧成车间',mgroup:'烧成车间合计',mpointName:'',hour_data:0,day_data:0,month_data:0,year_data:0},//26

                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'循环风机1906',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'系统风机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'水平涡流选粉机1915',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'水泥磨主电机',hour_data:0,day_data:0,month_data:0,year_data:0},//30
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'辊压机动辊电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'辊压机定辊电机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'新增磨尾风机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'水泥1#空压机',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥磨',mpointName:'水泥2#空压机',hour_data:0,day_data:0,month_data:0,year_data:0},//35
                    {dept:'水泥车间',mgroup:'水泥磨合',mpointName:'水泥磨合计计',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥包装合计',mpointName:'',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'水泥车间',mgroup:'水泥车间合计',mpointName:'',hour_data:0,day_data:0,month_data:0,year_data:0},
                    
                    {dept:'生活区',mgroup:'食堂',mpointName:'食堂',hour_data:0,day_data:0,month_data:0,year_data:0},//39
                    {dept:'生活区',mgroup:'单元楼',mpointName:'单元楼',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'生活区',mgroup:'单身宿舍',mpointName:'单身宿舍',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'生活区',mgroup:'办公楼',mpointName:'办公楼',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'生活区',mgroup:'余热锅炉',mpointName:'余热锅炉',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'生活区',mgroup:'污水站',mpointName:'污水站',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'生活区',mgroup:'南门办公区',mpointName:'南门办公区',hour_data:0,day_data:0,month_data:0,year_data:0},//45
                    {dept:'生活区',mgroup:'东门办公区',mpointName:'东门办公区',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'生活区',mgroup:'地磅',mpointName:'地磅',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'生活区',mgroup:'生活区合计',mpointName:'生活区合计',hour_data:0,day_data:0,month_data:0,year_data:0},
                    {dept:'全厂电量',mgroup:'',mpointName:'',hour_data:0,day_data:0,month_data:0,year_data:0},
                ],
                tableDatas:[
                    ['原料车间','电石渣','烘干破主电机',0,0,0,0 ],//0
                    ['原料车间','电石渣合计',0,0,0,0 ],
                    ['原料车间','原料磨','反击式破碎机',0,0,0,0],//2
                    ['原料车间','立磨主电机',0,0,0,0],
                    ['原料车间','循环风机',0,0,0,0],
                    ['原料车间','废气风机' ,0,0,0,0],//5
                    ['原料车间','尾排风机',0,0,0,0],
                    ['原料车间','原料磨合计' ,0,0,0,0],
                    ['原料车间','原料车间合计',0,0,0,0],//8

                    ['烧成车间','回转窑','高温风机',0,0,0,0],//9
                    ['烧成车间','窑主电机',0,0,0,0],//10
                    ['烧成车间','篦冷机一室风机电机',0,0,0,0],
                    ['烧成车间','篦冷机三室风机电机',0,0,0,0],
                    ['烧成车间','篦冷机二室风机电机',0,0,0,0],
                    ['烧成车间','篦冷机一室充气梁风机（左）',0,0,0,0],
                    ['烧成车间','篦冷机一室充气梁风机（右）',0,0,0,0],//15
                    ['烧成车间','窑头排风机主电机',0,0,0,0],
                    ['烧成车间','头煤粉罗茨电机（东、备用）',0,0,0,0],
                    ['烧成车间','尾煤粉罗茨电机（西）',0,0,0,0],
                    ['烧成车间','空压机3301',0,0,0,0],
                    ['烧成车间','空压机3308',0,0,0,0],//20
                    ['烧成车间','空压机3318',0,0,0,0],//21
                    ['烧成车间','回转窑合计',0,0,0,0],
                    ['烧成车间','煤磨','煤磨主电机',0,0,0,0],
                    ['烧成车间','煤磨排风机主电机',0,0,0,0],
                    ['烧成车间','煤磨合计',0,0,0,0],//25
                    ['烧成车间','烧成车间合计',0,0,0,0],//26

                    ['水泥车间','水泥磨','循环风机1906',0,0,0,0],//27
                    ['水泥车间','系统风机',0,0,0,0],
                    ['水泥车间','水平涡流选粉机1915',0,0,0,0],
                    ['水泥车间','水泥磨主电机',0,0,0,0],//30
                    ['水泥车间','辊压机动辊电机',0,0,0,0],
                    ['水泥车间','辊压机定辊电机',0,0,0,0],
                    ['水泥车间','新增磨尾风机',0,0,0,0],
                    ['水泥车间','水泥1#空压机',0,0,0,0],
                    ['水泥车间','水泥2#空压机',0,0,0,0],//35
                    ['水泥车间','水泥磨合计计',0,0,0,0],
                    ['水泥车间','水泥包装合计',0,0,0,0],//37
                    ['水泥车间','水泥车间合计',0,0,0,0],//38

                    ['生活区','食堂',0,0,0,0],//39
                    ['生活区','单元楼',0,0,0,0],//40
                    ['生活区','单身宿舍',0,0,0,0],
                    ['生活区','办公楼',0,0,0,0],
                    ['生活区','余热锅炉',0,0,0,0],
                    ['生活区','污水站',0,0,0,0],
                    ['生活区','南门办公区',0,0,0,0],//45
                    ['生活区','东门办公区',0,0,0,0],
                    ['生活区','地磅',0,0,0,0],
                    ['生活区','生活区合计',0,0,0,0],
                    ['全厂电量',0,0,0,0]
                ],
                sourceData:{}
            };
        },
        mounted() {
            let that = this;
            var nowDate = new Date();
            that.year = nowDate.getFullYear();
            that.month = nowDate.getMonth()+1;
            that.day = nowDate.getDate();
            that.hour = nowDate.getHours();
            that.timeDate = nowDate.getTime();
            this.getData();
        },
        methods: {
            chartShow(type,item){
                debugger;
                this.type=type;
                console.log(item);
                this.cate = item.mpointName
                if(item.mpoint==null){}else{
                    this.mpoint = item.mpoint;
                    if(type=='hour_data'){
                        this.title=item.mpointName+'电量小时统计';
                    }else if(type=='day_data'){
                        this.title=item.mpointName+'电量日统计';
                    }else if(type=='minth_data'){
                        this.title=item.mpointName+'电量月统计';
                    }else if(type=='year_data'){
                        this.title=item.mpointName+'电量年统计';
                    }
                    this.asynDialog = true;
                }

            },
            test(index){
                if(index!==8&&index==!22&&index==!26&&index==!37&&index==!38&&index!==48&&index!==49){
                    console.log(index)
                }
            },
            getData(){
                let that = this;
                function dataPush(name,value){
                    let index=0;
                    if(name=='烘干破主电机'){
                        index=0;
                        that.tableDatas[0][3] = value;
                    }else if(name=='电石渣合计'){
                        index=1;
                        that.tableDatas[1][2] = value;
                    }else if(name=='反击式破碎机'){
                        index=2;
                    }else if(name=='立磨主电机'){
                        index=3;
                    }else if(name=='循环风机'){
                        index=4;
                    }else if(name=='废气风机'){
                        index=5;
                    }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=12;
                    }else if(name=='篦冷机二室风机电机'){
                        index=13;
                    }else if(name=='篦冷机一室充气梁风机（左）'){
                        index=14;
                    }else if(name=='篦冷机一室充气梁风机（右）'){
                        index=15;
                    }else if(name=='窑头排风机主电机'){
                        index=16;
                    }else if(name=='头煤粉罗茨电机（东、备用）'){
                        index=17;
                    }else if(name=='尾煤粉罗茨电机（西）'){
                        index=18;
                    }else if(name=='空压机3301'){
                        index=19;
                    }else if(name=='空压机3308'){
                        index=20;
                    }else if(name=='空压机3318'){
                        index=21;
                    }else if(name=='回转窑合计'){
                        index=22;
                    }else if(name=='煤磨主电机'){
                        index=23;
                    }else if(name=='煤磨排风机主电机'){
                        index=24;
                    }else if(name=='煤磨合计'){
                        index=25;
                    }else if(name=='循环风机1906'){
                        index=27;
                    }else if(name=='系统风机'){
                        index=28;
                    }else if(name=='水平涡流选粉机1915'){
                        index=29;
                    }else if(name=='水泥磨主电机'){
                        index=30;
                    }else if(name=='辊压机动辊电机'){
                        index=31;
                    }else if(name=='辊压机定辊电机'){
                        index=32;
                    }else if(name=='新增磨尾风机'){
                        index=33;
                    }else if(name=='水泥1#空压机'){
                        index=34;
                    }else if(name=='水泥2#空压机'){
                        index=35;
                    }else if(name=='水泥磨合计计'){
                        index=36;
                    }else if(name=='水泥包装合计'){
                        index=37;
                    }else if(name=='食堂'){
                        index=39;
                    }else if(name=='单元楼'){
                        index=40;
                    }else if(name=='单身宿舍'){
                        index=41;
                    }else if(name=='办公楼'){
                        index=42;
                    }else if(name=='余热锅炉'){
                        index=43;
                    }else if(name=='污水站'){
                        index=44;
                    }else if(name=='南门办公区'){
                        index=45;
                    }else if(name=='东门办公区'){
                        index=46;
                    }else if(name=='生活区合计'){
                        index=47;
                    }else if(name=='地磅'){
                        index=48;
                    }
                    return index;
                }
                function sumNum(a,b,c,d){
                    let sum = 0;
                    if(a&&Number(a)){sum=sum+a;}
                    if(b&&Number(b)){sum=sum+b;}
                    if(c&&Number(c)){sum=sum+c;}
                    if(d&&Number(d)){sum=sum+d;}
                    return sum;
                }
                let obj = {};
                obj.type='hour_s';
                obj.year = that.year;
                obj.month = that.month;
                obj.day = that.day-1;
                obj.hour = that.hour;
                obj.page = 0;
                obj.mpoint__ep_monitored__power_kw__gte=100;
                this.$API.enm.mpoint.stat.req(obj).then((res) => {
                    //上小时
                    res.forEach(item=>{
                       let i = dataPush(item.mpoint_name);
                       that.tableData[i].hour_data = item.val;
                       that.tableData[i].mpoint = item.mpoint;
                    })
                    that.tableDatas[8].hour_data =sumNum(that.tableDatas[1].hour_data,that.tableDatas[7].hour_data);
                    that.tableDatas[26].hour_data =  sumNum(that.tableDatas[22].hour_data, that.tableDatas[25].hour_data);
                    that.tableDatas[38].hour_data =  sumNum(that.tableDatas[36].hour_data, that.tableDatas[37].hour_data);
                    that.tableDatas[49].hour_data =  sumNum(that.tableDatas[8].hour_data, that.tableDatas[26].hour_data, that.tableDatas[38].hour_data, that.tableDatas[48].hour_data);
                }).then(()=>{
                    //昨日
                    let obj1 = {};
                    obj1.type='day_s';
                    let dayTime = 24 * 60 * 60 * 1000;
                    let year_d = that.year,month_d = that.month,days_d = that.day;
                    if(that.hour<21){//21点前，查找昨日数据为前一天数据
                        let newDate = that.timeDate- dayTime;
                        let lastDate = new Date(newDate);
                        year_d = lastDate.getFullYear();
                        month_d = lastDate.getMonth()+1;
                        days_d = lastDate.getDate();
                    }
                    obj1.year_s = year_d;
                    obj1.month_s = month_d;
                    obj1.day_s = days_d;
                    obj1.page = 0;
                    obj1.mpoint__ep_monitored__power_kw__gte=100;
                    this.$API.enm.mpoint.stat.req(obj1).then((res1) => {
                        res1.forEach(item1=>{
                            let i = dataPush(item1.mpoint_name);
                            that.tableData[i].day_data = item1.val;
                        })
                        that.tableDatas[8].day_data = sumNum(that.tableDatas[1].day_data,that.tableDatas[7].day_data);
                        that.tableDatas[26].day_data = sumNum(that.tableDatas[22].day_data, that.tableDatas[25].day_data);
                        that.tableDatas[38].day_data = sumNum(that.tableDatas[36].day_data, that.tableDatas[37].day_data);
                        that.tableDatas[49].day_data = sumNum(that.tableDatas[8].day_data, that.tableDatas[26].day_data, that.tableDatas[38].day_data, that.tableDatas[48].day_data);
                    })
                    //本月
                    let obj2 = {};
                    obj2.type='month_s';
                    obj2.year_s = this.year;
                    obj2.month_s = this.month;
                    obj2.page = 0;
                    obj2.mpoint__ep_monitored__power_kw__gte=100;
                    this.$API.enm.mpoint.stat.req(obj2).then((res2) => {
                        res2.forEach(item2=>{
                            let i = dataPush(item2.mpoint_name);
                            that.tableData[i].month_data = item2.val;
                        })
                        that.tableDatas[8].month_data = sumNum(that.tableDatas[1].month_data,that.tableDatas[7].month_data);
                        that.tableDatas[26].month_data =  sumNum(that.tableDatas[22].month_data, that.tableDatas[25].month_data);
                        that.tableDatas[38].month_data =  sumNum(that.tableDatas[36].month_data, that.tableDatas[37].month_data);
                        that.tableDatas[49].month_data =  sumNum(that.tableDatas[8].month_data, that.tableDatas[26].month_data, that.tableDatas[38].month_data, that.tableDatas[48].month_data);
                    })
                     //本年
                     let obj3 = {};
                    obj3.type='year_s';
                    obj3.year_s = this.year;
                    obj3.page = 0;
                    obj3.mpoint__ep_monitored__power_kw__gte=100;
                    this.$API.enm.mpoint.stat.req(obj3).then((res3) => {
                        res3.forEach(item3=>{
                            let i = dataPush(item3.mpoint_name);
                            that.tableData[i].year_data = item3.val;
                        })
                        that.tableDatas[8].year_data = sumNum(that.tableDatas[1].year_data,that.tableDatas[7].year_data);
                        that.tableDatas[26].year_data = sumNum(that.tableDatas[22].year_data, that.tableDatas[25].year_data);
                        that.tableDatas[38].year_data = sumNum(that.tableDatas[36].year_data, that.tableDatas[37].year_data);
                        that.tableDatas[49].year_data = sumNum(that.tableDatas[8].year_data, that.tableDatas[26].year_data, that.tableDatas[38].year_data, that.tableDatas[48].year_data);
                    })
                })
            },
            exportExcel() {
                this.exportLoading = true;
                this.$XLSX('#myTable', this.tableName)
                this.exportLoading = false;
            },
            handlePrint() {
                this.$PRINT('#myReport');
            },
        }
    };
</script>
<style>
    #numTable{
        margin-left: 37px;
    }
    #numTable td{
        height: 32px;
        padding-left: 5px;
    }
    .numCell{
        width: 80px;
    }
    .numCell.numCell_last{
        width: 100px;
    }
    .searchHead{
        display:flex
    }
    .middleText{
        height: 32px;
        line-height: 32px;
        margin: 0 5px;
        display: inline-block;
    }
    .searchBtn{
        margin-left: 5px;
    }
</style>