ActivityService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Azure.Storage.Blobs.Models;
  2. using HTEXLib.COMM.Helpers;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Extension;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.Services;
  12. namespace TEAMModelOS.SDK
  13. {
  14. public static class ActivityService
  15. {
  16. public static async Task<ReviewRule> UpsertReviewRule(ReviewRuleTree reviewRuleTree,Activity activity,AzureCosmosFactory _azureCosmos)
  17. {
  18. var nodes = new List<RuleConfig>();
  19. nodes= TreeToList(reviewRuleTree.trees, nodes);
  20. ReviewRule reviewRule = new ReviewRule() {
  21. id= activity.id,
  22. code="ReviewRule-disposable",
  23. pk="ReviewRule",
  24. name=reviewRuleTree.name,
  25. desc=reviewRuleTree.desc,
  26. owner=activity.owner,
  27. type="disposable",
  28. configs=nodes,
  29. sourceName=activity.name
  30. };
  31. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS,Constant.Normal).UpsertItemAsync(reviewRule,new Azure.Cosmos.PartitionKey(reviewRule.code));
  32. if (reviewRuleTree.upsertAsTemplate==1) {
  33. reviewRule.code="ReviewRule-template";
  34. reviewRule.type="template";
  35. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).UpsertItemAsync(reviewRule,new Azure.Cosmos.PartitionKey(reviewRule.code));
  36. }
  37. return reviewRule;
  38. }
  39. public static List<RuleConfig> TreeToList(List<RuleConfigTree> trees, List<RuleConfig> nodes) {
  40. trees = trees.OrderBy(x => x.order).ToList();
  41. List<RuleConfig> list = new List<RuleConfig>();
  42. trees.ForEach(x => {
  43. List<string> cids = new List<string>();
  44. if (x.children.IsNotEmpty())
  45. {
  46. x.children.ForEach(y => cids.Add(y.id));
  47. }
  48. var node = new RuleConfig
  49. {
  50. id = x.id,
  51. pid = x.pid,
  52. cids= cids,
  53. label = x.label,
  54. desc = x.desc,
  55. score = x.score,
  56. order = x.order,
  57. };
  58. list.Add(node);
  59. });
  60. nodes.AddRange(list);
  61. foreach (RuleConfigTree tree in trees)
  62. {
  63. if (null != tree.children && tree.children.Count > 0)
  64. {
  65. TreeToList(tree.children, nodes);
  66. }
  67. }
  68. return nodes;
  69. }
  70. public static List<RuleConfigTree> ListToTree(List<RuleConfig> noes)
  71. {
  72. List<RuleConfigTree> list = noes.ToJsonString().ToObject<List<RuleConfigTree>>();
  73. var res = from r in list group r by r.id into g select g;
  74. Dictionary<string, RuleConfigTree> blockDict = new Dictionary<string, RuleConfigTree>();
  75. foreach (var s in res)
  76. {
  77. blockDict.TryAdd(s.First().id, s.First());
  78. }
  79. return GetChild(list, blockDict);
  80. }
  81. private static List<RuleConfigTree> GetChild(List<RuleConfigTree> list, Dictionary<string, RuleConfigTree> dict)
  82. {
  83. List<RuleConfigTree> trees = new List<RuleConfigTree>();
  84. trees = trees.OrderBy(x => x.order).ToList();
  85. foreach (RuleConfigTree node in list)
  86. {
  87. bool flag = dict.TryGetValue(node.pid, out RuleConfigTree syllabus);
  88. if (flag && syllabus != null)
  89. {
  90. syllabus.children.Add(node);
  91. }
  92. else
  93. {
  94. trees.Add(node);
  95. }
  96. }
  97. return trees;
  98. }
  99. }
  100. }