using Newtonsoft.Json; using YLErp.Abstract; using YLErp.BLL.Eod; using YLErp.Cache; using YLErp.Helpers; using YLErp.Modules.ClientModule; using YLErp.Modules.EodModule.QueryModule; using YLErp.Modules.SwapModule; namespace YLErp.Web.App.KafkaTask { /// /// TRS资金计算消费 /// public class CashCalcConsumerKafkaTask : IHostedService, IDisposable { private readonly IYcLogger _logger; private readonly CancellationTokenSource _cts = new CancellationTokenSource(); private KafkaConsumerHelper _cashCalcConsumer; private string cashCalcTopic = string.Empty; private string cashCalcGroupId = string.Empty; private string onRspAccountCapitalTopic=string.Empty; private IYLCache _yLCache; private IKafkaProduce kafkaProduceHelper; public CashCalcConsumerKafkaTask(IYLCache yLCache, IKafkaProduce kafkaProduce) { _logger = LogFactory.GetLogger("CashCalcConsumerKafkaTask"); cashCalcTopic = Environment.GetEnvironmentVariable("KafkaConfig_YiLian_CashCalcConsumerTopic"); cashCalcGroupId = Environment.GetEnvironmentVariable("KafkaConfig_YiLian_CashCalcConsumerGroup"); onRspAccountCapitalTopic = Environment.GetEnvironmentVariable("KafkaConfig_OnRspAccountCapitalTopic"); _cashCalcConsumer = new KafkaConsumerHelper(cashCalcGroupId, cashCalcTopic); _yLCache = yLCache; kafkaProduceHelper = kafkaProduce; } public void Dispose() { _cts.Dispose(); } public Task StartAsync(CancellationToken cancellationToken) { Task.Run(() => ExecuteTask(_cts.Token), _cts.Token); return Task.CompletedTask; } public async Task StopAsync(CancellationToken cancellationToken) { _cts.Cancel(); await Task.CompletedTask; } private void ExecuteTask(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { try { _cashCalcConsumer.Subscribe(msg => { if (!string.IsNullOrEmpty(msg)) { var result = JsonHelper.Deserialize(msg); if (result == null) { _logger.Error("资金计算失败", new Exception("缺少客户信息")); return; } using var clientDb = new ClientDBContext(); var client = clientDb.client.FirstOrDefault(x => x.id == result.clientId); if (client == null || client.SwapTradeType == 0) { _logger.Error("资金计算失败", new Exception("找不到客户信息或客户不是dma")); return; } //系统交易日 var valuedate = valuedateBLL.ValueDate; //获取根据系统时间 var lastBalanceDate = EodOperationBase.GetLastSettlementDate(valuedate); var cbs = new RealTimeClientBanlanceService(new OptUserInfo(0, "实时客户资金服务", OptUserFrom.Service)).GetBanlances(new List() { result.clientId }, valuedateBLL.ValueDate, calcDate: valuedateBLL.ValueDate); var cb = cbs.FirstOrDefault(); if (cb!=null) { cb.AvailableAmount = cb.AmountFund + cb.TotalCredit + cb.PayableMargin + cb.GuaranteesTotalAmount; var obj = new ClientBalanceForTrsResponse { TotalAmountTotal = cb.RoundedTotalAmountTotal, AvailableAmount = Math.Round(cb.AvailableAmount, 2), PositionPv = cb.RoundedPositionPv, PositionPnl = cb.RoundedPositionPnl, DaliyPnl = Math.Round(cb.DaliyPnl, 2), ClientId = client.id, ClientType = cb.ClientType, Credit = cb.TotalCredit }; if (_yLCache != null) { _yLCache.StringSet("ClientBalance:" + cb.ClientId, obj); } kafkaProduceHelper.Produce(onRspAccountCapitalTopic, JsonConvert.SerializeObject(obj)); } } }); } catch (Exception ex) { _logger.Error("CashCalcConsumerKafkaTask Exception", ex); } } } } }