148 lines
7.5 KiB
C#
148 lines
7.5 KiB
C#
using YLErp.BLL;
|
|
using YLErp.DBModels;
|
|
|
|
namespace YLErp.Modules.SwapModule
|
|
{
|
|
/// <summary>
|
|
/// 客户授信出入服务(R4 授信/现金标签体系,决策④)。
|
|
/// 授信出入表的写入口集中在 标签赋值(占用)与 平仓/到期返还(释放)两个链路内,禁止散落调用。
|
|
/// 金额符号口径(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)
|
|
{
|
|
}
|
|
|
|
public ClientCreditInoutService(YLBaseService baseService) : base(baseService)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否追加保证金相关记录(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)
|
|
{
|
|
var records = db.client_credit_inout.Where(x => x.client_id == clientId).ToList();
|
|
return Math.Round(records.Sum(x => x.amount), 2, MidpointRounding.AwayFromZero);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 客户当前已使用授信(实例方法,走服务 DbContext)
|
|
/// </summary>
|
|
public double GetUsedCredit(int clientId)
|
|
{
|
|
return GetUsedCredit(clientId, DbContext);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 写入一条授信变化记录(统一入口),并冗余记录变更后已使用授信。
|
|
/// amount 带符号直写(人工调整类可正可负);占用/释放请走 Occupy/Release 语义入口(内部定方向)。
|
|
/// </summary>
|
|
public client_credit_inout Record(int clientId, long? positionId, int? tradeId, int changeType,
|
|
double amount, DateTime happenDate, string remark)
|
|
{
|
|
if (amount == 0)
|
|
{
|
|
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
|
|
{
|
|
client_id = clientId,
|
|
position_id = positionId,
|
|
trade_id = tradeId,
|
|
change_type = changeType,
|
|
amount = amountRounded,
|
|
used_after = usedAfter,
|
|
happen_date = happenDate,
|
|
remark = remark,
|
|
OptId = UserId,
|
|
OptName = UserName,
|
|
OptTime = DateTime.Now
|
|
};
|
|
DbContext.client_credit_inout.Add(record);
|
|
DbContext.SaveChanges();
|
|
return record;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 占用:预付金腿标 Credit 的簿记入金(含拆单的授信部分;交易级占用 positionId 为空)。
|
|
/// 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, Math.Abs(amount), happenDate, remark);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 释放:平仓/到期按原标签返还授信部分(按腿的 position_id 匹配原占用记录)。
|
|
/// 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, -Math.Abs(amount), happenDate, remark);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除某交易的授信出入记录:与资金记录同生命周期——
|
|
/// 交易回退到开仓(DeleteTradeCashInCashOut)、修改清除(ClearSwapPositions)、删除交易时同步清理,
|
|
/// 重新确认/重补时按最新标签重写,避免占用悬挂。
|
|
/// keepAdditionalMargin=true 时保留追加保证金部分(阶段四落地):重确认自愈/修改清除只重写 应付预付金 相关占用,
|
|
/// 追加部分由 EOD 幂等维护(该两处不删除追加资金记录,授信占用须同步保留,否则已使用授信被低估);
|
|
/// 删除交易(资金记录全删)传 false 全清。
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按客户批量查询已使用授信(客户列表/详情"已使用授信"展示,数据源即本表)。
|
|
/// </summary>
|
|
public static Dictionary<int, double> GetUsedCreditByClients(List<int> clientIds, YLContext db)
|
|
{
|
|
var result = new Dictionary<int, double>();
|
|
if (clientIds == null || clientIds.Count == 0)
|
|
{
|
|
return result;
|
|
}
|
|
var records = db.client_credit_inout.Where(x => clientIds.Contains(x.client_id)).ToList();
|
|
foreach (var group in records.GroupBy(x => x.client_id))
|
|
{
|
|
result[group.Key] = Math.Round(group.Sum(x => x.amount), 2, MidpointRounding.AwayFromZero);
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|