/* uglified */
const path=require("path"),fs=require("fs"),_=require("../utils/Underscore"),mysql=require("mysql"),async=require("async"),db=function(e){e=e||2097152;const n=mysql.createPool({connectionLimit:20,host:"127.0.0.1",user:"root",password:"root",database:"test-wlw-b2c"});return n.getConn=function(e,o,t){n.getConnection(function(n,o){if(n)throw console.log("error connecting: ",n),new Error("服务器连接失败");console.log("getConn",o),e(o)})},n.exec=((e,o)=>{console.time("DB.exec"),e?n.getConn(function(n){n.beginTransaction(function(t){if(t)throw t;e="string"==typeof e?e.split(";"):e;let r=[];for(let o=0;o<e.length;o++)e[o].match(/(\w+)/)&&r.push(function(t){console.log("执行sql-------------"),console.log(e[o]),console.log("sql-------------"),n.query(e[o],function(e,n){if(e)return console.error(e),void t(e,null);console.log("第一次插入成功!"),t(null,n)})});async.series(r,function(e){if(e)return console.error(e),void n.rollback(function(){console.info("出现错误,回滚!"),n.release(),_.isFunction(o)&&o(e)});n.commit(function(e){n.release(),e?console.error(e):(console.log("成功,提交!"),_.isFunction(o)&&o())})})})}):console.warn("准备批量执行SQL，但参数SQL为空")}),n.run=((e,o,t)=>{if(console.time("DB.run"),o||(o=[]),!e)throw new Error("DB.run 方法，参数SQL 必须填写");if(_.isArray(e)){if(!_.isArray(o))throw new Error("批量执行多条SQL时，第二个参数可以是回调函数，但不支持传参。");n.exec(e,o)}else _.isFunction(o)&&(t=o,o=[]),n.getConn(function(n){console.log("执行sql-------------"),console.log(e),console.log("sql-------------"),n.query(e,o,function(e,o,r){if(e)throw e;n.release(),_.isFunction(t)&&t(o)})})}),n.select=((e,o,t)=>{if(!e)throw new Error("DB.select 方法，参数SQL 必须填写");o||(o=[]),!0===_.isFunction(o)&&(t=o,o=[]),n.run(e,o,function(e){_.isFunction(t)&&t(e)})}),n.insert=((e,o,t)=>{if(!e&&!o)throw new Error("DB.insert 方法，参数table, data缺一不可");if("string"!=typeof e)throw new Error("DB.insert 方法的table参数必须是 string");if(!_.isObject(o))throw new Error("DB.insert 方法的data参数必须是Object");let r=[],i=[],s=[];for(let e in o)r.push(e),/.+Time/g.test(e)?i.push("strftime('%Y-%m-%d %H:%M:%S', ?)"):i.push("?"),s.push(o[e]);let l="INSERT INTO "+e+"("+r.join(",")+")VALUES("+i.join(",")+")";n.run(l,s,function(e){_.isFunction(t)&&t(e.insertId)})}),n.update=((e,o,t,r,i)=>{if(!e&&!o&&!t)throw new Error("DB.update 方法，参数table, data, where缺一不可");r||(r=[]),_.isFunction(r)&&(i=r,r=[]);let s=n.__mkParam(o);for(let e=s.param.length-1;e>=0;e--)r.unshift(s.param[e]);let l="UPDATE "+e+" SET "+s.sql;t&&(l+=" WHERE "+t),n.run(l,r,function(e){_.isFunction(i)&&i(e.rowsAffected)})}),n.__dbInit__=(e=>{let o=path.resolve(__dirname,"../initialize.dll");fs.readFile(o,"utf-8",(o,t)=>{n.exec(t,e)})}),n.__dbUpdate__=(e=>{let o=path.resolve(__dirname,"../update.dll");fs.readFile(o,"utf-8",(t,r)=>{console.info("开始升级数据库"),console.info(r),r=r?r.split(";"):[];for(let e=0;e<r.length;e++)r[e].match(/(\w+)/)&&n.exec(r[e],function(n){n&&(console.info(r[e]),console.error(n))});try{fs.unlinkSync(o)}catch(e){console.warn(e)}_.isFunction(e)&&e()})}),n.__mkParam=(e=>{let n=[],o=[];if("object"==typeof e)for(let t in e)/.+Time/g.test(t)?n.push(t+"=strftime('%Y-%m-%d %H:%M:%S', ?)"):n.push(t+"=?"),o.push(e[t]);return{sql:n.join(" , "),param:o}}),n};module.exports=new db;