var laypage;
var layer;
var laydate;
var form;
var page = 1; //页面,默认第1页
var request = {}; //请求数据
var LIST_DEVICE;
var warnData;
var checkbox_warn;
var warnMap = [];
var excel;
$(function () {
layui.use(['laypage', 'layer','form', 'laydate'], function () {
laypage = layui.laypage;
layer = layui.layer;
form = layui.form;
laydate = layui.laydate;
//开始时间
laydate.render({
elem: '#start'
, type: 'date'
, trigger: 'click'
, theme: 'custom'
});
//截止时间
laydate.render({
elem: '#end'
, type: 'date'
, trigger: 'click'
, theme: 'custom'
});
//渲染按钮位置
renderParamBtn();
//请求设备列表
queryDevice();
//获取警告并渲染
// queryWarn(page);
query(page);
});
});
//请求获取设备列表
function queryDevice() {
$.get("./cgi-bin/device/query-all", function (data, status) {
if ("success" == status) {
LIST_DEVICE = data;
renderDeviceInfo();
}
}, "json");
}
//渲染设备下拉框
function renderDeviceInfo() {
var html = '';
html += '所有设备 ';
if (LIST_DEVICE != null && LIST_DEVICE.length > 0) {
$.each(LIST_DEVICE, function (index, item) {
html += '' + item.name + ' ';
});
}
$("#deviceId").html(html);
}
//获取请求参数
function query(page) {
//获取参数
request.id = $("#deviceId").val();
if(!request.id){
request.id = '0';
}
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;
queryWarn();
}
//请求获取报警数据
function queryWarn() {
$.post("./cgi-bin/warn-list/query", JSON.stringify(request), function (data, status) {
if ("success" == status) {
warnData = data;
//渲染警告列表
renderWarnInfo(warnData.data);
//渲染页码
renderPageInfo(request.page, warnData.total);
} else {
window.parent.notify("数据查询出错,请重新操作!");
}
}, "json");
}
//渲染警告列表
function renderWarnInfo(data) {
var html = '';
var total = 0;
if (data != null && data.length > 0) {
listSetMap(data);
// $.each(data, function (index, item) {
// if(item.flag == 0){
// total++;
// }
// });
$.each(data, function (index, item) {
// if(item.flag == 0){
html += '
';
// html += '' + (index + 1) + ' ';
html += '' + item.sn + ' ';
html += '' + item.name + ' ';
html += '' + item.id + ' ';
html += '' + timeStr(item.time) + ' ';
html += '' + (item.flag == 0 ? '未处理 ' : '已处理') + ' ';
var str = item.msg;
str = str.replace("供电方式 : 0","供电方式 : 市电供电");
str = str.replace("供电方式 : 1","供电方式 : 电池供电");
str = str.replace("工作模式 : 0","工作模式 : 正常模式");
str = str.replace("工作模式 : 1","工作模式 : 旁路模式");
str = str.replace("工作模式 : 2","工作模式 : 电池供电");
// str = str.replaceAll(": 0",": 正常");
// str = str.replaceAll(": 2",": 告警");
html += '' + str + ' ';
if(item.flag==0){
html +='处理 ';
}else {
html +='处理 ';
}
html +='查看 ';
// }
});
}else {
html += '暂未查到数据 ';
}
$("#warnList").html(html);
// //渲染页码
// renderPageInfo(request.page, total);
}
//修改警报状态
function warnUpdate() {
//获取表单内容
var data = form.val('edit_warn_example');
if(data.note){
console.log(data);
var comData={
"sn": data.sn,
"flag": 1,
"note": data.note,
"processTime": timeStr1(data.processTime),
"id": data.id
};
console.log(comData);
$.post("./cgi-bin/warn-list/update", JSON.stringify(comData), function (data, status) {
if ("success" == status) {
//关闭弹窗
closepopBtn();
//重新刷新此页面数据
queryWarn();
} else {
window.parent.notify("处理失败,请重新操作!");
}
}, "json");
}
}
//警报处理弹窗
function warnTodo(index) {
var winInfo = warnData.data[index];
$("#note").removeAttr("readOnly");
$(".pop-complete-btn").show();
form.val('edit_warn_example', {
"sn": winInfo.sn,
"name": winInfo.name,
"id": winInfo.id,
"flag": winInfo.flag == 0?'未处理':'已处理',
"time": timeStr(winInfo.time),
"msg": winInfo.msg,
"note": winInfo.note,
"processTime": new Date().Format("yyyy-MM-dd hh:mm:ss")
});
if(screen.width==1920 && screen.height==1080){
layer.open({
skin: 'mypop',
type: 1,
title: false,
area: ['50%', '62%'],
closeBtn: 0,
shade: 0,
scrollbar: false,
content: $('#editWarnDom')
});
}else if(screen.width==1280 && screen.height==1024){
layer.open({
skin: 'mypop',
type: 1,
title: false,
area: ['80%', '64%'],
closeBtn: 0,
shade: 0,
scrollbar: false,
content: $('#editWarnDom')
});
}else{
layer.open({
skin: 'mypop',
type: 1,
title: false,
area: ['50%', '62%'],
closeBtn: 0,
shade: 0,
scrollbar: false,
content: $('#editWarnDom')
});
}
};
//警报查看弹窗
function warnDetail(index) {
var winInfo = warnData.data[index];
$("#note").attr("readOnly","readOnly");
$(".pop-complete-btn").hide();
form.val('edit_warn_example', {
"sn": winInfo.sn,
"name": winInfo.name,
"id": winInfo.id,
"flag": winInfo.flag == 0?'未处理':'已处理',
"time": timeStr(winInfo.time),
"msg": winInfo.msg,
"note": winInfo.note,
"processTime": new Date().Format("yyyy-MM-dd hh:mm:ss")
});
layer.open({
skin: 'mypop',
type: 1,
title: false,
area: ['50%', '62%'],
closeBtn: 0,
shade: 0,
scrollbar: false,
content: $('#editWarnDom')
});
}
//页码渲染
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;
query(page);
}
}
});
}
//标准时间格式化
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, // 月份
"d+": this.getDate(), // 日
"h+": this.getHours(), // 小时
"m+": this.getMinutes(), // 分
"s+": this.getSeconds(), // 秒
"q+": Math.floor((this.getMonth() + 3) / 3), // 季度
"S": this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
//时间格式处理,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)
}
//时间格式处理,2021-02-26 10:10:10-----20210226101010
function timeStr1(time) {
return time.replace('-','').replace('-','').replace(' ','').replace(':','').replace(':','');
}
function closepopBtn(){
layer.closeAll();
}
/*------------------ tableToExcel导出模块-start ----------------- */
//初始化Excel导出,id=table表格的id
//导出Excel点击事件
function generateClick(){
queryAllWarn();
}
function queryAllWarn(){
$.post("./cgi-bin/warn-list/excel-warn", JSON.stringify(request), function (data, status) {
if ("success" == status) {
//渲染警告列表
var html = renderTable(data.data);
tableToExcel(html);
} else {
window.parent.notify("数据查询出错,请重新操作!");
}
}, "json");
}
function renderTable(data){
//表头
var thead = "";
thead += "";
thead += "序号 ";
thead += "设备ID ";
thead += "设备名称 ";
thead += "警告时间 ";
thead += "警告内容 ";
thead += "警告状态 ";
thead += "处理时间 ";
thead += "处理备注 ";
thead += " ";
var tbody = "";
if (data != null && data.length > 0) {
$.each(data, function (index, item) {
tbody += "" + item.sn + " ";
tbody += "" + item.id + " ";
tbody += "" + item.name + " ";
tbody += "" + timeStr(item.time) + " ";
tbody += "" + item.msg + " ";
tbody += "" + (item.flag == 0 ? "未处理 ": "已处理") + " ";
tbody += "" + timeStr(item.processTime) + " ";
tbody += "" + item.note + " ";
});
}else {
tbody += '暂未查到数据 ';
}
var html = thead + tbody;
return html;
}
function tableToExcel(html) {
var name = "告警记录";
var uri = 'data:application/vnd.ms-excel;base64,';
var template = ' ';
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 listSetMap(list) {
$.each(list,function (index,data) {
warnMap[data.sn] = data;
})
}
//批处理
function batchHandle() {
console.log("=============批处理============");
checkbox_warn = [];
var b;
var id= "";
$('[name=ckeck]:checked').each(function() {
id= "";
b = false;
b = $(this).prop('checked');
if(b){
id = $(this).attr('id');
checkbox_warn.push(warnMap[id]);
}
});
if(checkbox_warn.length <= 0){
layer.msg("选择不能为空!");
return;
}
console.log(checkbox_warn);
for(var i=0;i 0){
// for(var i=0;i 0){
// for(var i=0;i