formulaArrayToString.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. function isFormulaItem(ele){
  2. return ele.type == 'Element' || ele.type == 'ParamData'
  3. }
  4. //转换公式的参数
  5. // function transformArguments(children,curEle,eleMap){
  6. // let fcReg = /(FC\.\S+\()(.+)\)/;
  7. // let fcText = '';
  8. // for (let i = (children.length-1); i >= 0; i--) {
  9. // let ele = children[i];
  10. // if(ele.isOn === false){
  11. // //点击了开关关闭的,跳过
  12. // continue;
  13. // }
  14. // //console.log(ele.name)
  15. // let tmpArr = fcReg.exec(ele.template.ft);
  16. // fcText = tmpArr[1] + fcText;//fc.XX( 左括号部分
  17. // let argList = tmpArr[2].split(",");//括号里面参数部分#1,#2..
  18. // let isNestEle = false;//是否有过嵌套的元素了
  19. // let isNestEle2 = false;
  20. // //console.log(argList)
  21. // //console.log(text)
  22. // argList.forEach((argText,index)=>{
  23. // if(argText.indexOf('#')>-1){
  24. // //#动态参数
  25. // let argIndex = index;
  26. // for (let j = 0; j < ele.template.args.length; j++) {
  27. // if(ele.template.args[j].key == argText){
  28. // //找到相同的#编码
  29. // argIndex = j;
  30. // break;
  31. // }
  32. // }
  33. // let arg = ele.arguments[argIndex];
  34. // if(arg && isFormulaItem(arg)){
  35. // if(!isNestEle && !isNestEle2 && i != (children.length-1) && curEle.id == arg.id){
  36. // //不是第一个嵌套的公式,且和当前挂载的是同一个元素
  37. // //认为这个参数是之前公式计算的结果,不再写入元素
  38. // isNestEle = true;
  39. // }else{
  40. // if(arg.type == 'Element'){
  41. // eleMap[arg.tableElementKey] = {
  42. // id: arg.id,
  43. // name: arg.name,
  44. // tableElementKey: arg.tableElementKey,
  45. // type: "Element",
  46. // };
  47. // fcText += `E[${arg.tableElementKey}]`;
  48. // }else if(arg.type == 'ParamData'){
  49. // eleMap[arg.k] = {
  50. // id: arg.id,
  51. // name: arg.name,
  52. // v:arg.v,
  53. // k:arg.k,
  54. // type: "ParamData",
  55. // };
  56. // fcText += `WP[${arg.k}]`;
  57. // }
  58. // }
  59. // }else if(Array.isArray(arg)){
  60. // //ifsles方法会进来
  61. // arg.forEach((a)=>{
  62. // if(a && isFormulaItem(a)){
  63. // if(!isNestEle2 && i != (children.length-1) && curEle.id == a.id){
  64. // //不是第一个嵌套的公式,且和当前挂载的是同一个元素
  65. // //认为这个参数是之前公式计算的结果,不再写入元素
  66. // isNestEle2 = true
  67. // }else{
  68. // if(a.type == 'Element'){
  69. // eleMap[a.tableElementKey] = {
  70. // id: a.id,
  71. // name: a.name,
  72. // tableElementKey: a.tableElementKey,
  73. // type: "Element",
  74. // };
  75. // fcText += `E[${a.tableElementKey}]`;
  76. // }else if(a.type == 'ParamData'){
  77. // eleMap[a.k] = {
  78. // id: a.id,
  79. // name: a.name,
  80. // v:a.v,
  81. // k:a.k,
  82. // type: "ParamData",
  83. // };
  84. // fcText += `WP[${a.k}]`;
  85. // }
  86. // }
  87. // }else if(a && a.type == 'Operator'){
  88. // //在运算符前后加上空格
  89. // fcText += ' '+a.name+' ';
  90. // //console.log(fcText)
  91. // }else if(a && a.type){
  92. // fcText += a.name;
  93. // }else{
  94. // fcText += a?a:"''";
  95. // }
  96. // })
  97. // }else{
  98. // fcText += arg?arg:"''";
  99. // }
  100. // }else{
  101. // //已有参数
  102. // fcText += argText?argText:'';
  103. // }
  104. // if(index != argList.length-1){
  105. // fcText += ',';
  106. // }
  107. // })
  108. // fcText += ')';
  109. // }
  110. // return fcText;
  111. // }
  112. function transformArguments(children,curEle,eleMap){
  113. // debugger
  114. let fcReg = /(FC\.\S+\()(.+)\)/;
  115. let fcText = '';
  116. for (let i = (children.length-1); i >= 0; i--) {
  117. let ele = children[i];
  118. if(ele.isOn === false){
  119. //点击了开关关闭的,跳过
  120. continue;
  121. }
  122. if (ele.template != null && ele.template != undefined) {
  123. let tmpArr = fcReg.exec(ele.template.ft);
  124. fcText = tmpArr[1] + fcText;//fc.XX( 左括号部分
  125. let argList = tmpArr[2].split(",");//括号里面参数部分#1,#2..
  126. let isNestEle = false;//是否有过嵌套的元素了
  127. let isNestEle2 = false;
  128. //console.log(argList)
  129. //console.log(text)
  130. argList.forEach((argText,index)=>{
  131. if(argText.indexOf('#')>-1){
  132. //#动态参数
  133. let argIndex = index;
  134. for (let j = 0; j < ele.template.args.length; j++) {
  135. if(ele.template.args[j].key == argText){
  136. //找到相同的#编码
  137. argIndex = j;
  138. break;
  139. }
  140. }
  141. let arg = ele.arguments[argIndex];
  142. if(arg && isFormulaItem(arg)){
  143. if(!isNestEle && !isNestEle2 && i != (children.length-1) && curEle.id == arg.id){
  144. //不是第一个嵌套的公式,且和当前挂载的是同一个元素
  145. //认为这个参数是之前公式计算的结果,不再写入元素
  146. isNestEle = true;
  147. }else{
  148. // let atbkey=arg.tableElementKey.replace('_key',':key')
  149. if(arg.type == 'Element'){
  150. eleMap[arg.tableElementKey] = {
  151. id: arg.id,
  152. name: arg.name,
  153. tableElementKey: arg.tableElementKey,
  154. type: "Element",
  155. };
  156. fcText += `E[${arg.tableElementKey}]`;
  157. }else if(arg.type == 'ParamData'){
  158. eleMap[arg.k] = {
  159. id: arg.id,
  160. name: arg.name,
  161. v:arg.v,
  162. k:arg.k,
  163. type: "ParamData",
  164. };
  165. fcText += `WP[${arg.k}]`;
  166. }
  167. }
  168. }else if(Array.isArray(arg)){
  169. //ifsles方法会进来
  170. arg.forEach((a)=>{
  171. if(a && isFormulaItem(a)){
  172. if(!isNestEle2 && i != (children.length-1) && curEle.id == a.id){
  173. //不是第一个嵌套的公式,且和当前挂载的是同一个元素
  174. //认为这个参数是之前公式计算的结果,不再写入元素
  175. isNestEle2 = true
  176. }else{
  177. if(a.type == 'Element'){
  178. eleMap[a.tableElementKey] = {
  179. id: a.id,
  180. name: a.name,
  181. tableElementKey: a.tableElementKey,
  182. type: "Element",
  183. };
  184. fcText += `E[${a.tableElementKey}]`;
  185. }else if(a.type == 'ParamData'){
  186. eleMap[a.k] = {
  187. id: a.id,
  188. name: a.name,
  189. v:a.v,
  190. k:a.k,
  191. type: "ParamData",
  192. };
  193. fcText += `WP[${a.k}]`;
  194. }
  195. }
  196. }else if(a && a.type == 'Operator'){
  197. //在运算符前后加上空格
  198. fcText += ' '+a.name+' ';
  199. //console.log(fcText)
  200. }else if(a && a.type){
  201. fcText += a.name;
  202. }else{
  203. fcText += a?a:"''";
  204. }
  205. })
  206. }else{
  207. fcText += arg?arg:"''";
  208. }
  209. }else{
  210. //已有参数
  211. fcText += argText?argText:'';
  212. }
  213. if(index != argList.length-1){
  214. fcText += ',';
  215. }
  216. })
  217. fcText += ')';
  218. }
  219. console.log(fcText,'ele.name')
  220. }
  221. return fcText;
  222. }
  223. export const formulaArrayToString = (processFormula,resultFormula) => {
  224. let text = '';
  225. let eleMap = {};//元素字典,为了回显的时候查询信息
  226. let preIsOp=false;//前一个参数是否是四则运算(+-*/)
  227. processFormula.forEach((item,index) => {
  228. if(isFormulaItem(item)){
  229. //console.log(item)
  230. if(item.children.length <1){
  231. if(item.type == 'Element'){
  232. eleMap[item.tableElementKey] = {
  233. id: item.id,
  234. name: item.name,
  235. tableElementKey: item.tableElementKey,
  236. type: "Element",
  237. };
  238. text += `E[${item.tableElementKey}]`;
  239. }else if(item.type == 'ParamData'){
  240. eleMap[item.k] = {
  241. id: item.id,
  242. name: item.name,
  243. v:item.v,
  244. k:item.k,
  245. type: "ParamData",
  246. };
  247. text += `WP[${item.k}]`;
  248. }
  249. }else{
  250. text += (index>0&&!preIsOp?',':'')+transformArguments(item.children,item,eleMap);
  251. preIsOp=false;
  252. }
  253. }else if(item.type == 'Element'){
  254. text += item.template.ft;
  255. }else if(item.type == 'Operator'){
  256. //在运算符前后加上空格
  257. preIsOp=true;
  258. text += ' '+item.name+' ';
  259. //console.log(fcText)
  260. }else{
  261. text += item.name;
  262. }
  263. });
  264. if(resultFormula[0].children.length){
  265. //等号左侧部分
  266. let resText = transformArguments(resultFormula[0].children,resultFormula[0],eleMap);
  267. //等号左侧元素不需要,左侧的公式嵌套右侧所有结果
  268. let fcReg = /(FC\.\S+\()(.+)\)/;
  269. let leftArr=fcReg.exec(resText);
  270. //text = resText.replace(`E[${resultFormula[0].tableElementKey}]`,text);
  271. if(!!text&&!resText.includes(text)&&!!leftArr){
  272. let reg = leftArr[2].replace('OPTION,','');
  273. text = resText.replace(reg,text);
  274. }else{
  275. text = resText;
  276. }
  277. }
  278. console.log(text,'最终');
  279. //console.log(eleMap)
  280. return {text,eleMap};
  281. }