Files
zszq-trs/YLErpDAL/Modules/EodModule/SettlementModule/XiaMenXiangYuCashService.cs
T
2024-05-09 14:06:26 +08:00

552 lines
24 KiB
C#

using YLErp.Commons;
using YLErp.Enums;
namespace YLErp.Modules.EodModule.SettlementModule
{
public class XiaMenXiangYuCashService : YLBaseService
{
public XiaMenXiangYuCashService(OptUserInfo optUser) : base(optUser)
{
}
public XiaMenXiangYuCashService(YLBaseService baseService) : base(baseService)
{
}
/// <summary>
/// 厦门象屿专用
/// </summary>
/// <param name="client"></param>
/// <param name="clientbalancedaily"></param>
/// <param name="InOutFund">入金为正 出金为负</param>
public void Execute(XiaMenXiangYuClientInfo client, ClientBalanceDaily clientbalancedaily, FundObject fundObject, out double InOutFund, DateTime SettleDate, int positionCount)
{
var CashInCash = new ClientCashInCashOut();
CashInCash.CurrencyCode = "CNY";
InOutFund = 0;
bool isFundThreshold = client.FundThreshold != null && client.FundThreshold != 0;
var availableAmount = (clientbalancedaily.MarginBalance ?? 0.0) + (clientbalancedaily.PayableMargin ?? 0.0)
- (clientbalancedaily.FrozenMarginMoney ?? 0.0) - (clientbalancedaily.TodayRemianFundProduct ?? 0.0);
var DesirableFund = availableAmount + clientbalancedaily.FrozenRedeemFunds.Value - Math.Max(-clientbalancedaily.PositionPremiumNetCash.Value, 0);
//可取资金< 0 阈值不等于0 和 null,只做入金操作
if (isFundThreshold && DesirableFund < 0)
{
int n = (int)Math.Floor(Convert.ToDecimal((DesirableFund) / client.FundThreshold));
var inFund = Math.Abs(n) * client.FundThreshold;
clientbalancedaily.ToDayRemainFund += inFund;
CashInCash.Direction = CashICOEnum.入金.ToString();
CashInCash.Money = inFund;
InOutFund = CashInCash.Money ?? 0;
clientbalancedaily.InFund += inFund;
clientbalancedaily.NetFund += inFund;
clientbalancedaily.MarginBalance += inFund;
if (fundObject.InFund.ContainsKey("CNY"))
{
fundObject.InFund["CNY"] += inFund ?? 0;
}
else if (fundObject.InFund.ContainsKey(""))
{
fundObject.InFund[""] += inFund ?? 0;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.InFund.Add("CNY", inFund ?? 0);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += inFund ?? 0;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += inFund ?? 0;
}
else
{
fundObject.NetFund.Add("CNY", inFund ?? 0);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += inFund ?? 0;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += inFund ?? 0;
}
else
{
fundObject.TodayRemainFund.Add("CNY", inFund ?? 0);
}
CashInCash.Comments = "自动" + CashInCash.Direction;
}
//无持仓出入金操作
else if (positionCount == 0 && (DesirableFund - Math.Max(clientbalancedaily.FrozenBalance ?? 0, 0)) > 0 && SettleDate == DateTime.Today)
{
var outFund = (DesirableFund - Math.Max(clientbalancedaily.FrozenBalance ?? 0, 0)) * -1;//出金负数
clientbalancedaily.ToDayRemainFund += outFund;
CashInCash.Direction = CashICOEnum.出金.ToString();
CashInCash.Money = outFund * -1; //出金字段不分正负,因此取正数
InOutFund = CashInCash.Money * -1 ?? 0;//返回数据用负数
clientbalancedaily.OutFund += outFund * -1;//出金字段不分正负,因此取正数
clientbalancedaily.NetFund += outFund;
clientbalancedaily.MarginBalance += outFund;
if (fundObject.OutFund.ContainsKey("CNY"))
{
fundObject.OutFund["CNY"] += outFund * -1;
}
else if (fundObject.OutFund.ContainsKey(""))
{
fundObject.OutFund[""] += outFund * -1;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.OutFund.Add("CNY", outFund * -1);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += outFund;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += outFund;
}
else
{
fundObject.NetFund.Add("CNY", outFund);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += outFund;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += outFund;
}
else
{
fundObject.TodayRemainFund.Add("CNY", outFund);
}
CashInCash.Comments = "自动" + CashInCash.Direction + "(无持仓交易)";
}
//自动出金减掉冻结资金
//可取资金>阈值 阈值不等于0 和 null, 只做出金操作
else if (isFundThreshold && (DesirableFund - Math.Max(clientbalancedaily.FrozenBalance ?? 0, 0)) > client.FundThreshold)
{
DesirableFund = DesirableFund - Math.Max(clientbalancedaily.FrozenBalance ?? 0, 0);
int n = (int)Math.Ceiling(Convert.ToDecimal((DesirableFund - client.FundThreshold) / client.FundThreshold));
var outFund = client.FundThreshold * -n;//出金负数
clientbalancedaily.ToDayRemainFund += outFund;
CashInCash.Direction = CashICOEnum.出金.ToString();
CashInCash.Money = outFund * -1; //出金字段不分正负,因此取正数
InOutFund = CashInCash.Money * -1 ?? 0;//返回数据用负数
clientbalancedaily.OutFund += outFund * -1;//出金字段不分正负,因此取正数
clientbalancedaily.NetFund += outFund;
clientbalancedaily.MarginBalance += outFund;
if (fundObject.OutFund.ContainsKey("CNY"))
{
fundObject.OutFund["CNY"] += (outFund ?? 0) * -1;
}
else if (fundObject.OutFund.ContainsKey(""))
{
fundObject.OutFund[""] += (outFund ?? 0) * -1;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.OutFund.Add("CNY", (outFund ?? 0) * -1);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += outFund ?? 0;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += outFund ?? 0;
}
else
{
fundObject.NetFund.Add("CNY", outFund ?? 0);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += outFund ?? 0;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += outFund ?? 0;
}
else
{
fundObject.TodayRemainFund.Add("CNY", outFund ?? 0);
}
CashInCash.Comments = "自动" + CashInCash.Direction;
}
if (InOutFund != 0)
{
CashInCash.ClientId = client.ClientId;
CashInCash.ClientName = client.ClientName;
CashInCash.HappenDate = SettleDate;
CashInCash.ClientNumber = client.ClientNumber;
CashInCash.CashFlag = 0;
CashInCash.OptId = 0;
CashInCash.OptName = "系统";
CashInCash.OptDate = DateTime.Now;
CashInCash.CreatorId = 0;
CashInCash.CreatorName = "系统";
CashInCash.CreateDate = DateTime.Now;
CashInCash.State = "已结算";
CashInCash.Number = UniqueTimeId.GetStr();
DbContext.ClientCashInCashOut.Add(CashInCash);
//DbContext.SaveChanges();//日终计算统一保存
}
}
/// <summary>
/// 厦门象屿专用
/// </summary>
/// <param name="client"></param>
/// <param name="clientbalancedaily"></param>
/// <param name="InOutFund">入金为正 出金为负</param>
public void ExecuteV2(XiaMenXiangYuClientInfo client, ClientBalanceDaily clientbalancedaily, FundObject fundObject, out double InOutFund, DateTime SettleDate, int positionCount)
{
var CashInCash = new ClientCashInCashOut();
CashInCash.CurrencyCode = "CNY";
InOutFund = 0;
bool isFundThreshold = client.FundThreshold != null && client.FundThreshold != 0;
var availableAmount = (clientbalancedaily.MarginBalance ?? 0.0) + (clientbalancedaily.PayableMargin ?? 0.0)
- (clientbalancedaily.FrozenMarginMoney ?? 0.0) - (clientbalancedaily.TodayRemianFundProduct ?? 0.0);
//入金时可用资金
var DesirableFund = availableAmount + clientbalancedaily.FrozenRedeemFunds.Value - Math.Max(-clientbalancedaily.PositionPremiumNetCash.Value, 0);
//出金时可取资金(不包括冻结资金)
var outDesirableFund = DesirableFund - Math.Max(clientbalancedaily.FrozenBalance ?? 0, 0);
//无持仓出入金操作
if (positionCount == 0)
{
//无持仓全部出金
if (outDesirableFund>0 && SettleDate == DateTime.Today)
{
var outFund = outDesirableFund * -1;//出金负数
clientbalancedaily.ToDayRemainFund += outFund;
CashInCash.Direction = CashICOEnum.出金.ToString();
CashInCash.Money = outFund * -1; //出金字段不分正负,因此取正数
InOutFund = CashInCash.Money * -1 ?? 0;//返回数据用负数
clientbalancedaily.OutFund += outFund * -1;//出金字段不分正负,因此取正数
clientbalancedaily.NetFund += outFund;
clientbalancedaily.MarginBalance += outFund;
if (fundObject.OutFund.ContainsKey("CNY"))
{
fundObject.OutFund["CNY"] += outFund * -1;
}
else if (fundObject.OutFund.ContainsKey(""))
{
fundObject.OutFund[""] += outFund * -1;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.OutFund.Add("CNY", outFund * -1);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += outFund;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += outFund;
}
else
{
fundObject.NetFund.Add("CNY", outFund);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += outFund;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += outFund;
}
else
{
fundObject.TodayRemainFund.Add("CNY", outFund);
}
CashInCash.Comments = "自动" + CashInCash.Direction + "(无持仓交易)";
}
}
//有持仓出入金操作
else
{
//阈值不为空或不为0
if (isFundThreshold)
{
//可取资金< 0 ,入金:阈值的倍数
if (outDesirableFund < 0)
{
int n = (int)Math.Floor(Convert.ToDecimal(outDesirableFund / client.FundThreshold));
var inFund = Math.Abs(n) * client.FundThreshold;
clientbalancedaily.ToDayRemainFund += inFund;
CashInCash.Direction = CashICOEnum.入金.ToString();
CashInCash.Money = inFund;
InOutFund = CashInCash.Money ?? 0;
clientbalancedaily.InFund += inFund;
clientbalancedaily.NetFund += inFund;
clientbalancedaily.MarginBalance += inFund;
if (fundObject.InFund.ContainsKey("CNY"))
{
fundObject.InFund["CNY"] += inFund ?? 0;
}
else if (fundObject.InFund.ContainsKey(""))
{
fundObject.InFund[""] += inFund ?? 0;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.InFund.Add("CNY", inFund ?? 0);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += inFund ?? 0;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += inFund ?? 0;
}
else
{
fundObject.NetFund.Add("CNY", inFund ?? 0);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += inFund ?? 0;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += inFund ?? 0;
}
else
{
fundObject.TodayRemainFund.Add("CNY", inFund ?? 0);
}
CashInCash.Comments = "自动" + CashInCash.Direction;
}
//可取资金>阈值 出金:阈值的倍数
else if (outDesirableFund> client.FundThreshold)
{
int n = (int)Math.Floor(Convert.ToDecimal(outDesirableFund / client.FundThreshold));
var outFund = client.FundThreshold * -n;//出金负数
clientbalancedaily.ToDayRemainFund += outFund;
CashInCash.Direction = CashICOEnum.出金.ToString();
CashInCash.Money = outFund * -1; //出金字段不分正负,因此取正数
InOutFund = CashInCash.Money * -1 ?? 0;//返回数据用负数
clientbalancedaily.OutFund += outFund * -1;//出金字段不分正负,因此取正数
clientbalancedaily.NetFund += outFund;
clientbalancedaily.MarginBalance += outFund;
if (fundObject.OutFund.ContainsKey("CNY"))
{
fundObject.OutFund["CNY"] += (outFund ?? 0) * -1;
}
else if (fundObject.OutFund.ContainsKey(""))
{
fundObject.OutFund[""] += (outFund ?? 0) * -1;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.OutFund.Add("CNY", (outFund ?? 0) * -1);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += outFund ?? 0;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += outFund ?? 0;
}
else
{
fundObject.NetFund.Add("CNY", outFund ?? 0);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += outFund ?? 0;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += outFund ?? 0;
}
else
{
fundObject.TodayRemainFund.Add("CNY", outFund ?? 0);
}
CashInCash.Comments = "自动" + CashInCash.Direction;
}
}
//阈值为空或为0
else
{
//出金时可取资金>0, 出金:可取资金
if (outDesirableFund > 0 )
{
var outFund = outDesirableFund * -1;//出金负数
clientbalancedaily.ToDayRemainFund += outFund;
CashInCash.Direction = CashICOEnum.出金.ToString();
CashInCash.Money = outFund * -1; //出金字段不分正负,因此取正数
InOutFund = CashInCash.Money * -1 ?? 0;//返回数据用负数
clientbalancedaily.OutFund += outFund * -1;//出金字段不分正负,因此取正数
clientbalancedaily.NetFund += outFund;
clientbalancedaily.MarginBalance += outFund;
if (fundObject.OutFund.ContainsKey("CNY"))
{
fundObject.OutFund["CNY"] += outFund * -1;
}
else if (fundObject.OutFund.ContainsKey(""))
{
fundObject.OutFund[""] += outFund * -1;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.OutFund.Add("CNY", outFund * -1);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += outFund;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += outFund;
}
else
{
fundObject.NetFund.Add("CNY", outFund);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += outFund;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += outFund;
}
else
{
fundObject.TodayRemainFund.Add("CNY", outFund);
}
CashInCash.Comments = "自动" + CashInCash.Direction;
}
//入金时可取资金 < 0 入金:可取资金
else if(outDesirableFund < 0)
{
var inFund = outDesirableFund * -1;//入金
clientbalancedaily.ToDayRemainFund += inFund;
CashInCash.Direction = CashICOEnum.入金.ToString();
CashInCash.Money = inFund;
InOutFund = CashInCash.Money ?? 0;
clientbalancedaily.InFund += inFund;
clientbalancedaily.NetFund += inFund;
clientbalancedaily.MarginBalance += inFund;
if (fundObject.InFund.ContainsKey("CNY"))
{
fundObject.InFund["CNY"] += inFund;
}
else if (fundObject.InFund.ContainsKey(""))
{
fundObject.InFund[""] += inFund;
CashInCash.CurrencyCode = "";
}
else
{
fundObject.InFund.Add("CNY", inFund);
}
if (fundObject.NetFund.ContainsKey("CNY"))
{
fundObject.NetFund["CNY"] += inFund;
}
else if (fundObject.NetFund.ContainsKey(""))
{
fundObject.NetFund[""] += inFund;
}
else
{
fundObject.NetFund.Add("CNY", inFund);
}
if (fundObject.TodayRemainFund.ContainsKey("CNY"))
{
fundObject.TodayRemainFund["CNY"] += inFund;
}
else if (fundObject.TodayRemainFund.ContainsKey(""))
{
fundObject.TodayRemainFund[""] += inFund;
}
else
{
fundObject.TodayRemainFund.Add("CNY", inFund);
}
CashInCash.Comments = "自动" + CashInCash.Direction;
}
}
}
if (InOutFund != 0)
{
CashInCash.ClientId = client.ClientId;
CashInCash.ClientName = client.ClientName;
CashInCash.HappenDate = SettleDate;
CashInCash.ClientNumber = client.ClientNumber;
CashInCash.CashFlag = 0;
CashInCash.OptId = 0;
CashInCash.OptName = "系统";
CashInCash.OptDate = DateTime.Now;
CashInCash.CreatorId = 0;
CashInCash.CreatorName = "系统";
CashInCash.CreateDate = DateTime.Now;
CashInCash.State = "已结算";
CashInCash.Number = UniqueTimeId.GetStr();
DbContext.ClientCashInCashOut.Add(CashInCash);
//DbContext.SaveChanges();//日终计算统一保存
}
}
}
public class XiaMenXiangYuClientInfo
{
public int ClientId { get; set; }
public string ClientName { get; set; }
public string ClientNumber { get; set; }
public double? FundThreshold { get; set; }
}
}