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

405 lines
18 KiB
C#

using Newtonsoft.Json;
using ServerCore;
using ServerBase;
using MAIL_GUID = System.String;
using USER_GUID = System.String;
namespace ServerCommon
{
public class MailAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute
{
[JsonProperty]
public MAIL_GUID MailGuid { get; set; } = string.Empty;
public bool IsRead { get; set; } = false;
public bool IsGetItem { get; set; } = false;
public bool IsSystemMail { get; set; } = false;
[JsonProperty]
public string SenderNickName { get; set; } = string.Empty;
[JsonProperty]
public string SenderGuid { get; set; } = string.Empty;
[JsonProperty]
public string ReceiverNickName { get; set; } = string.Empty;
[JsonProperty]
public string ReceiverGuid { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Text { get; set; } = string.Empty;
[JsonProperty]
public DateTime CreateTime { get; set; } = DateTimeHelper.MinTime;
[JsonProperty]
public DateTime ExpireTime { get; set; } = DateTimeHelper.MinTime;
[JsonProperty]
public bool IsTextByMetaData { get; set; } = false;
[JsonProperty]
public List<MailItem> ItemList { get; set; } = new();
[JsonProperty]
public string packageOrderId { get; set; } = string.Empty;
[JsonProperty]
public List<string> ContentsArguments { get; set; } = new();
public MailAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType)
: base(owner, entityOfOwnerEntityType)
{
}
public override void onClear()
{
MailGuid = string.Empty;
IsRead = false;
IsGetItem = false;
IsSystemMail = false;
SenderNickName = string.Empty;
SenderGuid = string.Empty;
ReceiverNickName = string.Empty;
ReceiverGuid = string.Empty;
Title = string.Empty;
Text = string.Empty;
CreateTime = DateTimeHelper.MinTime;
ExpireTime = DateTimeHelper.MinTime;
IsTextByMetaData = false;
ItemList = new List<MailItem>();
packageOrderId = string.Empty;
ContentsArguments.Clear();
getAttributeState().reset();
}
public override IEntityAttributeTransactor onNewEntityAttributeTransactor()
{
return new MailAttributeTransactor(getOwner());
}
public override EntityAttributeBase onCloned()
{
var entity_type = getEntityOfOwnerEntityType();
NullReferenceCheckHelper.throwIfNull(entity_type, () => "Entity Type is null !!!");
var cloned = new MailAttribute(getOwner(), entity_type);
cloned.MailGuid = MailGuid;
cloned.IsRead = IsRead;
cloned.IsGetItem = IsGetItem;
cloned.IsSystemMail = IsSystemMail;
cloned.SenderNickName = SenderNickName;
cloned.SenderGuid = SenderGuid;
cloned.ReceiverNickName = ReceiverNickName;
cloned.ReceiverGuid = ReceiverGuid;
cloned.Title = Title;
cloned.Text = Text;
cloned.CreateTime = CreateTime;
cloned.ExpireTime = ExpireTime;
cloned.IsTextByMetaData = IsTextByMetaData;
cloned.ItemList = ItemList.Select(x => new MailItem() { ItemId = x.ItemId, Count = x.Count, ProductId = x.ProductId, isRepeatProduct = x.isRepeatProduct }).ToList();
cloned.packageOrderId = packageOrderId;
cloned.ContentsArguments = ContentsArguments.ToList(); //string 객체는 기본적으로 DeepCopy 처리 !!!
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 root_parent = owner.getRootParent();
NullReferenceCheckHelper.throwIfNull(root_parent, () => $"root_parent is null !!! - {owner.toBasicString()}");
var acoount_attribute = root_parent.getEntityAttribute<AccountAttribute>();
NullReferenceCheckHelper.throwIfNull(acoount_attribute, () => $"account_attribute is null !!! - {root_parent.toBasicString()}");
USER_GUID user_guid = acoount_attribute.UserGuid;
var ttl_sec = DynamoDbClientHelper.makeTTLTimeForDynamoDB(ExpireTime);
//=====================================================================================
// Attribute => try pending Doc
//=====================================================================================
var try_pending_doc = getTryPendingDocBase() as MailDoc;
if (null == try_pending_doc)
{
MailDoc? to_copy_doc;
if (SenderGuid == user_guid)
{
if (getAttributeState().hasFlag(StateType.Created) == true && ExpireTime != DateTimeHelper.MaxTime)
{
to_copy_doc = new SentMailDoc(user_guid, MailGuid, ttl_sec);
}
else
{
to_copy_doc = new SentMailDoc(user_guid, MailGuid);
}
}
else
{
if (getAttributeState().hasFlag(StateType.Created) == true && ExpireTime != DateTimeHelper.MaxTime)
{
to_copy_doc = new ReceivedMailDoc(user_guid, MailGuid, ttl_sec);
}
else
{
to_copy_doc = new ReceivedMailDoc(user_guid, MailGuid);
}
}
var origin_doc = getOriginDocBase<MailAttribute>();
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<MailAttrib>();
NullReferenceCheckHelper.throwIfNull(to_copy_doc_attrib, () => "to_copy_doc_attrib is null !!!");
to_copy_doc_attrib.MailGuid = MailGuid;
to_copy_doc_attrib.IsRead = IsRead;
to_copy_doc_attrib.IsGetItem = IsGetItem;
to_copy_doc_attrib.IsSystemMail = IsSystemMail;
to_copy_doc_attrib.SenderNickName = SenderNickName;
to_copy_doc_attrib.SenderGuid = SenderGuid;
to_copy_doc_attrib.ReceiverNickName = ReceiverNickName;
to_copy_doc_attrib.ReceiverGuid = ReceiverGuid;
to_copy_doc_attrib.Title = Title;
to_copy_doc_attrib.Text = Text;
to_copy_doc_attrib.CreateTime = CreateTime;
to_copy_doc_attrib.ExpireTime = ExpireTime;
to_copy_doc_attrib.IsTextByMetaData = IsTextByMetaData;
to_copy_doc_attrib.ItemList = ItemList.Select(x => new MailItem() { ItemId = x.ItemId, Count = x.Count, ProductId = x.ProductId, isRepeatProduct = x.isRepeatProduct }).ToList();
to_copy_doc_attrib.packageOrderId = packageOrderId;
to_copy_doc_attrib.ContentsArguments = ContentsArguments.ToList(); //string 객체는 기본적으로 DeepCopy 처리 !!!
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 result = new Result();
var err_msg = string.Empty;
var to_cast_string = typeof(MailDoc).Name;
var mail_doc_base = docBase as MailDoc;
if (mail_doc_base == null)
{
err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, mail_doc_base is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
//=====================================================================================
// New Doc => Origin Doc
//=====================================================================================
syncOriginDocBaseWithNewDoc<MailAttribute>(mail_doc_base);
//=====================================================================================
// Doc => Attribute
//=====================================================================================
var doc_attrib = mail_doc_base.getAttrib<MailAttrib>();
if (doc_attrib == null)
{
err_msg = $"Failed to get mail attrib : {nameof(MailAttrib)}";
result.setFail(ServerErrorCode.EntityAttributeIsNull, err_msg);
Log.getLogger().error(err_msg);
return false;
}
MailGuid = doc_attrib.MailGuid;
IsRead = doc_attrib.IsRead;
IsGetItem = doc_attrib.IsGetItem;
IsSystemMail = doc_attrib.IsSystemMail;
SenderNickName = doc_attrib.SenderNickName;
SenderGuid = doc_attrib.SenderGuid;
ReceiverNickName = doc_attrib.ReceiverNickName;
ReceiverGuid = doc_attrib.ReceiverGuid;
Title = doc_attrib.Title;
Text = doc_attrib.Text;
CreateTime = doc_attrib.CreateTime;
ExpireTime = doc_attrib.ExpireTime;
IsTextByMetaData = doc_attrib.IsTextByMetaData;
ItemList = doc_attrib.ItemList.Select(x => new MailItem() { ItemId = x.ItemId, Count = x.Count, ProductId = x.ProductId, isRepeatProduct = x.isRepeatProduct }).ToList();
packageOrderId = doc_attrib.packageOrderId;
ContentsArguments = doc_attrib.ContentsArguments.ToList(); //string 객체는 기본적으로 DeepCopy 처리 !!!
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 (otherEntityAttribute == null)
{
err_msg = $"Invalid Param !!!, otherEntityAttribute is null";
result.setFail(ServerErrorCode.FunctionParamNull, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
//=====================================================================================
// OtherAttribute => Attribute
//=====================================================================================
var mail_attribute = otherEntityAttribute as MailAttribute;
if (null == mail_attribute)
{
err_msg = $"Failed to cast MailAttribute !!!, mail_attribute is null";
result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
MailGuid = mail_attribute.MailGuid;
IsRead = mail_attribute.IsRead;
IsGetItem = mail_attribute.IsGetItem;
IsSystemMail = mail_attribute.IsSystemMail;
SenderNickName = mail_attribute.SenderNickName;
SenderGuid = mail_attribute.SenderGuid;
ReceiverNickName = mail_attribute.ReceiverNickName;
ReceiverGuid = mail_attribute.ReceiverGuid;
Title = mail_attribute.Title;
Text = mail_attribute.Text;
CreateTime = mail_attribute.CreateTime;
ExpireTime = mail_attribute.ExpireTime;
IsTextByMetaData = mail_attribute.IsTextByMetaData;
ItemList = mail_attribute.ItemList.Select(x => new MailItem() { ItemId = x.ItemId, Count = x.Count, ProductId = x.ProductId, isRepeatProduct = x.isRepeatProduct }).ToList();
packageOrderId = mail_attribute.packageOrderId;
ContentsArguments = mail_attribute.ContentsArguments.ToList(); //string 객체는 기본적으로 DeepCopy 처리 !!!
//=====================================================================================
// Attribute Try Pending Doc => Origin Doc
//=====================================================================================
var try_pending_doc = otherEntityAttribute.getTryPendingDocBase() as MailDoc;
if (null != try_pending_doc)
{
mail_attribute.resetTryPendingDocBase();
syncOriginDocBaseWithNewDoc<MailAttribute>(try_pending_doc);
}
var origin_doc_base = getOriginDocBase<MailAttribute>();
if (null == origin_doc_base)
{
// DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!!
return result;
}
var mail_attrib = origin_doc_base.getAttrib<MailAttrib>();
NullReferenceCheckHelper.throwIfNull(mail_attrib, () => $"mail_attrib is null !!! - {owner.toBasicString()}");
mail_attrib.MailGuid = MailGuid;
mail_attrib.IsRead = IsRead;
mail_attrib.IsGetItem = IsGetItem;
mail_attrib.IsSystemMail = IsSystemMail;
mail_attrib.SenderNickName = SenderNickName;
mail_attrib.SenderGuid = SenderGuid;
mail_attrib.ReceiverNickName = ReceiverNickName;
mail_attrib.ReceiverGuid = ReceiverGuid;
mail_attrib.Title = Title;
mail_attrib.Text = Text;
mail_attrib.CreateTime = CreateTime;
mail_attrib.ExpireTime = ExpireTime;
mail_attrib.IsTextByMetaData = IsTextByMetaData;
mail_attrib.ItemList = ItemList.Select(x => new MailItem() { ItemId = x.ItemId, Count = x.Count, ProductId = x.ProductId, isRepeatProduct = x.isRepeatProduct }).ToList();
mail_attrib.packageOrderId = packageOrderId;
mail_attrib.ContentsArguments = ContentsArguments.ToList(); //string 객체는 기본적으로 DeepCopy 처리 !!!
return result;
}
}
public class MailAttributeTransactor : EntityAttributeTransactorBase<MailAttribute>, ICopyEntityAttributeTransactorFromEntityAttribute
{
public MailAttributeTransactor(EntityBase owner)
: base(owner)
{
}
public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase? entityAttributeBase)
{
var err_msg = string.Empty;
var to_cast_string = typeof(MailAttribute).Name;
var copy_from_mail_attribute = entityAttributeBase as MailAttribute;
if (copy_from_mail_attribute == null)
{
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_from_mail_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
var copy_to_mail_attribute = getClonedEntityAttribute() as MailAttribute;
if (copy_to_mail_attribute == null)
{
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_to_mail_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
copy_to_mail_attribute.MailGuid = copy_from_mail_attribute.MailGuid;
copy_to_mail_attribute.IsRead = copy_from_mail_attribute.IsRead;
copy_to_mail_attribute.IsGetItem = copy_from_mail_attribute.IsGetItem;
copy_to_mail_attribute.IsSystemMail = copy_from_mail_attribute.IsSystemMail;
copy_to_mail_attribute.SenderNickName = copy_from_mail_attribute.SenderNickName;
copy_to_mail_attribute.SenderGuid = copy_from_mail_attribute.SenderGuid;
copy_to_mail_attribute.ReceiverNickName = copy_from_mail_attribute.ReceiverNickName;
copy_to_mail_attribute.ReceiverGuid = copy_from_mail_attribute.ReceiverGuid;
copy_to_mail_attribute.Title = copy_from_mail_attribute.Title;
copy_to_mail_attribute.Text = copy_from_mail_attribute.Text;
copy_to_mail_attribute.CreateTime = copy_from_mail_attribute.CreateTime;
copy_to_mail_attribute.ExpireTime = copy_from_mail_attribute.ExpireTime;
copy_to_mail_attribute.IsTextByMetaData = copy_from_mail_attribute.IsTextByMetaData;
copy_to_mail_attribute.ItemList = copy_from_mail_attribute.ItemList.Select(x => new MailItem() { ItemId = x.ItemId, Count = x.Count, ProductId = x.ProductId, isRepeatProduct = x.isRepeatProduct }).ToList();
copy_to_mail_attribute.packageOrderId = copy_from_mail_attribute.packageOrderId;
copy_to_mail_attribute.ContentsArguments = copy_from_mail_attribute.ContentsArguments.ToList(); //string 객체는 기본적으로 DeepCopy 처리 !!!
return true;
}
}
}