Order.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Domain.Entities.Members;
  4. using Domain.Entities.Store.ValueObject;
  5. namespace Domain.Entities.Store;
  6. /// <summary>
  7. /// 상점 주문 헤더. 단일 결제 = 단일 Order.
  8. /// 결제 시점에 수익 배분 (PlatformFee/GameRevenue/ChannelReward) 동결.
  9. /// 채널 미선택 주문은 ChannelRewardAmount=0, ChannelID=null. ChannelReward 만큼 PlatformFee 가 증가 (antooza 100%).
  10. /// </summary>
  11. public class Order
  12. {
  13. [ForeignKey(nameof(MemberID))]
  14. public virtual Member? Member { get; private set; }
  15. [ForeignKey(nameof(ChannelID))]
  16. public virtual Channel? Channel { get; private set; }
  17. private readonly List<OrderItem> _items = [];
  18. public IReadOnlyCollection<OrderItem> Items => _items;
  19. private readonly List<OrderRefund> _refunds = [];
  20. public IReadOnlyCollection<OrderRefund> Refunds => _refunds;
  21. [Key]
  22. public int ID { get; private set; }
  23. /// <summary>주문 번호 (예: "20260522-A1B2C3"). UNIQUE. 사용자 표시용.</summary>
  24. public string OrderNumber { get; private set; } = default!;
  25. public int MemberID { get; private set; }
  26. /// <summary>선물 수신자 회원 ID (선택). null 이면 본인 구매. 값이 있으면 발급 대상은 수신자, 결제자는 MemberID.</summary>
  27. public int? GiftToMemberID { get; private set; }
  28. /// <summary>선물 메시지 (선택, 최대 200자).</summary>
  29. public string? GiftMessage { get; private set; }
  30. /// <summary>후원 채널 (선택). null 이면 antooza 100% 수익.</summary>
  31. public int? ChannelID { get; private set; }
  32. public int TotalAmount { get; private set; }
  33. /// <summary>antooza 몫 (= TotalAmount - GameRevenueAmount - ChannelRewardAmount).</summary>
  34. public int PlatformFeeAmount { get; private set; }
  35. /// <summary>게임사 정산 큐 적재 금액 (= TotalAmount × Game.CommissionRate / 100).</summary>
  36. public int GameRevenueAmount { get; private set; }
  37. /// <summary>[비활성] 채널 보상 입금 금액 — 채널 보상 적립 제거됨(2026-07-03), 과거 주문 호환용 유지. 신규 주문은 항상 0.</summary>
  38. public int ChannelRewardAmount { get; private set; }
  39. public OrderStatus Status { get; private set; } = OrderStatus.Pending;
  40. public string? CancelReason { get; private set; }
  41. public string? RefundReason { get; private set; }
  42. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  43. public DateTime? PaidAt { get; private set; }
  44. [Timestamp]
  45. public byte[] RowVersion { get; private set; } = default!;
  46. private Order() { }
  47. public static Order Create(
  48. string orderNumber,
  49. int memberID,
  50. int totalAmount,
  51. int? channelID,
  52. int platformFeeAmount,
  53. int gameRevenueAmount,
  54. int channelRewardAmount,
  55. int? giftToMemberID = null,
  56. string? giftMessage = null
  57. ) {
  58. if (string.IsNullOrWhiteSpace(orderNumber))
  59. {
  60. throw new ArgumentException("OrderNumber is required.", nameof(orderNumber));
  61. }
  62. if (orderNumber.Length > 20)
  63. {
  64. throw new ArgumentOutOfRangeException(nameof(orderNumber));
  65. }
  66. if (memberID <= 0)
  67. {
  68. throw new ArgumentOutOfRangeException(nameof(memberID));
  69. }
  70. if (giftToMemberID is <= 0)
  71. {
  72. throw new ArgumentOutOfRangeException(nameof(giftToMemberID));
  73. }
  74. if (giftToMemberID == memberID)
  75. {
  76. throw new ArgumentException("본인에게는 선물할 수 없습니다.", nameof(giftToMemberID));
  77. }
  78. var trimmedGiftMessage = string.IsNullOrWhiteSpace(giftMessage) ? null : giftMessage.Trim();
  79. if (trimmedGiftMessage is { Length: > 200 })
  80. {
  81. throw new ArgumentOutOfRangeException(nameof(giftMessage), "선물 메시지는 200자 이하여야 합니다.");
  82. }
  83. if (totalAmount <= 0)
  84. {
  85. throw new ArgumentOutOfRangeException(nameof(totalAmount));
  86. }
  87. if (platformFeeAmount < 0 || gameRevenueAmount < 0 || channelRewardAmount < 0)
  88. {
  89. throw new ArgumentOutOfRangeException(null, "수익 배분 금액은 음수 불가.");
  90. }
  91. if (platformFeeAmount + gameRevenueAmount + channelRewardAmount != totalAmount)
  92. {
  93. throw new ArgumentException("PlatformFee + GameRevenue + ChannelReward 의 합이 TotalAmount 와 일치해야 합니다.");
  94. }
  95. if (channelID is null && channelRewardAmount > 0)
  96. {
  97. throw new ArgumentException("채널 미선택 주문은 ChannelRewardAmount=0 이어야 합니다.");
  98. }
  99. return new Order
  100. {
  101. OrderNumber = orderNumber,
  102. MemberID = memberID,
  103. GiftToMemberID = giftToMemberID,
  104. GiftMessage = trimmedGiftMessage,
  105. ChannelID = channelID,
  106. TotalAmount = totalAmount,
  107. PlatformFeeAmount = platformFeeAmount,
  108. GameRevenueAmount = gameRevenueAmount,
  109. ChannelRewardAmount = channelRewardAmount
  110. };
  111. }
  112. /// <summary>선물 주문 여부.</summary>
  113. public bool IsGift => GiftToMemberID is not null;
  114. /// <summary>아이템 발급 대상 회원 ID (선물이면 수신자, 아니면 구매자).</summary>
  115. public int RecipientMemberID => GiftToMemberID ?? MemberID;
  116. public void AddItem(OrderItem item)
  117. {
  118. ArgumentNullException.ThrowIfNull(item);
  119. _items.Add(item);
  120. }
  121. public void MarkPaid()
  122. {
  123. if (Status != OrderStatus.Pending)
  124. {
  125. throw new InvalidOperationException($"Cannot mark paid from status {Status}.");
  126. }
  127. Status = OrderStatus.Paid;
  128. PaidAt = DateTime.UtcNow;
  129. }
  130. /// <summary>Digital 상품 단독 주문 시 Paid → Delivered 점프.</summary>
  131. public void MarkDelivered()
  132. {
  133. if (Status is not OrderStatus.Paid and not OrderStatus.Shipped)
  134. {
  135. throw new InvalidOperationException($"Cannot mark delivered from status {Status}.");
  136. }
  137. Status = OrderStatus.Delivered;
  138. }
  139. public void MarkPreparing()
  140. {
  141. if (Status != OrderStatus.Paid)
  142. {
  143. throw new InvalidOperationException($"Cannot mark preparing from status {Status}.");
  144. }
  145. Status = OrderStatus.Preparing;
  146. }
  147. public void MarkShipped()
  148. {
  149. if (Status is not OrderStatus.Paid and not OrderStatus.Preparing)
  150. {
  151. throw new InvalidOperationException($"Cannot mark shipped from status {Status}.");
  152. }
  153. Status = OrderStatus.Shipped;
  154. }
  155. public void Cancel(string reason)
  156. {
  157. if (Status is not OrderStatus.Pending and not OrderStatus.Paid)
  158. {
  159. throw new InvalidOperationException($"Cannot cancel from status {Status}.");
  160. }
  161. if (string.IsNullOrWhiteSpace(reason))
  162. {
  163. throw new ArgumentException("Cancel reason is required.", nameof(reason));
  164. }
  165. Status = OrderStatus.Cancelled;
  166. CancelReason = reason;
  167. }
  168. public void MarkRefunded(string reason)
  169. {
  170. if (string.IsNullOrWhiteSpace(reason))
  171. {
  172. throw new ArgumentException("Refund reason is required.", nameof(reason));
  173. }
  174. Status = OrderStatus.Refunded;
  175. RefundReason = reason;
  176. }
  177. public void MarkExchanged()
  178. {
  179. Status = OrderStatus.Exchanged;
  180. }
  181. }