formulaArrayToString.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. let fcReg = /(FC\.\S+\()(.+)\)/;
  114. let fcText = '';
  115. for (let i = (children.length-1); i >= 0; i--) {
  116. let ele = children[i];
  117. if(ele.isOn === false){
  118. //点击了开关关闭的,跳过
  119. continue;
  120. }
  121. if (ele.template != null && ele.template != undefined) {
  122. let tmpArr = fcReg.exec(ele.template.ft);
  123. fcText = tmpArr[1] + fcText;//fc.XX( 左括号部分
  124. let argList = tmpArr[2].split(",");//括号里面参数部分#1,#2..
  125. let isNestEle = false;//是否有过嵌套的元素了
  126. let isNestEle2 = false;
  127. //console.log(argList)
  128. //console.log(text)
  129. argList.forEach((argText,index)=>{
  130. if(argText.indexOf('#')>-1){
  131. //#动态参数
  132. let argIndex = index;
  133. for (let j = 0; j < ele.template.args.length; j++) {
  134. if(ele.template.args[j].key == argText){
  135. //找到相同的#编码
  136. argIndex = j;
  137. break;
  138. }
  139. }
  140. let arg = ele.arguments[argIndex];
  141. if(arg && isFormulaItem(arg)){
  142. if(!isNestEle && !isNestEle2 && i != (children.length-1) && curEle.id == arg.id){
  143. //不是第一个嵌套的公式,且和当前挂载的是同一个元素
  144. //认为这个参数是之前公式计算的结果,不再写入元素
  145. isNestEle = true;
  146. }else{
  147. // let atbkey=arg.tableElementKey.replace('_key',':key')
  148. if(arg.type == 'Element'){
  149. eleMap[arg.tableElementKey] = {
  150. id: arg.id,
  151. name: arg.name,
  152. tableElementKey: arg.tableElementKey,
  153. type: "Element",
  154. };
  155. fcText += `E[${arg.tableElementKey}]`;
  156. }else if(arg.type == 'ParamData'){
  157. eleMap[arg.k] = {
  158. id: arg.id,
  159. name: arg.name,
  160. v:arg.v,
  161. k:arg.k,
  162. type: "ParamData",
  163. };
  164. fcText += `WP[${arg.k}]`;
  165. }
  166. }
  167. }else if(Array.isArray(arg)){
  168. //ifsles方法会进来
  169. arg.forEach((a)=>{
  170. if(a && isFormulaItem(a)){
  171. if(!isNestEle2 && i != (children.length-1) && curEle.id == a.id){
  172. //不是第一个嵌套的公式,且和当前挂载的是同一个元素
  173. //认为这个参数是之前公式计算的结果,不再写入元素
  174. isNestEle2 = true
  175. }else{
  176. if(a.type == 'Element'){
  177. eleMap[a.tableElementKey] = {
  178. id: a.id,
  179. name: a.name,
  180. tableElementKey: a.tableElementKey,
  181. type: "Element",
  182. };
  183. fcText += `E[${a.tableElementKey}]`;
  184. }else if(a.type == 'ParamData'){
  185. eleMap[a.k] = {
  186. id: a.id,
  187. name: a.name,
  188. v:a.v,
  189. k:a.k,
  190. type: "ParamData",
  191. };
  192. fcText += `WP[${a.k}]`;
  193. }
  194. }
  195. }else if(a && a.type == 'Operator'){
  196. //在运算符前后加上空格
  197. fcText += ' '+a.name+' ';
  198. //console.log(fcText)
  199. }else if(a && a.type){
  200. fcText += a.name;
  201. }else{
  202. fcText += a?a:"''";
  203. }
  204. })
  205. }else{
  206. fcText += arg?arg:"''";
  207. }
  208. }else{
  209. //已有参数
  210. fcText += argText?argText:'';
  211. }
  212. if(index != argList.length-1){
  213. fcText += ',';
  214. }
  215. })
  216. fcText += ')';
  217. }
  218. console.log(fcText,'ele.name')
  219. }
  220. return fcText;
  221. }
  222. export const formulaArrayToString = (processFormula,resultFormula) => {
  223. let text = '';
  224. let eleMap = {};//元素字典,为了回显的时候查询信息
  225. processFormula.forEach(item => {
  226. if(isFormulaItem(item)){
  227. //console.log(item)
  228. if(item.children.length <1){
  229. if(item.type == 'Element'){
  230. eleMap[item.tableElementKey] = {
  231. id: item.id,
  232. name: item.name,
  233. tableElementKey: item.tableElementKey,
  234. type: "Element",
  235. };
  236. text += `E[${item.tableElementKey}]`;
  237. }else if(item.type == 'ParamData'){
  238. eleMap[item.k] = {
  239. id: item.id,
  240. name: item.name,
  241. v:item.v,
  242. k:item.k,
  243. type: "ParamData",
  244. };
  245. text += `WP[${item.k}]`;
  246. }
  247. }else{
  248. text += transformArguments(item.children,item,eleMap);
  249. }
  250. }else if(item.type == 'Element'){
  251. text += item.template.ft;
  252. }else if(item.type == 'Operator'){
  253. //在运算符前后加上空格
  254. text += ' '+item.name+' ';
  255. //console.log(fcText)
  256. }else{
  257. text += item.name;
  258. }
  259. });
  260. if(resultFormula[0].children.length){
  261. //等号左侧部分
  262. let resText = transformArguments(resultFormula[0].children,resultFormula[0],eleMap);
  263. //等号左侧元素不需要,左侧的公式嵌套右侧所有结果
  264. text = resText.replace(`E[${resultFormula[0].tableElementKey}]`,text);
  265. }
  266. console.log(text,'最终');
  267. //console.log(eleMap)
  268. return {text,eleMap};
  269. }