ActivityService.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. desc=reviewRuleTree.desc,
  24. owner=activity.owner,
  25. type="disposable",
  26. configs=nodes,
  27. sourceName=activity.name
  28. };
  29. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS,Constant.Normal).UpsertItemAsync(reviewRule);
  30. if (reviewRuleTree.upsertAsTemplate==1) {
  31. reviewRule.code="ReviewRule-template";
  32. reviewRule.type="template";
  33. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal).UpsertItemAsync(reviewRule);
  34. }
  35. return reviewRule;
  36. }
  37. public static List<RuleConfig> TreeToList(List<RuleConfigTree> trees, List<RuleConfig> nodes) {
  38. trees = trees.OrderBy(x => x.order).ToList();
  39. List<RuleConfig> list = new List<RuleConfig>();
  40. trees.ForEach(x => {
  41. List<string> cids = new List<string>();
  42. if (x.children.IsNotEmpty())
  43. {
  44. x.children.ForEach(y => cids.Add(y.id));
  45. }
  46. var node = new RuleConfig
  47. {
  48. id = x.id,
  49. pid = x.pid,
  50. cids= cids,
  51. label = x.label,
  52. score = x.score,
  53. order = x.order,
  54. };
  55. list.Add(node);
  56. });
  57. nodes.AddRange(list);
  58. foreach (RuleConfigTree tree in trees)
  59. {
  60. if (null != tree.children && tree.children.Count > 0)
  61. {
  62. TreeToList(tree.children, nodes);
  63. }
  64. }
  65. return nodes;
  66. }
  67. public static List<RuleConfigTree> ListToTree(List<RuleConfig> noes)
  68. {
  69. List<RuleConfigTree> list = noes.ToJsonString().ToObject<List<RuleConfigTree>>();
  70. var res = from r in list group r by r.id into g select g;
  71. Dictionary<string, RuleConfigTree> blockDict = new Dictionary<string, RuleConfigTree>();
  72. foreach (var s in res)
  73. {
  74. blockDict.TryAdd(s.First().id, s.First());
  75. }
  76. return GetChild(list, blockDict);
  77. }
  78. private static List<RuleConfigTree> GetChild(List<RuleConfigTree> list, Dictionary<string, RuleConfigTree> dict)
  79. {
  80. List<RuleConfigTree> trees = new List<RuleConfigTree>();
  81. trees = trees.OrderBy(x => x.order).ToList();
  82. foreach (RuleConfigTree node in list)
  83. {
  84. bool flag = dict.TryGetValue(node.pid, out RuleConfigTree syllabus);
  85. if (flag && syllabus != null)
  86. {
  87. syllabus.children.Add(node);
  88. }
  89. else
  90. {
  91. trees.Add(node);
  92. }
  93. }
  94. return trees;
  95. }
  96. }
  97. }