"use strict";(function(){var e=0;"use strict";var t=require("util");var r=require("events");var i=require("moment");function n(e){r.EventEmitter.call(this);if(false===this instanceof n){return new n}this.ignores=[];var t="CREATE TABLE IF NOT EXISTS ignore_table(dev VARCHAR(32), time DATETIME, level VARCHAR(32), type VARCHAR(72), subtype VARCHAR(72), desc VARCHAR(132), advice VARCHAR(132));";this.setUpDb(e,t)}t.inherits(n,r.EventEmitter);n.prototype.setUpDb=function(e,t){var r=this;this.db=e;this.db.serialize(function(){r.db.run(t)})};n.prototype.insertIntoDb=function(e){var t=this;t.emit("check_result",e);var r=t.sn;var n=i().format("YYYY-MM-DD HH:mm:ss");if(e.level!="ignore"){this.db.all("delete from ignore_table where dev =? and subtype=? and type=? ",[e.dev,e.subtype,e.type]);return}this.db.all("delete from ignore_table where dev =? and subtype=? and type=? ",[e.dev,e.subtype,e.type],function(r){if(!r){t.db.run("INSERT INTO ignore_table(dev, time, level, type, subtype, desc, advice) "+"VALUES (?, ?, ?, ?, ?, ?, ?);",[e.dev,n,e.level,e.type,e.subtype,e.desc,e.device])}})};var s=function u(e,t){var r=false;for(var i=0;i<e.ignores.length;i++){if(e.ignores[i].dev==t.dev&&e.ignores[i].type==t.type&&e.ignores[i].subtype==t.subtype){r=true;break}}if(r==false){e.ignores.push(t)}};var o=function p(e,t){var r=false;for(var i=0;i<e.ignores.length;i++){if(e.ignores[i].dev==t.dev&&e.ignores[i].type==t.type&&e.ignores[i].subtype==t.subtype){r=true;e.ignores.splice(i,1);break}}if(r==false){return}};var l=function d(e,t){var r=false;if(t.level!="ignore"){for(var i=0;i<e.ignores.length;i++){if(e.ignores[i].dev==t.dev&&e.ignores[i].type==t.type&&e.ignores[i].subtype==t.subtype){r=true;e.ignores.splice(i,1);break}}return}for(var i=0;i<e.ignores.length;i++){if(e.ignores[i].dev==t.dev&&e.ignores[i].type==t.type&&e.ignores[i].subtype==t.subtype){r=true;break}}if(r==false){e.ignores.push(t)}};var a=function v(e,t){e.insertIntoDb(t)};n.prototype.readIgnoreFromDb=function(){var e=this;for(var t=0;t<e.ignores.length;t++){e.ignores.splice(t,1)}console.log("ignore长度:"+e.ignores.length);var r=i().format("YYYY-MM-DD HH:mm:ss");e.queryFromDb(null,function(t,i){if(t==null){for(var n=0;n<i.length;n++){if(i[n].time.split(" ")[0]!=r.split(" ")[0]){e.db.all("delete from ignore_table where dev =? and subtype=? and type=? ",[i[n].dev,i[n].subtype,i[n].type]);console.log("删除ignore："+i[n].dev,i[n].subtype,i[n].type);continue}s(e,i[n])}}})};n.prototype.setIgnore=function(e){var t=this;e.level="ignore";a(t,e);l(t,e)};n.prototype.delIgnore=function(e){var t=this;a(t,e);l(t,e)};n.prototype.getIgnore=function(e){var t=this;var r=false;for(var i=0;i<t.ignores.length;i++){if(t.ignores[i].dev==e.dev&&t.ignores[i].type==e.type&&t.ignores[i].subtype==e.subtype){r=true;break}}return r};n.prototype.queryFromDb=function(e,t){var r=this;var i=r.sn;var n="select * from ignore_table";var s=0;console.log(e);if(e!=null&&e.subtype=="全部"&&e.keyword==""&&e.start_time==""&&e.end_time==""){e=null}if(e!=null){n+=" where ";if(e.subtype!=""&&e.subtype!="全部"){n+="(subtype="+"'"+e.subtype+"')";s++}if(e.keyword!=""){if(s!=0){n+=" and (subtype like "+"'%"+e.keyword+"%'"+" or desc like "+"'%"+e.keyword+"%')"}else{n+="(subtype like "+"'%"+e.keyword+"%'"+" or desc like "+"'%"+e.keyword+"%')"}s++}if(e.start_time!=""&&e.end_time!=""){if(s!=0){n+=" and (time between "+"'"+e.start_time+"'"+" and "+"'"+e.end_time+"')"}else{n+="time between "+"'"+e.start_time+"'"+" and "+"'"+e.end_time+"'"}}if(i!=""&&i!=undefined){}}n+=" order by time desc";console.log(n);this.db.all(n,function(e,r){t(e,r)})};module.exports=n})();