| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace DRG.DRGInterfaceClient
- {
- /// <summary>--sync 无头模式:供 Windows 任务计划程序调用,完成一次同步后退出。</summary>
- internal static class HeadlessRunner
- {
- public static async Task<int> RunOnceAsync()
- {
- try
- {
- var codes = Config.Current.ScheduledHospCodes;
- if (codes == null || codes.Count == 0)
- {
- Logger.Error("ScheduledHospCodes 为空,无法执行 --sync(请在 GUI 勾选机构并「设为定时机构」)");
- return 1;
- }
- if (string.IsNullOrWhiteSpace(Config.Current.Endpoint))
- {
- Logger.Error("接口地址(Endpoint)为空,无法执行 --sync");
- return 1;
- }
- var hospResp = await DrgApiClient.QueryHospitalInfoAsync("", "",
- SessionBuilder.BuildDefault(Config.Current.SessionDefaults));
- if (!DrgApiClient.IsOk(hospResp))
- {
- Logger.Error("加载机构失败: " + DrgApiClient.GetError(hospResp));
- return 1;
- }
- var all = new List<HospitalInfoItem>();
- var arr = DrgApiClient.GetResultArray(hospResp);
- if (arr != null)
- {
- foreach (var o in arr)
- {
- if (o is Dictionary<string, object> d)
- all.Add(DrgApiClient.ParseHospital(d));
- }
- }
- int ok = 0, fail = 0;
- foreach (var schedCode in codes)
- {
- var hosp = all.Find(h => string.Equals(h.code, schedCode, StringComparison.OrdinalIgnoreCase));
- if (hosp == null)
- {
- Logger.Error("按 code(医保编码) 匹配不到机构: " + schedCode);
- fail++;
- continue;
- }
- var session = SessionBuilder.Build(hosp, Config.Current.SessionDefaults);
- var cfgResp = await DrgApiClient.GetSyncConfigAsync(hosp.hospCode, session);
- if (!DrgApiClient.IsOk(cfgResp))
- {
- Logger.Error("机构 " + hosp.descripts + " 配置加载失败: " + DrgApiClient.GetError(cfgResp));
- fail++;
- continue;
- }
- var cfg = DrgApiClient.ParseSyncConfig(DrgApiClient.GetResult(cfgResp));
- var (st, ed) = DrgApiClient.ComputeDateRange(
- cfg.syncType, cfg.lookbackDays, Config.Current.FullSyncStartDays);
- // 跨度超过一天时按天拆分,每天一次 02010067,避免大区间请求超时
- var batches = DrgApiClient.SplitByDay(st, ed);
- Logger.Info(string.Format("机构 {0} 区间[{1},{2}] 拆分为 {3} 批(按天执行 02010067)",
- hosp.descripts, st, ed, batches.Count));
- int dayOk = 0, dayFail = 0;
- foreach (var b in batches)
- {
- var syncResp = await DrgApiClient.GetInpatientDataAsync(
- hosp.hospCode, b.Item1, b.Item2, cfg.synFlag ?? "Y", session);
- if (DrgApiClient.IsOk(syncResp))
- {
- dayOk++;
- Logger.Info(string.Format("机构 {0} 日期 {1} 同步成功", hosp.descripts, b.Item1));
- }
- else
- {
- dayFail++;
- Logger.Error(string.Format("机构 {0} 日期 {1} 同步失败: {2}",
- hosp.descripts, b.Item1, DrgApiClient.GetError(syncResp)));
- }
- }
- if (dayFail == 0)
- {
- Logger.Info(string.Format("机构 {0} 同步成功({1} 天全部成功)", hosp.descripts, dayOk));
- ok++;
- }
- else
- {
- Logger.Error(string.Format("机构 {0} 同步完成但存在失败:成功 {1} 天,失败 {2} 天",
- hosp.descripts, dayOk, dayFail));
- fail++;
- }
- }
- Logger.Info(string.Format("sync 完成:成功 {0} 家,失败 {1} 家(共 {2} 家)", ok, fail, codes.Count));
- return fail > 0 ? 1 : 0;
- }
- catch (Exception ex)
- {
- Logger.Error("--sync 执行异常", ex);
- return 1;
- }
- }
- }
- }
|