Files
zszq-trs/YLErpWeb/Hubs/SwapConfirmSendEmailHub.cs
T

113 lines
4.7 KiB
C#

using Microsoft.AspNetCore.SignalR;
using YLErp.DBModels;
using YLErp.Enums;
using YLErp.MailKit;
using YLErp.Modules.RiskModule;
using YLErp.Modules.SwapModule;
using static YLErp.ConsGlobal;
namespace YLErp.Web.Hubs
{
public class SwapConfirmSendEmailHub : Hub
{
private static bool isProcessing = false;
protected static IYcLogger Log = LogFactory.GetLogger(typeof(SwapConfirmSendEmailHub).FullName);
static readonly Dictionary<string, DateTime> _dic = new Dictionary<string, DateTime>(StringComparer.OrdinalIgnoreCase);
static readonly Dictionary<string, string> _clientProgressDic = new Dictionary<string, string>();
public async Task StartProcessing(string jsonString)
{
SwapTradeSendEmailReq req = JsonHelper.Deserialize<SwapTradeSendEmailReq>(jsonString);
var connectionId = Context.ConnectionId;
var client = Clients.Client(connectionId);
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;
}
var service = new SwapEndConfirmService(user);
if (isProcessing)
{
await client.SendAsync("ExceptionMessage", "正在发送邮件,请稍后再试");
foreach (var contractCode in req.contractCodes) {
var msg= GetProcess(contractCode, async (d) => {
await client.SendAsync("UpdateProgress", getUpdateProcess(contractCode, d));
});
await client.SendAsync("UpdateProgress", getUpdateProcess(contractCode, msg));
}
await client.SendAsync("ProcessCompleted", "");
return;
}
try
{
isProcessing = true;
_clientProgressDic.Clear();
_dic.Clear();
//根据tradeIds 获取contractCode
foreach (var contractCode in req.contractCodes)
{
_clientProgressDic[contractCode] = "发送中";
await client.SendAsync("UpdateProgress", getUpdateProcess(contractCode, "发送中"));
if (PS.Config.ErpElement.MailMessageRateLimit > 0)
{
var milliSeconds = 60d * 1000 / PS.Config.ErpElement.MailMessageRateLimit;
lock (_dic)
{
if (_dic.TryGetValue("sendEmail", out var dt))
{
while (dt < DateTime.Now && dt.AddMilliseconds(milliSeconds) > DateTime.Now)
{
Thread.Sleep(500);
}
}
_dic["sendEmail"] = DateTime.Now;
}
}
var result = service.SendConfirmEamil(contractCode);
_clientProgressDic[contractCode] = result;
await client.SendAsync("UpdateProgress", getUpdateProcess(contractCode, result));
}
isProcessing = false;
await client.SendAsync("ProcessCompleted", "");
}
catch (Exception ex)
{
Log.Error("确认书发邮件报错", ex);
isProcessing = false;
await client.SendAsync("ExceptionMessage", ex.Message);
}
}
private string GetProcess(string contractCode, Action<string> action)
{
if (_clientProgressDic.ContainsKey(contractCode))
{
string process= _clientProgressDic[contractCode];
if (action != null&& process=="发送中")
{
action.Invoke(process);
Thread.Sleep(1000);
return GetProcess(contractCode, action);
}
return process;
}
return "";
}
private string getUpdateProcess(string contractCode, string msg)
{
SwapTradeSendEmailResp resp = new SwapTradeSendEmailResp()
{
contractCode = contractCode,
send_email_result = msg
};
return JsonHelper.Serialize(resp);
}
}
}