"use strict";(function(){"use strict";var e=require("util");var i=require("events");var d=require("./jsUtil");function n(e){i.EventEmitter.call(this);if(false===this instanceof n){return new n(e)}this.setUpDb(e)}e.inherits(n,i.EventEmitter);n.prototype.setUpDb=function(e){var i=this;var d="CREATE TABLE IF NOT EXISTS oper_log(diagnose_id VARCHAR(80) PRIMARY KEY, log_id  VARCHAR(32), client_id VARCHAR(80), "+"device_name VARCHAR(32), device_type VARCHAR(32), diag_time DATETIME, device_ip VARCHAR(32), rule_name VARCHAR(128), sub_rule_name VARCHAR(128), diag_result VARCHAR(10), "+"has_diag INTEGER, has_collect INTEGER, is_upload INTEGER, cloud_id VARCHAR(80))";var n="CREATE INDEX IF NOT EXISTS diag_idx_time ON oper_log (diag_time);";console.log("setUpDb sql:",d);i.db=e;i.db.run(d,function(e){if(e){console.log(e)}else{i.db.run(n);i._deleteExpLog()}})};n.prototype._deleteExpLog=function(){var e=this;var i=d.getLogStartTime();e.db.run("DELETE FROM oper_log WHERE diag_time < ?;",[i],function(e){if(e){console.log(e)}})};n.prototype.replace=function(e,i){var n=this;if(e==undefined||e.diagnoseId==undefined){console.error("in operDb.prototype.replace error: diagnoseId is invalid!");return}if(e.diagTime==undefined){console.error("in operDb.prototype.replace error: diagTime is invalid!");return}var o=[e.diagnoseId,e.logId!=undefined?e.logId:"",d.uuidByMac(),e.deviceName!=undefined?e.deviceName:"",e.deviceType!=undefined?e.deviceType:"",e.diagTime,e.deviceIp!=undefined?e.deviceIp:"",e.ruleName!=undefined?e.ruleName:"",e.subRuleName!=undefined?e.subRuleName:"",e.diagResult!=undefined?e.diagResult:"",e.hasDiag!=undefined?e.hasDiag?1:0:0,e.hasCollect!=undefined?e.hasCollect?1:0:0,e.isUpload!=undefined?e.isUpload?1:0:0,e.cloudId!=undefined?e.cloudId:""];n.db.run("REPLACE INTO oper_log(diagnose_id, log_id, client_id, device_name, device_type, diag_time, device_ip, rule_name, sub_rule_name, diag_result, has_diag, has_collect, is_upload, cloud_id) "+"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",o,function(e){if(i!=undefined){if(e){i(e)}else{i(null)}}})};n.prototype.queryLogsByCondition=function(e,i){var d="";var n=e.pageNum;var o=e.pageSize;var t=(n-1)*o;t=t<0?0:t;var u=[];if(e.startTime==undefined){d="SELECT * FROM oper_log ORDER BY diag_time DESC LIMIT ?, ?;";u=[t,o]}else{d="SELECT * FROM oper_log WHERE diag_time BETWEEN ? AND ? ORDER BY diag_time DESC LIMIT ?, ?;";u=[e.startTime,e.endTime,t,o]}this.db.all(d,u,function(e,d){if(i!=undefined){i(e,d)}})};n.prototype.updateLoadFlag=function(e,i,d,n){this.db.run("update oper_log set is_upload = ?, cloud_id = ? where diagnose_id = ?",[i?1:0,d,e],function(e){if(n!=undefined){n(e)}})};n.prototype.updateDiagFlag=function(e,i,d){this.db.run("update oper_log set has_diag = ? where diagnose_id = ?",[i?1:0,e],function(e){if(d!=undefined){d(e)}})};n.prototype.updateCollectFlag=function(e,i,d){this.db.run("update oper_log set has_collect = ? where diagnose_id = ?",[i?1:0,e],function(e){if(d!=undefined){d(e)}})};n.prototype.updateDiagResult=function(e,i,d){this.db.run("update oper_log set diag_result = ?, has_diag = ? where diagnose_id = ?",[i,1,e],function(e){if(d!=undefined){d(e)}})};n.prototype.queryCloudId=function(e,i){var d="select cloud_id from oper_log where diagnose_id = ?";this.db.get(d,[e],function(e,d){if(i!=undefined){if(d!=undefined){i(true,d.cloud_id)}else{i(false)}}})};n.prototype.queryLogsByTime=function(e,i){};module.exports=n})();