186 lines
6.2 KiB
C#
186 lines
6.2 KiB
C#
using System.Text;
|
|
using YLErp.BLL;
|
|
using YLErp.Models;
|
|
|
|
namespace YLErp.Modules.UnderlyingModule
|
|
{
|
|
public class VarietyHisDataService : YLBaseService
|
|
{
|
|
public VarietyHisDataService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
|
|
}
|
|
|
|
public VarietyHisDataService(YLBaseService baseService) : base(baseService)
|
|
{
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 删除品种历史数据
|
|
/// </summary>
|
|
public int RemoveHisData(int varietyId, DateTime valueDate, string valueType)
|
|
{
|
|
if (varietyId < 1)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
valueDate = valueDate.Date;
|
|
|
|
var data = DbContext.VarietyHisData.FirstOrDefault(
|
|
n => n.VarietyId == varietyId && n.ValueDate == valueDate && n.ValueType == valueType);
|
|
|
|
if (data != null)
|
|
{
|
|
DbContext.VarietyHisData.Remove(data);
|
|
return DbContext.SaveChanges();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新品种历史数据
|
|
/// </summary>
|
|
public int AddOrUpdateHisData(VarietyHisDataAddOrUpdateRequest req, bool saveChanges = true)
|
|
{
|
|
if (req is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(req));
|
|
}
|
|
|
|
if (saveChanges && (req.VarietyId < 1 || !DbContext.variety.Any(n => n.id == req.VarietyId)))
|
|
{
|
|
throw new ServiceException("品种数据 不存在");
|
|
}
|
|
|
|
switch (req.ValueType)
|
|
{
|
|
case nameof(MarginParamModel.MarginRate):
|
|
case nameof(MarginParamModel.UpDownLimit):
|
|
case nameof(MarginParamModel.VolatilityRate): break;
|
|
default: throw new ServiceException("ValueType 不支持:" + req.ValueType);
|
|
}
|
|
|
|
var hisdata = DbContext.VarietyHisData.OrderByDescending(n => n.ValueDate).FirstOrDefault(
|
|
n => n.VarietyId == req.VarietyId && n.ValueDate <= req.ValueDate && n.ValueType == req.ValueType);
|
|
|
|
if (hisdata != null)
|
|
{
|
|
var newValue = req.Value;
|
|
|
|
//如果新值和旧值一致则不需要处理
|
|
if (newValue.HasValue && Math.Abs(newValue.Value - hisdata.Value) < 1e-6)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
else if (!req.Value.HasValue)
|
|
{
|
|
return 0; //如果没有历史数据并且新值为null
|
|
}
|
|
|
|
if (hisdata == null || hisdata.ValueDate < req.ValueDate)
|
|
{
|
|
hisdata = new VarietyHisData
|
|
{
|
|
VarietyId = req.VarietyId,
|
|
ValueDate = req.ValueDate,
|
|
ValueType = req.ValueType
|
|
};
|
|
|
|
DbContext.VarietyHisData.Add(hisdata);
|
|
}
|
|
|
|
hisdata.Value = req.Value ?? 0;
|
|
|
|
SetDBModelOpt(hisdata);
|
|
|
|
return saveChanges ? DbContext.SaveChanges() : 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 回写历史数据到主表
|
|
/// </summary>
|
|
public int BackHisDataToMainTable()
|
|
{
|
|
var db = DbContextFactory.GetYLDbContext();
|
|
var valueDate = valuedateBLL.ValueDate;
|
|
var startDate = valuedateBLL.ValueDate.AddDays(-3);
|
|
var uhgQuery = from a in db.VarietyHisData
|
|
where a.ValueDate >= startDate && a.ValueDate <= valueDate
|
|
group a by new
|
|
{
|
|
a.VarietyId,
|
|
a.ValueType
|
|
} into aa
|
|
select new
|
|
{
|
|
aa.Key.VarietyId,
|
|
aa.Key.ValueType,
|
|
ValueDate = aa.Max(n => n.ValueDate)
|
|
};
|
|
|
|
var uhQuery = from a in uhgQuery
|
|
join b in db.VarietyHisData on new { a.VarietyId, a.ValueType, a.ValueDate } equals new { b.VarietyId, b.ValueType, b.ValueDate }
|
|
select new
|
|
{
|
|
b.VarietyId,
|
|
b.Value,
|
|
b.ValueType
|
|
};
|
|
|
|
var hisdatas = uhQuery.ToArray();
|
|
|
|
if (!hisdatas.Any())
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
var sb = new StringBuilder(1000);
|
|
|
|
const string sqlfmtMarginRate = "update variety set Margin='{0}' WHERE id='{1}';";
|
|
const string sqlfmtUpDownLimit = "update variety set UpLimit='{0}',DownLimit='{0}' WHERE id='{1}';";
|
|
const string sqlfmtVolatilityRate = "update variety set VolatilityRate='{0}' WHERE id='{1}';";
|
|
|
|
foreach (var item in hisdatas)
|
|
{
|
|
switch (item.ValueType)
|
|
{
|
|
case nameof(MarginParamModel.MarginRate):
|
|
sb.AppendFormat(sqlfmtMarginRate, item.Value.ToString("0.0#####"), item.VarietyId).AppendLine();
|
|
break;
|
|
case nameof(MarginParamModel.UpDownLimit):
|
|
sb.AppendFormat(sqlfmtUpDownLimit, item.Value.ToString("0.0###%"), item.VarietyId).AppendLine();
|
|
break;
|
|
case nameof(MarginParamModel.VolatilityRate):
|
|
sb.AppendFormat(sqlfmtVolatilityRate, item.Value.ToString("0.0###%"), item.VarietyId).AppendLine();
|
|
break;
|
|
}
|
|
}
|
|
|
|
var sql = sb.ToString();
|
|
LogFactory.GetLogger("回写品种历史数据到主表").Info(sql);
|
|
return DbContext.Database.ExecuteSqlRaw(sql);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class VarietyHisDataAddOrUpdateRequest
|
|
{
|
|
public int VarietyId { get; set; }
|
|
|
|
/// <summary>
|
|
/// 使用MarginParamModel的属性字段
|
|
/// </summary>
|
|
public string ValueType { get; set; }
|
|
|
|
public DateTime ValueDate { get; set; }
|
|
|
|
public double? Value { get; set; }
|
|
}
|
|
}
|