从山证v2.3.0拷贝

This commit is contained in:
吴方海
2024-05-09 14:06:26 +08:00
parent 566ff33259
commit f9d8a256a6
4471 changed files with 1203456 additions and 9 deletions
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLErp.BLL;
using YLErp.Modules.TradeMsgOutputModule.Dto;
namespace YLErp.Modules.TradeMsgOutputModule
{
public class BaseTradeAfterEodOutputService
{
/// <summary>
/// 检测收盘状态
/// </summary>
/// <param name="valueDate"></param>
/// <exception cref="ServiceException"></exception>
public void CheckEodStatus(DateTime valueDate)
{
using (var db = DbContextFactory.GetYLDbContext())
{
var eodStatus = db.eodStatus.AsNoTracking().Any(p => p.ValueDate == valueDate && ConsGlobal.EodStatusVlue.Success.Equals(p.Status));
if (!eodStatus)
{
throw new ServiceException($"日期{valueDate:yyyy-MM-dd}未完成收盘");
}
}
}
protected Dictionary<int, TradeDicSimpleDto> GetTradeDicSimpleDic(YLContext db,List<int> tradeIds)
{
return db.trade.AsNoTracking().Where(p => tradeIds.Contains(p.id)).ToDictionary(p => p.id, p => new TradeDicSimpleDto
{
TradeNumber = p.TradeNumber,
AssetId = p.AssetId,
TradeDate = p.TradeDate,
TradeType = p.TradeType,
ExerciseDate = p.ExerciseDate,
UnderlyingCode = p.UnderlyingCode,
PremiumPayDate = p.PremiumPayDate,
OriginalStockEqvNotional = p.OriginalStockEqvNotional,
StockEqvNotional = p.StockEqvNotional
});
}
protected Dictionary<int, TradeDicSimpleDto> GetEodTradeDicSimpleDic(YLContext db,DateTime valueDate, List<int> tradeIds)
{
var eodTradeList = db.eod_trade.AsNoTracking().Where(p => p.ValueDate == valueDate && tradeIds.Contains(p.TradeId)).ToList();
if (eodTradeList != null && eodTradeList.Count > 0)
{
return eodTradeList.ToDictionary(p => p.TradeId, p => new TradeDicSimpleDto
{
TradeNumber = p.trade.TradeNumber,
AssetId = p.trade.AssetId,
TradeDate = p.trade.TradeDate,
TradeType = p.trade.TradeType,
ExerciseDate = p.trade.ExerciseDate,
UnderlyingCode = p.trade.UnderlyingCode,
PremiumPayDate = p.trade.PremiumPayDate,
OriginalStockEqvNotional = p.trade.OriginalStockEqvNotional,
StockEqvNotional = p.trade.StockEqvNotional,
OriginalNotional=p.trade.OriginalNotional??.0
});
}
return new Dictionary<int, TradeDicSimpleDto>();
}
}
}
@@ -0,0 +1,150 @@
using FluentFTP;
using SocialExplorer.IO.FastDBF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLErp.Modules.TradeMsgOutputModule.Dto;
namespace YLErp.Modules.TradeMsgOutputModule.Company
{
public abstract class BaseDongWanFileGenerateService
{
//protected string DBFFileSavePath = OtcAppContext.MapPath("~/App_Docs/Temp/DbfFile");
protected string DBFFileSavePath = "D:\\测试文件";
private string FtpServer = Environment.GetEnvironmentVariable("AppSettings:FtpServer");
private string FtpPort = Environment.GetEnvironmentVariable("AppSettings:FtpPort");
private string FtpFilePath = Environment.GetEnvironmentVariable("AppSettings:FtpFilePath");
private string FtpUserName = Environment.GetEnvironmentVariable("AppSettings:FtpUserName");
private string FtpPassword = Environment.GetEnvironmentVariable("AppSettings:FtpPassword");
/// <summary>
/// 组装文件名称
/// </summary>
/// <param name="namePrefix"></param>
/// <param name="valueDate"></param>
/// <returns></returns>
protected string GetFileName(string namePrefix,DateTime valueDate)
{
return namePrefix + "_" + valueDate.ToString("yyyyMMdd") + ".dbf";
}
/// <summary>
/// 获取文件路径
/// </summary>
/// <returns></returns>
private string GetFilePath()
{
if (!Directory.Exists(DBFFileSavePath))
{
Directory.CreateDirectory(DBFFileSavePath);
}
return DBFFileSavePath;
}
/// <summary>
/// 创建文件
/// </summary>
/// <param name="namePrefix"></param>
/// <param name="valueDate"></param>
/// <returns></returns>
protected DbfFile CreateFile(string namePrefix, DateTime valueDate, out string fullFilePath)
{
return CreateDbfFile(GetFileName(namePrefix, valueDate), GetDbfFileColumns(),out fullFilePath);
}
private DbfFile CreateDbfFile(string dbfName, List<DbfColumn> columns,out string fullFilePath)
{
var dbfPath = GetFilePath();
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
fullFilePath = Path.Combine(dbfPath, dbfName);
if (File.Exists(fullFilePath))
{
File.Delete(fullFilePath);
}
var odbf = new DbfFile(Encoding.UTF8);
odbf.Open(fullFilePath, FileMode.Create);
if (columns == null || columns.Count == 0)
{
throw new ArgumentException("columns 不能为空");
}
columns.ForEach(p =>
{
odbf.Header.AddColumn(p);
});
return odbf;
}
protected abstract List<DbfColumn> GetDbfFileColumns();
public abstract GenerateDbfFileResult GenerateDbfFile(DateTime valueDate);
/// <summary>
/// 获取部门代码
/// </summary>
/// <param name="groupName"></param>
/// <returns></returns>
protected string GetBmdmByAssetGroupName(string groupName)
{
switch (groupName)
{
case "投资管理部":
return "1003";
case "固定收益部":
return "1004";
case "做市业务部":
return "1006";
case "柜台市场业务部":
return "1007";
case "量化投资部":
return "1009";
case "基金期权做市部":
return "1011";
default:
return "";
}
}
/// <summary>
/// 获取业务类别
/// </summary>
/// <param name="tradeType"></param>
/// <returns></returns>
protected string GetYWLBByTradeType(string tradeType)
{
if (ConsGlobal.TradeType.PayoffSwap.Equals(tradeType))
{
return "QYHH";
}
return "CWQQ";
}
protected string UploadFile(string filePath,string fileName)
{
using (var ftp = new FtpClient(FtpServer, FtpUserName, FtpPassword,int.Parse(FtpPort)))
{
ftp.Connect();
if (ftp.DirectoryExists(FtpFilePath))
{
ftp.CreateDirectory(FtpFilePath);
}
// upload a file to an existing FTP directory
ftp.UploadFile(filePath, FtpFilePath+"/"+fileName, FtpRemoteExists.Overwrite, true, FtpVerify.Retry);
}
File.Delete(filePath);
return FtpServer + ":" + FtpPort + FtpFilePath + "/" + fileName;
}
}
}
@@ -0,0 +1,332 @@
using SocialExplorer.IO.FastDBF;
using YLErp.Modules.TradeMsgOutputModule.Dto;
namespace YLErp.Modules.TradeMsgOutputModule.Company
{
public class DongWanCashLogFileGenerateService : BaseDongWanFileGenerateService
{
private readonly string _fileNamePrefix = "GXGTSC";
public override GenerateDbfFileResult GenerateDbfFile(DateTime valueDate)
{
var result = new GenerateDbfFileResult();
var tradeCashOutputService = new TradeCashOutputService();
var list = tradeCashOutputService.GetCashLog(valueDate);
if (list == null || list.Count == 0)
{
return result;
}
var dbffile = CreateFile(_fileNamePrefix, valueDate, out var filePath);
list.ForEach(p =>
{
var rows = WriteData(dbffile, p, valueDate);
if (rows != null && rows.Count > 0)
{
rows.ForEach(orec =>
{
dbffile.Write(orec);
});
}
});
dbffile.Close();
var fileName = GetFileName(_fileNamePrefix, valueDate);
result.FileName = fileName;
//上传Ftp
result.FilePath = UploadFile(filePath, fileName);
return result;
}
protected override List<DbfColumn> GetDbfFileColumns()
{
return new List<DbfColumn> {
new DbfColumn("cjrq", DbfColumn.DbfColumnType.Date),
new DbfColumn("htxh", DbfColumn.DbfColumnType.Character,50,0),
new DbfColumn("hydqr", DbfColumn.DbfColumnType.Date),
new DbfColumn("Ywlb", DbfColumn.DbfColumnType.Character,6,0),
new DbfColumn("ywdm", DbfColumn.DbfColumnType.Character,10,0),
new DbfColumn("Je", DbfColumn.DbfColumnType.Number,19,4),
new DbfColumn("Dqje", DbfColumn.DbfColumnType.Number,19,4),
new DbfColumn("Lx", DbfColumn.DbfColumnType.Number,19,4),
new DbfColumn("Bdzq", DbfColumn.DbfColumnType.Character,30,0),
new DbfColumn("ZJZH", DbfColumn.DbfColumnType.Character,30,0),
new DbfColumn("Gdll", DbfColumn.DbfColumnType.Number,12,8),
new DbfColumn("Njxts", DbfColumn.DbfColumnType.Number,4,0),
new DbfColumn("Bmdm", DbfColumn.DbfColumnType.Character,10,0),
new DbfColumn("Sypzlb", DbfColumn.DbfColumnType.Character,1,0),
};
}
/// <summary>
/// 写数据
/// </summary>
/// <param name="orec"></param>
/// <param name="cashLog"></param>
private List<DbfRecord> WriteData(DbfFile dbfFile, CashLogOutputDto cashLog, DateTime valueDate)
{
var ywlb = GetYWLBByTradeType(cashLog.TradeType);
if ("QYHH".Equals(ywlb))
{
return WriteDataForQYHH(dbfFile, cashLog, valueDate);
}
else
{
return WriteDataForCWQQ(dbfFile, cashLog, valueDate);
}
}
/// <summary>
/// 互换交易赋值
/// </summary>
/// <param name="orec"></param>
/// <param name="cashLog"></param>
private List<DbfRecord> WriteDataForQYHH(DbfFile dbfFile, CashLogOutputDto cashLog, DateTime valueDate)
{
var result = new List<DbfRecord>();
switch (cashLog.CashAction)
{
case ClientCashInCashOut._期权费:
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
orec["ywdm"] = "HHKS"; //互换首期
orec["Je"] = cashLog.OriginalStockEqvNotional != null ? cashLog.OriginalStockEqvNotional.ToString() : "0";
orec["Dqje"] = "0";
orec["hydqr"] = cashLog.ExerciseDate != null ? ((DateTime)cashLog.ExerciseDate).ToString("yyyy-MM-dd") : "";
result.Add(orec);
}
break;
case ClientCashInCashOut._互换:
{
if (valueDate == cashLog.ValueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
orec["ywdm"] = "DQJSJE"; //期间收益收支
orec["Je"] = cashLog.Amount.ToString();
orec["Dqje"] = "0";
result.Add(orec);
}
if (cashLog.HappenedDate != null && cashLog.HappenedDate == valueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
orec["ywdm"] = "QJSYDZ"; //期间收益到账
orec["Je"] = cashLog.Amount.ToString();
orec["Dqje"] = "0";
result.Add(orec);
}
}
break;
case ClientCashInCashOut._行权费:
{
if (valueDate == cashLog.ValueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
orec["ywdm"] = "HHJS"; //互换到期
orec["Je"] = cashLog.OriginalStockEqvNotional != null ? cashLog.OriginalStockEqvNotional.ToString() : "0";
orec["Dqje"] = cashLog.Amount.ToString();
result.Add(orec);
}
if (cashLog.HappenedDate != null && cashLog.HappenedDate == valueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
orec["ywdm"] = "JSJEDZ"; //结算金额到账
orec["Je"] = cashLog.Amount.ToString();
orec["Dqje"] = "0";
result.Add(orec);
}
}
break;
case ClientCashInCashOut._平仓费:
{
if (valueDate == cashLog.ValueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
orec["ywdm"] = "HHJS"; //互换到期
orec["Je"] = cashLog.OriginalStockEqvNotional != null ? cashLog.OriginalStockEqvNotional.ToString() : "0";
orec["Dqje"] = cashLog.Amount.ToString();
result.Add(orec);
}
if (cashLog.HappenedDate != null && cashLog.HappenedDate == valueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
orec["ywdm"] = "JSJEDZ"; //结算金额到账
orec["Je"] = cashLog.Amount.ToString();
orec["Dqje"] = "0";
result.Add(orec);
}
}
break;
}
//orec["ywdm"] = "A00";
//orec["Je"] = "10000000.1112";
//orec["Dqje"] = "1111";
return result;
}
/// <summary>
/// 场外期权交易赋值
/// </summary>
/// <param name="orec"></param>
/// <param name="cashLog"></param>
private List<DbfRecord> WriteDataForCWQQ(DbfFile dbfFile, CashLogOutputDto cashLog, DateTime valueDate)
{
var result = new List<DbfRecord>();
switch (cashLog.CashAction)
{
case ClientCashInCashOut._期权费:
{
if (valueDate == cashLog.ValueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
if (cashLog.Amount > 0) //应收
{
if (cashLog.PremiumPayDate == cashLog.TradeDate) //前付费方式
{
orec["ywdm"] = "YSQQF"; //应收前付费
}
else
{
orec["ywdm"] = "YSQQF_HFF"; //应收后付费
}
}
else //应付
{
if (cashLog.PremiumPayDate == cashLog.TradeDate) //前付费方式
{
orec["ywdm"] = "YFQQF"; // 应付 前付费到账
}
else
{
orec["ywdm"] = "YFQQF_HFF"; // 应付 后付费到账
}
}
orec["hydqr"] = cashLog.ExerciseDate != null ? ((DateTime)cashLog.ExerciseDate).ToString("yyyy-MM-dd") : "";
orec["Je"] = cashLog.Amount.ToString();
orec["Dqje"] = "0";
result.Add(orec);
}
if (cashLog.HappenedDate != null && cashLog.HappenedDate == valueDate)
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
if (cashLog.Amount > 0) //应收
{
if (cashLog.PremiumPayDate == cashLog.TradeDate) //前付费方式
{
orec["ywdm"] = "QQFDZ"; //应收前付费到账
}
else
{
orec["ywdm"] = "QQFDZ_HFF"; //应收后付费到账
}
}
else //应付
{
if (cashLog.PremiumPayDate == cashLog.TradeDate) //前付费方式
{
orec["ywdm"] = "QQFZF"; // 应付 前付费到账
}
else
{
orec["ywdm"] = "QQFZF_HFF"; // 应付 后付费到账
}
}
orec["hydqr"] = "";
orec["Je"] = cashLog.Amount.ToString();
orec["Dqje"] = "0";
result.Add(orec);
}
}
break;
case ClientCashInCashOut._票息:
case ClientCashInCashOut._行权费:
case ClientCashInCashOut._平仓费:
{
if (valueDate == cashLog.ValueDate) //应收
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
if (cashLog.Amount > 0)
{
orec["ywdm"] = "DQSQSY"; //收取收益
}
else
{
orec["ywdm"] = "DQZFSY"; //支付收益
}
orec["Je"] = (cashLog.TradePrice*(cashLog.OriginalNotional>0?(cashLog.Notional/cashLog.OriginalNotional):1)).ToString();
orec["Dqje"] = cashLog.Amount.ToString();
result.Add(orec);
}
if (cashLog.HappenedDate != null && cashLog.HappenedDate == valueDate) //到账
{
var orec = GetDefaultRecord(dbfFile, cashLog, valueDate);
if (cashLog.Amount > 0)
{
if (cashLog.HappenedDate > cashLog.ExerciseDate)
{
orec["ywdm"] = "DQSYDZ_HFF"; //应收后付费到账
}
else
{
orec["ywdm"] = "DQSYDZ"; //应收前付费到账
}
}
else
{
if (cashLog.HappenedDate > cashLog.ExerciseDate)
{
orec["ywdm"] = "DQSYZF_HFF"; //应付后付费到账
}
else
{
orec["ywdm"] = "DQSYZF"; //应付前付费到账
}
}
orec["Je"] = cashLog.Amount.ToString();
orec["Dqje"] = "0";
result.Add(orec);
}
}
break;
}
return result;
}
private DbfRecord GetDefaultRecord(DbfFile dbfFile, CashLogOutputDto cashLog, DateTime valueDate)
{
var orec = new DbfRecord(dbfFile.Header) { AllowDecimalTruncate = true };
orec["cjrq"] = (valueDate == cashLog.ValueDate) ? cashLog.ValueDate.ToString("yyyy-MM-dd") : ((DateTime)cashLog.HappenedDate).ToString("yyyy-MM-dd");
orec["htxh"] = cashLog.TradeNumber;
orec["hydqr"] = "";
var ywlb = GetYWLBByTradeType(cashLog.TradeType);
orec["Ywlb"] = ywlb;
orec["Lx"] = "0";
orec["Bdzq"] = cashLog.UnderlyingCode;
orec["ZJZH"] = cashLog.ExChangeAccount;
orec["Gdll"] = "0";
orec["Njxts"] = "0";
orec["Bmdm"] = GetBmdmByAssetGroupName(cashLog.AssetUnitGroupName);
orec["Sypzlb"] = "1";
return orec;
}
}
}
@@ -0,0 +1,70 @@
using DotNetDBF;
using SocialExplorer.IO.FastDBF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLErp.Modules.TradeMsgOutputModule.Dto;
namespace YLErp.Modules.TradeMsgOutputModule.Company
{
/// <summary>
/// 东莞持仓PV文件生成服务
/// </summary>
public class DongWanPVPostionFileGenerateService: BaseDongWanFileGenerateService
{
private string _fileNamePrefix = "GXGTSXGZSJ";
public override GenerateDbfFileResult GenerateDbfFile(DateTime valueDate)
{
var result = new GenerateDbfFileResult();
var positionList = new TradePvOutputService().GetTradePositionPV<eod_trade_position>(valueDate);
if (positionList == null || positionList.Count == 0)
{
return result;
}
string filePath = "";
var dbffile = CreateFile(_fileNamePrefix, valueDate,out filePath);
positionList.ForEach(p =>
{
var orec = new DbfRecord(dbffile.Header) { AllowDecimalTruncate = true };
orec["cjrq"] = p.ValueDate.ToString("yyyy-MM-dd");
orec["htxh"] = p.TradeNumber;
orec["Ywlb"] = GetYWLBByTradeType(p.TradeType);
orec["qybdsy"] = p.EquitySubjectPV.ToString();
orec["gdlvsy"] = p.FixedInterestRatePV.ToString();
orec["htsyje"] = p.PV.ToString();
orec["Bmdm"] = GetBmdmByAssetGroupName(p.AssetUnitGroupName);
dbffile.Write(orec);
});
dbffile.Close();
var fileName = GetFileName(_fileNamePrefix, valueDate);
result.FileName = fileName;
//上传Ftp
result.FilePath = UploadFile(filePath, fileName);
return result;
}
protected override List<DbfColumn> GetDbfFileColumns()
{
return new List<DbfColumn> {
new DbfColumn("cjrq", DbfColumn.DbfColumnType.Date),
new DbfColumn("htxh", DbfColumn.DbfColumnType.Character,50,0),
new DbfColumn("Ywlb", DbfColumn.DbfColumnType.Character,6,0),
new DbfColumn("qybdsy", DbfColumn.DbfColumnType.Number,19,4),
new DbfColumn("gdlvsy", DbfColumn.DbfColumnType.Number,19,4),
new DbfColumn("htsyje", DbfColumn.DbfColumnType.Number,19,4),
new DbfColumn("Bmdm", DbfColumn.DbfColumnType.Character,10,0)
};
}
}
}
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YLErp.Modules.TradeMsgOutputModule.Dto
{
/// <summary>
/// 资金记录导出实体
/// </summary>
public class CashLogOutputDto
{
/// <summary>
/// trade_cash 表 ValueDate
/// </summary>
public DateTime ValueDate { get; set; }
/// <summary>
/// trade_cash
/// </summary>
public DateTime? HappenedDate { get; set; }
/// <summary>
/// 交易成交日期
/// </summary>
public DateTime? TradeDate { get; set; }
/// <summary>
/// 交易到期日
/// </summary>
public DateTime? ExerciseDate { get; set; }
/// <summary>
/// 权利金应付时间
/// </summary>
public DateTime? PremiumPayDate { get; set; }
/// <summary>
/// 交易Id
/// </summary>
public int TradeId { get; set; }
/// <summary>
/// 交易编号
/// </summary>
public string TradeNumber { get; set; }
/// <summary>
/// 交易类型
/// </summary>
public string TradeType { get; set; }
/// <summary>
/// 资金行为
/// </summary>
public string CashAction { get; set; }
/// <summary>
/// 资金金额
/// </summary>
public double Amount { get; set; }
/// <summary>
/// 簿记账户组名称
/// </summary>
public string AssetUnitGroupName { get; set; }
/// <summary>
/// 标的资产
/// </summary>
public string UnderlyingCode { get; set; }
/// <summary>
/// 对冲账户
/// </summary>
public string ExChangeAccount { get; set; }
/// <summary>
/// 期初名义本金
/// </summary>
public double? OriginalStockEqvNotional { get; set; }
/// <summary>
/// 名义本金
/// </summary>
public double StockEqvNotional { get; set; }
/// <summary>
/// 期权费
/// </summary>
public double TradePrice { get; set; }
/// <summary>
/// 成交份额
/// </summary>
public double OriginalNotional { get; set; }
/// <summary>
/// 份额
/// </summary>
public double Notional { get; set; }
}
}
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YLErp.Modules.TradeMsgOutputModule.Dto
{
public class GenerateDbfFileResult
{
public string FileName { get; set; }
public string FilePath { get; set; }
}
}
@@ -0,0 +1,57 @@
using Org.BouncyCastle.Asn1.Mozilla;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YLErp.Modules.TradeMsgOutputModule.Dto
{
/// <summary>
/// 交易资金流水
/// </summary>
public class TradeCashOutputDto
{
/// <summary>
/// 主键Id
/// </summary>
public int Id { get; set; }
/// <summary>
/// 交易Id
/// </summary>
public int TradeId { get; set; }
/// <summary>
/// 资金发生时间
/// </summary>
public DateTime ValueDate { get; set; }
/// <summary>
/// 资金到账时间
/// </summary>
public DateTime? HappenedDate { get; set; }
/// <summary>
/// 金额
/// </summary>
public double Amount { get; set; }
/// <summary>
/// 资金行为
/// </summary>
public string Action { get; set; }
/// <summary>
/// 份额
/// </summary>
public double Notional { get; set; }
/// <summary>
/// 交易期初份额
/// </summary>
public double OriginalNotional { get; set; }
}
}
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YLErp.Modules.TradeMsgOutputModule.Dto
{
public class TradeDicSimpleDto
{
public string TradeNumber { get; set; }
public int AssetId { get; set; }
public string TradeType { get; set; }
public DateTime? TradeDate { get; set; }
/// <summary>
/// 交易到期日
/// </summary>
public DateTime? ExerciseDate { get; set; }
/// <summary>
/// 权利金应付时间
/// </summary>
public DateTime? PremiumPayDate { get; set; }
public string UnderlyingCode { get; set; }
/// <summary>
/// 期初名义本金
/// </summary>
public double? OriginalStockEqvNotional { get; set; }
/// <summary>
/// 名义本金
/// </summary>
public double StockEqvNotional { get; set; }
/// <summary>
/// 成交份额
/// </summary>
public double OriginalNotional { get; set; }
}
}
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YLErp.Modules.TradeMsgOutputModule.Dto
{
/// <summary>
/// 交易持仓PV
/// </summary>
public class TradePositionPV
{
/// <summary>
/// 估值日期
/// </summary>
public DateTime ValueDate { get; set; }
/// <summary>
/// 交易编号
/// </summary>
public string TradeNumber { get; set; }
/// <summary>
/// 交易类型
/// </summary>
public string TradeType { get; set; }
/// <summary>
/// 权益标的收益
/// </summary>
public double EquitySubjectPV { get; set; }
/// <summary>
/// 固定利率收益
/// </summary>
public double FixedInterestRatePV { get; set; }
/// <summary>
/// 总PV
/// </summary>
public double PV { get; set; }
/// <summary>
/// 簿记账户组名称
/// </summary>
public string AssetUnitGroupName { get; set; }
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YLErp.Modules.TradeMsgOutputModule.Dto
{
public class TradePositionPvDto
{
public int TradeId { get; set; }
public string TradeType { get; set; }
public double PV { get; set; }
}
}
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YLErp.Modules.TradeMsgOutputModule.Dto
{
public class TradePriceDto
{
/// <summary>
/// 交易Id
/// </summary>
public int TradeId { get; set; }
/// <summary>
/// 期权费
/// </summary>
public double TradePrice { get; set; }
}
}
@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLErp.DBModels;
using YLErp.Modules.BasicDataModule;
using YLErp.Modules.TradeMsgOutputModule.Dto;
namespace YLErp.Modules.TradeMsgOutputModule
{
/// <summary>
/// 交易资金导出服务
/// </summary>
public class TradeCashOutputService: BaseTradeAfterEodOutputService
{
/// <summary>
/// 获取资金记录
/// </summary>
/// <param name="valueDate"></param>
/// <returns></returns>
public List<CashLogOutputDto> GetCashLog(DateTime valueDate)
{
CheckEodStatus(valueDate);
var list = GetListByDate(valueDate);
var result = new List<CashLogOutputDto>();
if (list == null || list.Count == 0)
{
return result;
}
var tradeIds=list.Select(x => x.TradeId).ToList();
Dictionary<int, TradeDicSimpleDto> tradeDic = null;
using (var db = DbContextFactory.GetYLDbContext())
{
tradeDic = GetEodTradeDicSimpleDic(db, valueDate, tradeIds);
}
if (tradeDic == null)
{
tradeDic = new Dictionary<int, TradeDicSimpleDto>();
}
Dictionary<int, AssetUnitDto> assetUnitDic = null;
var assertIds = tradeDic.Values.Select(p => p.AssetId).Distinct().ToList();
if (assertIds != null && assertIds.Count > 0)
{
assetUnitDic = new AssetUnitDataService(new OptUserInfo(0, "系统", OptUserFrom.System)).GetAssertByAssertIds(assertIds);
}
if (assetUnitDic == null)
{
assetUnitDic = new Dictionary<int, AssetUnitDto>();
}
List<ExchangeAccount> exchangeAccountList = null;
if (assertIds != null && assertIds.Count > 0)
{
exchangeAccountList = new ExchangeAccountService(new OptUserInfo(0, "系统", OptUserFrom.System)).GetExchangeAccountByBookIds(assertIds);
}
if(exchangeAccountList == null)
{
exchangeAccountList = new List<ExchangeAccount>();
}
var tradePriceList = GetTradePrice(tradeIds);
if (tradePriceList == null)
{
tradePriceList = new List<TradePriceDto>();
}
list.ForEach(p =>
{
var model = new CashLogOutputDto
{
ValueDate = p.ValueDate,
HappenedDate = p.HappenedDate,
TradeId = p.TradeId,
CashAction = p.Action,
Amount = p.Amount,
Notional = p.Notional,
};
model.TradePrice = tradePriceList.Where(d => d.TradeId == p.TradeId).Sum(p => p.TradePrice);
if (tradeDic.ContainsKey(p.TradeId))
{
var trade = tradeDic[p.TradeId];
model.TradeType = trade.TradeType;
model.TradeNumber = trade.TradeNumber;
model.TradeDate = trade.TradeDate;
model.ExerciseDate = trade.ExerciseDate;
model.UnderlyingCode = trade.UnderlyingCode;
model.PremiumPayDate = trade.PremiumPayDate;
model.OriginalStockEqvNotional = trade.OriginalStockEqvNotional;
model.StockEqvNotional = trade.StockEqvNotional;
model.OriginalNotional = trade.OriginalNotional;
if (trade.AssetId > 0 && assetUnitDic.ContainsKey(trade.AssetId))
{
model.AssetUnitGroupName = assetUnitDic[trade.AssetId].GroupName;
}
if (trade.AssetId > 0)
{
var exchangeAccount = exchangeAccountList.FirstOrDefault(d => d.DefaultBookId == trade.AssetId);
if(exchangeAccount != null)
{
model.ExChangeAccount = exchangeAccount.AccountCode;
}
}
result.Add(model);
}
});
return result;
}
private List<TradeCashOutputDto> GetByValueDate(DateTime valueDate)
{
using(var db = DbContextFactory.GetYLDbContext())
{
return db.trade_cash.AsNoTracking().Where(p => p.ValueDate == valueDate && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState))&&!p.IsDeleted).Select(p => new TradeCashOutputDto
{
Id=p.id,
ValueDate=p.ValueDate,
HappenedDate=p.HappenedDate,
Action=p.Action,
Amount=p.Amount,
TradeId=p.TradeId,
Notional=p.Notional
}).ToList();
}
}
private List<TradeCashOutputDto> GetByHappendDate(DateTime valueDate)
{
var endDate = valueDate.AddDays(1);
using (var db = DbContextFactory.GetYLDbContext())
{
var tradeCashIds = db.ClientCashInCashOut.AsNoTracking().Where(p => p.HappenDate >= valueDate && p.HappenDate < endDate && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState)) && p.TradeCashId > 0).Select(p => p.TradeCashId).Distinct().ToList();
if (tradeCashIds == null || tradeCashIds.Count == 0)
{
return null;
}
return db.trade_cash.AsNoTracking().Where(p => tradeCashIds.Contains(p.id) && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState)) && !p.IsDeleted).Select(p => new TradeCashOutputDto
{
Id = p.id,
ValueDate = p.ValueDate,
HappenedDate = valueDate,
Action = p.Action,
Amount = p.Amount,
TradeId = p.TradeId,
Notional=p.Notional,
}).ToList();
}
}
private List<TradeCashOutputDto> GetListByDate(DateTime valueDate)
{
var valueList = GetByValueDate(valueDate);
var happendList = GetByHappendDate(valueDate);
var result = new List<TradeCashOutputDto>();
if (happendList != null && happendList.Count > 0)
{
result.AddRange(happendList);
}
if (valueList != null && valueList.Count > 0)
{
valueList = valueList.Where(p => !result.Any(d => d.Id == p.Id)).ToList();
}
if (valueList != null && valueList.Count > 0)
{
result.AddRange(valueList);
}
return result;
}
private List<TradePriceDto> GetTradePrice(List<int> tradeIds)
{
using (var db = DbContextFactory.GetYLDbContext())
{
return db.trade_cash.AsNoTracking().Where(p => tradeIds.Contains(p.TradeId)&& ClientCashInCashOut._期权费.Equals(p.Action) && (p.ValidState != "InValid" || string.IsNullOrEmpty(p.ValidState)) && !p.IsDeleted).Select(p => new TradePriceDto
{
TradeId = p.TradeId,
TradePrice=p.Amount
}).ToList();
}
}
}
}
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLErp.DBModels;
using YLErp.Modules.BasicDataModule;
using YLErp.Modules.CalculationModule;
using YLErp.Modules.TradeMsgOutputModule.Dto;
namespace YLErp.Modules.TradeMsgOutputModule
{
/// <summary>
/// 交易持仓PV导出服务
/// </summary>
public class TradePvOutputService:BaseTradeAfterEodOutputService
{
public List<TradePositionPV> GetTradePositionPV<T>(DateTime valueDate) where T: EodTradePosition
{
CheckEodStatus(valueDate);
var result = new List<TradePositionPV>();
List<TradePositionPvDto> positionPVList = null;
Dictionary<int, TradeDicSimpleDto> tradeDic = null;
using (var db = DbContextFactory.GetYLDbContext())
{
positionPVList = db.Set<T>().Where(p => p.ValueDate == valueDate && p.TradeId > 0).Select(p => new TradePositionPvDto
{
TradeId = p.TradeId,
TradeType = p.TradeType,
PV = p.Pv
}).ToList();
if(positionPVList != null && positionPVList.Count > 0)
{
var tradeIds = positionPVList.Select(p => p.TradeId).Distinct().ToList();
tradeDic = GetEodTradeDicSimpleDic(db, valueDate, tradeIds);
}
}
if (positionPVList == null || tradeDic == null || positionPVList.Count == 0 || tradeDic.Count == 0)
{
return result;
}
Dictionary<int, AssetUnitDto> assetUnitDic = null;
var assertIds = tradeDic.Values.Select(p => p.AssetId).Distinct().ToList();
if(assertIds!=null&& assertIds.Count > 0)
{
assetUnitDic = new AssetUnitDataService(new OptUserInfo(0, "系统", OptUserFrom.System)).GetAssertByAssertIds(assertIds);
}
if(assetUnitDic== null)
{
assetUnitDic = new Dictionary<int, AssetUnitDto>();
}
Dictionary<int, double> swapFixedInterestRatePVDic = null;
var swapTradeIds = positionPVList.Where(p => ConsGlobal.TradeType.PayoffSwap.Equals(p.TradeType)).Select(p => p.TradeId).ToList();
if (swapTradeIds != null && swapTradeIds.Count > 0)
{
swapFixedInterestRatePVDic = PayoffSwapCalcService.GetFixedInterestRatePV(swapTradeIds, valueDate);
}
if (swapFixedInterestRatePVDic == null)
{
swapFixedInterestRatePVDic = new Dictionary<int, double>();
}
positionPVList.ForEach(p =>
{
if (tradeDic.ContainsKey(p.TradeId))
{
var tradeDto = tradeDic[p.TradeId];
var model = new TradePositionPV
{
TradeNumber = tradeDto.TradeNumber,
ValueDate = valueDate,
TradeType = p.TradeType,
PV = p.PV
};
if (ConsGlobal.TradeType.PayoffSwap.Equals(model.TradeType))
{
model.FixedInterestRatePV = swapFixedInterestRatePVDic.ContainsKey(p.TradeId) ? swapFixedInterestRatePVDic[p.TradeId] : 0;
model.EquitySubjectPV = model.PV - model.FixedInterestRatePV;
}
if (tradeDto.AssetId > 0&&assetUnitDic.ContainsKey(tradeDto.AssetId))
{
model.AssetUnitGroupName = assetUnitDic[tradeDto.AssetId].GroupName;
}
result.Add(model);
}
});
return result;
}
private Dictionary<int, double> GetFixedInterestRatePV(List<int> tradeIds, DateTime valueDate)
{
var result = new Dictionary<int, double>();
List<trade_swap> tradeSwapList = null;
List<trade_cash> tradeCashList = null;
List<trade_cash_swap> tradeCashSwapList = null;
using (var db = DbContextFactory.GetYLDbContext())
{
tradeSwapList = db.trade_swap.AsNoTracking().Where(p => tradeIds.Contains(p.TradeId)).ToList();
tradeCashList = db.trade_cash.AsNoTracking().Where(y => tradeIds.Contains(y.TradeId) && y.Action == "系统操作-互换" && y.ValidState != "InValid" && !y.IsDeleted && y.ValueDate <= valueDate).ToList();
tradeCashSwapList = db.trade_cash_swap.AsNoTracking().Where(p => tradeIds.Contains(p.TradeId)).ToList();
}
if (tradeSwapList == null)
{
tradeSwapList = new List<trade_swap>();
}
if (tradeCashList == null)
{
tradeCashList = new List<trade_cash>();
}
if (tradeCashSwapList == null)
{
tradeCashSwapList = new List<trade_cash_swap>();
}
tradeIds.ForEach(p =>
{
var tradeSwap = tradeSwapList.FirstOrDefault(d => d.TradeId == p);
if (tradeSwap != null)
{
var tradeCashIds = tradeCashList.Where(d => d.TradeId == p).Select(d => d.id).ToList();
if (tradeCashIds != null && tradeCashIds.Count > 0)
{
double amount = 0;
if (!tradeSwap.IsGetFloatingProfit)
{
amount = tradeCashSwapList.Where(d => tradeCashIds.Contains(d.TradeCashId)).Sum(d => (double)d.GetAmount);
}
if (!tradeSwap.IsPayFloatingProfit)
{
amount= -tradeCashSwapList.Where(d => tradeCashIds.Contains(d.TradeCashId)).Sum(d => (double)d.PayAmount);
}
result.Add(p, amount);
}
}
});
return result;
}
}
}