using System.Collections.Generic; using System.Numerics; using Newtonsoft.Json; using ServerCore; using ServerBase; using USER_GUID = System.String; namespace ServerCommon; public class RoomAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { [JsonProperty] public int RoomId { get; set; } [JsonProperty] public string Owner { get; set; } = string.Empty; [JsonProperty] public string Name { get; set; } = string.Empty; [JsonProperty] public string Description { get; set; } = string.Empty; [JsonProperty] public Dictionary PropInfo = new(); public RoomAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType) : base(owner, entityOfOwnerEntityType) { } public override void onClear() { RoomId = 0; Owner = string.Empty; Name = string.Empty; Description = string.Empty; PropInfo.Clear(); getAttributeState().reset(); } public override EntityAttributeBase onCloned() { var entity_base = getEntityOfOwnerEntityType(); NullReferenceCheckHelper.throwIfNull(entity_base, () => $"entity_base is null !!!"); var cloned = new RoomAttribute(getOwner(), entity_base) { RoomId = RoomId, Owner = Owner, Name = Name, Description = Description }; foreach (var prop in PropInfo) { var data = new AnchorProp { TableId = prop.Value.TableId, GuidByType = prop.Value.GuidByType, IsMannequinsChanged = prop.Value.IsMannequinsChanged }; if (null != prop.Value.Mannequins) { data.Mannequins = new(); data.Mannequins.AddRange(prop.Value.Mannequins); } cloned.PropInfo.Add(prop.Key, prop.Value); } return cloned; } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { return new IndividualRoomAttributeTransactor(getOwner()); } public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true) { var result = new Result(); var owner = getOwner() as UserBase; NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var user_attribute = owner.getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(user_attribute, () => $"user_attribute is null !!!"); USER_GUID user_guid = user_attribute.UserGuid; //===================================================================================== // Attribute => Origin Doc //===================================================================================== var origin_doc = getOriginDocBase(); if (null == origin_doc) { var err_msg = $"Failed to toDocBase() !!!, origin_doc is null : {owner.toBasicString()}"; result.setFail(ServerErrorCode.OwnedRoomDocIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return (result, null); } var to_copy_doc_attrib = origin_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(to_copy_doc_attrib, () => $"to_copy_doc_attrib is null !!!"); to_copy_doc_attrib.RoomId = RoomId; to_copy_doc_attrib.Owner = Owner; to_copy_doc_attrib.Name = Name; to_copy_doc_attrib.Description = Description; foreach (var prop in PropInfo) { var data = new AnchorProp { TableId = prop.Value.TableId, GuidByType = prop.Value.GuidByType, IsMannequinsChanged = prop.Value.IsMannequinsChanged }; if (null != prop.Value.Mannequins) { data.Mannequins = new(); data.Mannequins.AddRange(prop.Value.Mannequins); } to_copy_doc_attrib.PropInfo.Add(prop.Key, prop.Value); } if (false == isForQuery) { return (result, origin_doc); } //===================================================================================== // Doc QueryType 반영 //===================================================================================== (result, var to_query_doc) = await applyDoc4Query(origin_doc); if (result.isFail()) { return (result, null); } return (result, to_query_doc); } public Result onMerge(EntityAttributeBase otherEntityAttribute) { var owner = getOwner(); NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var result = new Result(); string err_msg; //===================================================================================== // OtherAttribute => Attribute //===================================================================================== var other_room_attribute = otherEntityAttribute as RoomAttribute; if (null == other_room_attribute) { err_msg = $"Failed to cast RoomAttribute !!!, other_room_attribute is null"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } RoomId = other_room_attribute.RoomId; Owner = other_room_attribute.Owner; Name = other_room_attribute.Name; Description = other_room_attribute.Description; foreach (var prop in other_room_attribute.PropInfo) { var data = new AnchorProp { TableId = prop.Value.TableId, GuidByType = prop.Value.GuidByType, IsMannequinsChanged = prop.Value.IsMannequinsChanged }; if (null != prop.Value.Mannequins) { data.Mannequins = new(); data.Mannequins.AddRange(prop.Value.Mannequins); } PropInfo.Add(prop.Key, prop.Value); } //===================================================================================== // Attribute => Origin Doc //===================================================================================== var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var room_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(room_attrib, () => $"room_attrib is null !!! - {owner.toBasicString()}"); room_attrib.RoomId = RoomId; room_attrib.Owner = Owner; room_attrib.Name = Name; room_attrib.Description = Description; foreach (var prop in PropInfo) { var data = new AnchorProp { TableId = prop.Value.TableId, GuidByType = prop.Value.GuidByType, IsMannequinsChanged = prop.Value.IsMannequinsChanged }; if (null != prop.Value.Mannequins) { data.Mannequins = new(); data.Mannequins.AddRange(prop.Value.Mannequins); } room_attrib.PropInfo.Add(prop.Key, prop.Value); } return result; } public bool copyEntityAttributeFromDoc(DynamoDbDocBase docBase) { if (docBase is not RoomDoc room_doc) { var err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, room_doc_base is null :{nameof(RoomDoc)}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(room_doc); var room_attrib = room_doc.getAttrib(); if (null == room_attrib) { var err_msg = $"Failed to copyEntityAttributeFromDocs() !!!, not found doc attrib !!! :{toBasicString()}"; Log.getLogger().error(err_msg); return false; } RoomId = room_attrib.RoomId; Owner = room_attrib.Owner; Name = room_attrib.Name; Description = room_attrib.Description; foreach (var prop in room_attrib.PropInfo) { var data = new AnchorProp { TableId = prop.Value.TableId, GuidByType = prop.Value.GuidByType, IsMannequinsChanged = prop.Value.IsMannequinsChanged }; if (null != prop.Value.Mannequins) { data.Mannequins = new(); data.Mannequins.AddRange(prop.Value.Mannequins); } PropInfo.Add(prop.Key, prop.Value); } return true; } } public class IndividualRoomAttributeTransactor: EntityAttributeTransactorBase, ICopyEntityAttributeTransactorFromEntityAttribute { public IndividualRoomAttributeTransactor(EntityBase owner) : base (owner) { } public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase entityAttributeBase) { string err_msg; var copy_from_room_attribute = entityAttributeBase as RoomAttribute; if (null == copy_from_room_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_from_room_attribute is null - {nameof(RoomAttribute)}"; Log.getLogger().error(err_msg); return false; } var copy_to_room_attribute = getClonedEntityAttribute() as RoomAttribute; if (null == copy_to_room_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_to_room_attribute is null - {nameof(RoomAttribute)}"; Log.getLogger().error(err_msg); return false; } copy_to_room_attribute.RoomId = copy_from_room_attribute.RoomId; copy_to_room_attribute.Owner = copy_from_room_attribute.Owner; copy_to_room_attribute.Name = copy_from_room_attribute.Name; copy_to_room_attribute.Description = copy_from_room_attribute.Description; foreach (var prop in copy_from_room_attribute.PropInfo) { var data = new AnchorProp { TableId = prop.Value.TableId, GuidByType = prop.Value.GuidByType, IsMannequinsChanged = prop.Value.IsMannequinsChanged }; if (null != prop.Value.Mannequins) { data.Mannequins = new(); data.Mannequins.AddRange(prop.Value.Mannequins); } copy_to_room_attribute.PropInfo.Add(prop.Key, prop.Value); } return true; } }