ActivityService.cs 3.7 KB

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