GroupSet.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. namespace NTMiner.Core.Impl {
  4. public class GroupSet : IGroupSet {
  5. private readonly Dictionary<Guid, GroupData> _dicById = new Dictionary<Guid, GroupData>();
  6. public GroupSet(IServerContext context) {
  7. context.BuildCmdPath<AddGroupCommand>("添加组", LogEnum.DevConsole,
  8. action: (message) => {
  9. InitOnece();
  10. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  11. throw new ArgumentNullException();
  12. }
  13. if (_dicById.ContainsKey(message.Input.GetId())) {
  14. return;
  15. }
  16. if (string.IsNullOrEmpty(message.Input.Name)) {
  17. throw new ValidationException("Group name can't be null or empty");
  18. }
  19. GroupData entity = new GroupData().Update(message.Input);
  20. _dicById.Add(entity.Id, entity);
  21. var repository = NTMinerRoot.CreateServerRepository<GroupData>();
  22. repository.Add(entity);
  23. VirtualRoot.RaiseEvent(new GroupAddedEvent(entity));
  24. });
  25. context.BuildCmdPath<UpdateGroupCommand>("更新组", LogEnum.DevConsole,
  26. action: (message) => {
  27. InitOnece();
  28. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  29. throw new ArgumentNullException();
  30. }
  31. if (string.IsNullOrEmpty(message.Input.Name)) {
  32. throw new ValidationException("Group name can't be null or empty");
  33. }
  34. if (!_dicById.ContainsKey(message.Input.GetId())) {
  35. return;
  36. }
  37. GroupData entity = _dicById[message.Input.GetId()];
  38. if (ReferenceEquals(entity, message.Input)) {
  39. return;
  40. }
  41. entity.Update(message.Input);
  42. var repository = NTMinerRoot.CreateServerRepository<GroupData>();
  43. repository.Update(entity);
  44. VirtualRoot.RaiseEvent(new GroupUpdatedEvent(entity));
  45. });
  46. context.BuildCmdPath<RemoveGroupCommand>("移除组", LogEnum.DevConsole,
  47. action: (message) => {
  48. InitOnece();
  49. if (message == null || message.EntityId == Guid.Empty) {
  50. throw new ArgumentNullException();
  51. }
  52. if (!_dicById.ContainsKey(message.EntityId)) {
  53. return;
  54. }
  55. GroupData entity = _dicById[message.EntityId];
  56. Guid[] toRemoves = context.CoinGroupSet.GetGroupCoinIds(entity.Id).ToArray();
  57. foreach (var id in toRemoves) {
  58. VirtualRoot.Execute(new RemoveCoinGroupCommand(id));
  59. }
  60. _dicById.Remove(entity.GetId());
  61. var repository = NTMinerRoot.CreateServerRepository<GroupData>();
  62. repository.Remove(message.EntityId);
  63. VirtualRoot.RaiseEvent(new GroupRemovedEvent(entity));
  64. });
  65. }
  66. private bool _isInited = false;
  67. private readonly object _locker = new object();
  68. private void InitOnece() {
  69. if (_isInited) {
  70. return;
  71. }
  72. Init();
  73. }
  74. private void Init() {
  75. lock (_locker) {
  76. if (!_isInited) {
  77. var repository = NTMinerRoot.CreateServerRepository<GroupData>();
  78. foreach (var item in repository.GetAll()) {
  79. if (!_dicById.ContainsKey(item.GetId())) {
  80. _dicById.Add(item.GetId(), item);
  81. }
  82. }
  83. _isInited = true;
  84. }
  85. }
  86. }
  87. public int Count {
  88. get {
  89. InitOnece();
  90. return _dicById.Count;
  91. }
  92. }
  93. public bool Contains(Guid groupId) {
  94. InitOnece();
  95. return _dicById.ContainsKey(groupId);
  96. }
  97. public bool TryGetGroup(Guid groupId, out IGroup group) {
  98. InitOnece();
  99. GroupData g;
  100. bool r = _dicById.TryGetValue(groupId, out g);
  101. group = g;
  102. return r;
  103. }
  104. public IEnumerable<IGroup> AsEnumerable() {
  105. InitOnece();
  106. return _dicById.Values;
  107. }
  108. }
  109. }