Stock.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.ComponentModel.DataAnnotations;
  2. using Domain.Entities.Stocks.ValueObject;
  3. namespace Domain.Entities.Stocks;
  4. /// <summary>
  5. /// 종목 마스터 (KRX 상장종목) — D1 유일 원천.
  6. /// 타 도메인(D2/D4)은 char(6) Code 를 FK 없이 denorm 보관하고 쓰기 시 AnyAsync 사전 검증한다 (d0 §2.1).
  7. /// LastTradingDate~MarketCap 은 목록/quotes 조인 회피용 denorm — 일별 시세 배치가 갱신.
  8. /// </summary>
  9. public class Stock
  10. {
  11. [Key]
  12. public int ID { get; private set; }
  13. /// <summary>단축코드 (char 6, 예: "005930") — UNIQUE</summary>
  14. public string Code { get; private set; } = default!;
  15. /// <summary>표준코드 ISIN (char 12) — 미제공 시 null</summary>
  16. public string? ISIN { get; private set; }
  17. /// <summary>종목명 (예: "삼성전자")</summary>
  18. public string Name { get; private set; } = default!;
  19. /// <summary>영문 종목명 (선택)</summary>
  20. public string? EnglishName { get; private set; }
  21. /// <summary>시장 구분 (1=KOSPI, 2=KOSDAQ, 3=KONEX)</summary>
  22. public StockMarket Market { get; private set; }
  23. /// <summary>거래 상태 (0=정상, 1=거래정지, 2=관리종목)</summary>
  24. public TradingStatus TradingStatus { get; private set; }
  25. /// <summary>DART 고유번호 (char 8) — corpCode 매핑, 미매핑 null (매핑 배치는 M2)</summary>
  26. public string? CorpCode { get; private set; }
  27. /// <summary>KRX 업종명 (선택)</summary>
  28. public string? SectorName { get; private set; }
  29. /// <summary>상장 여부 — 상폐 시 soft-off</summary>
  30. public bool IsActive { get; private set; } = true;
  31. /// <summary>상장일 — 원천 API 미제공으로 최초 수집 기준일(basDt) 근사값</summary>
  32. public DateOnly ListedDate { get; private set; }
  33. /// <summary>상장폐지일 (상폐 시)</summary>
  34. public DateOnly? DelistedDate { get; private set; }
  35. /// <summary>최근 거래일 (denorm)</summary>
  36. public DateOnly? LastTradingDate { get; private set; }
  37. /// <summary>최근 종가 (denorm)</summary>
  38. public int? LastClosePrice { get; private set; }
  39. /// <summary>최근 등락률 % (denorm)</summary>
  40. public decimal? LastChangeRate { get; private set; }
  41. /// <summary>시가총액 (denorm)</summary>
  42. public long? MarketCap { get; private set; }
  43. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  44. public DateTime? UpdatedAt { get; private set; }
  45. private Stock() { }
  46. public static Stock Create(string code, string name, StockMarket market, DateOnly listedDate, string? isin = null, string? corpCode = null, string? englishName = null, string? sectorName = null)
  47. {
  48. if (code is not { Length: 6 } || code.Any(c => !char.IsAsciiDigit(c)))
  49. {
  50. throw new ArgumentException("code must be 6 digits", nameof(code));
  51. }
  52. if (string.IsNullOrWhiteSpace(name))
  53. {
  54. throw new ArgumentException("name required", nameof(name));
  55. }
  56. if (!Enum.IsDefined(market))
  57. {
  58. throw new ArgumentOutOfRangeException(nameof(market));
  59. }
  60. return new Stock
  61. {
  62. Code = code,
  63. Name = name.Trim(),
  64. Market = market,
  65. TradingStatus = TradingStatus.Normal,
  66. ListedDate = listedDate,
  67. ISIN = string.IsNullOrWhiteSpace(isin) ? null : isin.Trim(),
  68. CorpCode = string.IsNullOrWhiteSpace(corpCode) ? null : corpCode.Trim(),
  69. EnglishName = string.IsNullOrWhiteSpace(englishName) ? null : englishName.Trim(),
  70. SectorName = string.IsNullOrWhiteSpace(sectorName) ? null : sectorName.Trim()
  71. };
  72. }
  73. /// <summary>마스터 동기화 — 이름/시장/ISIN(+영문명/업종) 반영. 상폐 상태였다면 재상장으로 복구. englishName/sectorName 은 null 전달 시 미변경.</summary>
  74. public void UpdateMaster(string name, StockMarket market, string? isin, string? englishName = null, string? sectorName = null)
  75. {
  76. var changed = false;
  77. if (!string.IsNullOrWhiteSpace(name) && Name != name.Trim())
  78. {
  79. Name = name.Trim();
  80. changed = true;
  81. }
  82. if (Enum.IsDefined(market) && Market != market)
  83. {
  84. Market = market;
  85. changed = true;
  86. }
  87. var normalizedIsin = string.IsNullOrWhiteSpace(isin) ? null : isin.Trim();
  88. if (normalizedIsin is not null && ISIN != normalizedIsin)
  89. {
  90. ISIN = normalizedIsin;
  91. changed = true;
  92. }
  93. var normalizedEnglishName = string.IsNullOrWhiteSpace(englishName) ? null : englishName.Trim();
  94. if (normalizedEnglishName is not null && EnglishName != normalizedEnglishName)
  95. {
  96. EnglishName = normalizedEnglishName;
  97. changed = true;
  98. }
  99. var normalizedSectorName = string.IsNullOrWhiteSpace(sectorName) ? null : sectorName.Trim();
  100. if (normalizedSectorName is not null && SectorName != normalizedSectorName)
  101. {
  102. SectorName = normalizedSectorName;
  103. changed = true;
  104. }
  105. if (!IsActive)
  106. {
  107. IsActive = true;
  108. DelistedDate = null;
  109. changed = true;
  110. }
  111. if (changed)
  112. {
  113. UpdatedAt = DateTime.UtcNow;
  114. }
  115. }
  116. /// <summary>상장폐지 — soft-off (row 는 보존, 과거 시세/참조 유지)</summary>
  117. public void MarkDelisted(DateOnly date)
  118. {
  119. if (!IsActive)
  120. {
  121. return;
  122. }
  123. IsActive = false;
  124. DelistedDate = date;
  125. UpdatedAt = DateTime.UtcNow;
  126. }
  127. public void SetTradingStatus(TradingStatus status)
  128. {
  129. if (!Enum.IsDefined(status) || TradingStatus == status)
  130. {
  131. return;
  132. }
  133. TradingStatus = status;
  134. UpdatedAt = DateTime.UtcNow;
  135. }
  136. public void SetCorpCode(string? corpCode)
  137. {
  138. var normalized = string.IsNullOrWhiteSpace(corpCode) ? null : corpCode.Trim();
  139. if (CorpCode == normalized)
  140. {
  141. return;
  142. }
  143. CorpCode = normalized;
  144. UpdatedAt = DateTime.UtcNow;
  145. }
  146. /// <summary>일별 시세 배치가 최근 시세 denorm 을 갱신. 과거 일자 재수집은 무시.</summary>
  147. public void UpdateLastPrice(DateOnly tradingDate, int closePrice, decimal changeRate, long? marketCap)
  148. {
  149. if (LastTradingDate.HasValue && tradingDate < LastTradingDate.Value)
  150. {
  151. return;
  152. }
  153. LastTradingDate = tradingDate;
  154. LastClosePrice = closePrice;
  155. LastChangeRate = changeRate;
  156. MarketCap = marketCap;
  157. UpdatedAt = DateTime.UtcNow;
  158. }
  159. }