using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Script.Serialization;
namespace DRG.DRGInterfaceClient
{
/// 医疗机构信息项(来自 01010064)。
public class HospitalInfoItem
{
public string id { get; set; }
public string hospCode { get; set; }
/// 医保机构编码(OrganizationCode),用于 session.hospYBCode 与 --sync 匹配。
public string code { get; set; }
public string descripts { get; set; }
public string MedinsLv { get; set; }
internal static object Parse(Dictionary d)
{
throw new NotImplementedException();
}
}
/// 同步配置(来自 02010079)。
public class SyncConfig
{
public string syncType { get; set; }
public string frequency { get; set; }
public string scheduledTime { get; set; }
public int lookbackDays { get; set; }
public bool enabled { get; set; }
public string synFlag { get; set; }
public string warningFlag { get; set; }
public string status { get; set; }
public string errorMessage { get; set; }
internal static SyncConfig Parse(object p)
{
throw new NotImplementedException();
}
}
/// session 中非机构字段的默认值(来自 appsettings.json)。
public class SessionDefaults
{
public string userID { get; set; }
public string userCode { get; set; }
public string userName { get; set; }
public string locID { get; set; }
public string locDesc { get; set; }
public string groupID { get; set; }
public string groupDesc { get; set; }
public string sessionID { get; set; }
public string userYBCode { get; set; }
public string provID { get; set; }
public string cityID { get; set; }
public string areaCode { get; set; }
public string path { get; set; }
public int langID { get; set; }
public string langDesc { get; set; }
public string changeFlag { get; set; }
public string changeDesc { get; set; }
public string lastLoginDate { get; set; }
public string lastLoginTime { get; set; }
public string directorAuth { get; set; }
public string defaultMenuType { get; set; }
public string titleDesc { get; set; }
public string errorMessageTime { get; set; }
public string language { get; set; }
public int messageTime { get; set; }
}
public class AppConfig
{
public string Endpoint { get; set; }
public string AuthToken { get; set; }
/// 已勾选/定时要同步的医疗机构 code(医保编码)列表,支持多机构。
public List ScheduledHospCodes { get; set; }
/// 已废弃:旧版单机构定时字段,仅用于加载旧配置时迁移到 ScheduledHospCodes。
public string ScheduledHospCode { get; set; }
public string TaskName { get; set; }
public int MaxLogSizeMB { get; set; }
public int FullSyncStartDays { get; set; }
public SessionDefaults SessionDefaults { get; set; }
}
/// appsettings.json 加载与运行时单例。
public static class Config
{
private static AppConfig _current;
private static readonly string ConfigPath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
public static AppConfig Current
{
get { if (_current == null) _current = Load(); return _current; }
}
public static AppConfig Load()
{
// 先置默认,避免加载异常时 Logger -> Config.Current 产生重入递归
if (_current == null) _current = Default();
try
{
if (File.Exists(ConfigPath))
{
string json = File.ReadAllText(ConfigPath);
var cfg = new JavaScriptSerializer().Deserialize(json);
if (cfg != null)
{
if (cfg.SessionDefaults == null) cfg.SessionDefaults = new SessionDefaults();
if (cfg.ScheduledHospCodes == null) cfg.ScheduledHospCodes = new List();
// 旧版单机构配置迁移:把 ScheduledHospCode 并入 ScheduledHospCodes
if (!string.IsNullOrWhiteSpace(cfg.ScheduledHospCode)
&& !cfg.ScheduledHospCodes.Contains(cfg.ScheduledHospCode, StringComparer.OrdinalIgnoreCase))
cfg.ScheduledHospCodes.Add(cfg.ScheduledHospCode);
_current = cfg;
}
}
}
catch (Exception ex)
{
Logger.Error("加载 appsettings.json 失败,使用默认值", ex);
}
return _current;
}
public static AppConfig Default()
{
return new AppConfig
{
Endpoint = "http://127.0.0.1:52773/csp/drg/sysInternalMutiple",
AuthToken = "X1NZU1RFTTpwcnlrQDIwMjA=",
ScheduledHospCodes = new List(),
TaskName = "DRGInterfaceClientSync",
MaxLogSizeMB = 5,
FullSyncStartDays = 365,
SessionDefaults = new SessionDefaults
{
userID = "158",
userCode = "admin",
userName = "admin",
locID = "1",
locDesc = "DRG平台",
groupID = "1",
groupDesc = "管理员",
sessionID = "",
userYBCode = "",
provID = "12",
cityID = "98",
areaCode = "340100",
path = "",
langID = 1,
langDesc = "简体中文",
changeFlag = "N",
changeDesc = "",
lastLoginDate = "",
lastLoginTime = "",
directorAuth = "N",
defaultMenuType = "",
titleDesc = "",
errorMessageTime = "",
language = "CN",
messageTime = 1
}
};
}
public static void Save()
{
try
{
var json = new JavaScriptSerializer().Serialize(_current);
File.WriteAllText(ConfigPath, json);
}
catch (Exception ex)
{
Logger.Error("保存 appsettings.json 失败", ex);
}
}
}
}