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"); /// /// 组装文件名称 /// /// /// /// protected string GetFileName(string namePrefix,DateTime valueDate) { return namePrefix + "_" + valueDate.ToString("yyyyMMdd") + ".dbf"; } /// /// 获取文件路径 /// /// private string GetFilePath() { if (!Directory.Exists(DBFFileSavePath)) { Directory.CreateDirectory(DBFFileSavePath); } return DBFFileSavePath; } /// /// 创建文件 /// /// /// /// protected DbfFile CreateFile(string namePrefix, DateTime valueDate, out string fullFilePath) { return CreateDbfFile(GetFileName(namePrefix, valueDate), GetDbfFileColumns(),out fullFilePath); } private DbfFile CreateDbfFile(string dbfName, List 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 GetDbfFileColumns(); public abstract GenerateDbfFileResult GenerateDbfFile(DateTime valueDate); /// /// 获取部门代码 /// /// /// 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 ""; } } /// /// 获取业务类别 /// /// /// 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; } } }