AbilityService.cs 3.1 KB

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