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

248 lines
9.9 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;
using ALERT_KEY = System.String;
using OWNER_GUID = System.String;
namespace ServerCommon
{
public class EntityAlertRecordAttribute : EntityAttributeBase, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute
{
[JsonProperty]
public ALERT_KEY AlertKey { get; set; } = string.Empty;
[JsonProperty]
public DateTime AlertedTime { get; set; } = DateTimeHelper.MinTime;
[JsonProperty]
public OwnerEntityType OwnerEntityType { get; set; } = OwnerEntityType.None;
[JsonProperty]
public OWNER_GUID OwnerGuid { get; set; } = string.Empty;
[JsonProperty]
public EntityAlertTriggerType EntityAlertTriggerType { get; set; } = EntityAlertTriggerType.None;
[JsonProperty]
public META_ID MetaId { get; set; } = 0;
public EntityAlertRecordAttribute(EntityBase owner, EntityBase entityOfOwnerEntityType)
: base(owner, entityOfOwnerEntityType)
{
}
public override IEntityAttributeTransactor onNewEntityAttributeTransactor()
{
return new EntityAlertHistoryAttributeTransactor(getOwner());
}
public override void onClear()
{
AlertKey = string.Empty;
AlertedTime = DateTimeHelper.MinTime;
OwnerEntityType = OwnerEntityType.None;
OwnerGuid = string.Empty;
EntityAlertTriggerType = EntityAlertTriggerType.None;
MetaId = 0;
getAttributeState().reset();
}
public override EntityAttributeBase onCloned()
{
var owner_entity_type = getEntityOfOwnerEntityType();
NullReferenceCheckHelper.throwIfNull(owner_entity_type, () => $"Owner Entity Type is null !!! ");
var cloned = new EntityAlertRecordAttribute(getOwner(), owner_entity_type);
cloned.deepCopyFromBase(this);
cloned.AlertKey = AlertKey;
cloned.AlertedTime = AlertedTime;
cloned.OwnerEntityType = OwnerEntityType;
cloned.OwnerGuid = OwnerGuid;
cloned.EntityAlertTriggerType = EntityAlertTriggerType;
cloned.MetaId = MetaId;
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 owner_entity_type = owner.onGetOwnerEntityType();
var owner_guid = owner.onGetGuidOfOwnerEntityType();
//=====================================================================================
// Attribute => try pending Doc
//=====================================================================================
var try_pending_doc = getTryPendingDocBase() as EntityAlertRecordDoc;
if (null == try_pending_doc)
{
var to_copy_doc = new EntityAlertRecordDoc( owner_guid, EntityAlertRecordDoc.makeALERT_KEY(EntityAlertTriggerType, MetaId), AlertedTime
, owner_entity_type, EntityAlertTriggerType, MetaId );
var origin_doc = getOriginDocBase<EntityAlertRecordAttribute>();
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<EntityAlertRecordAttrib>();
NullReferenceCheckHelper.throwIfNull(to_copy_doc_attrib, () => $"to_copy_doc_attrib is null !!! - {owner.toBasicString()}");
to_copy_doc_attrib.AlertKey = AlertKey;
to_copy_doc_attrib.AlertedTime = AlertedTime;
to_copy_doc_attrib.OwnerEntityType = OwnerEntityType;
to_copy_doc_attrib.OwnerGuid = OwnerGuid;
to_copy_doc_attrib.EntityAlertTriggerType = EntityAlertTriggerType;
to_copy_doc_attrib.MetaId = MetaId;
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(EntityAlertRecordDoc).Name;
var alert_record_doc = docBase as EntityAlertRecordDoc;
if (null == alert_record_doc)
{
err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, alert_record_doc is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
//=====================================================================================
// New Doc => Origin Doc
//=====================================================================================
syncOriginDocBaseWithNewDoc<EntityAlertRecordAttribute>(alert_record_doc);
//=====================================================================================
// Doc => Attribute
//=====================================================================================
var doc_attrib = alert_record_doc.getAttrib<EntityAlertRecordAttrib>();
NullReferenceCheckHelper.throwIfNull(doc_attrib, () => $"doc_attrib is null !!! - {getOwner().toBasicString()}");
AlertKey = doc_attrib.AlertKey;
AlertedTime = doc_attrib.AlertedTime;
OwnerEntityType = doc_attrib.OwnerEntityType;
OwnerGuid = doc_attrib.OwnerGuid;
EntityAlertTriggerType = doc_attrib.EntityAlertTriggerType;
MetaId = doc_attrib.MetaId;
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 alert_record_attribute = otherEntityAttribute as EntityAlertRecordAttribute;
if (null == alert_record_attribute)
{
err_msg = $"Failed to cast EntityAlertHistoryAttribute !!!, alert_record_attribute is null";
result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
AlertKey = alert_record_attribute.AlertKey;
AlertedTime = alert_record_attribute.AlertedTime;
OwnerEntityType = alert_record_attribute.OwnerEntityType;
OwnerGuid = alert_record_attribute.OwnerGuid;
EntityAlertTriggerType = alert_record_attribute.EntityAlertTriggerType;
MetaId = alert_record_attribute.MetaId;
//=====================================================================================
// Attribute Try Pending Doc => Origin Doc
//=====================================================================================
var try_pending_doc = alert_record_attribute.getTryPendingDocBase() as EntityAlertRecordDoc;
if (null != try_pending_doc)
{
alert_record_attribute.resetTryPendingDocBase();
syncOriginDocBaseWithNewDoc<EntityAlertRecordAttribute>(try_pending_doc);
}
var origin_doc_base = getOriginDocBase<EntityAlertRecordAttribute>();
if (null == origin_doc_base)
{
// DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!!
return result;
}
var alert_record_attrib = origin_doc_base.getAttrib<EntityAlertRecordAttrib>();
NullReferenceCheckHelper.throwIfNull(alert_record_attrib, () => $"alert_record_attrib is null !!! - {owner.toBasicString()}");
alert_record_attrib.AlertKey = AlertKey;
alert_record_attrib.AlertedTime = AlertedTime;
alert_record_attrib.OwnerEntityType = OwnerEntityType;
alert_record_attrib.OwnerGuid = OwnerGuid;
alert_record_attrib.EntityAlertTriggerType = EntityAlertTriggerType;
alert_record_attrib.MetaId = MetaId;
return result;
}
}
public class EntityAlertHistoryAttributeTransactor : EntityAttributeTransactorBase<EntityAlertRecordAttribute>
{
public EntityAlertHistoryAttributeTransactor(EntityBase owner)
: base(owner)
{
}
}
}