createProcessData.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { GAP_X, GAP_Y, START_Y, MIDDMLE_X } from "../config"
  2. //创建节点
  3. function createNode(raw, level = 0, index = 0, max = 0, _middle = -1) {
  4. let x = 0;
  5. const _index = index + 1;
  6. const y = START_Y + Number(level) * GAP_Y
  7. const middle = _middle >= 0 ? _middle + 1 : Math.ceil(max / 2);
  8. if (_index === middle || max === 0) x = MIDDMLE_X;
  9. if (_index > middle && max !== 0) x = MIDDMLE_X + (_index * GAP_X)
  10. if (_index < middle && max !== 0) x = MIDDMLE_X - (_index * GAP_X)
  11. return ({
  12. id: raw.order_process,
  13. text: raw.status_name,
  14. type: 'NormalTask',
  15. x,
  16. y,
  17. })
  18. }
  19. //创建开始节点
  20. function createStartNode(source) {
  21. const raw = source.find(({ action_type }) => action_type == '1');
  22. const start = createNode(raw);
  23. return {
  24. start,
  25. startRaw: raw
  26. }
  27. }
  28. //创建连线
  29. function createEdge(sourceNodeId, targetNodeId) {
  30. return {
  31. sourceNodeId,
  32. targetNodeId,
  33. isHitable: false,
  34. type: "dashe-edge"
  35. }
  36. }
  37. //去掉相同的审批记录
  38. function uniqueRecordData(source) {
  39. const data = [];
  40. source.forEach((item) => {
  41. const { order_process: order_process_current } = item;
  42. const itemIndex = data.findIndex(({order_process}) => order_process === order_process_current);
  43. if(itemIndex >= 0) data.splice(itemIndex)
  44. data.push(item)
  45. })
  46. return data;
  47. };
  48. //根据开始节点递归寻找生成其他节点和连线
  49. function createNodes(source, startNode) {
  50. const nodesRaw = [];
  51. const edgesRaw = [];
  52. function convert(raw, parent = "0", level = 0) {
  53. level++;
  54. const { next_actions } = raw;
  55. const next = source.filter(({ status_name }) => next_actions.includes(status_name));
  56. for (let i = 0; i < next.length; i++) {
  57. const index = i;
  58. const current = next[i]
  59. const maxLength = next.length;
  60. //根据主流程找到中间的节点
  61. const _middle = next.findIndex(({ is_master }) => is_master === '1');
  62. const edge = createEdge(parent, current.order_process);
  63. const node = createNode(current, level, index, maxLength, _middle);
  64. const prevSameNodeIndex = nodesRaw.findIndex(({ text }) => text === node.text);
  65. if (prevSameNodeIndex >= 0) nodesRaw.splice(prevSameNodeIndex, 1);
  66. nodesRaw.push(node);
  67. edgesRaw.push(edge);
  68. if (current.next_actions.length > 0) convert(current, current.order_process, level);
  69. }
  70. }
  71. convert(startNode, startNode.order_process);
  72. return ({ nodesRaw, edgesRaw })
  73. }
  74. //根据审批记录改变连线和节点类型
  75. function createNodesWithRecord(nodesRaw, edgesRaw, record) {
  76. for (let i = 0; i < record.length; i++) {
  77. const current = record[i];
  78. const next = record[i + 1];
  79. const isLast = i === record.length - 1;
  80. const sourceNodeIndex = nodesRaw.findIndex(({ id }) => String(id) === String(current.order_process));
  81. if (sourceNodeIndex >= 0) {
  82. nodesRaw[sourceNodeIndex].type = isLast ? 'ProcessTask' : 'StartTask';
  83. nodesRaw[sourceNodeIndex].properties = {
  84. name: current.action_name,
  85. time: current.addtime
  86. }
  87. }
  88. const sourceEdgeIndex = edgesRaw.findIndex(({ sourceNodeId, targetNodeId }) => {
  89. return String(sourceNodeId) === String(current?.order_process) && String(targetNodeId) === String(next?.order_process);
  90. })
  91. if (edgesRaw.length === 1 && record.length === 3) {
  92. edgesRaw[0].type = 'polyline'
  93. }
  94. if (sourceEdgeIndex >= 0) edgesRaw[sourceEdgeIndex].type = 'polyline';
  95. }
  96. return {
  97. edges: edgesRaw,
  98. nodes: nodesRaw
  99. };
  100. }
  101. function generateTargetStartNode(soruceStartNode){
  102. const startNode = {
  103. action_type : "1",
  104. addtime : "",
  105. creater: "",
  106. id: "-1",
  107. is_master : "1",
  108. next_action_ids: "",
  109. next_actions : [soruceStartNode.status_name],
  110. operation_type : "2",
  111. order_process : "-1",
  112. remark : "",
  113. status : "-1",
  114. status_name : "创建",
  115. }
  116. return startNode
  117. }
  118. export function createProcessData(_source, record) {
  119. console.log(record)
  120. record = uniqueRecordData(record)
  121. const processChart = { nodes: [], edges: [] }
  122. const sourceStartIndex = _source.findIndex(({ action_type }) => action_type == '1')
  123. const source = [generateTargetStartNode(_source[sourceStartIndex]),..._source]
  124. const { start, startRaw } = createStartNode(source);
  125. const { nodesRaw: _nodesRaw, edgesRaw } = createNodes(source, startRaw);
  126. const nodesRaw = [start, ..._nodesRaw];
  127. const { nodes, edges } = createNodesWithRecord(nodesRaw, edgesRaw, record)
  128. processChart.nodes.push(...nodes);
  129. processChart.edges.push(...edges);
  130. return processChart
  131. }