优化“收益互换”确认书查询与编号生成逻辑

This commit is contained in:
锦麟 王
2026-04-14 11:24:03 +08:00
parent 130cbedaba
commit f11e8ea27a
6 changed files with 94 additions and 46 deletions
@@ -229,49 +229,9 @@ namespace YLErp.Modules.TradeModule.DealModule
var clientIds = trades.Select(x => x.ClientId).Distinct().ToList();
logger.Info($"[GJGenerateConfirmBook] 客户ID数: {clientIds.Count}");
if (trades.Any(o => o.TradeType.Equals("收益互换")))
foreach (var item in trades)
{
var allTrades = DbContext.trade.Where(x => clientIds.Contains(x.ClientId) && x.TradeType.Equals("收益互换") && x.ValidState != ConsGlobal.InValid);
if (startDate.HasValue)
{
allTrades = allTrades.Where(x => x.TradeDate >= startDate);
}
if (endDate.HasValue)
{
allTrades = allTrades.Where(x => x.TradeDate <= endDate);
}
var Ids = allTrades.Select(o => o.id).Distinct().ToArray();
var swapPositions = DbContext.swap_position.Where(s => Ids.Contains(s.SwapTradeId) && s.IsInitial && s.PositionType > 0 && !s.Invalid)
.Distinct().ToList();
logger.Info($"[GJGenerateConfirmBook] 查询到持仓记录数: {swapPositions.Count}");
var allTradeList = allTrades.AsNoTracking().ToList();
foreach (var item in allTradeList)
{
item.swap_positions.Add(swapPositions.FirstOrDefault(o => o.SwapTradeId == item.id));
}
allTradeList = allTradeList.Where(x => x.swap_positions.Any()).ToList();
logger.Info($"[GJGenerateConfirmBook] 过滤后有持仓的交易数: {allTradeList.Count}");
// 按照客户ID、交易日期、多空头进行分组
var group = allTradeList.GroupBy(O => new { O.ClientId, O.StartDate, O.swap_positions.FirstOrDefault()?.PositionType })
.ToDictionary(K => K.Key, V => V.Select(O => O.id).ToList());
logger.Info($"[GJGenerateConfirmBook] 分组数: {group.Count}");
foreach (var item in group)
{
var tradeCount = item.Value.Distinct().Count();
logger.Info($"[GJGenerateConfirmBook] 处理分组 - ClientId: {item.Key.ClientId}, StartDate: {item.Key.StartDate}, PositionType: {item.Key.PositionType}, 交易数: {tradeCount}");
groupGenerateHandle(item.Value.Distinct().ToList(), docType, startDate, endDate, results, error, file);
}
}
else
{
foreach (var item in trades)
{
groupGenerateHandle_Date(new List<int> { item.id }, docType, startDate, endDate, results, error, file);
}
groupGenerateHandle_Date(new List<int> { item.id }, docType, startDate, endDate, results, error, file);
}
errors = results.Where(n => !string.IsNullOrWhiteSpace(n.ErrorMessage)).Select(n => n.ErrorMessage).ToList();