AbilityService.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using TEAMModelOS.SDK.Extension;
  4. using TEAMModelOS.SDK.Models;
  5. using TEAMModelOS.SDK.Models.Cosmos;
  6. namespace TEAMModelOS.SDK.Services
  7. {
  8. public class AbilityService
  9. {
  10. public static List<Tnode> TreeToList(List<AbilityTaskTree> trees, List<Tnode> nodes, long now)
  11. {
  12. int index = 0;
  13. foreach (AbilityTaskTree tr in trees)
  14. {
  15. tr.order = index;
  16. index++;
  17. }
  18. trees = trees.OrderBy(x => x.order).ToList();
  19. List<Tnode> list = new List<Tnode>();
  20. //var list = trees.ToJsonString().ToObject<List<Tnode>>();
  21. trees.ForEach(x => {
  22. List<string> cids = new List<string>();
  23. if (x.children.IsNotEmpty())
  24. {
  25. x.children.ForEach(y => cids.Add(y.id));
  26. }
  27. var node = new Tnode
  28. {
  29. title = x.title,
  30. id = x.id,
  31. pid = x.pid,
  32. order = x.order,
  33. rnodes = x.rnodes,
  34. cids = cids,
  35. creatorId = x.creatorId,
  36. creatorName = x.creatorName,
  37. updateTime = now
  38. };
  39. list.Add(node);
  40. });
  41. nodes.AddRange(list);
  42. foreach (AbilityTaskTree tree in trees)
  43. {
  44. if (null != tree.children && tree.children.Count > 0)
  45. {
  46. TreeToList(tree.children, nodes, now);
  47. }
  48. }
  49. return nodes;
  50. }
  51. public static List<AbilityTaskTree> ListToTree(List<Tnode> noes)
  52. {
  53. List<AbilityTaskTree> list = noes.ToJsonString().ToObject<List<AbilityTaskTree>>();
  54. //var lookup = list.ToDictionary(n => n.RowKey, n => n);
  55. var res = from r in list group r by r.id into g select g;
  56. Dictionary<string, AbilityTaskTree> blockDict = new Dictionary<string, AbilityTaskTree>();
  57. foreach (var s in res)
  58. {
  59. blockDict.TryAdd(s.First().id, s.First());
  60. }
  61. return GetChild(list, blockDict);
  62. }
  63. private static List<AbilityTaskTree> GetChild(List<AbilityTaskTree> list, Dictionary<string, AbilityTaskTree> dict)
  64. {
  65. // list = list.OrderBy(m => m.Order).ToList();
  66. List<AbilityTaskTree> trees = new List<AbilityTaskTree>();
  67. trees = trees.OrderBy(x => x.order).ToList();
  68. foreach (AbilityTaskTree node in list)
  69. {
  70. bool flag = dict.TryGetValue(node.pid, out AbilityTaskTree ability);
  71. if (flag && ability != null)
  72. {
  73. ability.children.Add(node);
  74. }
  75. else
  76. {
  77. trees.Add(node);
  78. }
  79. }
  80. return trees;
  81. }
  82. }
  83. }