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

246 lines
10 KiB
C#

using Newtonsoft.Json;
using ServerCore; using ServerBase;
using USER_GUID = System.String;
using META_ID = System.UInt32;
namespace ServerCommon
{
public class PackageRepeatAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute
{
[JsonProperty]
public string OrderGuid { get; set; } = string.Empty;
[JsonProperty]
public META_ID ProductMetaId { get; set; } = 0;
[JsonProperty]
public int LeftCount { get; set; } = 0;
[JsonProperty]
public DateTime NextGiveTime { get; set; } = new();
public PackageRepeatAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType)
: base(owner, entityOfOwnerEntityType)
{
}
public override void onClear()
{
OrderGuid = string.Empty;
ProductMetaId = 0;
LeftCount = 0;
NextGiveTime = new();
}
public override EntityAttributeBase onCloned()
{
var owner_entity = getEntityOfOwnerEntityType();
ArgumentNullReferenceCheckHelper.throwIfNull(owner_entity, () => $"owner_entity is null !!!");
var cloned = new PackageRepeatAttribute(getOwner(), owner_entity);
cloned.OrderGuid = OrderGuid;
cloned.ProductMetaId = ProductMetaId;
cloned.LeftCount = LeftCount;
cloned.NextGiveTime = NextGiveTime;
return cloned;
}
public override IEntityAttributeTransactor onNewEntityAttributeTransactor()
{
return new PackageRepeatAttributeTransactor(getOwner());
}
public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true)
{
var result = new Result();
var acoount_attribute = getOwner().getRootParent().getEntityAttribute<AccountAttribute>();
ArgumentNullException.ThrowIfNull(acoount_attribute);
USER_GUID user_guid = acoount_attribute.UserGuid;
//=====================================================================================
// Attribute => try pending Doc
//=====================================================================================
var try_pending_doc = getTryPendingDocBase() as PackageRepeatDoc;
if (null == try_pending_doc)
{
var to_copy_doc = new PackageRepeatDoc(user_guid, OrderGuid);
var origin_doc = getOriginDocBase<PackageRepeatAttribute>();
if (null != origin_doc)
{
to_copy_doc.copyTimestampsFromOriginDocBase(origin_doc);
}
try_pending_doc = to_copy_doc;
setTryPendingDocBase(try_pending_doc);
}
var package_repeat_attrib = try_pending_doc.getAttrib<PackageRepeatAttrib>();
NullReferenceCheckHelper.throwIfNull(package_repeat_attrib, () => $"package_repeat_attrib is null !!!");
package_repeat_attrib.OrderGuid = OrderGuid;
package_repeat_attrib.ProductMetaId = ProductMetaId;
package_repeat_attrib.LeftCount = LeftCount;
package_repeat_attrib.NextGiveTime = NextGiveTime;
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, to_query_doc);
}
return (result, to_query_doc);
}
public bool copyEntityAttributeFromDoc(DynamoDbDocBase docBase)
{
var result = new Result();
var err_msg = string.Empty;
var to_cast_string = typeof(PackageRepeatDoc).Name;
var package_doc_base = docBase as PackageRepeatDoc;
if (null == package_doc_base)
{
err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, package_doc_base is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
//=====================================================================================
// New Doc => Origin Doc
//=====================================================================================
syncOriginDocBaseWithNewDoc<PackageRepeatAttribute>(package_doc_base);
//=====================================================================================
// Doc => Attribute
//=====================================================================================
var packate_repeat_attrib = package_doc_base.getAttrib<PackageRepeatAttrib>();
if (packate_repeat_attrib == null)
{
err_msg = $"Failed to get package attrib : {nameof(PackageRepeatAttrib)}";
result.setFail(ServerErrorCode.EntityAttributeIsNull, err_msg);
Log.getLogger().error(err_msg);
return false;
}
OrderGuid = packate_repeat_attrib.OrderGuid;
ProductMetaId = packate_repeat_attrib.ProductMetaId;
LeftCount = packate_repeat_attrib.LeftCount;
NextGiveTime = packate_repeat_attrib.NextGiveTime;
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 package_repeat_attribute = otherEntityAttribute as PackageRepeatAttribute;
if (null == package_repeat_attribute)
{
err_msg = $"Failed to cast Attribute !!!, package_repeat_attribute is null";
result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
OrderGuid = package_repeat_attribute.OrderGuid;
ProductMetaId = package_repeat_attribute.ProductMetaId;
LeftCount = package_repeat_attribute.LeftCount;
NextGiveTime = package_repeat_attribute.NextGiveTime;
//=====================================================================================
// Attribute Try Pending Doc => Origin Doc
//=====================================================================================
var try_pending_doc = package_repeat_attribute.getTryPendingDocBase() as PackageRepeatDoc;
if (null != try_pending_doc)
{
package_repeat_attribute.resetTryPendingDocBase();
syncOriginDocBaseWithNewDoc<PackageRepeatAttribute>(try_pending_doc);
}
var origin_doc_base = getOriginDocBase<PackageRepeatAttribute>();
if (null == origin_doc_base)
{
// DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!!
return result;
}
var package_repeat_attrib = origin_doc_base.getAttrib<PackageRepeatAttrib>();
NullReferenceCheckHelper.throwIfNull(package_repeat_attrib, () => $"package_repeat_attrib is null !!! - {owner.toBasicString()}");
package_repeat_attrib.OrderGuid = OrderGuid;
package_repeat_attrib.ProductMetaId = ProductMetaId;
package_repeat_attrib.LeftCount = LeftCount;
package_repeat_attrib.NextGiveTime = NextGiveTime;
return result;
}
}
public class PackageRepeatAttributeTransactor : EntityAttributeTransactorBase<PackageRepeatAttribute>, ICopyEntityAttributeTransactorFromEntityAttribute
{
public PackageRepeatAttributeTransactor(EntityBase owner)
: base(owner)
{
}
public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase)
{
var err_msg = string.Empty;
var to_cast_string = typeof(PackageRepeatAttribute).Name;
var copy_from_attribute = entityAttributeBase as PackageRepeatAttribute;
if (null == copy_from_attribute)
{
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_from_package_repeat_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
var copy_to_attribute = getClonedEntityAttribute() as PackageRepeatAttribute;
if (null == copy_to_attribute)
{
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_to_package_repeat_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
copy_to_attribute.OrderGuid = copy_from_attribute.OrderGuid;
copy_to_attribute.ProductMetaId = copy_from_attribute.ProductMetaId;
copy_to_attribute.LeftCount = copy_from_attribute.LeftCount;
copy_to_attribute.NextGiveTime = copy_from_attribute.NextGiveTime;
return true;
}
}
}