225 lines
11 KiB
C#
225 lines
11 KiB
C#
using iTextSharp.text;
|
|
using iTextSharp.text.pdf;
|
|
using YLErp.BLL;
|
|
using YLErp.Configuration;
|
|
using YLErp.DBModels.Converts;
|
|
using YLErp.Helpers;
|
|
using YLErp.Model;
|
|
using YLErp.Modules.ClientModule;
|
|
using YLErp.Office.iTextModule;
|
|
|
|
namespace YLErp.Modules.ReportModule
|
|
{
|
|
/// <summary>
|
|
/// 到期日报告服务
|
|
/// </summary>
|
|
public class MaturityDateTradeService : YLBaseService
|
|
{
|
|
public MaturityDateTradeService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送到期日报告邮件
|
|
/// </summary>
|
|
public string SendEMailMaturityDateTrade(int? ClientId, DateTime? ExerciseDate)
|
|
{
|
|
if (ExerciseDate == null)
|
|
{
|
|
throw new ServiceException("到期日不可为空");
|
|
}
|
|
|
|
//获取当前用户可操作的所有交易信息
|
|
var bll = new tradeBLL();
|
|
var req = new TradeReq
|
|
{
|
|
ExerciseDate = ExerciseDate
|
|
};
|
|
req.ExerciseDateStart = req.ExerciseDate;
|
|
req.ExerciseDateEnd = req.ExerciseDate;
|
|
//req.TradeStatus = ConsTrade.确认成交;
|
|
req.TradeStatusList = new List<string> { ConsTrade.已到期, ConsTrade.已执行, ConsTrade.确认成交, ConsTrade.期初价格已确认 };
|
|
req.ClientId = ClientId;
|
|
req.sidx = "OptDate";
|
|
req.sord = "desc";
|
|
var tradeList = bll.SearchTradeList(req, true, true);
|
|
if (tradeList == null || tradeList.Count == 0)
|
|
{
|
|
throw new ServiceException(ExerciseDate.Value.Date.ToString("yyyy年MM月dd日") + "没有可行权报告信息需要发送!");
|
|
}
|
|
|
|
var clientIds = tradeList.Select(t => t.ClientId).ToList();
|
|
var clientListOn = clientIds.Any() ? ClientDataQueryService.GetClientsFromDB(t => clientIds.Contains(t.id)).ToList() : null;
|
|
|
|
if (clientListOn == null || clientListOn.Count < 1)
|
|
{
|
|
return "发送客户" + ExerciseDate.Value.Date.ToString("yyyy年MM月dd日") + "行权报告成功。";
|
|
}
|
|
|
|
var errorMsg = new List<string>();
|
|
var errorClientList = new List<string>();
|
|
|
|
foreach (var client in clientListOn)
|
|
{
|
|
var tempTradeList = tradeList.Where(t => t.ClientId == client.id).ToList();
|
|
//生成文档保存到备份目录
|
|
//string saveFileDir = Server.MapPath("~/App_Docs/MaturityDateTrade");
|
|
//string fileName = @"D:\pdf\tests.pdf";
|
|
//string filePath = Path.Combine(saveFileDir, client.Name+"_"+req.ExerciseDate.Value.Date);
|
|
|
|
//设置文件名称
|
|
var saveFileDir = OtcAppContext.MapPath("~/App_Docs/ManturityDateTrade");
|
|
if (!Directory.Exists(saveFileDir))
|
|
{
|
|
Directory.CreateDirectory(saveFileDir);
|
|
}
|
|
|
|
var fileName = $"行权日报告-{req.ExerciseDate.Value:yyyy-MM-dd}-{client.Name}.pdf";
|
|
|
|
var filePath = Path.Combine(saveFileDir, fileName);
|
|
|
|
var document = new Document(HeaderAndFooterEvent.rect);
|
|
//此处使用的是http请求的流,你也可以使用文件流Stream
|
|
var writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
|
|
try
|
|
{
|
|
document.Open();
|
|
writer.PageEvent = new HeaderAndFooterEvent();
|
|
|
|
HeaderAndFooterEvent.PAGE_NUMBER = true;//实现页眉跟页脚
|
|
HeaderAndFooterEvent.tpl = writer.DirectContent.CreateTemplate(1000, 1000); //定义模板
|
|
|
|
HeaderAndFooterEvent.HeaderLeft = "";
|
|
HeaderAndFooterEvent.HeaderRight = PS.Config.CompanyName ?? "";
|
|
//HeaderAndFooterEvent.FooterLeft = "TEL:010-87922095";
|
|
HeaderAndFooterEvent.FooterRight = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "(UTC)";
|
|
|
|
//每次在添加文本内容之前可以先设置字体,有效期持续到重新设置之前
|
|
|
|
HeaderAndFooterEvent.SetFont(BaseColor.DARK_GRAY, "宋体", 15, Font.BOLD);
|
|
document.Add(HeaderAndFooterEvent.AddParagraph("客户行权日报告", 1, 1.5f));
|
|
HeaderAndFooterEvent.SetFont(BaseColor.DARK_GRAY, "宋体", 12);
|
|
document.Add(HeaderAndFooterEvent.AddParagraph("客户: " + client.Name, 0, 1.5f));
|
|
document.Add(HeaderAndFooterEvent.AddParagraph("日期: " + req.ExerciseDate.Value.ToString("yyyy-MM-dd"), 0, 1.5f));
|
|
//增加空行
|
|
document.Add(HeaderAndFooterEvent.AddParagraph(" ", 0, 1.5f));
|
|
//输出客户行权报告表格信息
|
|
var table = new PdfPTable(10);
|
|
table.SetWidths(new float[] { 100, 120, 100, 150, 180, 130, 100, 100, 100, 100 });
|
|
|
|
//table.HorizontalAlignment = Element.ALIGN_RIGHT;
|
|
//table.SpacingBefore = (5);
|
|
//table.SpacingAfter = (5);
|
|
|
|
var bftitle = BaseFont.CreateFont("C:\\Windows\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //用系统中的字体文件SimHei.ttf创建文件字体
|
|
var fonttitle = new Font(bftitle, 10);
|
|
|
|
var cellTexts = new string[] { "交易方向", "期权类型", "看涨看跌", "行权方式", "到期日", "标的", "行权价", "期初价格", "名义本金", "交易数量" };
|
|
foreach (var text in cellTexts)
|
|
{
|
|
var cell = new PdfPCell(new Phrase(text, fonttitle))
|
|
{
|
|
BackgroundColor = (BaseColor.GRAY)
|
|
};
|
|
table.AddCell(cell);
|
|
}
|
|
|
|
if (tempTradeList != null && tempTradeList.Count > 0)
|
|
{
|
|
foreach (var trade in tempTradeList)
|
|
{
|
|
var underlyingCodes = tempTradeList.Select(t => t.UnderlyingId).Distinct().ToList();
|
|
var varietyDict = (from um in DbContext.underlying_manager
|
|
join var in DbContext.variety on um.UnderlyingTypeId equals var.id
|
|
where underlyingCodes.Contains(um.id)
|
|
select new { underlyingId = um.id, variety = var }).ToDictionary(t => t.underlyingId, t => t.variety);
|
|
|
|
table.AddCell(new Phrase(BuySellConvert.GetClientBuySell(trade.BuySell), fonttitle));
|
|
table.AddCell(new Phrase(trade.TradeType, fonttitle));
|
|
table.AddCell(new Phrase(trade.CallPut == "Call" ? "看涨" : "看跌", fonttitle));
|
|
|
|
if (!string.IsNullOrEmpty(trade.ExerciseMode))
|
|
{
|
|
if (trade.ExerciseMode == "European")
|
|
{
|
|
table.AddCell(new Phrase("欧式", fonttitle));
|
|
}
|
|
else if (trade.ExerciseMode == "American")
|
|
{
|
|
table.AddCell(new Phrase("美式", fonttitle));
|
|
}
|
|
else
|
|
{
|
|
table.AddCell(new Phrase("", fonttitle));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
table.AddCell(new Phrase("", fonttitle));
|
|
}
|
|
table.AddCell(trade.ExerciseDate == null ? "" : trade.ExerciseDate.Value.ToString("yyyy-MM-dd"));
|
|
table.AddCell(trade.UnderlyingCode);
|
|
table.AddCell(new Phrase(trade.Strike == null ? "" : trade.Strike.Value + "", fonttitle));
|
|
table.AddCell(new Phrase(trade.SpotPrice == null ? "" : trade.SpotPrice.Value + "", fonttitle));
|
|
table.AddCell(new Phrase(trade.StockEqvNotional == 0 ? "" : trade.StockEqvNotional + "", fonttitle));
|
|
|
|
if (trade.UnderlyingInstrumentType == "Stock")
|
|
{
|
|
table.AddCell(new Phrase("", fonttitle));
|
|
}
|
|
else
|
|
{
|
|
var QuoteUnitSingle = "";
|
|
if (varietyDict != null && varietyDict.ContainsKey(trade.UnderlyingId))
|
|
{
|
|
QuoteUnitSingle = varietyDict[trade.UnderlyingId].QuoteUnitSingle;
|
|
}
|
|
table.AddCell(new Phrase(trade.TradeAmount == 0 ? "" : trade.TradeAmount + QuoteUnitSingle, fonttitle));
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var cell = new PdfPCell(new Phrase("无", fonttitle))
|
|
{
|
|
Colspan = (10)
|
|
};
|
|
table.AddCell(cell);
|
|
}
|
|
|
|
table.DefaultCell.BackgroundColor = (null);
|
|
|
|
document.Add(table);
|
|
}
|
|
finally
|
|
{
|
|
writer.Flush();
|
|
writer.CloseStream = true;
|
|
document.Close();
|
|
}
|
|
|
|
// 要向该客户的所有订阅了邮件通知的人员发送邮件
|
|
var emails = ClientDataQueryService.GetClientEmails(client.id, false);
|
|
|
|
var sendMail = EmailHelper.SendMail(string.Join(";", emails), "行权日报告-" + req.ExerciseDate.Value.ToString("yyyy-MM-dd"), "", true, new[] { filePath });
|
|
if (!string.IsNullOrEmpty(sendMail))
|
|
{
|
|
errorClientList.Add(client.Name);
|
|
if (!errorMsg.Contains(sendMail))
|
|
{
|
|
errorMsg.Add(sendMail);
|
|
}
|
|
}
|
|
}
|
|
|
|
//判断是否有发送失败的客户
|
|
if (errorClientList.Count > 0)
|
|
{
|
|
throw new ServiceException($"错误:{string.Join(",", errorClientList.ToArray())}发送报告时出错,{string.Join(",", errorMsg)}。");
|
|
}
|
|
|
|
return "发送客户" + ExerciseDate.Value.Date.ToString("yyyy年MM月dd日") + "行权报告成功。";
|
|
}
|
|
}
|
|
}
|