using BaseOUDAL; using Newtonsoft.Json; using System.Data; using System.Linq.Expressions; using YLErp.Commons; using YLErp.Model; namespace YLErp.Modules.QuoteFileModule { /// /// 报价文件服务 /// public class QuoteFileService : YLBaseService { readonly string WebPath = string.Empty; public QuoteFileService(OptUserInfo userInfo, string WebRootPath) : base(userInfo) { WebPath = WebRootPath; } /// /// 查询报价文件信息 /// public SearchListResult SearchList(QouteFileQueryRequest req) { Expression> expression = x => 1 == 1; if (!string.IsNullOrEmpty(req.QuoteSection)) { expression = expression.And(x => x.QuoteSection == req.QuoteSection); } if (req.QuoteDate.HasValue) { expression = expression.And(x => x.QuoteDate == req.QuoteDate.Value); } if (!string.IsNullOrEmpty(req.FileName)) { expression = expression.And(x => x.FileName.Contains(req.FileName)); } var query = DbContext.quotefile.Where(expression); var retListResult = query.ToSearchList(req); return retListResult; } /// /// 新增上传报价文件 /// /// /// /// /// /// public void AddQuoteFile(QuoteFileUpRequest req, Stream streamIn, string fileName, out int totalNum, out int successNum) { totalNum = 0; successNum = 0; var filePath = SaveFile(fileName, streamIn); // CheckQuoteFile(req.QuoteDate, req.QuoteSection); var ds = Office.ExcelHelper.ReadExcelAsDataSet(streamIn, new[] { 0 }, 0); if (ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 2) { throw new ServiceException("读取上传数据失败:数据为空"); } var table = ds.Tables[0]; var quoteFile = DbContext.quotefile.FirstOrDefault(x => x.QuoteDate == req.QuoteDate && x.QuoteSection == req.QuoteSection); if (quoteFile != null) { var quoteFileDetails = DbContext.quotefiledetail.Where(x => x.QuoteFileId == quoteFile.id); DbContext.quotefiledetail.RemoveRange(quoteFileDetails); DbContext.quotefile.Remove(quoteFile); } var model = SaveQuotefile(req, table, fileName, filePath); var rowIndex = 1; totalNum = table.Rows.Count - rowIndex - 8; SaveQuoteFileDetail(model, table, ref totalNum, ref successNum); DbContext.SaveChanges(); } /// /// 上传覆盖报价文件信息 /// /// /// /// /// /// public void UploadQuoteFile(long quoteFileId, Stream streamIn, string fileName, out int totalNum, out int successNum) { totalNum = 0; successNum = 0; var quoteFile = DbContext.quotefile.Find(quoteFileId); if (quoteFile == null) { throw new ServiceException("未找到报价文件信息"); } var quoteFileDetails = DbContext.quotefiledetail.Where(x => x.QuoteFileId == quoteFileId); DbContext.quotefiledetail.RemoveRange(quoteFileDetails); var filePath = SaveFile(fileName, streamIn); var ds = Office.ExcelHelper.ReadExcelAsDataSet(streamIn, new[] { 0 }, 0); if (ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 2) { throw new ServiceException("读取上传数据失败:数据为空") { Tag = "111" }; } var oldFilePath = quoteFile.FilePath; var table = ds.Tables[0]; var rowIndex = 1; totalNum = table.Rows.Count - rowIndex - 8; var mjson = GetMJson(table); quoteFile.FileName = fileName.Split(".")[0]; quoteFile.FilePath = filePath; quoteFile.MJson = mjson; quoteFile.OptId = UserId; quoteFile.OptName = UserName; quoteFile.OptTime = DateTime.Now; SaveQuoteFileDetail(quoteFile, table, ref totalNum, ref successNum); DbContext.SaveChanges(); if (oldFilePath != quoteFile.FilePath) { if (oldFilePath.Contains("\\")) { oldFilePath = "\\" + oldFilePath; } else { oldFilePath = "/" + oldFilePath; } var oldfilePath = Path.Combine(WebPath + oldFilePath); if (File.Exists(oldfilePath)) { File.Delete(oldfilePath); } } } /// /// 删除报价文件信息 /// /// /// public void DelteQuoteFile(long quoteFileId) { var quoteFile = DbContext.quotefile.Find(quoteFileId); if (quoteFile == null) { throw new ServiceException("未找到报价文件信息"); } if (quoteFile.FilePath.Contains("\\")) { quoteFile.FilePath = "\\" + quoteFile.FilePath; } else { quoteFile.FilePath = "/" + quoteFile.FilePath; } var filePath = Path.Combine(WebPath + quoteFile.FilePath); if (File.Exists(filePath)) { File.Delete(filePath); } DbContext.quotefile.Remove(quoteFile); var quoteFileDetails = DbContext.quotefiledetail.Where(x => x.QuoteFileId == quoteFileId); DbContext.quotefiledetail.RemoveRange(quoteFileDetails); DbContext.SaveChanges(); } /// /// 保存报价文件明细 /// /// /// /// /// private void SaveQuoteFileDetail(quotefile quoteFile, DataTable table, ref int totalNum, ref int successNum) { var dataReader = new DataRowReader(table, 8); var mcCloumnNames = GetMCCloumnNames(table); var rowIndex = 1; foreach (var row in table.Rows.Cast().Skip(9)) { rowIndex++; dataReader.SetDataRow(row); if (row.ItemArray.All(n => string.IsNullOrWhiteSpace(n?.ToString()))) { totalNum--; continue; } var quotefiledetail = InitQuoteFileDetail(quoteFile, dataReader, mcCloumnNames); if (quotefiledetail != null) { successNum++; DbContext.quotefiledetail.Add(quotefiledetail); } } } /// /// 获取m1-m6 数据列名称 /// /// /// private List GetMCCloumnNames(DataTable table) { var colCount = table.Columns.Count; var mcList = new List(); for (var index = 2; index < colCount; index++) { var row = table.Rows[8]; var col = row[index]?.ToString()?.Trim(); if (string.IsNullOrEmpty(col)) { continue; } var colName = col.Replace("%", "").Trim(); mcList.Add(colName); } return mcList; } /// /// 从excel获取数据初始化报价文件 /// /// /// /// /// private quotefiledetail InitQuoteFileDetail(quotefile quoteFile, DataRowReader reader, List mcCloumnNames) { var model = new quotefiledetail { QuoteFileId = quoteFile.id, UnderlyingCode = reader.GetString("证券代码", false), UnderlyingName = reader.GetString("证券简称", false) }; var data = new Dictionary(); foreach (var name in mcCloumnNames) { data.Add(name, reader.GetString(name, false)); } model.DataJson = JsonConvert.SerializeObject(data); return model; } /// /// 校验报价文件 /// /// /// /// private void CheckQuoteFile(DateTime quoteDate, string quoteSection) { if (DbContext.quotefile.Any(x => x.QuoteDate == quoteDate && x.QuoteSection == quoteSection)) { throw new ServiceException($"{quoteSection}报价日期为{quoteDate.ToString("D")}的文件已存在"); } } /// /// 保存报价文件 /// /// /// /// private quotefile SaveQuotefile(QuoteFileUpRequest req, DataTable table, string fileName, string filePath) { var mjson = GetMJson(table); var quoteFile = new quotefile { QuoteDate = req.QuoteDate, QuoteSection = req.QuoteSection, Disclaimer = req.Disclaimer, MJson = mjson, OptId = UserId, OptName = UserName, OptTime = DateTime.Now, FileName = fileName.Split(".")[0], FilePath = filePath }; DbContext.quotefile.Add(quoteFile); DbContext.SaveChanges(); return quoteFile; } /// /// 获取M1-M6 date /// /// /// private string GetMJson(DataTable table) { var quoteFileMJsons = new List(); foreach (var row in table.Rows.Cast().Skip(4).Take(4)) { if (row.ItemArray == null || row.ItemArray.Length < 2) { continue; } var quoteFileMJson = new QuoteFileMJson(); if (row.ItemArray[0] != null) { quoteFileMJson.MName = row.ItemArray[0].ToString(); } DateTime.TryParse(row.ItemArray[1].ToString(), out var mDate); quoteFileMJson.MDate = mDate.ToString("yyyy-MM-dd"); if (!string.IsNullOrEmpty(quoteFileMJson.MName)) { quoteFileMJsons.Add(quoteFileMJson); } } if (quoteFileMJsons.Count == 0) { return string.Empty; } return JsonConvert.SerializeObject(quoteFileMJsons); } /// /// 获取上传文件根路径 /// /// private string GetUploadRootPath() { var uploadRootPath = Path.Combine("uploads", "quote"); var _uploadRealPth = Path.Combine(WebPath, uploadRootPath); if (!Directory.Exists(_uploadRealPth)) { Directory.CreateDirectory(_uploadRealPth); } return uploadRootPath; } /// /// 保存文件 /// /// /// /// private string SaveFile(string fileName, Stream streamIn) { var uploadRootPath = GetUploadRootPath(); var filePath = Path.Combine(uploadRootPath, fileName); var fileRealPath = Path.Combine(WebPath, filePath); using var fstream = File.OpenWrite(fileRealPath); streamIn.CopyTo(fstream); return filePath; } } }