jiazx0107@163.com
2023-10-11 cbe1226ae82f1818e13b7a4faa965b591d631633
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
var layer;
var form;
var table;
var element;
var grainData;// 仓库信息及温湿度信息
 
$(function () {
    layui.use(['layer', 'laydate', 'form', 'table', 'element'], function () {
        element = layui.element;
        form = layui.form;
        layer = layui.layer;
        var laydate = layui.laydate;
        table = layui.table;
 
        // 日期
        laydate.render({
            elem: '#checkDateMore',
            type: 'month',
            theme: '#7b8e9f',
            position: 'static',
            done: function (value, date, endDate) {
                flushGrain(value);
            }
        });
 
        laydate.render({
            elem: '#compareCheckDate',
            type: 'month',
            theme: '#7b8e9f',
            done: function (value, date, endDate) {
                compareStep2(value);
            }
        });
 
        laydate.render({
            elem: '#printCheckDate',
            theme: '#7b8e9f'
        });
        laydate.render({
            elem: '#printCheckDate2',
            theme: '#7b8e9f'
        });
        form.render();
 
        // 监听仓库选择
        form.on('select(select_depotId)', function (obj) {
            return onDepotChange(obj);
        });
 
        //初始化刷新数据
        initData();
    });
 
    // 初始化WebSocket
    initWS(deptId, bizType, null, userId);
});
 
function initData() {
    if (depotId) {
        $("#depotId").val(depotId);
        flushGrain(null);
        form.render();
    }
}
 
function queryDeviceIot() {
    depotId = $("#depotId").val();
    if (!depotId) {
        layer.msg("请先选择仓库!");
        return;
    }
    $.ajax({
        type: "POST",
        url: "../../basic/grain/list-device-iot",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify({
            depotId: depotId
        }),
        success: function (result) {
            if (result.code != "0000") {
                layer.msg(result.msg);
                return;
            }
            deviceList = result.data;
            console.log(deviceList);
            // 根据设备渲染
            addDevice(deviceList);
        },
        error: function (result) {
            layer.msg("获取设备失败,请重新尝试!");
        }
    });
}
 
function addDevice(list) {
    console.log(list);
 
    if (!list || list.length == 0) {
        return;
    }
    //清空设备信息
    $("#lq-center").empty();
    $("#lq-center").html('<img src="../../static/images/cpc-pic.png" class="qt-pic"/>');
 
    var mainImg = $("#lq-center");
 
    var temp;
    var left = 0, top = 0;
    $.each(list, function (index, item) {
        left = item.posX;
        top = item.posY;
 
        if (left < 1) left = 50;
        if (top < 1) top = 50;
 
        temp = '';
        temp += '<div id="' + item.id + '" class="wsd device" onclick=showTips("' + item.name + '") ';
        temp += 'style="left:' + left + 'px;top:' + top + 'px;position: absolute;"><div>';
        temp += '<img src="../../static/images/icon_wendu.png"/><span id="' + item.id + '_temp">--℃</span></div>';
        temp += '<div><img src="../../static/images/icon_shidu.png"/><span id="' + item.id + '_hum">--%</span>';
        temp += '</div></div>';
 
        mainImg.append(temp);
    });
}
 
function showTips(name) {
    $("#device_info").text("提示:设备名称-" + name);
}
 
// 开始拖拽
function dragDevice() {
    var tips = $(".device");
    if (tips.length == 0) {
        return;
    }
 
    var maxX = 1280;
    var maxY = 500;
 
    $.each(tips, function (index, item) {
        var dd = new Dragdrop({
            target: item,
            area: [0, maxX, 0, maxY],
            callback: function (obj) {
                //console.log('x:' + (obj.moveX) + ' y:' + (obj.moveY));
            }
        });
        dd.dragAll();
    });
}
 
// 保存设备位置信息
function updateGrainPos() {
    var tips = $(".device");
    if (tips.length == 0) {
        return;
    }
    var container = $("#lq-center");
    var parentLeft = container.offset().left;
    var parentTop = container.offset().top;
 
    // 封装数据进行保存
    var data = new Array();
    var id = null, curDepotId = null;
    var offset;
 
    $.each(tips, function (index, item) {
        id = item.id;
        curDepotId = item.getAttribute("depotId");
        offset = $('#' + id).offset();
 
        data[index] = {
            bizId: id,
            depotId: curDepotId,
            posX: offset.left - parentLeft,
            posY: offset.top - parentTop
        };
    });
    $.ajax({
        type: 'POST',
        url: "../../basic/common/update-grain-pos",
        dataType: 'JSON',
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(data),
        success: function (result) {
            layer.msg(result.msg);
        },
        error: function (result) {
            layer.msg(result.msg);
        }
    });
}
 
/**
 * 检测仓库类型是否发生改变,如果发生改变,重新切换页面
 */
function onDepotChange(obj) {
    var depotType = obj.elem[obj.elem.selectedIndex].getAttribute('type');
    //储粮方式
    var storeType = listDepot[obj.elem.selectedIndex - 1].storeType;
    if (depotType == DEPOT_TYPE) {
        if (storeType && storeType == "2") {
            flushGrain(null);
            return true;
        } else {
            changeView(depotType, storeType);
        }
    }else {
        changeView(depotType, storeType);
    }
}
 
function changeView(depotType, storeType) {
    //类型改变,自动切换页面
    if (socket) {
        socket.close();
    }
    depotId = $("#depotId").val();
    window.location.href = "../../basic/grain/gateway?depotId=" + depotId
        + "&depotType=" + depotType + "&storeType=" + storeType;
    return true;
}
 
// socket信息返回處理
function socketOnMessage(pocket) {
    if (pocket.orderResp == "MSG_SUCCESS") {// 信息解析成功刷新当前数据
        window.parent.sysNotify(pocket.data);
    }
};
 
// 单仓打印
function printSingle() {
    layer.msg("功能待开放……");
    return;
    layer.msg("开始调用后台打印模板……");
    toPrintSingle(grainData);
}
 
// 批量打印
function printBatch() {
    layer.msg("功能待开放……");
    return;
    // 首先去除之前的选择
    $('#batchPrintSelect input').each(function () {
        var name = $(this).prop("name");
        if (name == "printCheckDate") {// 跳过
            return true;
        }
        $(this).prop("disabled", false);
        $(this).prop("checked", false);
    });
    form.render();
 
    //初始化批量模板
    initModel();
 
    // 弹出选择框
    layer.open({
        type: 1,
        title: '批量粮情打印(红色表示没有粮情记录,默认为当天)',
        area: ['690px', '450px'],
        shade: 0,
        content: $('#batchPrintSelect'),
        btn: ['全选', '反选', '重选', '执行打印', '取消打印'],
        yes: function () {
            var name;
            $('#batchPrintSelect input').each(function () {
                name = $(this).prop("name");
                if (name == "printCheckDate" || $(this).prop("disabled")) {
                    // doNothing
                } else {
                    $(this).prop("checked", true);
                }
            });
            form.render();
        },
        btn2: function () {
            var name;
            $('#batchPrintSelect input').each(function () {
                name = $(this).prop("name");
                if (name == "printCheckDate" || $(this).prop("disabled")) {
                    // doNothing
                } else {
                    if ($(this).prop("checked")) {
                        $(this).prop("checked", false);
                    } else {
                        $(this).prop("checked", false);
                    }
                }
            });
            form.render();
            return false;
        },
        btn3: function () {
            var name;
            $('#batchPrintSelect input').each(function () {
                name = $(this).prop("name");
                if (name == "printCheckDate" || $(this).prop("disabled")) {
                    // doNothing
                } else {
                    $(this).prop("checked", false);
                }
            });
            form.render();
            return false;
        },
        btn4: function () {
            printBatchTodo();
        },
        btn5: function () {
            layer.close(0);
        },
        closeBtn: 0
    });
    // 默认调用获取数据更新
    $("#printCheckDate").prop("value", dateFtt("yyyy-MM-dd", new Date()));
    getPrintBatchDepot();
};
 
// 获取批量打印的信息
function getPrintBatchDepot() {
    layer.msg("验证粮情检测记录……");
    var checkDate = $("#printCheckDate").val();
    $.ajax({
        type: "POST",
        url: "../../basic/grain/query-checkDate-map",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify({
            checkDate: checkDate
        }),
        success: function (result) {
            if (result.code != "0000") {
                layer.msg(result.msg);
                disabledAllSelectDepot();
            } else {
                mapGrainData = result.data;
                updateSelectDepot();
            }
        },
        error: function () {
            layer.msg("批量打印获取粮情信息失败!");
        }
    });
};
 
// 根据查询的数据批量打印预览
function printBatchTodo() {
    layer.msg("开始批量生成打印模版……");
    var strHtml;
    var value;
    var checked;
    var printGrainData;
    // 将仓库数据列表转换为MAP
    var mapDepot = {};
    $.each(listDepot, function (index, data) {
        mapDepot[data.id] = data;
    });
    var LODOP = CLODOP;
    // 设置默认满张打印
    LODOP.PRINT_INIT("粮情报表");// 初始化在循环外
    LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4");
    $('#batchPrintSelect input').each(function () {
        if ($(this).prop("name") == "printCheckDate") {// 跳过
            return true;
        }
        checked = $(this).prop("checked");
        if (checked) {
            value = $(this).val();
            printGrainData = mapGrainData[value];
            if (!printGrainData) {// 如果没有粮情数据跳过执行下一个
                return true;
            }
            LODOP.NewPage();
            printGrainData.depotData = mapDepot[value];
            strHtml = builderModel(printGrainData);
            LODOP.ADD_PRINT_HTM(30, 40, "180mm", "100%", strHtml);
        }
    });
    layer.msg("开始调用打印程序预览……");
    LODOP.SET_PRINT_MODE("PRINT_PAGE_PERCENT", "Full-Page");
    LODOP.PREVIEW();
};
 
// 设置批量打印中所有仓库不可选择
function disabledAllSelectDepot() {
    var name;
    $('#batchPrintSelect input').each(function () {
        name = $(this).prop("name");
        if (name == "printCheckDate") {// 跳过
            return true;
        }
        $(this).prop("disabled", true);
    });
    form.render();
};
 
function updateSelectDepot() {
    var value, name;
    $('#batchPrintSelect input').each(function () {
        value = $(this).val();
        name = $(this).prop("name");
        if (name == "printCheckDate") {// 跳过
            return true;
        }
        if (mapGrainData[value]) {
            $(this).prop("disabled", false);
        } else {
            $(this).prop("disabled", true);
        }
    });
    form.render();
};
 
// 点击刷新操作
function flushGrain() {
    queryDeviceIot();
    compareData = null;
    depotId = $("#depotId").val();
 
    if (!depotId) {
        layer.alert("请先选择仓库!");
        return;
    }
    var data = {
        depotId: depotId
    };
 
    $.ajax({
        type: "POST",
        url: "../../basic/grain/query-grain-iot-data",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(data),
        success: function (result) {
            if (result.code != "0000") {
                layer.msg(result.msg);
            }else {
 
            }
            grainData = result.data;
            if (!grainData) {
                return;
            }
 
            // 粮情信息赋值
            renderGrainInfo();
 
            // 调用全部关闭
            layer.closeAll();
            layer.msg("更新成功……");
        },
        error: function () {
            layer.msg("根据当前条件获取粮情数据渲染图标失败!!");
        }
    });
};
 
// 填写粮情信息
function renderGrainInfo() {
    // 配置仓库卡片信息
    var depotData = grainData.depotData;
    $("#depotTypeName").text(depotData.depotTypeName);
    $("#storeDate").text(depotData.storeDate);
    $("#storageReal").text(depotData.storageReal);
    $("#foodVarietyName").text(depotData.foodVarietyName);
    $("#perWet").text(depotData.perWet);
    $("#depotStatus").text(DEPOT_STATUS_MSG(depotData.depotStatus));
    $("#foodLocation").text(depotData.foodLocation);
    $("#perImpurity").text(depotData.perImpurity);
    $("#storeKeeperName").text(depotData.storeKeeperName);
 
    var grainIotData = grainData.grainIotData;
 
    if (!grainIotData) {
        return;
    }
 
    var tempMax = null, humMax = null, tempMin = null, humMin = null, tempSum = 0.0, humSum = 0.0;
    var i = 0, j = 0;
    var time = "";
    $.each(grainIotData, function (index, item) {
        if(index == 0){
            $("#tempOut").text(item.outTemp == null?"--":item.outTemp.toFixed(1));
            $("#humidityOut").text(item.outHum == null?"--":item.outHum.toFixed(1));
        }
        time = item.time;
        if(item.temp){
            $("#" + item.deviceId + "_temp").text(item.temp + "℃");
            tempSum += item.temp;
            i ++;
            if(!tempMax){
                tempMax = item.temp;
            }
            if(!tempMin){
                tempMin = item.temp;
            }
            if(item.temp > tempMax){
                tempMax = item.temp;
            }
            if(item.temp < tempMin){
                tempMin = item.temp;
            }
        }
        if(item.hum){
            $("#" + item.deviceId + "_hum").text(item.hum + "%");
            humSum += item.hum;
            j ++;
            if(!humMax){
                humMax = item.hum;
            }
            if(!humMin){
                humMin = item.hum;
            }
            if(item.hum > humMax){
                humMax = item.hum;
            }
            if(item.hum < humMin){
                humMin = item.hum;
            }
        }
    });
    $("#tempMax").text(tempMax == null?"--":tempMax.toFixed(1));
    $("#tempMin").text(tempMin == null?"--":tempMin.toFixed(1));
 
    $("#humMax").text(humMax == null?"--":humMax.toFixed(1));
    $("#humMin").text(humMin == null?"--":humMin.toFixed(1));
 
    if(i > 0){
        $("#tempAve").text((tempSum/i).toFixed(1));
    }
    if(j > 0){
        $("#humAve").text((humSum/j).toFixed(1));
    }
    $("#batchId").empty();
    $('#batchId').append(new Option(time, time));// 下拉菜单里添加元素
    form.render();
}