Files
zszq-trs/YLErpDAL/BLL/CorrelationBLL.cs
T
2024-05-09 14:06:26 +08:00

143 lines
5.2 KiB
C#

using BaseOUDAL;
using YLErp.Model;
namespace YLErp.BLL
{
public class correlationBLL
{
private YLContext db = new YLContext();
public static object CorrelationLocker = new object();
/// <summary>
///
/// </summary>
/// <param name="cor"></param>
/// <returns>error</returns>
public string InsertCorrelation(CorrelationTable cor)
{
lock (CorrelationLocker)
{
//检查是否有存在cor
if (db.correlation.Any(c => c.State == valuedate.当前使用 && (c.UnderlyingId1 == cor.UnderlyingId1 && c.UnderlyingId2 == cor.UnderlyingId2) || (c.UnderlyingId1 == cor.UnderlyingId2 && c.UnderlyingId2 == cor.UnderlyingId1)))
{
return "已经存在对应关系数据,不能新增";
}
cor.DayLastState = CorrelationTable.LatestState;
cor.LiveState = "Live";
cor.OptDate = DateTime.Now;
cor.State = valuedate.当前使用;
//根据id先后顺序加入
//需要根据给定的underlying根据id顺序填入underlyingid1,和2否则在分组时会有问题
if (cor.UnderlyingId1 > cor.UnderlyingId2)
{
//交换
int? temp = cor.UnderlyingId1;
string tempcode = cor.UnderlyingCode1;
cor.UnderlyingId1 = cor.UnderlyingId2;
cor.UnderlyingCode1 = cor.UnderlyingCode2;
cor.UnderlyingId2 = temp;
cor.UnderlyingCode2 = tempcode;
}
db.correlation.Add(cor);
db.SaveChanges();
}
return "";
}
public SearchListResult<CorrelationTable> SearchGroupList(CorrelationReq req)
{
var predicate = PredicateBuilder.Create<CorrelationTable>(n => n.ValueDate >= req.ValueDateStart && n.DayLastState == CorrelationTable.LatestState && n.LiveState != "Matured");
if (!string.IsNullOrWhiteSpace(req.UnderlyingCode1))
{
predicate = predicate.And(n => n.UnderlyingCode1 == req.UnderlyingCode1);
}
if (!string.IsNullOrWhiteSpace(req.UnderlyingCode2))
{
predicate = predicate.And(n => n.UnderlyingCode2 == req.UnderlyingCode2);
}
if (req.ValueDateEnd.Year > 1)
{
predicate = predicate.And(n => n.ValueDate <= req.ValueDateEnd);
}
var query = db.correlation.Where(predicate).OrderBy(n => n.id);
if (string.IsNullOrEmpty(req.sidx))
{
req.sidx = "id";
req.sord = "desc";
}
return query.ToSearchList(req, false);
}
/// <summary>
/// 查询correlation
/// </summary>
public SearchListResult<CorrelationTable> SearchList(CorrelationReq req)
{
var query = from source in db.correlation select source;
if (req.UnderlyingId1 != null)
{
query = query.Where(d => d.UnderlyingId1 == req.UnderlyingId1);
}
if (!string.IsNullOrEmpty(req.UnderlyingCode1))
{
query = query.Where(d => d.UnderlyingCode1.Contains(req.UnderlyingCode1));
}
if (req.UnderlyingId2 != null)
{
query = query.Where(d => d.UnderlyingId2 == req.UnderlyingId2);
}
if (!string.IsNullOrEmpty(req.UnderlyingCode2))
{
query = query.Where(d => d.UnderlyingCode2.Contains(req.UnderlyingCode2));
}
if (req.OptId != null)
{
if (req.OptId > 1)
{
query = query.Where(d => d.OptId == req.OptId);
}
}
if (req.ValueDateStart != DateTime.MinValue)
{
query = query.Where(d => d.ValueDate >= req.ValueDateStart);
}
if (req.ValueDateEnd != DateTime.MinValue)
{
DateTime ValueDateTemp = req.ValueDateEnd.AddDays(1);
query = query.Where(d => d.ValueDate < ValueDateTemp);
}
if (!string.IsNullOrEmpty(req.State))
{
query = query.Where(d => d.State.Contains(req.State));
}
if (string.IsNullOrEmpty(req.sidx))
{
req.sidx = "id";
req.sord = "desc";
}
return query.ToSearchList(req);
}
// get correlation value given two underlyings' id
public static double findCorrelation(int underlyingId1, int underlyingId2, YLContext con)
{
var correlation = con.correlation.FirstOrDefault(c => c.State == valuedate.当前使用 && (c.UnderlyingId1 == underlyingId1 && c.UnderlyingId2 == underlyingId2) || (c.UnderlyingId1 == underlyingId2 && c.UnderlyingId2 == underlyingId1));
return correlation == null || correlation.Correlation == null ? 0.0 : correlation.Correlation.Value;
}
}
}