| 
					
				 | 
			
			
				@@ -0,0 +1,163 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+'use strict'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const fs = require('fs'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const path = require('path'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+var util = require('util'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+var logPath = path.join(__dirname, 'change_rela.log'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+var logFile = fs.createWriteStream(logPath, { flags: 'a' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+console.log = function() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    logFile.write(util.format.apply(null, arguments) + '\n'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    process.stdout.write(util.format.apply(null, arguments) + '\n'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const mysql = require('mysql'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const config = process.argv.splice(2)[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+if (['local', 'uat', 'default'].indexOf(config) < 0) throw `参数错误: ${config}`; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const options = require(`../config/config.${config}`)({ baseDir: __dirname + '/app', root: __dirname, name: 'calc' }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const pool = mysql.createPool(options.mysql.client); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const querySql = async function(sql, sqlParam) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return new Promise(function(resolve, reject) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        pool.getConnection(function(err, conn) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (err) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (err) console.log(err); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                reject(err); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                conn.query(sql, sqlParam, function(err, rows, fields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    if (err) console.log(err); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    // 释放连接 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    conn.release(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    // 传递Promise回调对象 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    resolve(rows); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const timesLen = 100; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const filterLastestData = function (data, keyFields, timesField = 'times', orderField = 'order') { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const dataIndex = {}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for (const d of data) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        let key = 'd'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (const kf of keyFields) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            key = key + '.' + (d[kf] || ''); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        const di = dataIndex[key]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (di) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if ((di[timesField] * timesLen + di[orderField]) < (d[timesField] * timesLen + d[orderField])) dataIndex[key] = d; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            dataIndex[key] = d; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const result = []; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for (const prop in dataIndex) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        result.push(dataIndex[prop]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const zeroRange = 0.0000000001; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const bc = require('../app/lib/base_calc.js'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const Decimal = require('decimal.js'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+Decimal.set({ precision: 50, defaults: true }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const ZhCalc = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 加减乘除方法,为方便调用,兼容num为空的情况 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // 加减法使用base_calc,乘除法使用Decimal(原因详见demo/calc_test) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 加法 num1 + num2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {number} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    add(num1, num2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return bc.add(num1 ? num1 : 0, num2 ? num2 : 0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 减法 num1 - num2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {number} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    sub(num1, num2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return bc.sub(num1 ? num1 : 0, num2 ? num2 : 0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 乘法 num1 * num2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {*} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    mul(num1, num2, digit = 6) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (num1 === '' || num1 === null || num2 === '' || num2 === null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return Decimal.mul(num1 ? num1 : 0, num2 ? num2 : 0).toDecimalPlaces(digit).toNumber(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 除法 num1 / num2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num1 - 被除数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param num2 - 除数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {*} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    div(num1, num2, digit = 6) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (num2 && !this.checkZero(num2)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return Decimal.div(num1 ? num1 : 0, num2).toDecimalPlaces(digit).toNumber(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 四舍五入(统一,方便以后万一需要置换) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param {Number} value - 舍入的数字 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param {Number} decimal - 要保留的小数位数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {*} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    round(value, decimal) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return value ? new Decimal(value.toString()).toDecimalPlaces(decimal).toNumber() : 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 汇总 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param array 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {number} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    sum(array) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        let result = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (const a of array) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            result = this.add(result, a); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 检查数字是否为0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param {Number} value 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {boolean} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    checkZero(value) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return value === undefined || value === null || (this._.isNumber(value) && Math.abs(value) < zeroRange); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 检查数字是否相等 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param {Number} value1 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param {Number} value2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return {boolean} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    numEqual(value1, value2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (value1 && value2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return Math.abs(value2 - value1) < zeroRange; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return (!value1 && !value2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+module.exports = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    querySql, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    filterLastestData, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    closePool: function () { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        pool.end(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ZhCalc, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+}; 
			 |