using Google.Protobuf.WellKnownTypes; using Newtonsoft.Json; using ServerCore; using ServerBase; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServerCommon { public class BuildingProfitHistoryAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute { [JsonProperty] public int BuildingMetaId { get; set; } [JsonProperty] public int Floor { get; set; } = 0; [JsonProperty] public DateTime ProfitTime { get; set; } = new(); [JsonProperty] public ProfitHistoryType ProfitHistoryType { get; set; } = ProfitHistoryType.None; [JsonProperty] public Dictionary Profits { get; set; } = new(); public BuildingProfitHistoryAttribute(EntityBase owner) : base(owner) { } public override void newEntityAttribute() { base.newEntityAttribute(); } public override IEntityAttributeTransactor onNewEntityAttributeTransactor() { return new BuildingProfitHistoryAttributeTransactor(getOwner()); } public override void onClear() { BuildingMetaId = 0; Floor = 0; ProfitTime = new(); ProfitHistoryType = ProfitHistoryType.None; Profits = new(); getAttributeState().reset(); } public override EntityAttributeBase onCloned() { var cloned = new BuildingProfitHistoryAttribute(getOwner()); cloned.deepCopyFromBase(this); cloned.BuildingMetaId = BuildingMetaId; cloned.Floor = Floor; cloned.ProfitTime = ProfitTime; cloned.ProfitHistoryType = ProfitHistoryType; cloned.Profits = new(Profits); return cloned; } public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true) { var result = new Result(); //===================================================================================== // Attribute => try pending Doc //===================================================================================== var try_pending_doc = getTryPendingDocBase() as BuildingProfitHistoryDoc; if (null == try_pending_doc) { var to_copy_doc = new BuildingProfitHistoryDoc(BuildingMetaId, ProfitTime.ToTimestamp()); 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.BuildingMetaId = BuildingMetaId; to_copy_doc_attrib.Floor = Floor; to_copy_doc_attrib.ProfitTime = ProfitTime; to_copy_doc_attrib.ProfitHistoryType = ProfitHistoryType; to_copy_doc_attrib.Profits = new(Profits); 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(BuildingProfitHistoryDoc).Name; var building_profit_history_doc_base = docBase as BuildingProfitHistoryDoc; if (null == building_profit_history_doc_base) { err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, building_profit_history_doc_base is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } //===================================================================================== // New Doc => Origin Doc //===================================================================================== syncOriginDocBaseWithNewDoc(building_profit_history_doc_base); //===================================================================================== // Doc => Attribute //===================================================================================== var doc_attrib = building_profit_history_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(doc_attrib, () => $"doc_attrib is null !!!"); BuildingMetaId = doc_attrib.BuildingMetaId; Floor = doc_attrib.Floor; ProfitTime = doc_attrib.ProfitTime; ProfitHistoryType = doc_attrib.ProfitHistoryType; Profits = new(doc_attrib.Profits); return true; } public Result onMerge(EntityAttributeBase otherEntityAttribute) { 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 building_profit_history_attribute = otherEntityAttribute as BuildingProfitHistoryAttribute; if (null == building_profit_history_attribute) { err_msg = $"Failed to cast BuildingProfitHistoryAttribute !!!, building_profit_attribute is null"; result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg); Log.getLogger().error(result.toBasicString()); return result; } BuildingMetaId = building_profit_history_attribute.BuildingMetaId; Floor = building_profit_history_attribute.Floor; ProfitTime = building_profit_history_attribute.ProfitTime; ProfitHistoryType = building_profit_history_attribute.ProfitHistoryType; Profits = new(building_profit_history_attribute.Profits); //===================================================================================== // Attribute Try Pending Doc => Origin Doc //===================================================================================== var try_pending_doc = building_profit_history_attribute.getTryPendingDocBase() as BuildingProfitHistoryDoc; if (null != try_pending_doc) { building_profit_history_attribute.resetTryPendingDocBase(); syncOriginDocBaseWithNewDoc(try_pending_doc); } var origin_doc_base = getOriginDocBase(); if (null == origin_doc_base) { // DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!! return result; } var building_profit_history_attrib = origin_doc_base.getAttrib(); NullReferenceCheckHelper.throwIfNull(building_profit_history_attrib, () => $"building_profit_attrib is null !!!"); building_profit_history_attrib.BuildingMetaId = BuildingMetaId; building_profit_history_attrib.Floor = Floor; building_profit_history_attrib.ProfitTime = ProfitTime; building_profit_history_attrib.ProfitHistoryType = ProfitHistoryType; building_profit_history_attrib.Profits = new(Profits); return result; } } public class BuildingProfitHistoryAttributeTransactor : EntityAttributeTransactorBase, ICopyEntityAttributeTransactorFromEntityAttribute { public BuildingProfitHistoryAttributeTransactor(EntityBase owner) : base(owner) { } public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase) { var err_msg = string.Empty; var to_cast_string = typeof(BuildingProfitHistoryAttribute).Name; var copy_from_building_profit_history_attribute = entityAttributeBase as BuildingProfitHistoryAttribute; if (null == copy_from_building_profit_history_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_from_building_profit_history_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } var copy_to_building_profit_history_attribute = getClonedEntityAttribute() as BuildingProfitHistoryAttribute; if (null == copy_to_building_profit_history_attribute) { err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_to_building_profit_history_attribute is null :{to_cast_string}"; Log.getLogger().error(err_msg); return false; } copy_to_building_profit_history_attribute.BuildingMetaId = copy_from_building_profit_history_attribute.BuildingMetaId; copy_to_building_profit_history_attribute.Floor = copy_from_building_profit_history_attribute.Floor; copy_to_building_profit_history_attribute.ProfitTime = copy_from_building_profit_history_attribute.ProfitTime; copy_to_building_profit_history_attribute.ProfitHistoryType = copy_from_building_profit_history_attribute.ProfitHistoryType; copy_to_building_profit_history_attribute.Profits = new(copy_from_building_profit_history_attribute.Profits); return true; } } }