342 lines
13 KiB
C#
342 lines
13 KiB
C#
using BaseOUDAL;
|
|
using Newtonsoft.Json;
|
|
using System.Data;
|
|
using System.Linq.Expressions;
|
|
using YLErp.Commons;
|
|
using YLErp.Model;
|
|
|
|
namespace YLErp.Modules.QuoteFileModule
|
|
{
|
|
/// <summary>
|
|
/// 报价文件服务
|
|
/// </summary>
|
|
public class QuoteFileService : YLBaseService
|
|
{
|
|
readonly string WebPath = string.Empty;
|
|
|
|
public QuoteFileService(OptUserInfo userInfo, string WebRootPath) : base(userInfo)
|
|
{
|
|
WebPath = WebRootPath;
|
|
}
|
|
/// <summary>
|
|
/// 查询报价文件信息
|
|
/// </summary>
|
|
public SearchListResult<quotefile> SearchList(QouteFileQueryRequest req)
|
|
{
|
|
Expression<Func<quotefile, bool>> 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;
|
|
}
|
|
/// <summary>
|
|
/// 新增上传报价文件
|
|
/// </summary>
|
|
/// <param name="req"></param>
|
|
/// <param name="streamIn"></param>
|
|
/// <param name="totalNum"></param>
|
|
/// <param name="successNum"></param>
|
|
/// <exception cref="ServiceException"></exception>
|
|
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();
|
|
|
|
}
|
|
/// <summary>
|
|
/// 上传覆盖报价文件信息
|
|
/// </summary>
|
|
/// <param name="quoteFileId"></param>
|
|
/// <param name="streamIn"></param>
|
|
/// <param name="totalNum"></param>
|
|
/// <param name="successNum"></param>
|
|
/// <exception cref="ServiceException"></exception>
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 删除报价文件信息
|
|
/// </summary>
|
|
/// <param name="quoteFileId"></param>
|
|
/// <exception cref="ServiceException"></exception>
|
|
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();
|
|
}
|
|
/// <summary>
|
|
/// 保存报价文件明细
|
|
/// </summary>
|
|
/// <param name="quoteFile"></param>
|
|
/// <param name="table"></param>
|
|
/// <param name="totalNum"></param>
|
|
/// <param name="successNum"></param>
|
|
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<DataRow>().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);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取m1-m6 数据列名称
|
|
/// </summary>
|
|
/// <param name="table"></param>
|
|
/// <returns></returns>
|
|
private List<string> GetMCCloumnNames(DataTable table)
|
|
{
|
|
var colCount = table.Columns.Count;
|
|
var mcList = new List<string>();
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 从excel获取数据初始化报价文件
|
|
/// </summary>
|
|
/// <param name="quoteFile"></param>
|
|
/// <param name="reader"></param>
|
|
/// <param name="mcCloumnNames"></param>
|
|
/// <returns></returns>
|
|
private quotefiledetail InitQuoteFileDetail(quotefile quoteFile, DataRowReader reader, List<string> mcCloumnNames)
|
|
{
|
|
var model = new quotefiledetail
|
|
{
|
|
QuoteFileId = quoteFile.id,
|
|
UnderlyingCode = reader.GetString("证券代码", false),
|
|
UnderlyingName = reader.GetString("证券简称", false)
|
|
};
|
|
var data = new Dictionary<string, string>();
|
|
foreach (var name in mcCloumnNames)
|
|
{
|
|
data.Add(name, reader.GetString(name, false));
|
|
}
|
|
model.DataJson = JsonConvert.SerializeObject(data);
|
|
return model;
|
|
|
|
|
|
}
|
|
/// <summary>
|
|
/// 校验报价文件
|
|
/// </summary>
|
|
/// <param name="quoteDate"></param>
|
|
/// <param name="quoteSection"></param>
|
|
/// <exception cref="ServiceException"></exception>
|
|
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")}的文件已存在");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 保存报价文件
|
|
/// </summary>
|
|
/// <param name="req"></param>
|
|
/// <param name="table"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 获取M1-M6 date
|
|
/// </summary>
|
|
/// <param name="table"></param>
|
|
/// <returns></returns>
|
|
private string GetMJson(DataTable table)
|
|
{
|
|
var quoteFileMJsons = new List<QuoteFileMJson>();
|
|
foreach (var row in table.Rows.Cast<DataRow>().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);
|
|
}
|
|
/// <summary>
|
|
/// 获取上传文件根路径
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private string GetUploadRootPath()
|
|
{
|
|
var uploadRootPath = Path.Combine("uploads", "quote");
|
|
var _uploadRealPth = Path.Combine(WebPath, uploadRootPath);
|
|
if (!Directory.Exists(_uploadRealPth))
|
|
{
|
|
Directory.CreateDirectory(_uploadRealPth);
|
|
}
|
|
return uploadRootPath;
|
|
}
|
|
/// <summary>
|
|
/// 保存文件
|
|
/// </summary>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="streamIn"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|