lgq
2025-07-19 45c4742c0cf31b0217874944127491990bf1a097
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
var laypage;
var layer;
var laydate;
var page = 1;  //页面,默认第1页
var LIST_DEVICE;
var recordData;
var dataMap = [];
 
$(function(){
    layui.use(['laypage', 'layer', 'laydate'], function () {
        laypage = layui.laypage;
        layer = layui.layer;
        laydate = layui.laydate;
        //开始时间
        laydate.render({
            elem: '#start'
            , type: 'date'
            , trigger: 'click'
            , theme: 'custom'
        });
        //截止时间
        laydate.render({
            elem: '#end'
            , type: 'date'
            , trigger: 'click'
            , theme: 'custom'
        });
 
        if(TEST_TAG){
            initData();
 
        }else{
            //请求设备列表
            queryDevice();
 
            //提示用户选择设备进行查询
            layer.msg("请先选择设备再进行查询!");
 
            //获取历史数据并渲染
            //queryRecord(page);
        }
 
        //提示用户选择设备进行查询
        layer.msg("请先选择设备再进行查询!");
        //请求设备列表
        // queryDevice();
    });
});
 
function initData() {
    var record = {};
    var data = new Array();
 
    var maxI = 7;
    var maxJ = 4;
 
    for(var i=1;i<=maxI;i++){
        var param = {};
        param.id = "设备id"+i;
        param.name = "设备名称";
        param.time = "20220217101523";
        var value = "";
        value += "[";
        if(i == 1){
            for(var j=1;j<=3;j++){
                value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
                if(j < 3){
                    value += ",";
                }
            }
        }else if(i == 2){
            for(var j=1;j<=4;j++){
                value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
                if(j < 4){
                    value += ",";
                }
            }
        }else if(i == 3){
            for(var j=1;j<=1;j++){
                value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
                if(j < 1){
                    value += ",";
                }
            }
        }else{
            for(var j=1;j<=maxJ;j++){
                value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
                if(j < maxJ){
                    value += ",";
                }
            }
        }
 
        value += "]";
        param.value = value;
        data.push(param);
    }
 
    record.total = 10;
    record.data = data;
 
    renderRecordInfo(record.data);
}
 
//请求获取设备列表
function queryDevice() {
    $.get("./cgi-bin/device/query-all", function (data, status) {
        if ("success" == status) {
            LIST_DEVICE = data;
            renderDeviceInfo();
        }
    }, "json");
}
 
//渲染设备下拉框
function renderDeviceInfo() {
    var html = '';
    html += '<option value="" selected>所有设备</option>';
    if (LIST_DEVICE != null && LIST_DEVICE.length > 0) {
        $.each(LIST_DEVICE, function (index, item) {
            html += '<option value="' + item.id + '">' + item.name + '</option>';
        });
    }
    $("#deviceId").html(html);
}
 
//请求获取历史数据
function queryRecord(page) {
    var request = {};
    request.id = $("#deviceId").val();
    if(!request.id)
    {
        request.id = '0';
        layer.msg("请选择设备进行查询!");
        return ;
    }
 
    request.beginTime = $("#start").val().replace('-', '').replace('-', '');
    if (request.beginTime) {
        request.beginTime += '000000';
    }
 
    request.endTime = $("#end").val().replace('-', '').replace('-', '');
    if (request.endTime) {
        request.endTime += '235959';
    }
 
    request.page = page;
    console.log(request);
    $.post("./cgi-bin/record-his/query", JSON.stringify(request), function (data, status) {
        if ("success" == status) {
            recordData = data;
            //渲染历史数据列表
            renderRecordInfo(recordData.data);
            //渲染页码
            renderPageInfo(page,recordData.total);
        }else {
            window.parent.notify("数据查询出错,请重新操作!");
        }
    }, "json");
}
 
//渲染历史数据列表
function renderRecordInfo(data) {
    var num = getWHNum();
    var div_body_x = false;
    var thead = "";
    thead += "<tr><th style='width: 100px'>ID</th>";
    thead += "<th style='width: 180px'>设备名称</th>";
    thead += "<th style='width: 180px'>时间</th>";
    var tbody = '';
    var theadData = [];
 
    if (data != null && data.length > 0) {
        //获取表头
        $.each(data, function (index, item) {
            var tempData = eval(item.value);
            if(theadData.length < tempData.length){
                theadData = tempData;
            }
        });
        // console.log(theadData);
        for(var i=0;i<theadData.length;i++){
            if(i >= num){
                break;
            }
            thead += "<th style='width: 150px'>"+ theadData[i].name +"</th>";
        }
        thead += "<th style=''>详情</th>";
        thead += "</tr>";
 
        $.each(data, function (index, item) {
            tbody += "<tr><td>" + item.id + "</td>";
            tbody += "<td>" + item.name + "</td>";
            tbody += "<td>" + timeStr(item.time) + "</td>";
            var tempData = eval(item.value);
            for (var i = 0; i < theadData.length; i++) {
                if(i >= num){
                    break;
                }
                if(i < tempData.length){
                    tbody += "<td>"+ tempData[i].value +"</td>";
                }else {
                    tbody += "<td></td>";
                }
            }
            var key = item.id + item.time;
            tbody += "<td><button class='layui-btn layui-btn-normal' onclick='detail(\""+key+"\");'>详情</button></td>";
            tbody += "</tr>";
            dataMap[key] = tempData;
        });
    } else {
        thead += "<th style=''>数据</th></tr>";
        tbody += '<tr><td colspan="4">暂未查到数据</td></tr>';
    }
    // $("#div-body").attr("style","height: 615px;");
    $("#thead").html(thead);
    $("#recordList").html(tbody);
}
 
//页码渲染
function renderPageInfo(page, total) {
    laypage.render({
        elem: 'pdgxq-page',
        count: total,
        theme: '#e45163',
        layout: ['prev', 'page', 'next', 'skip'],
        curr: page,
        limit: 10,
        jump: function (obj, first) {
            if (!first) {
                page = obj.curr;
                queryRecord(page);
            }
        }
    });
}
 
//时间格式处理,20210226101010-----2021-02-26 10:10:10
function timeStr(time) {
    return time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(8, 10) + ":" + time.substring(10, 12) + ":" + time.substring(12, 14)
}
 
/*------------------ tableToExcel导出模块-start ----------------- */
 
//导出Excel点击事件
function generateClick(){
    var request = {};
    request.id = $("#deviceId").val();
    if(!request.id)
    {
        request.id = '0';
        layer.msg("请选择设备进行查询!");
        return ;
    }
 
    request.beginTime = $("#start").val().replace('-', '').replace('-', '');
    if (request.beginTime) {
        request.beginTime += '000000';
    }
 
    request.endTime = $("#end").val().replace('-', '').replace('-', '');
    if (request.endTime) {
        request.endTime += '235959';
    }
 
    request.page = page;
    console.log(request);
 
    var html = queryAllRecord(request);
 
    // var record = {};
    // var data = new Array();
    //
    // var maxI = 5;
    // var maxJ = 9;
    //
    // for(var i=1;i<=maxI;i++){
    //     var param = {};
    //     param.id = "设备id";
    //     param.name = "设备名称";
    //     param.time = "20220217101523";
    //     var value = "";
    //     value += "[";
    //     if(i == 1){
    //         for(var j=1;j<=4;j++){
    //             value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
    //             if(j < 4){
    //                 value += ",";
    //             }
    //         }
    //     }else if(i == 3){
    //         for(var j=1;j<=15;j++){
    //             value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
    //             if(j < 15){
    //                 value += ",";
    //             }
    //         }
    //     }else if(i == 2){
    //         for(var j=1;j<=18;j++){
    //             value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
    //             if(j < 18){
    //                 value += ",";
    //             }
    //         }
    //     }else if(i == 4){
    //         for(var j=1;j<=7;j++){
    //             value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
    //             if(j < 7){
    //                 value += ",";
    //             }
    //         }
    //     }else{
    //         for(var j=1;j<=maxJ;j++){
    //             value += "{'name':'名称" + j +"','value':'数据" + i + "" + j + "'}";
    //             if(j < maxJ){
    //                 value += ",";
    //             }
    //         }
    //     }
    //
    //     value += "]";
    //     param.value = value;
    //     data.push(param);
    // }
    //
    // record.total = 10;
    // record.data = data;
    // // console.log(record);
    // var html = getTable(record.data);
    // tableToExcel(html);
}
 
function queryAllRecord(request){
    var html = "";
    $.ajaxSettings.async = false;
    $.post("./cgi-bin/record-his/excel-his", JSON.stringify(request), function (data, status) {
        if ("success" == status) {
            console.log(data.data);
            //渲染历史数据列表
            var html = getTable(data.data);
            tableToExcel(html);
        }else {
            window.parent.notify("数据查询出错,请重新操作!");
        }
    }, "json");
    return html;
}
 
function getTable(data) {
 
    var thead = "";
    thead += "<tr><th style='width: 100px'>ID</th>";
    thead += "<th style='width: 180px'>设备名称</th>";
    thead += "<th style='width: 180px'>时间</th>";
    var tbody = '';
    var theadData = [];
 
    if (data != null && data.length > 0) {
        //获取表头
        $.each(data, function (index, item) {
            var tempData = eval(item.value);
            if(theadData.length < tempData.length){
                theadData = tempData;
            }
        });
        console.log(theadData);
        for(var i=0;i<theadData.length;i++){
            thead += "<th style='width: 150px'>"+ theadData[i].name +"</th>";
        }
        thead += "</tr>";
 
        $.each(data, function (index, item) {
 
            tbody += "<tr><td>" + item.id + "</td>";
            tbody += "<td>" + item.name + "</td>";
            tbody += "<td>" + timeStr(item.time) + "</td>";
            var tempData = eval(item.value);
 
            for (var i = 0; i < theadData.length; i++) {
                if(i < tempData.length){
                    tbody += "<td>"+ tempData[i].value +"</td>";
                }else {
                    tbody += "<td></td>";
                }
                // tbody += "<td>"+ tempData[i].value +"</td>";
            }
            tbody += "</tr>";
        });
    } else {
        thead += "<th style='width: 150px'>数据</th></tr>";
        tbody += '<tr><td colspan="4">暂未查到数据</td></tr>';
    }
 
    var html = thead + tbody;
    // console.log(html);
    return html;
}
 
function tableToExcel(html) {
    var name = "历史记录";
    var uri = 'data:application/vnd.ms-excel;base64,';
    var template = '<html><head><meta charset="UTF-8"></head><body><table border="1">{table}</table></body></html>';
 
    if(!html){
        layer.msg("没有获取到要导出的内容!");
    }
 
    var ctx = {worksheet: name || 'Worksheet', table: html};
    window.location.href = uri + base64(format(template, ctx))
}
 
function format(s, c) {
    return s.replace(/{(\w+)}/g,function(m, p) { return c[p]; });
}
 
function base64(s) {
    return window.btoa(unescape(encodeURIComponent(s)))
};
 
/*------------------ tableToExcel导出模块-end ----------------- */
 
function detail(key) {
    var items = dataMap[key];
    var html = "";
 
    $.each(items, function (index, item) {
        html +="<tr><td>"+item.name+"</td><td>"+item.value+"</td></tr>";
    });
 
    $("#tbody").html(html);
 
    var paramWH = getWH("detail");
    layer.open({
        skin: 'mypop',
        type: 1,
        title: false,
        // area: ['40%', '65%'],
        area: [paramWH.w, paramWH.h],
        closeBtn: 0,
        shade: 0,
        scrollbar: false,
        content: $('#editWarnDom')
    });
}
 
function closepopBtn(){
    layer.closeAll();
    $("#tbody").html("");
}
 
 
 
 
function getWH(tag) {
    var res = {
        w:"50%",
        h:"64%"
    };
    if(screen.width==1920 && screen.height==1080){
        res = {w:"80%",h:"64%"};
        if(tag == "batchHandle"){
            res = {w:"80%",h:"64%"};
        }
    }else if(screen.width==1280 && screen.height==1024){
        res = {w:"80%",h:"64%"};
        if(tag == "detail"){
            res = {w:"80%",h:"95%"};
        }
        if(tag == "batchHandle"){
            res = {w:"80%",h:"64%"};
        }
    }else if(screen.width==1280 && screen.height==800){
        res = {w:"80%",h:"64%"};
        if(tag == "detail"){
            res = {w:"80%",h:"95%"};
        }
        if(tag == "batchHandle"){
            res = {w:"80%",h:"64%"};
        }
    }
    return res;
}
 
function getWHNum() {
    var res = 5;
    if(screen.width==1920){
        res = 5;
    }else if(screen.width==1280){
        res = 1;
    }
    return res;
}