#EQD-6948 国联民生-实现保证金规则(2)追保金额的产生与收盘计算 收盘计算和授信占用
This commit is contained in:
@@ -6,12 +6,20 @@ namespace YLErp.Modules.SwapModule
|
||||
/// <summary>
|
||||
/// 客户授信出入服务(R4 授信/现金标签体系,决策④)。
|
||||
/// 授信出入表的写入口集中在 标签赋值(占用)与 平仓/到期返还(释放)两个链路内,禁止散落调用。
|
||||
/// 金额符号口径(2026-08-20 业务确认):与资金流水同号——入金为负、出金为正;
|
||||
/// 已使用授信 = SUM(amount) 直接求和(入金使已使用授信下降、可用授信=有效授信−已使用授信 上升;出金反之)。
|
||||
/// 金额符号口径(BUG-01 修正,2026-08-24):**占用记正数、释放记负数**(与 §2.2 原表"占用/调整为正"一致)——
|
||||
/// 已使用授信 = SUM(amount) 直接求和(占用使其上升、释放使其回落),可用授信 = 有效授信 − 已使用授信(占用使其收缩)。
|
||||
/// 2026-08-20 曾裁定"与资金流水同号(入金负/出金正)"——但授信不入资金流水(§2.1),同号对齐的对手方记录并不存在,
|
||||
/// 且该口径下"占用越多已使用授信越负、可用授信越大"(越占越多),已废弃;历史同号行由迁移脚本翻符号。
|
||||
/// change_type(占用/释放/调整)仅作分类审计,不参与求和方向。
|
||||
/// </summary>
|
||||
public class ClientCreditInoutService : YLBaseService
|
||||
{
|
||||
/// <summary>
|
||||
/// 追加保证金相关授信出入记录的 remark 前缀(阶段四 §4.1 EOD 占用写入"追加保证金占用";
|
||||
/// 累计保证金/缺口口径与清理链路以此识别,后续如写释放类记录沿用同一前缀)。
|
||||
/// </summary>
|
||||
public const string AdditionalMarginRemark = "追加保证金";
|
||||
|
||||
public ClientCreditInoutService(OptUserInfo userInfo) : base(userInfo)
|
||||
{
|
||||
}
|
||||
@@ -21,9 +29,19 @@ namespace YLErp.Modules.SwapModule
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 客户已使用授信 = SUM(amount)(金额与资金流水同号:入金负、出金正)。
|
||||
/// 可用授信 = 有效授信 − 本值;入金使可用授信增长、出金使其收缩。
|
||||
/// 供交易确认校验(RealtimePnlCalc.TradeCanBeConfirm)等静态上下文直接调用。
|
||||
/// 是否追加保证金相关记录(remark 前缀标识,阶段四 EOD 写入)。
|
||||
/// </summary>
|
||||
public static bool IsAdditionalMarginRecord(client_credit_inout record)
|
||||
{
|
||||
return record != null && !string.IsNullOrEmpty(record.remark)
|
||||
&& record.remark.StartsWith(AdditionalMarginRemark);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 客户已使用授信 = SUM(amount)(占用为正、释放为负)。
|
||||
/// 可用授信 = 有效授信 − 本值:占用使其收缩、释放使其回升。
|
||||
/// 供交易确认校验(RealtimePnlCalc.TradeCanBeConfirm)、拆单额度(GetAvailableCredit)、
|
||||
/// 估值报告可用资金(阶段三 §3.2)等消费方统一调用。
|
||||
/// </summary>
|
||||
public static double GetUsedCredit(int clientId, YLContext db)
|
||||
{
|
||||
@@ -40,8 +58,8 @@ namespace YLErp.Modules.SwapModule
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入一条授信变化记录(占用/释放/调整统一入口),并冗余记录变更后已使用授信。
|
||||
/// amount 带符号:入金(客户付)为负、出金(客户收)为正,与资金流水 Money 同号。
|
||||
/// 写入一条授信变化记录(统一入口),并冗余记录变更后已使用授信。
|
||||
/// amount 带符号直写(人工调整类可正可负);占用/释放请走 Occupy/Release 语义入口(内部定方向)。
|
||||
/// </summary>
|
||||
public client_credit_inout Record(int clientId, long? positionId, int? tradeId, int changeType,
|
||||
double amount, DateTime happenDate, string remark)
|
||||
@@ -51,7 +69,7 @@ namespace YLErp.Modules.SwapModule
|
||||
return null;
|
||||
}
|
||||
var amountRounded = Math.Round(amount, 2, MidpointRounding.AwayFromZero);
|
||||
//变更后已使用授信快照:直接求和口径(符号已含方向)
|
||||
//变更后已使用授信快照:直接求和口径(占用正/释放负,符号已含方向)
|
||||
var usedAfter = Math.Round(GetUsedCredit(clientId) + amountRounded, 2, MidpointRounding.AwayFromZero);
|
||||
var record = new client_credit_inout
|
||||
{
|
||||
@@ -74,31 +92,37 @@ namespace YLErp.Modules.SwapModule
|
||||
|
||||
/// <summary>
|
||||
/// 占用:预付金腿标 Credit 的簿记入金(含拆单的授信部分;交易级占用 positionId 为空)。
|
||||
/// amount 传负数(入金方向,与资金流水同号)。
|
||||
/// amount 传正数(占用数量),内部记正——已使用授信随之上升。
|
||||
/// </summary>
|
||||
public client_credit_inout Occupy(int clientId, long? positionId, int tradeId, double amount, DateTime happenDate, string remark)
|
||||
{
|
||||
return Record(clientId, positionId, tradeId, client_credit_inout.ChangeTypeOccupy, amount, happenDate, remark);
|
||||
return Record(clientId, positionId, tradeId, client_credit_inout.ChangeTypeOccupy, Math.Abs(amount), happenDate, remark);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放:平仓/到期按原标签返还授信部分(按腿的 position_id 匹配原占用记录)。
|
||||
/// amount 传正数(出金方向,与资金流水同号)。
|
||||
/// amount 传正数(释放数量),内部记负——已使用授信随之回落。
|
||||
/// </summary>
|
||||
public client_credit_inout Release(int clientId, long? positionId, int tradeId, double amount, DateTime happenDate, string remark)
|
||||
{
|
||||
return Record(clientId, positionId, tradeId, client_credit_inout.ChangeTypeRelease, amount, happenDate, remark);
|
||||
return Record(clientId, positionId, tradeId, client_credit_inout.ChangeTypeRelease, -Math.Abs(amount), happenDate, remark);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除某交易的全部授信出入记录:与资金记录同生命周期——
|
||||
/// 删除某交易的授信出入记录:与资金记录同生命周期——
|
||||
/// 交易回退到开仓(DeleteTradeCashInCashOut)、修改清除(ClearSwapPositions)、删除交易时同步清理,
|
||||
/// 重新确认/重补时按最新标签重写,避免占用悬挂。
|
||||
/// 注意:阶段四追加保证金占用落地后,此处需区分保留追加部分。
|
||||
/// keepAdditionalMargin=true 时保留追加保证金部分(阶段四落地):重确认自愈/修改清除只重写 应付预付金 相关占用,
|
||||
/// 追加部分由 EOD 幂等维护(该两处不删除追加资金记录,授信占用须同步保留,否则已使用授信被低估);
|
||||
/// 删除交易(资金记录全删)传 false 全清。
|
||||
/// </summary>
|
||||
public void RemoveByTrade(int tradeId)
|
||||
public void RemoveByTrade(int tradeId, bool keepAdditionalMargin = false)
|
||||
{
|
||||
var records = DbContext.client_credit_inout.Where(x => x.trade_id == tradeId).ToList();
|
||||
if (keepAdditionalMargin)
|
||||
{
|
||||
records = records.Where(x => !IsAdditionalMarginRecord(x)).ToList();
|
||||
}
|
||||
DbContext.client_credit_inout.RemoveRange(records);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user