140 lines
4.5 KiB
C#
140 lines
4.5 KiB
C#
using CsvHelper;
|
|
using CsvHelper.Configuration;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using YieldChain.Helpers;
|
|
using YLErp.Model;
|
|
|
|
namespace YLErp.Modules.VolatilityModule
|
|
{
|
|
/// <summary>
|
|
/// 品种波动率调整服务
|
|
/// </summary>
|
|
public class VarietyVolAdjustService : YLBaseService
|
|
{
|
|
public VarietyVolAdjustService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
public IEnumerable<VarietyVolAdjustModel> GetList()
|
|
{
|
|
var query = from n in DbContext.variety
|
|
//where n.UnderlyingInstrumentType == "CommodityFutures"
|
|
orderby n.VarietyCode
|
|
select new VarietyVolAdjustModel
|
|
{
|
|
VarietyId = n.id,
|
|
VarietyCode = n.VarietyCode,
|
|
Volitality = n.VolatilityAdjust
|
|
};
|
|
return query.ToArray();
|
|
}
|
|
|
|
public int SaveData(VarietyVolAdjustModel model)
|
|
{
|
|
if (model is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
|
|
var dbData = DbContext.variety.Find(model.VarietyId);
|
|
if (dbData == null)
|
|
{
|
|
throw new ServiceException("保存失败:品种数据不存在");
|
|
}
|
|
dbData.VolatilityAdjust = model.Volitality;
|
|
return DbContext.SaveChanges();
|
|
}
|
|
|
|
public ImportResultModel ImportCsvDatas(Stream stream)
|
|
{
|
|
var result = new ImportResultModel();
|
|
|
|
IEnumerable<ImportModel> importModels;
|
|
|
|
var csvConfig = new CsvConfiguration(CultureInfo.InvariantCulture)
|
|
{
|
|
//规避空数据行
|
|
ShouldSkipRecord = n => n.Row.Parser.Record == null || n.Row.Parser.Record.All(m => string.IsNullOrWhiteSpace(m))
|
|
};
|
|
|
|
//当前编码支持ansi和utf with bom
|
|
using (var sr = new StreamReader(stream, Encoding.Default))
|
|
using (var csv = new CsvReader(sr, csvConfig))
|
|
{
|
|
csv.Context.TypeConverterCache.AddConverter<string>(Helpers.CsvTypeConverts.StringConverter.Required);
|
|
csv.Context.TypeConverterCache.AddConverter<double>(Helpers.CsvTypeConverts.DoubleConverter.Required);
|
|
csv.Context.RegisterClassMap<ImportModel.ImportMap>();
|
|
importModels = csv.GetRecords<ImportModel>().ToArray();
|
|
}
|
|
|
|
if (!importModels.Any())
|
|
{
|
|
throw new ServiceException("没有可导入的数据");
|
|
}
|
|
|
|
result.TotalCount = importModels.Count();
|
|
|
|
var dic = DbContext.variety.ToDictionary(n => n.VarietyCode, StringComparer.OrdinalIgnoreCase);
|
|
|
|
foreach (var item in importModels)
|
|
{
|
|
if (dic.TryGetValue(item.code, out var variety))
|
|
{
|
|
result.SuccessCount++;
|
|
variety.VolatilityAdjust = item.vol;
|
|
}
|
|
else
|
|
{
|
|
result.Errors.Add(item.code + ": 品种数据不存在");
|
|
}
|
|
}
|
|
|
|
DbContext.SaveChanges();
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取导入模板
|
|
/// </summary>
|
|
public static string GetCsvTemplate()
|
|
{
|
|
return new ImportModel.ImportMap().GenTemplate();
|
|
}
|
|
|
|
class ImportModel
|
|
{
|
|
public string code { get; set; }
|
|
|
|
public double vol { get; set; }
|
|
|
|
public class ImportMap : ClassMap<ImportModel>
|
|
{
|
|
public ImportMap()
|
|
{
|
|
Map(m => m.code).Name("品种代码").TypeConverter(Helpers.CsvTypeConverts.StringConverter.Required);
|
|
Map(m => m.vol).Name("历史波动率").TypeConverter(Helpers.CsvTypeConverts.DoubleConverter.Required);
|
|
}
|
|
|
|
public string GenTemplate()
|
|
{
|
|
var sb = new StringBuilder(512);
|
|
|
|
foreach (var map in MemberMaps)
|
|
{
|
|
sb.AppendCSVCell(map.Data.Names.First()).Append(',');
|
|
}
|
|
|
|
if (sb.Length > 0)
|
|
{
|
|
sb.Remove(sb.Length - 1, 1);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|