Files
caliverse_server/ServerCommon/Entity/Attribute/ItemFirstPurchaseHistoryAttribute.cs
2025-05-01 07:20:41 +09:00

242 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ServerCore; using ServerBase;
using META_ID = System.UInt32;
namespace ServerCommon
{
public class ItemFirstPurchaseHistoryAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute
{
[JsonProperty]
public META_ID ItemMetaId { get; set; } = 0;
[JsonProperty]
public DateTime FirstPurchaseTime { get; set; } = new();
public ItemFirstPurchaseHistoryAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType)
: base(owner, entityOfOwnerEntityType)
{
}
public override IEntityAttributeTransactor onNewEntityAttributeTransactor()
{
return new ItemFirstPurchaseHistoryAttributeTransactor(getOwner());
}
public override void onClear()
{
ItemMetaId = 0;
FirstPurchaseTime = new();
getAttributeState().reset();
}
public override EntityAttributeBase onCloned()
{
var owner_entity_type = getEntityOfOwnerEntityType();
NullReferenceCheckHelper.throwIfNull(owner_entity_type, () => $"Owner Entity Type is null !!! ");
var cloned = new ItemFirstPurchaseHistoryAttribute(getOwner(), owner_entity_type);
cloned.deepCopyFromBase(this);
cloned.ItemMetaId = ItemMetaId;
cloned.FirstPurchaseTime = FirstPurchaseTime;
return cloned;
}
public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true)
{
var result = new Result();
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => "owner is null !!!");
var user_attribute = owner.getRootParent().getEntityAttribute<UserAttribute>();
NullReferenceCheckHelper.throwIfNull(user_attribute, () => $"user attribute is null !!! - {owner.toBasicString()}");
var user_guid = user_attribute.UserGuid;
//=====================================================================================
// Attribute => try pending Doc
//=====================================================================================
var try_pending_doc = getTryPendingDocBase() as ItemFirstPurchaseHistoryDoc;
if (null == try_pending_doc)
{
var to_copy_doc = new ItemFirstPurchaseHistoryDoc(user_guid, ItemMetaId);
var origin_doc = getOriginDocBase<ItemFirstPurchaseHistoryAttribute>();
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<ItemFirstPurchaseHistoryAttrib>();
NullReferenceCheckHelper.throwIfNull(to_copy_doc_attrib, () => $"to_copy_doc_attrib is null !!! - {owner.toBasicString()}");
to_copy_doc_attrib.ItemMetaId = ItemMetaId;
to_copy_doc_attrib.FirstPurchaseTime = FirstPurchaseTime;
if (false == isForQuery)
{
return (result, try_pending_doc);
}
//=====================================================================================
// Doc QueryType 반영
//=====================================================================================
(result, var to_query_docs) = await applyDoc4Query(try_pending_doc);
if (result.isFail())
{
return (result, null);
}
return (result, to_query_docs);
}
public bool copyEntityAttributeFromDoc(DynamoDbDocBase? docBase)
{
var err_msg = string.Empty;
var to_cast_string = typeof(ItemFirstPurchaseHistoryDoc).Name;
var item_first_purchase_history_doc_base = docBase as ItemFirstPurchaseHistoryDoc;
if (null == item_first_purchase_history_doc_base)
{
err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, item_first_purchase_history_doc_base is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
//=====================================================================================
// New Doc => Origin Doc
//=====================================================================================
syncOriginDocBaseWithNewDoc<ItemFirstPurchaseHistoryAttribute>(item_first_purchase_history_doc_base);
//=====================================================================================
// Doc => Attribute
//=====================================================================================
var doc_attrib = item_first_purchase_history_doc_base.getAttrib<ItemFirstPurchaseHistoryAttrib>();
NullReferenceCheckHelper.throwIfNull(doc_attrib, () => $"doc_attrib is null !!! - {getOwner().toBasicString()}");
ItemMetaId = doc_attrib.ItemMetaId;
FirstPurchaseTime = doc_attrib.FirstPurchaseTime;
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 item_first_purchase_histroy_attribute = otherEntityAttribute as ItemFirstPurchaseHistoryAttribute;
if (null == item_first_purchase_histroy_attribute)
{
err_msg = $"Failed to cast ItemFirstPurchaseHistoryAttribute !!!, item_first_purchase_histroy_attribute is null";
result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
ItemMetaId = item_first_purchase_histroy_attribute.ItemMetaId;
FirstPurchaseTime = item_first_purchase_histroy_attribute.FirstPurchaseTime;
//=====================================================================================
// Attribute Try Pending Doc => Origin Doc
//=====================================================================================
var try_pending_doc = item_first_purchase_histroy_attribute.getTryPendingDocBase() as ItemFirstPurchaseHistoryDoc;
if (null != try_pending_doc)
{
item_first_purchase_histroy_attribute.resetTryPendingDocBase();
syncOriginDocBaseWithNewDoc<ItemFirstPurchaseHistoryAttribute>(try_pending_doc);
}
var origin_doc_base = getOriginDocBase<ItemFirstPurchaseHistoryAttribute>();
if (null == origin_doc_base)
{
// DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!!
return result;
}
var item_first_purchase_history_attrib = origin_doc_base.getAttrib<ItemFirstPurchaseHistoryAttrib>();
NullReferenceCheckHelper.throwIfNull(item_first_purchase_history_attrib, () => $"item_first_purchase_history_attrib is null !!! - {owner.toBasicString()}");
item_first_purchase_history_attrib.ItemMetaId = ItemMetaId;
item_first_purchase_history_attrib.FirstPurchaseTime = FirstPurchaseTime;
return result;
}
}
public class ItemFirstPurchaseHistoryAttributeTransactor : EntityAttributeTransactorBase<ItemFirstPurchaseHistoryAttribute>, ICopyEntityAttributeTransactorFromEntityAttribute
{
public ItemFirstPurchaseHistoryAttributeTransactor(EntityBase owner)
: base(owner)
{
}
public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase)
{
var err_msg = string.Empty;
var to_cast_string = typeof(ItemFirstPurchaseHistoryAttribute).Name;
var copy_from_item_first_purchase_history_attribute = entityAttributeBase as ItemFirstPurchaseHistoryAttribute;
if (null == copy_from_item_first_purchase_history_attribute)
{
err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_from_item_first_purchase_history_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
var copy_to_item_first_purchase_history_attribute = getClonedEntityAttribute() as ItemFirstPurchaseHistoryAttribute;
if (null == copy_to_item_first_purchase_history_attribute)
{
err_msg = $"Failed to copyEntityAttributeTransactorBaseFromEntityAttribute() !!!, copy_to_item_first_purchase_history_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
copy_to_item_first_purchase_history_attribute.ItemMetaId = copy_from_item_first_purchase_history_attribute.ItemMetaId;
copy_to_item_first_purchase_history_attribute.FirstPurchaseTime = copy_from_item_first_purchase_history_attribute.FirstPurchaseTime;
return true;
}
}
}