|
@@ -0,0 +1,228 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @author Mai
|
|
|
+ * @date
|
|
|
+ * @version
|
|
|
+ */
|
|
|
+
|
|
|
+const TreeExprCalc = (function(){
|
|
|
+ const calcRela = {
|
|
|
+ SerialReg: new RegExp('<<f[0-9]+\\$[a-z]+>>', 'ig'),
|
|
|
+ SerialFirstReg: new RegExp('^<<f[0-9]+\\$[a-z]+>>', 'i'),
|
|
|
+ IdReg: new RegExp('<<[a-z0-9\-]+\\$[a-z]+>>', 'ig'),
|
|
|
+ OrderReg: new RegExp('f[0-9]+', 'ig'),
|
|
|
+ valueChar: '$',
|
|
|
+ calcCache: {},
|
|
|
+ exprInitFields: ['contract_tp', 'qc_tp', 'gather_tp'],
|
|
|
+ calcType: {zero: 0, direct: 1, cache: 2}
|
|
|
+ };
|
|
|
+ const checkExprValid = function(expr, invalidOrders = []) {
|
|
|
+ if (!expr) return [true, ''];
|
|
|
+
|
|
|
+ const param = [];
|
|
|
+ let num = '';
|
|
|
+ for (let i = 0, iLen = expr.length; i < iLen; i++) {
|
|
|
+ const subExpr = expr.substring(i, expr.length);
|
|
|
+ if (/^[\d\.%]+/.test(expr[i])) {
|
|
|
+ num = num + expr[i];
|
|
|
+ } else if (calcRela.SerialFirstReg.test(subExpr)) {
|
|
|
+ if (num !== '') {
|
|
|
+ param.push({type: 'num', value: num});
|
|
|
+ num = '';
|
|
|
+ }
|
|
|
+ const order = calcRela.SerialFirstReg.exec(subExpr);
|
|
|
+ param.push({type: 'order', value: order[0]});
|
|
|
+ i = i + order[0].length - 1;
|
|
|
+ } else if (expr[i] === '(') {
|
|
|
+ if (num !== '') {
|
|
|
+ param.push({type: 'num', value: num});
|
|
|
+ num = '';
|
|
|
+ }
|
|
|
+ param.push({type: 'left', value: '('});
|
|
|
+ } else if (expr[i] === ')') {
|
|
|
+ if (num !== '') {
|
|
|
+ param.push({type: 'num', value: num});
|
|
|
+ num = '';
|
|
|
+ }
|
|
|
+ param.push({type: 'right', value: ')'});
|
|
|
+ } else if (/^[\+\-*\/]/.test(expr[i])) {
|
|
|
+ if (num !== '') {
|
|
|
+ param.push({type: 'num', value: num});
|
|
|
+ num = '';
|
|
|
+ }
|
|
|
+ param.push({type: 'calc', value: expr[i]});
|
|
|
+ } else {
|
|
|
+ return [false, '输入的表达式含有非法字符: ' + expr[i]];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (num !== '') {
|
|
|
+ param.push({type: 'num', value: num});
|
|
|
+ num = '';
|
|
|
+ }
|
|
|
+ if (param.length === 0) return [true, ''];
|
|
|
+ if (param.length > 1) {
|
|
|
+ if (param[0].value === '-' && param[1].type === 'num') {
|
|
|
+ param[1].value = '-' + param[1].value;
|
|
|
+ param.shift();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const iLen = param.length;
|
|
|
+ let iLeftCount = 0, iRightCount = 0;
|
|
|
+ for (const [i, p] of param.entries()) {
|
|
|
+ if (p.type === 'calc') {
|
|
|
+ if (i === 0 || i === iLen - 1)
|
|
|
+ return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
|
|
|
+ }
|
|
|
+ if (p.type === 'num') {
|
|
|
+ num = p.value.replace('%', '');
|
|
|
+ if (p.value.length - num.length > 1)
|
|
|
+ return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
|
|
|
+ num = _.toNumber(num);
|
|
|
+ if (num === undefined || num === null || _.isNaN(num))
|
|
|
+ return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
|
|
|
+ if (i > 0) {
|
|
|
+ if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
|
|
|
+ return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
|
|
|
+ } else if (param[i - 1].value === '/' && num === 0) {
|
|
|
+ return [false, '输入的表达式非法:请勿除0'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (p.type === 'order') {
|
|
|
+ const match = invalidOrders.find(x => { p.value.indexOf(`f${x.orderStr}$`) > 0; });
|
|
|
+ if (match) return [false, `输入的表达式非法:循环引用,请勿引用${match.orderStr}`];
|
|
|
+
|
|
|
+ if (i > 0) {
|
|
|
+ if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
|
|
|
+ return [false, '输入的表达式非法:' + p.value.replace(/</ig, '<').replace(/>/ig, '>') + '前应有运算符'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (p.type === 'left') {
|
|
|
+ iLeftCount += 1;
|
|
|
+ if (i !== 0 && param[i-1].type !== 'calc')
|
|
|
+ return [false, '输入的表达式非法:(前应有运算符'];
|
|
|
+ }
|
|
|
+ if (p.type === 'right') {
|
|
|
+ iRightCount += 1;
|
|
|
+ if (i !== iLen - 1 && param[i+1].type !== 'calc')
|
|
|
+ return [false, '输入的表达式非法:)后应有运算符'];
|
|
|
+ if (iRightCount > iLeftCount)
|
|
|
+ return [false, '输入的表达式非法:")"前无对应的"("'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (iLeftCount > iRightCount)
|
|
|
+ return [false, '输入的表达式非法:"("后无对应的")"'];
|
|
|
+ return [true, ''];
|
|
|
+ };
|
|
|
+ const init = function(tree, decimal) {
|
|
|
+ calcRela.tree = tree;
|
|
|
+ calcRela.decimal = decimal;
|
|
|
+ };
|
|
|
+ const getCalcField = function(value) {
|
|
|
+ switch(value) {
|
|
|
+ case 'tzje': return 'total_price';
|
|
|
+ case 'qyje': return 'deal_tp';
|
|
|
+ case 'tzsl': return 'quantity';
|
|
|
+ case 'qysl': return 'deal_qty';
|
|
|
+ case 'bqhtje': return 'contract_tp';
|
|
|
+ }
|
|
|
+ };
|
|
|
+ const getCalcNumZero = function (node, field) {
|
|
|
+ return node.calc_expr && calcRela.exprInitFields.indexOf(field) >= 0 ? 0 : node[field] || 0;
|
|
|
+ };
|
|
|
+ const getCalcNumDirect = function (node, field) {
|
|
|
+ return node[field] || 0;
|
|
|
+ };
|
|
|
+ const getCalcNumCache = function (node, field) {
|
|
|
+ if (node.calc_expr && calcRela.exprInitFields.indexOf(field) >= 0) {
|
|
|
+ return calcRela.calcCache[node.id] ? calcRela.calcCache[node.id][field] || 0 : 0;
|
|
|
+ }
|
|
|
+ return node[field] || 0;
|
|
|
+ };
|
|
|
+ const getIdParamValue = function(idParam) {
|
|
|
+ const [id, value] = idParam.substring(2, idParam.length - 2).split(calcRela.valueChar);
|
|
|
+ const node = calcRela.tree.nodes.find(x => { return x.id === id; });
|
|
|
+ if (!node) return 0;
|
|
|
+
|
|
|
+ const calcField = getCalcField(value);
|
|
|
+
|
|
|
+ if (!node.children || node.children.length === 0) {
|
|
|
+ return calcRela.getCalcNum(node, calcField);
|
|
|
+ } else {
|
|
|
+ const posterity = calcRela.tree.getLeafPosterity(node);
|
|
|
+ const calcMap = posterity.map(x => { return calcRela.getCalcNum(x, calcField) });
|
|
|
+ return ZhCalc.sum(calcMap);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ const calcExpr = function(expr) {
|
|
|
+ if (!expr) return 0;
|
|
|
+ let formula = expr;
|
|
|
+ const idParam = expr.match(calcRela.IdReg);
|
|
|
+ if (idParam) {
|
|
|
+ for (const ip of idParam) {
|
|
|
+ formula = formula.replace(ip, getIdParamValue(ip));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return [formula, math.evaluate(formula)];
|
|
|
+ };
|
|
|
+ const addCache = function(expr) {
|
|
|
+ const cache = { id: expr.id };
|
|
|
+ if (expr.calcField === 'contract_qty') {
|
|
|
+ cache.contract_qty = ZhCalc.round(expr.value, calcRela.decimal.qty);
|
|
|
+ cache.contract_tp = ZhCalc.mul(cache.contract_tp, expr.unit_price, calcRela.decimal.tp);
|
|
|
+ } else if (expr.calcField === 'contract_tp') {
|
|
|
+ cache.contract_qty = 0;
|
|
|
+ cache.contract_tp = ZhCalc.round(expr.value, calcRela.decimal.tp);
|
|
|
+ }
|
|
|
+ calcRela.calcCache[expr.id] = cache;
|
|
|
+ };
|
|
|
+ const calcAllExpr = function(exprList) {
|
|
|
+ calcRela.calcCache = {};
|
|
|
+ for (const expr of exprList) {
|
|
|
+ [expr.formula, expr.value] = calcExpr(expr.expr);
|
|
|
+ addCache(expr);
|
|
|
+ }
|
|
|
+ calcRela.calcCache = {};
|
|
|
+ };
|
|
|
+ const expr2ExprStr = function(expr) {
|
|
|
+ if (!expr) return '';
|
|
|
+ let formula = expr;
|
|
|
+ const idParam = expr.match(calcRela.IdReg);
|
|
|
+ if (idParam) {
|
|
|
+ for (const ip of idParam) {
|
|
|
+ const [id, value] = ip.substring(2, ip.length - 2).split(calcRela.valueChar);
|
|
|
+ const order = calcRela.tree.nodes.findIndex(x => { return x.id === id });
|
|
|
+ const orderParam = `<<f${order + 1}$${value}>>`;
|
|
|
+ formula = formula.replace(ip, orderParam);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return formula;
|
|
|
+ };
|
|
|
+ const exprStr2Expr = function(exprStr) {
|
|
|
+ if (!exprStr) return '';
|
|
|
+ let formula = exprStr;
|
|
|
+ const orderParam = exprStr.match(calcRela.SerialReg);
|
|
|
+ if (orderParam) {
|
|
|
+ for (const op of orderParam) {
|
|
|
+ const orderStr = op.match(calcRela.OrderReg)[0];
|
|
|
+ const order = parseInt(orderStr.substring(1, op.length));
|
|
|
+ const node = calcRela.tree.nodes[order - 1];
|
|
|
+ const idParam = op.replace(orderStr, node.id);
|
|
|
+ formula = formula.replace(op, idParam);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return formula;
|
|
|
+ };
|
|
|
+ const setCalcType = function (calcType) {
|
|
|
+ if (calcType === calcRela.calcType.zero) calcRela.getCalcNum = getCalcNumZero;
|
|
|
+ if (calcType === calcRela.calcType.direct) calcRela.getCalcNum = getCalcNumDirect;
|
|
|
+ if (calcType === calcRela.calcType.cache) calcRela.getCalcNum = getCalcNumCache;
|
|
|
+ };
|
|
|
+
|
|
|
+ return { init, checkExprValid, calcExpr, calcAllExpr, expr2ExprStr, exprStr2Expr, setCalcType, calcType: calcRela.calcType}
|
|
|
+})();
|