Config.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web.Script.Serialization;
  6. namespace DRG.DRGInterfaceClient
  7. {
  8. /// <summary>医疗机构信息项(来自 01010064)。</summary>
  9. public class HospitalInfoItem
  10. {
  11. public string id { get; set; }
  12. public string hospCode { get; set; }
  13. /// <summary>医保机构编码(OrganizationCode),用于 session.hospYBCode 与 --sync 匹配。</summary>
  14. public string code { get; set; }
  15. public string descripts { get; set; }
  16. public string MedinsLv { get; set; }
  17. internal static object Parse(Dictionary<string, object> d)
  18. {
  19. throw new NotImplementedException();
  20. }
  21. }
  22. /// <summary>同步配置(来自 02010079)。</summary>
  23. public class SyncConfig
  24. {
  25. public string syncType { get; set; }
  26. public string frequency { get; set; }
  27. public string scheduledTime { get; set; }
  28. public int lookbackDays { get; set; }
  29. public bool enabled { get; set; }
  30. public string synFlag { get; set; }
  31. public string warningFlag { get; set; }
  32. public string status { get; set; }
  33. public string errorMessage { get; set; }
  34. internal static SyncConfig Parse(object p)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. }
  39. /// <summary>session 中非机构字段的默认值(来自 appsettings.json)。</summary>
  40. public class SessionDefaults
  41. {
  42. public string userID { get; set; }
  43. public string userCode { get; set; }
  44. public string userName { get; set; }
  45. public string locID { get; set; }
  46. public string locDesc { get; set; }
  47. public string groupID { get; set; }
  48. public string groupDesc { get; set; }
  49. public string sessionID { get; set; }
  50. public string userYBCode { get; set; }
  51. public string provID { get; set; }
  52. public string cityID { get; set; }
  53. public string areaCode { get; set; }
  54. public string path { get; set; }
  55. public int langID { get; set; }
  56. public string langDesc { get; set; }
  57. public string changeFlag { get; set; }
  58. public string changeDesc { get; set; }
  59. public string lastLoginDate { get; set; }
  60. public string lastLoginTime { get; set; }
  61. public string directorAuth { get; set; }
  62. public string defaultMenuType { get; set; }
  63. public string titleDesc { get; set; }
  64. public string errorMessageTime { get; set; }
  65. public string language { get; set; }
  66. public int messageTime { get; set; }
  67. }
  68. public class AppConfig
  69. {
  70. public string Endpoint { get; set; }
  71. public string AuthToken { get; set; }
  72. /// <summary>已勾选/定时要同步的医疗机构 code(医保编码)列表,支持多机构。</summary>
  73. public List<string> ScheduledHospCodes { get; set; }
  74. /// <summary>已废弃:旧版单机构定时字段,仅用于加载旧配置时迁移到 ScheduledHospCodes。</summary>
  75. public string ScheduledHospCode { get; set; }
  76. public string TaskName { get; set; }
  77. public int MaxLogSizeMB { get; set; }
  78. public int FullSyncStartDays { get; set; }
  79. public SessionDefaults SessionDefaults { get; set; }
  80. }
  81. /// <summary>appsettings.json 加载与运行时单例。</summary>
  82. public static class Config
  83. {
  84. private static AppConfig _current;
  85. private static readonly string ConfigPath =
  86. Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
  87. public static AppConfig Current
  88. {
  89. get { if (_current == null) _current = Load(); return _current; }
  90. }
  91. public static AppConfig Load()
  92. {
  93. // 先置默认,避免加载异常时 Logger -> Config.Current 产生重入递归
  94. if (_current == null) _current = Default();
  95. try
  96. {
  97. if (File.Exists(ConfigPath))
  98. {
  99. string json = File.ReadAllText(ConfigPath);
  100. var cfg = new JavaScriptSerializer().Deserialize<AppConfig>(json);
  101. if (cfg != null)
  102. {
  103. if (cfg.SessionDefaults == null) cfg.SessionDefaults = new SessionDefaults();
  104. if (cfg.ScheduledHospCodes == null) cfg.ScheduledHospCodes = new List<string>();
  105. // 旧版单机构配置迁移:把 ScheduledHospCode 并入 ScheduledHospCodes
  106. if (!string.IsNullOrWhiteSpace(cfg.ScheduledHospCode)
  107. && !cfg.ScheduledHospCodes.Contains(cfg.ScheduledHospCode, StringComparer.OrdinalIgnoreCase))
  108. cfg.ScheduledHospCodes.Add(cfg.ScheduledHospCode);
  109. _current = cfg;
  110. }
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. Logger.Error("加载 appsettings.json 失败,使用默认值", ex);
  116. }
  117. return _current;
  118. }
  119. public static AppConfig Default()
  120. {
  121. return new AppConfig
  122. {
  123. Endpoint = "http://127.0.0.1:52773/csp/drg/sysInternalMutiple",
  124. AuthToken = "X1NZU1RFTTpwcnlrQDIwMjA=",
  125. ScheduledHospCodes = new List<string>(),
  126. TaskName = "DRGInterfaceClientSync",
  127. MaxLogSizeMB = 5,
  128. FullSyncStartDays = 365,
  129. SessionDefaults = new SessionDefaults
  130. {
  131. userID = "158",
  132. userCode = "admin",
  133. userName = "admin",
  134. locID = "1",
  135. locDesc = "DRG平台",
  136. groupID = "1",
  137. groupDesc = "管理员",
  138. sessionID = "",
  139. userYBCode = "",
  140. provID = "12",
  141. cityID = "98",
  142. areaCode = "340100",
  143. path = "",
  144. langID = 1,
  145. langDesc = "简体中文",
  146. changeFlag = "N",
  147. changeDesc = "",
  148. lastLoginDate = "",
  149. lastLoginTime = "",
  150. directorAuth = "N",
  151. defaultMenuType = "",
  152. titleDesc = "",
  153. errorMessageTime = "",
  154. language = "CN",
  155. messageTime = 1
  156. }
  157. };
  158. }
  159. public static void Save()
  160. {
  161. try
  162. {
  163. var json = new JavaScriptSerializer().Serialize(_current);
  164. File.WriteAllText(ConfigPath, json);
  165. }
  166. catch (Exception ex)
  167. {
  168. Logger.Error("保存 appsettings.json 失败", ex);
  169. }
  170. }
  171. }
  172. }