PaperLedger.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Domain.Entities.Paper.ValueObject;
  4. namespace Domain.Entities.Paper;
  5. /// <summary>
  6. /// 모의투자 계좌 입출금 감사 원장 — 지갑 토큰 ↔ 계좌 토큰 이동 이력 (d4 §③).
  7. /// WalletTxRefID 로 지갑 거래(WalletTransaction.RefID)와 연결.
  8. /// </summary>
  9. public class PaperLedger
  10. {
  11. [ForeignKey(nameof(AccountID))]
  12. public virtual PaperAccount Account { get; private set; } = null!;
  13. [Key]
  14. public int ID { get; private set; }
  15. public int AccountID { get; private set; }
  16. public PaperLedgerType Type { get; private set; }
  17. /// <summary>이동 토큰 (18,0)</summary>
  18. public decimal TokenAmount { get; private set; }
  19. /// <summary>좌수 변화 (입금 +발행 / 출금 -소각, 18,8)</summary>
  20. public decimal UnitsDelta { get; private set; }
  21. /// <summary>연결된 지갑 거래 RefID</summary>
  22. public string? WalletTxRefID { get; private set; }
  23. /// <summary>이동 후 계좌 자유 토큰 잔액</summary>
  24. public decimal BalanceAfter { get; private set; }
  25. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  26. private PaperLedger() { }
  27. public static PaperLedger Create(
  28. int accountID,
  29. PaperLedgerType type,
  30. decimal tokenAmount,
  31. decimal unitsDelta,
  32. string? walletTxRefID,
  33. decimal balanceAfter
  34. ) {
  35. if (accountID <= 0)
  36. {
  37. throw new ArgumentOutOfRangeException(nameof(accountID));
  38. }
  39. if (tokenAmount <= 0)
  40. {
  41. throw new ArgumentOutOfRangeException(nameof(tokenAmount));
  42. }
  43. return new PaperLedger
  44. {
  45. AccountID = accountID,
  46. Type = type,
  47. TokenAmount = tokenAmount,
  48. UnitsDelta = unitsDelta,
  49. WalletTxRefID = walletTxRefID,
  50. BalanceAfter = balanceAfter
  51. };
  52. }
  53. /// <summary>
  54. /// 신규 계좌(ID 미확정) 케이스 — Account 내비게이션으로 생성해 EF 가 저장 시 FK 를 자동 채우게 한다.
  55. /// </summary>
  56. public static PaperLedger CreateFor(
  57. PaperAccount account,
  58. PaperLedgerType type,
  59. decimal tokenAmount,
  60. decimal unitsDelta,
  61. string? walletTxRefID,
  62. decimal balanceAfter
  63. ) {
  64. ArgumentNullException.ThrowIfNull(account);
  65. if (tokenAmount <= 0)
  66. {
  67. throw new ArgumentOutOfRangeException(nameof(tokenAmount));
  68. }
  69. return new PaperLedger
  70. {
  71. Account = account,
  72. Type = type,
  73. TokenAmount = tokenAmount,
  74. UnitsDelta = unitsDelta,
  75. WalletTxRefID = walletTxRefID,
  76. BalanceAfter = balanceAfter
  77. };
  78. }
  79. }