use-method.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {
  2. SceneListType,
  3. GetListWorkshop,
  4. WorkspaceAddDatas,
  5. LabelModuleListType,
  6. } from '@/api/scene/sceneOperate';
  7. import { WorkShopTempleteType } from '@/api/scene/secene-templet';
  8. export const colomns = [
  9. { label: '场景名称', prop: 'name', width: 300 },
  10. { label: '场景标签', prop: 'labelName', width: 300 },
  11. { label: '代码', prop: 'code' },
  12. ];
  13. interface DataSourceUser
  14. extends SceneListType<
  15. GetListWorkshop<WorkspaceAddDatas, WorkShopTempleteType>,
  16. LabelModuleListType
  17. > {
  18. parent?: SceneListType<
  19. GetListWorkshop<WorkspaceAddDatas, WorkShopTempleteType>,
  20. LabelModuleListType
  21. > | null;
  22. }
  23. export const dataSourceWithParent = (d: DataSourceUser[], parent: DataSourceUser | null) => {
  24. d.forEach((item, _index) => {
  25. item.parent = parent;
  26. if (item.children && item.children?.length > 0) {
  27. dataSourceWithParent(item.children, item);
  28. }
  29. });
  30. return d;
  31. };
  32. export function removeParent(data) {
  33. return data.map((item) => {
  34. // 删除 tag 属性
  35. delete item.parent;
  36. // 递归处理子项
  37. if (item.children && item.children.length > 0) {
  38. item.children = removeParent(item.children);
  39. }
  40. return item;
  41. });
  42. }
  43. //找到各个层级
  44. // export function findIndexByItem(data, targetItem, path = []) {
  45. // for (let i = 0; i < data.length; i++) {
  46. // const currentPath = path.concat(i);
  47. // if (data[i].id === targetItem.id && data[i].name === targetItem.name) {
  48. // return currentPath;
  49. // }
  50. // if (data[i].children && data[i].children.length > 0) {
  51. // const childResult = findIndexByItem(data[i].children, targetItem, currentPath);
  52. // if (childResult.length > 0) {
  53. // return childResult;
  54. // }
  55. // }
  56. // }
  57. // return [];
  58. // }
  59. //用于重新修改serial
  60. export const updateSerials = (data) => {
  61. for (let i = 0; i < data.length; i++) {
  62. data[i].serial = i;
  63. // if (currentItem.id === targetId) {
  64. // if (!currentItem.children) {
  65. // currentItem.children = [];
  66. // }
  67. // currentItem.children.push(newAdd);
  68. // return true; // 表示已经找到并修改
  69. // }
  70. // 如果有子项,递归查找
  71. if (data[i].children && data[i].children.length > 0) {
  72. updateSerials(data[i].children);
  73. // if (found) {
  74. // return true; // 如果在子项中找到目标项,停止继续查找
  75. // }
  76. }
  77. }
  78. return data;
  79. // return false; // 表示未找到目标项
  80. };
  81. //用于新增数据
  82. export const updateData = (data, targetId, newAdd) => {
  83. for (let i = 0; i < data.length; i++) {
  84. const currentItem = data[i];
  85. if (currentItem.id === targetId) {
  86. if (!currentItem.children) {
  87. currentItem.children = [];
  88. }
  89. currentItem.children.push(newAdd);
  90. return true; // 表示已经找到并修改
  91. }
  92. // 如果有子项,递归查找
  93. if (currentItem.children && currentItem.children.length > 0) {
  94. const found = updateData(currentItem.children, targetId, newAdd);
  95. if (found) {
  96. return true; // 如果在子项中找到目标项,停止继续查找
  97. }
  98. }
  99. }
  100. return false; // 表示未找到目标项
  101. };
  102. //判断该条数据的层级
  103. export const findItemLevel = (data, targetId, targetName, currentLevel = 0) => {
  104. for (let i = 0; i < data.length; i++) {
  105. const item = data[i];
  106. if (item.id === targetId && item.name === targetName) {
  107. return currentLevel;
  108. }
  109. if (item.children && item.children.length > 0) {
  110. const childLevel = findItemLevel(item.children, targetId, targetName, currentLevel + 1);
  111. if (childLevel !== -1) {
  112. return childLevel;
  113. }
  114. }
  115. }
  116. return -1;
  117. };
  118. //删除行
  119. export const deleteTableRow = (dataSource, targetId) => {
  120. const deleteRecursive = (data) => {
  121. for (let i = 0; i < data.length; i++) {
  122. const currentItem = data[i];
  123. if (currentItem.id === targetId) {
  124. // 删除当前项
  125. data.splice(i, 1);
  126. return true;
  127. }
  128. // 如果有子项,递归查找
  129. if (currentItem.children && currentItem.children.length > 0) {
  130. const found = deleteRecursive(currentItem.children);
  131. if (found) {
  132. return true; // 如果在子项中找到目标项,停止继续查找
  133. }
  134. }
  135. }
  136. return false; // 表示未找到目标项
  137. };
  138. // 从顶层开始递归删除
  139. deleteRecursive(dataSource);
  140. };
  141. //行的编辑提交功能
  142. export const editTableRow = (
  143. dataSource,
  144. targetId,
  145. level,
  146. name,
  147. newName,
  148. newCode,
  149. newStatus,
  150. newLeader = '',
  151. newTemplete = '',
  152. newTag = 0,
  153. newToptag = [] as unknown as string[],
  154. ) => {
  155. const editTRecursive = (data) => {
  156. for (let i = 0; i < data.length; i++) {
  157. const currentItem = data[i];
  158. if (currentItem.id === targetId) {
  159. // 修改
  160. currentItem.name = newName;
  161. currentItem.code = newCode;
  162. currentItem.status = newStatus;
  163. if (level === 0) {
  164. currentItem.Toptag = newToptag;
  165. } else if (level === 1) {
  166. currentItem.companyLabelId = newTag;
  167. currentItem.templete = newTemplete;
  168. } else if (level === 2) {
  169. currentItem.leader = newLeader;
  170. }
  171. return true;
  172. }
  173. // 如果有子项,递归查找
  174. if (currentItem.children && currentItem.children.length > 0) {
  175. const found = editTRecursive(currentItem.children);
  176. if (found) {
  177. return true; // 如果在子项中找到目标项,停止继续查找
  178. }
  179. }
  180. }
  181. return false; // 表示未找到目标项
  182. };
  183. editTRecursive(dataSource);
  184. };
  185. //找出数据中的全部code
  186. export const flattenCodes = (data) => {
  187. const codes: string[] = [];
  188. const traverse = (node: { code: string; children: any[] }) => {
  189. codes.push(node.code);
  190. if (node.children && node.children.length > 0) {
  191. node.children.forEach(traverse);
  192. }
  193. };
  194. data.forEach(traverse);
  195. return codes;
  196. };
  197. // export enum ENABLED {
  198. // FALSE = 1,
  199. // TRUE = 0,
  200. // }