| 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(); | 
| } |