using Amazon.S3.Model; using Newtonsoft.Json; using ServerCore; using ServerBase; using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace ServerCommon { public class RentalAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { [JsonProperty] public int LandMetaId { get; set; } [JsonProperty] public int BuildingMetaId { get; set; } [JsonProperty] public int Floor { get; set; } [JsonProperty] public string MyhomeGuid { get; set; } = string.Empty; [JsonProperty] public DateTime RentalFinishTime { get; set; } = new(); public RentalAttribute(EntityBase owner) : base(owner) { } public override void newEntityAttribute() { base.newEntityAttribute(); } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { return new RentalAttributeTransactor(getOwner()); } public override void onClear() { LandMetaId = 0; BuildingMetaId = 0; Floor = 0; MyhomeGuid = string.Empty; RentalFinishTime = new(); getAttributeState().reset(); } public override EntityAttributeBase onCloned() { var cloned = new RentalAttribute(getOwner()); cloned.deepCopyFromBase(this); cloned.LandMetaId = LandMetaId; cloned.BuildingMetaId = BuildingMetaId; cloned.Floor = Floor; cloned.MyhomeGuid = MyhomeGuid; cloned.RentalFinishTime = RentalFinishTime; return cloned; } public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true) { var result = new Result(); var owner = getOwner(); var user_attribute = owner.getRootParent().getEntityAttribute(); NullReferenceCheckHelper.throwIfNull(user_attribute, () => $"user_attribute is null !!!"); string user_guid = user_attribute.UserGuid; //===================================================================================== // Attribute => try pending Doc //===================================================================================== var try_pending_doc = getTryPendingDocBase() as RentalDoc; if (null == try_pending_doc) { var address = makeAddress(LandMetaId, BuildingMetaId, Floor); var ttl_seconds = DynamoDbClientHelper.makeTTLTimeForDynamoDB(RentalFinishTime); var to_copy_doc = new RentalDoc(user_guid, address, ttl_seconds); var origin_doc = getOriginDocBase(); if (null != origin_doc) { to_copy_doc.copyTimestampsFromOriginDocBase(origin_doc); } try_pending_doc = to_copy_doc; setTryPendingDocBase(try_pending_doc); } var to_copy_doc_attrib = try_pending_doc.getAttrib(); NullReferenceCheckHelper.throwIfNull(to_copy_doc_attrib, () => $"to_copy_doc_attrib is null !!!"); to_copy_doc_attrib.LandMetaId = LandMetaId; to_copy_doc_attrib.BuildingMetaId = BuildingMetaId; to_copy_doc_attrib.Floor = Floor; to_copy_doc_attrib.MyhomeGuid = MyhomeGuid; to_copy_doc_attrib.RentalFinishTime = RentalFinishTime; if (false == isForQuery) { return (result, try_pending_doc); } //===================================================================================== // Doc QueryType 반영 //===================================================================================== (result, var to_query_doc) = await applyDoc4Query(try_pending_doc); if (result.isFail()) { return (result, null); } return (result, to_query_doc); } public bool copyEntityAttributeFromDoc(DynamoDbDocBase docBase) { var err_msg = string.Empty; var to_cast_string = typeof(RentalDoc).Name; var rental_doc_base = docBase as RentalDoc; if (null == rental_doc_base) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, rental_doc_base is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(rental_doc_base); //===================================================================================== // Doc => Attribute //===================================================================================== var doc_attrib = rental_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(doc_attrib, () => $"doc_attrib is null !!!"); LandMetaId = doc_attrib.LandMetaId; BuildingMetaId = doc_attrib.BuildingMetaId; Floor = doc_attrib.Floor; MyhomeGuid = doc_attrib.MyhomeGuid; RentalFinishTime = doc_attrib.RentalFinishTime; return true; } public Result onMerge(EntityAttributeBase otherEntityAttribute) { var owner = getOwner(); NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!"); var result = new Result(); var err_msg = string.Empty; if (null == otherEntityAttribute) { err_msg = $"Invalid Param !!!, otherEntityAttribute is null"; result.setFail(ServerErrorCode.FunctionParamNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } //===================================================================================== // OtherAttribute => Attribute //===================================================================================== var rental_attribute = otherEntityAttribute as RentalAttribute; if (null == rental_attribute) { err_msg = $"Failed to cast RentalAttribute !!!, rental_attribute is null"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } LandMetaId = rental_attribute.LandMetaId; BuildingMetaId = rental_attribute.BuildingMetaId; Floor = rental_attribute.Floor; MyhomeGuid = rental_attribute.MyhomeGuid; RentalFinishTime = rental_attribute.RentalFinishTime; //===================================================================================== // Attribute Try Pending Doc => Origin Doc //===================================================================================== var try_pending_doc = rental_attribute.getTryPendingDocBase() as RentalDoc; if (null != try_pending_doc) { rental_attribute.resetTryPendingDocBase(); syncOriginDocBaseWithNewDoc(try_pending_doc); } var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var rental_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(rental_attrib, () => $"rental_attrib is null !!! - {owner.toBasicString()}"); rental_attrib.LandMetaId = LandMetaId; rental_attrib.BuildingMetaId = BuildingMetaId; rental_attrib.Floor = Floor; rental_attrib.MyhomeGuid = MyhomeGuid; rental_attrib.RentalFinishTime = RentalFinishTime; return result; } string makeAddress(int landMetaId, int buildingMetaId, int floor) { var address = string.Empty; if (landMetaId <= 0) return address; address = $"{landMetaId}"; if (buildingMetaId <= 0) return address; address = $"{landMetaId}#{buildingMetaId}"; if (floor <= 0) return address; address = $"{landMetaId}#{buildingMetaId}#{floor}"; return address; } } public class RentalAttributeTransactor : EntityAttributeTransactorBase, ICopyEntityAttributeTransactorFromEntityAttribute { public RentalAttributeTransactor(EntityBase owner) : base(owner) { } public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase) { var err_msg = string.Empty; var to_cast_string = typeof(RentalAttribute).Name; var copy_from_rental_attribute = entityAttributeBase as RentalAttribute; if (null == copy_from_rental_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_from_rental_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } var copy_to_rental_attribute = getClonedEntityAttribute() as RentalAttribute; if (null == copy_to_rental_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_to_rental_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } copy_to_rental_attribute.LandMetaId = copy_from_rental_attribute.LandMetaId; copy_to_rental_attribute.BuildingMetaId = copy_from_rental_attribute.BuildingMetaId; copy_to_rental_attribute.Floor = copy_from_rental_attribute.Floor; copy_to_rental_attribute.MyhomeGuid = copy_from_rental_attribute.MyhomeGuid; copy_to_rental_attribute.RentalFinishTime = copy_from_rental_attribute.RentalFinishTime; return true; } } }