123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Collections.Generic;
- namespace NTMiner.Core.Impl {
- public class GroupSet : SetBase, IGroupSet {
- private readonly Dictionary<Guid, GroupData> _dicById = new Dictionary<Guid, GroupData>();
- private readonly IServerContext _context;
- public GroupSet(IServerContext context) {
- _context = context;
- context.AddCmdPath<AddGroupCommand>("添加组", LogEnum.DevConsole,
- action: (message) => {
- InitOnece();
- if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
- throw new ArgumentNullException();
- }
- if (_dicById.ContainsKey(message.Input.GetId())) {
- return;
- }
- if (string.IsNullOrEmpty(message.Input.Name)) {
- throw new ValidationException("Group name can't be null or empty");
- }
- GroupData entity = new GroupData().Update(message.Input);
- _dicById.Add(entity.Id, entity);
- var repository = context.CreateServerRepository<GroupData>();
- repository.Add(entity);
- VirtualRoot.RaiseEvent(new GroupAddedEvent(message.MessageId, entity));
- }, location: this.GetType());
- context.AddCmdPath<UpdateGroupCommand>("更新组", LogEnum.DevConsole,
- action: (message) => {
- InitOnece();
- if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
- throw new ArgumentNullException();
- }
- if (string.IsNullOrEmpty(message.Input.Name)) {
- throw new ValidationException("Group name can't be null or empty");
- }
- if (!_dicById.TryGetValue(message.Input.GetId(), out GroupData entity)) {
- return;
- }
- if (ReferenceEquals(entity, message.Input)) {
- return;
- }
- entity.Update(message.Input);
- var repository = context.CreateServerRepository<GroupData>();
- repository.Update(entity);
- VirtualRoot.RaiseEvent(new GroupUpdatedEvent(message.MessageId, entity));
- }, location: this.GetType());
- context.AddCmdPath<RemoveGroupCommand>("移除组", LogEnum.DevConsole,
- action: (message) => {
- InitOnece();
- if (message == null || message.EntityId == Guid.Empty) {
- throw new ArgumentNullException();
- }
- if (!_dicById.ContainsKey(message.EntityId)) {
- return;
- }
- GroupData entity = _dicById[message.EntityId];
- Guid[] toRemoves = context.CoinGroupSet.GetGroupCoinIds(entity.Id).ToArray();
- foreach (var id in toRemoves) {
- VirtualRoot.Execute(new RemoveCoinGroupCommand(id));
- }
- _dicById.Remove(entity.GetId());
- var repository = context.CreateServerRepository<GroupData>();
- repository.Remove(message.EntityId);
- VirtualRoot.RaiseEvent(new GroupRemovedEvent(message.MessageId, entity));
- }, location: this.GetType());
- }
- protected override void Init() {
- var repository = _context.CreateServerRepository<GroupData>();
- foreach (var item in repository.GetAll()) {
- if (!_dicById.ContainsKey(item.GetId())) {
- _dicById.Add(item.GetId(), item);
- }
- }
- }
- public int Count {
- get {
- InitOnece();
- return _dicById.Count;
- }
- }
- public bool Contains(Guid groupId) {
- InitOnece();
- return _dicById.ContainsKey(groupId);
- }
- public bool TryGetGroup(Guid groupId, out IGroup group) {
- InitOnece();
- bool r = _dicById.TryGetValue(groupId, out GroupData g);
- group = g;
- return r;
- }
- public IEnumerable<IGroup> AsEnumerable() {
- InitOnece();
- return _dicById.Values;
- }
- }
- }
|