using Microsoft.AspNetCore.SignalR; using YLErp.DBModels; using YLErp.Enums; using YLErp.MailKit; using YLErp.Model; using YLErp.Modules.RiskModule; using YLErp.Modules.SwapModule; using YLErp.Modules.TradeModule; using static YLErp.ConsGlobal; namespace YLErp.Web.Hubs { public class TradeConfirmSendOAHub : Hub { private static bool isProcessing = false; protected static IYcLogger Log = LogFactory.GetLogger(typeof(TradeConfirmSendOAHub).FullName); static readonly Dictionary _dic = new Dictionary(StringComparer.OrdinalIgnoreCase); private static readonly Dictionary _clientProgressDic = new Dictionary(); public async Task StartProcessing(string jsonString) { var req = JsonHelper.Deserialize(jsonString); var client = Clients.Caller; if (Context.User == null) { await client.SendAsync("ExceptionMessage", "登录已失效,请重新登录"); return; } var user = Server.CacheProvider.Get("loginUser^" + Context.User.GetUserId()) as UserInfo; if (user == null) { await client.SendAsync("ExceptionMessage", "登录已失效,请重新登录"); return; } if (isProcessing) { await client.SendAsync("ExceptionMessage", "正在发送OA,请稍后再试"); return; } isProcessing = true; try { await ProcessOAByIds(req.ids.ToString(), user); isProcessing = false; await Clients.Caller.SendAsync("ProcessCompleted", ""); } catch (Exception ex) { Log.Error(ex.Message, ex); isProcessing = false; await Clients.Caller.SendAsync("ExceptionMessage", ex.Message); } } private async Task ProcessOAByIds(string idsJson, UserInfo user) { var ids = JsonHelper.Deserialize>(idsJson); foreach (var id in ids) { var key = id.ToString(); _clientProgressDic[key] = "提交中"; await Clients.All.SendAsync("UpdateProgress", GetUpdateProcess(key, "提交中")); var result = await ProcessSingleOA(id, user); _clientProgressDic[key] = result; await Clients.All.SendAsync("UpdateProgress", GetUpdateProcess(key, result)); } } private async Task ProcessSingleOA(int id, UserInfo user) { try { var oaService = new TradeOAService(user); var result = await oaService.ProcessSingleOAAsync(id); return result.Success ? "提交成功" : result.Message; } catch (Exception ex) { Log.Error($"处理交易ID {id} 的OA时发生错误: {ex.Message}", ex); return $"提交失败: {ex.Message}"; } } private string GetUpdateProcess(string key, string msg) { var resp = new { key = key, send_oa_result = msg }; return JsonHelper.Serialize(resp); } } }