SysDicItemSet.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace NTMiner.Core.Impl {
  5. internal class SysDicItemSet : SetBase, ISysDicItemSet {
  6. private readonly IServerContext _context;
  7. private readonly Dictionary<Guid, Dictionary<string, SysDicItemData>> _dicByDicId
  8. = new Dictionary<Guid, Dictionary<string, SysDicItemData>>();
  9. private readonly Dictionary<Guid, SysDicItemData> _dicById = new Dictionary<Guid, SysDicItemData>();
  10. public SysDicItemSet(IServerContext context) {
  11. _context = context;
  12. _context.AddCmdPath<AddSysDicItemCommand>(LogEnum.DevConsole,
  13. action: (message) => {
  14. InitOnece();
  15. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  16. throw new ArgumentNullException();
  17. }
  18. if (string.IsNullOrEmpty(message.Input.Code)) {
  19. throw new ValidationException("dicitem code can't be null or empty");
  20. }
  21. if (_dicById.ContainsKey(message.Input.GetId())) {
  22. return;
  23. }
  24. if (!_dicByDicId.ContainsKey(message.Input.DicId)) {
  25. _dicByDicId.Add(message.Input.DicId, new Dictionary<string, SysDicItemData>(StringComparer.OrdinalIgnoreCase));
  26. }
  27. if (_dicByDicId[message.Input.DicId].ContainsKey(message.Input.Code)) {
  28. throw new ValidationException("编码重复");
  29. }
  30. SysDicItemData entity = new SysDicItemData().Update(message.Input);
  31. _dicById.Add(entity.Id, entity);
  32. _dicByDicId[message.Input.DicId].Add(entity.Code, entity);
  33. var repository = context.CreateCompositeRepository<SysDicItemData>();
  34. repository.Add(entity);
  35. VirtualRoot.RaiseEvent(new SysDicItemAddedEvent(message.MessageId, entity));
  36. }, location: this.GetType());
  37. _context.AddCmdPath<UpdateSysDicItemCommand>(LogEnum.DevConsole,
  38. action: (message) => {
  39. InitOnece();
  40. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  41. throw new ArgumentNullException();
  42. }
  43. if (string.IsNullOrEmpty(message.Input.Code)) {
  44. throw new ValidationException("sysDicItem code can't be null or empty");
  45. }
  46. if (!_dicById.TryGetValue(message.Input.GetId(), out SysDicItemData entity)) {
  47. return;
  48. }
  49. if (ReferenceEquals(entity, message.Input)) {
  50. return;
  51. }
  52. string oldCode = entity.Code;
  53. entity.Update(message.Input);
  54. // 如果编码变更了
  55. if (oldCode != entity.Code) {
  56. _dicByDicId[entity.DicId].Remove(oldCode);
  57. _dicByDicId[entity.DicId].Add(entity.Code, entity);
  58. }
  59. var repository = context.CreateCompositeRepository<SysDicItemData>();
  60. repository.Update(entity);
  61. VirtualRoot.RaiseEvent(new SysDicItemUpdatedEvent(message.MessageId, entity));
  62. }, location: this.GetType());
  63. _context.AddCmdPath<RemoveSysDicItemCommand>(LogEnum.DevConsole,
  64. action: (message) => {
  65. InitOnece();
  66. if (message == null || message.EntityId == Guid.Empty) {
  67. throw new ArgumentNullException();
  68. }
  69. if (!_dicById.TryGetValue(message.EntityId, out SysDicItemData entity)
  70. || !_context.SysDicSet.TryGetSysDic(entity.DicId, out ISysDic sysDic)) {
  71. return;
  72. }
  73. switch (sysDic.Code) {
  74. case NTKeyword.KernelBrandSysDicCode:
  75. if (NTMinerContext.Instance.ServerContext.KernelSet.AsEnumerable().Any(a => a.BrandId == message.EntityId)) {
  76. VirtualRoot.Out.ShowWarn("该内核品牌字典项关联有内核品牌不能删除,请先解除关联");
  77. return;
  78. }
  79. break;
  80. case NTKeyword.PoolBrandSysDicCode:
  81. if (NTMinerContext.Instance.ServerContext.PoolSet.AsEnumerable().Any(a => a.BrandId == message.EntityId)) {
  82. VirtualRoot.Out.ShowWarn("该矿池品牌字典项关联有矿池品牌不能删除,请先解除关联");
  83. return;
  84. }
  85. break;
  86. case NTKeyword.AlgoSysDicCode:
  87. if (NTMinerContext.Instance.ServerContext.PackageSet.AsEnumerable().Any(a => a.AlgoIds.Contains(message.EntityId))) {
  88. VirtualRoot.Out.ShowWarn("该算法字典项关联有内核不能删除,请先解除关联");
  89. return;
  90. }
  91. break;
  92. default:
  93. break;
  94. }
  95. _dicById.Remove(entity.Id);
  96. if (_dicByDicId.TryGetValue(entity.DicId, out Dictionary<string, SysDicItemData> dicItemDic)) {
  97. dicItemDic.Remove(entity.Code);
  98. }
  99. var repository = context.CreateCompositeRepository<SysDicItemData>();
  100. repository.Remove(entity.Id);
  101. VirtualRoot.RaiseEvent(new SysDicItemRemovedEvent(message.MessageId, entity));
  102. }, location: this.GetType());
  103. }
  104. protected override void Init() {
  105. var repository = _context.CreateCompositeRepository<SysDicItemData>();
  106. foreach (var item in repository.GetAll()) {
  107. if (!_dicById.ContainsKey(item.GetId())) {
  108. _dicById.Add(item.GetId(), item);
  109. }
  110. if (!_dicByDicId.ContainsKey(item.DicId)) {
  111. _dicByDicId.Add(item.DicId, new Dictionary<string, SysDicItemData>(StringComparer.OrdinalIgnoreCase));
  112. }
  113. if (!_dicByDicId[item.DicId].ContainsKey(item.Code)) {
  114. _dicByDicId[item.DicId].Add(item.Code, item);
  115. }
  116. }
  117. }
  118. public bool ContainsKey(Guid dicItemId) {
  119. InitOnece();
  120. return _dicById.ContainsKey(dicItemId);
  121. }
  122. public bool ContainsKey(Guid dicId, string dicItemCode) {
  123. InitOnece();
  124. if (!_dicByDicId.TryGetValue(dicId, out _)) {
  125. return false;
  126. }
  127. return _dicByDicId[dicId].ContainsKey(dicItemCode);
  128. }
  129. public bool ContainsKey(string dicCode, string dicItemCode) {
  130. InitOnece();
  131. if (!_context.SysDicSet.TryGetSysDic(dicCode, out ISysDic sysDic)) {
  132. return false;
  133. }
  134. if (!_dicByDicId.ContainsKey(sysDic.GetId())) {
  135. return false;
  136. }
  137. return _dicByDicId[sysDic.GetId()].ContainsKey(dicItemCode);
  138. }
  139. public bool TryGetDicItem(Guid dicItemId, out ISysDicItem dicItem) {
  140. InitOnece();
  141. var r = _dicById.TryGetValue(dicItemId, out SysDicItemData di);
  142. dicItem = di;
  143. return r;
  144. }
  145. public bool TryGetDicItem(string dicCode, string dicItemCode, out ISysDicItem dicItem) {
  146. InitOnece();
  147. if (!_context.SysDicSet.TryGetSysDic(dicCode, out ISysDic sysDic)) {
  148. dicItem = null;
  149. return false;
  150. }
  151. if (!_dicByDicId.TryGetValue(sysDic.GetId(), out Dictionary<string, SysDicItemData> items)) {
  152. dicItem = null;
  153. return false;
  154. }
  155. var r = items.TryGetValue(dicItemCode, out SysDicItemData di);
  156. dicItem = di;
  157. return r;
  158. }
  159. public bool TryGetDicItem(Guid dicId, string dicItemCode, out ISysDicItem dicItem) {
  160. InitOnece();
  161. if (!_dicByDicId.TryGetValue(dicId, out Dictionary<string, SysDicItemData> items)) {
  162. dicItem = null;
  163. return false;
  164. }
  165. var r = items.TryGetValue(dicItemCode, out SysDicItemData di);
  166. dicItem = di;
  167. return r;
  168. }
  169. public IEnumerable<ISysDicItem> GetSysDicItems(string dicCode) {
  170. InitOnece();
  171. if (!_context.SysDicSet.TryGetSysDic(dicCode, out ISysDic sysDic)) {
  172. return new List<ISysDicItem>();
  173. }
  174. return _dicByDicId[sysDic.GetId()].Values.ToList();
  175. }
  176. public IEnumerable<ISysDicItem> AsEnumerable() {
  177. InitOnece();
  178. return _dicById.Values;
  179. }
  180. }
  181. }