weapp_contract_catalog.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. // 合同目录与 Web 端使用相同的节点关系;数量只统计当前用户可见的合同。
  3. function buildCatalog(folders, contracts) {
  4. const folderById = new Map();
  5. const nodes = folders.map(folder => {
  6. const node = {
  7. id: String(folder.contract_id),
  8. parentId: String(folder.contract_pid),
  9. code: folder.code || '',
  10. name: folder.name || '',
  11. order: folder.order || 0,
  12. kind: 'folder',
  13. count: 0,
  14. };
  15. folderById.set(node.id, node);
  16. return node;
  17. });
  18. const seenContracts = new Set();
  19. for (const contract of contracts) {
  20. const contractId = String(contract.contract_id);
  21. if (seenContracts.has(contractId)) continue;
  22. const parentId = String(contract.contract_pid);
  23. // 与 Web 端树结构一致,不展示无法挂接到目录的合同。
  24. if (!folderById.has(parentId)) continue;
  25. seenContracts.add(contractId);
  26. nodes.push({
  27. id: contractId,
  28. rowId: String(contract.id),
  29. parentId,
  30. code: contract.c_code || '',
  31. name: contract.name || '',
  32. order: contract.order || 0,
  33. kind: 'contract',
  34. fileCount: contract.fileCount || 0,
  35. });
  36. const visited = new Set();
  37. let currentId = parentId;
  38. while (folderById.has(currentId) && !visited.has(currentId)) {
  39. visited.add(currentId);
  40. const folder = folderById.get(currentId);
  41. folder.count++;
  42. currentId = folder.parentId;
  43. }
  44. }
  45. return nodes;
  46. }
  47. module.exports = { buildCatalog };