ColumnsShowSet.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using NTMiner.MinerServer;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace NTMiner.Core.MinerServer.Impl {
  5. public class ColumnsShowSet : IColumnsShowSet {
  6. private readonly Dictionary<Guid, ColumnsShowData> _dicById = new Dictionary<Guid, ColumnsShowData>();
  7. public ColumnsShowSet() {
  8. VirtualRoot.BuildCmdPath<AddColumnsShowCommand>(action: (message) => {
  9. InitOnece();
  10. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty || message.Input.GetId() == ColumnsShowData.PleaseSelect.Id) {
  11. throw new ArgumentNullException();
  12. }
  13. if (_dicById.ContainsKey(message.Input.GetId())) {
  14. return;
  15. }
  16. ColumnsShowData entity = new ColumnsShowData().Update(message.Input);
  17. Server.ColumnsShowService.AddOrUpdateColumnsShowAsync(entity, (response, exception) => {
  18. if (response.IsSuccess()) {
  19. _dicById.Add(entity.Id, entity);
  20. VirtualRoot.RaiseEvent(new ColumnsShowAddedEvent(message.Id, entity));
  21. }
  22. else {
  23. Write.UserFail(response.ReadMessage(exception));
  24. }
  25. });
  26. });
  27. VirtualRoot.BuildCmdPath<UpdateColumnsShowCommand>(action: (message) => {
  28. InitOnece();
  29. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  30. throw new ArgumentNullException();
  31. }
  32. if (!_dicById.ContainsKey(message.Input.GetId())) {
  33. return;
  34. }
  35. ColumnsShowData entity = _dicById[message.Input.GetId()];
  36. ColumnsShowData oldValue = new ColumnsShowData().Update(entity);
  37. entity.Update(message.Input);
  38. Server.ColumnsShowService.AddOrUpdateColumnsShowAsync(entity, (response, exception) => {
  39. if (!response.IsSuccess()) {
  40. entity.Update(oldValue);
  41. VirtualRoot.RaiseEvent(new ColumnsShowUpdatedEvent(message.Id, entity));
  42. Write.UserFail(response.ReadMessage(exception));
  43. }
  44. });
  45. VirtualRoot.RaiseEvent(new ColumnsShowUpdatedEvent(message.Id, entity));
  46. });
  47. VirtualRoot.BuildCmdPath<RemoveColumnsShowCommand>(action: (message) => {
  48. InitOnece();
  49. if (message == null || message.EntityId == Guid.Empty || message.EntityId == ColumnsShowData.PleaseSelect.Id) {
  50. throw new ArgumentNullException();
  51. }
  52. if (!_dicById.ContainsKey(message.EntityId)) {
  53. return;
  54. }
  55. ColumnsShowData entity = _dicById[message.EntityId];
  56. Server.ColumnsShowService.RemoveColumnsShowAsync(entity.Id, (response, exception) => {
  57. if (response.IsSuccess()) {
  58. _dicById.Remove(entity.Id);
  59. VirtualRoot.RaiseEvent(new ColumnsShowRemovedEvent(message.Id, entity));
  60. }
  61. else {
  62. Write.UserFail(response.ReadMessage(exception));
  63. }
  64. });
  65. });
  66. }
  67. private bool _isInited = false;
  68. private readonly object _locker = new object();
  69. private void InitOnece() {
  70. if (_isInited) {
  71. return;
  72. }
  73. Init();
  74. }
  75. private void Init() {
  76. if (!_isInited) {
  77. lock (_locker) {
  78. if (!_isInited) {
  79. var result = Server.ColumnsShowService.GetColumnsShows();
  80. foreach (var item in result) {
  81. if (!_dicById.ContainsKey(item.GetId())) {
  82. _dicById.Add(item.GetId(), item);
  83. }
  84. }
  85. if (!_dicById.ContainsKey(ColumnsShowData.PleaseSelect.Id)) {
  86. _dicById.Add(ColumnsShowData.PleaseSelect.Id, ColumnsShowData.PleaseSelect);
  87. Server.ColumnsShowService.AddOrUpdateColumnsShowAsync(ColumnsShowData.PleaseSelect, (response, exception) => {
  88. if (!response.IsSuccess()) {
  89. Logger.ErrorDebugLine("AddOrUpdateColumnsShowAsync " + response.ReadMessage(exception));
  90. }
  91. });
  92. }
  93. _isInited = true;
  94. }
  95. }
  96. }
  97. }
  98. public bool Contains(Guid columnsShowId) {
  99. InitOnece();
  100. return _dicById.ContainsKey(columnsShowId);
  101. }
  102. public bool TryGetColumnsShow(Guid columnsShowId, out IColumnsShow columnsShow) {
  103. InitOnece();
  104. var r = _dicById.TryGetValue(columnsShowId, out ColumnsShowData g);
  105. columnsShow = g;
  106. return r;
  107. }
  108. public IEnumerable<IColumnsShow> AsEnumerable() {
  109. InitOnece();
  110. return _dicById.Values;
  111. }
  112. }
  113. }