tree_expr_calc.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const TreeExprCalc = (function(){
  10. const calcRela = {
  11. SerialReg: new RegExp('<<f[0-9]+\\$[a-z]+>>', 'ig'),
  12. SerialFirstReg: new RegExp('^<<f[0-9]+\\$[a-z]+>>', 'i'),
  13. IdReg: new RegExp('<<[a-z0-9\-]+\\$[a-z]+>>', 'ig'),
  14. OrderReg: new RegExp('f[0-9]+', 'ig'),
  15. valueChar: '$',
  16. calcCache: {},
  17. exprInitFields: ['contract_tp', 'qc_tp', 'gather_tp'],
  18. calcType: {zero: 0, direct: 1, cache: 2}
  19. };
  20. const checkExprValid = function(expr, invalidOrders = []) {
  21. if (!expr) return [true, ''];
  22. const param = [];
  23. let num = '';
  24. for (let i = 0, iLen = expr.length; i < iLen; i++) {
  25. const subExpr = expr.substring(i, expr.length);
  26. if (/^[\d\.%]+/.test(expr[i])) {
  27. num = num + expr[i];
  28. } else if (calcRela.SerialFirstReg.test(subExpr)) {
  29. if (num !== '') {
  30. param.push({type: 'num', value: num});
  31. num = '';
  32. }
  33. const order = calcRela.SerialFirstReg.exec(subExpr);
  34. param.push({type: 'order', value: order[0]});
  35. i = i + order[0].length - 1;
  36. } else if (expr[i] === '(') {
  37. if (num !== '') {
  38. param.push({type: 'num', value: num});
  39. num = '';
  40. }
  41. param.push({type: 'left', value: '('});
  42. } else if (expr[i] === ')') {
  43. if (num !== '') {
  44. param.push({type: 'num', value: num});
  45. num = '';
  46. }
  47. param.push({type: 'right', value: ')'});
  48. } else if (/^[\+\-*\/]/.test(expr[i])) {
  49. if (num !== '') {
  50. param.push({type: 'num', value: num});
  51. num = '';
  52. }
  53. param.push({type: 'calc', value: expr[i]});
  54. } else {
  55. return [false, '输入的表达式含有非法字符: ' + expr[i]];
  56. }
  57. }
  58. if (num !== '') {
  59. param.push({type: 'num', value: num});
  60. num = '';
  61. }
  62. if (param.length === 0) return [true, ''];
  63. if (param.length > 1) {
  64. if (param[0].value === '-' && param[1].type === 'num') {
  65. param[1].value = '-' + param[1].value;
  66. param.shift();
  67. }
  68. }
  69. const iLen = param.length;
  70. let iLeftCount = 0, iRightCount = 0;
  71. for (const [i, p] of param.entries()) {
  72. if (p.type === 'calc') {
  73. if (i === 0 || i === iLen - 1)
  74. return [false, '输入的表达式非法:计算符号' + p.value + '前后应有数字或计算基数'];
  75. }
  76. if (p.type === 'num') {
  77. num = p.value.replace('%', '');
  78. if (p.value.length - num.length > 1)
  79. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  80. num = _.toNumber(num);
  81. if (num === undefined || num === null || _.isNaN(num))
  82. return [false, '输入的表达式非法:' + p.value + '不是一个有效的数字'];
  83. if (i > 0) {
  84. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  85. return [false, '输入的表达式非法:' + p.value + '前应有运算符'];
  86. } else if (param[i - 1].value === '/' && num === 0) {
  87. return [false, '输入的表达式非法:请勿除0'];
  88. }
  89. }
  90. }
  91. if (p.type === 'order') {
  92. const match = invalidOrders.find(x => { p.value.indexOf(`f${x.orderStr}$`) > 0; });
  93. if (match) return [false, `输入的表达式非法:循环引用,请勿引用${match.orderStr}`];
  94. if (i > 0) {
  95. if (param[i - 1].type !== 'calc' && param[i - 1].type !== 'left') {
  96. return [false, '输入的表达式非法:' + p.value.replace(/</ig, '&lt;').replace(/>/ig, '&gt;') + '前应有运算符'];
  97. }
  98. }
  99. }
  100. if (p.type === 'left') {
  101. iLeftCount += 1;
  102. if (i !== 0 && param[i-1].type !== 'calc')
  103. return [false, '输入的表达式非法:(前应有运算符'];
  104. }
  105. if (p.type === 'right') {
  106. iRightCount += 1;
  107. if (i !== iLen - 1 && param[i+1].type !== 'calc')
  108. return [false, '输入的表达式非法:)后应有运算符'];
  109. if (iRightCount > iLeftCount)
  110. return [false, '输入的表达式非法:")"前无对应的"("'];
  111. }
  112. }
  113. if (iLeftCount > iRightCount)
  114. return [false, '输入的表达式非法:"("后无对应的")"'];
  115. return [true, ''];
  116. };
  117. const init = function(tree, decimal) {
  118. calcRela.tree = tree;
  119. calcRela.decimal = decimal;
  120. };
  121. const getCalcField = function(value) {
  122. switch(value) {
  123. case 'tzje': return 'total_price';
  124. case 'qyje': return 'deal_tp';
  125. case 'tzsl': return 'quantity';
  126. case 'qysl': return 'deal_qty';
  127. case 'bqhtje': return 'contract_tp';
  128. }
  129. };
  130. const getCalcNumZero = function (node, field) {
  131. return node.calc_expr && calcRela.exprInitFields.indexOf(field) >= 0 ? 0 : node[field] || 0;
  132. };
  133. const getCalcNumDirect = function (node, field) {
  134. return node[field] || 0;
  135. };
  136. const getCalcNumCache = function (node, field) {
  137. if (node.calc_expr && calcRela.exprInitFields.indexOf(field) >= 0) {
  138. return calcRela.calcCache[node.id] ? calcRela.calcCache[node.id][field] || 0 : 0;
  139. }
  140. return node[field] || 0;
  141. };
  142. const getIdParamValue = function(idParam) {
  143. const [id, value] = idParam.substring(2, idParam.length - 2).split(calcRela.valueChar);
  144. const node = calcRela.tree.nodes.find(x => { return x.id === id; });
  145. if (!node) return 0;
  146. const calcField = getCalcField(value);
  147. if (!node.children || node.children.length === 0) {
  148. return calcRela.getCalcNum(node, calcField);
  149. } else {
  150. const posterity = calcRela.tree.getLeafPosterity(node);
  151. const calcMap = posterity.map(x => { return calcRela.getCalcNum(x, calcField) });
  152. return ZhCalc.sum(calcMap);
  153. }
  154. };
  155. const calcExpr = function(expr) {
  156. if (!expr) return 0;
  157. let formula = expr;
  158. const idParam = expr.match(calcRela.IdReg);
  159. if (idParam) {
  160. for (const ip of idParam) {
  161. formula = formula.replace(ip, getIdParamValue(ip));
  162. }
  163. }
  164. return [formula, math.evaluate(formula)];
  165. };
  166. const addCache = function(expr) {
  167. const cache = { id: expr.id };
  168. if (expr.calcField === 'contract_qty') {
  169. cache.contract_qty = ZhCalc.round(expr.value, calcRela.decimal.qty);
  170. cache.contract_tp = ZhCalc.mul(cache.contract_tp, expr.unit_price, calcRela.decimal.tp);
  171. } else if (expr.calcField === 'contract_tp') {
  172. cache.contract_qty = 0;
  173. cache.contract_tp = ZhCalc.round(expr.value, calcRela.decimal.tp);
  174. }
  175. calcRela.calcCache[expr.id] = cache;
  176. };
  177. const calcAllExpr = function(exprList) {
  178. calcRela.calcCache = {};
  179. for (const expr of exprList) {
  180. [expr.formula, expr.value] = calcExpr(expr.expr);
  181. addCache(expr);
  182. }
  183. calcRela.calcCache = {};
  184. };
  185. const expr2ExprStr = function(expr) {
  186. if (!expr) return '';
  187. let formula = expr;
  188. const idParam = expr.match(calcRela.IdReg);
  189. if (idParam) {
  190. for (const ip of idParam) {
  191. const [id, value] = ip.substring(2, ip.length - 2).split(calcRela.valueChar);
  192. const order = calcRela.tree.nodes.findIndex(x => { return x.id === id });
  193. const orderParam = `<<f${order + 1}$${value}>>`;
  194. formula = formula.replace(ip, orderParam);
  195. }
  196. }
  197. return formula;
  198. };
  199. const exprStr2Expr = function(exprStr) {
  200. if (!exprStr) return '';
  201. let formula = exprStr;
  202. const orderParam = exprStr.match(calcRela.SerialReg);
  203. if (orderParam) {
  204. for (const op of orderParam) {
  205. const orderStr = op.match(calcRela.OrderReg)[0];
  206. const order = parseInt(orderStr.substring(1, op.length));
  207. const node = calcRela.tree.nodes[order - 1];
  208. const idParam = op.replace(orderStr, node.id);
  209. formula = formula.replace(op, idParam);
  210. }
  211. }
  212. return formula;
  213. };
  214. const setCalcType = function (calcType) {
  215. if (calcType === calcRela.calcType.zero) calcRela.getCalcNum = getCalcNumZero;
  216. if (calcType === calcRela.calcType.direct) calcRela.getCalcNum = getCalcNumDirect;
  217. if (calcType === calcRela.calcType.cache) calcRela.getCalcNum = getCalcNumCache;
  218. };
  219. return { init, checkExprValid, calcExpr, calcAllExpr, expr2ExprStr, exprStr2Expr, setCalcType, calcType: calcRela.calcType}
  220. })();