340 lines
12 KiB
C#
340 lines
12 KiB
C#
using BaseOUDAL;
|
|
using System.Data;
|
|
using System.Runtime.Intrinsics.Arm;
|
|
using YLErp.Commons;
|
|
using YLErp.DBModels;
|
|
using YLErp.Helpers;
|
|
using YLErp.Modules.ClientModule;
|
|
|
|
namespace YLErp.Modules.UnderlyingModule
|
|
{
|
|
/// <summary>
|
|
/// 分红率服务
|
|
/// </summary>
|
|
public class UnderlyingSpanTwoService : YLBaseService
|
|
{
|
|
public UnderlyingSpanTwoService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取最新分红率
|
|
/// </summary>
|
|
public SearchListResult<UnderlyingSpanTwoDto> GetLatestList(UnderlyingSpanTwoQueryModel req)
|
|
{
|
|
// DbContext.SetDebugLog();
|
|
var groupSpan = DbContext.UnderlyingSpanTwo.AsNoTracking()
|
|
.GroupBy(s => s.UnderlyingId)
|
|
.Select(g => new
|
|
{
|
|
UnderlyingId = g.Key,
|
|
ValueDate = g.Max(s => s.ValueDate)
|
|
});
|
|
|
|
var span = DbContext.UnderlyingSpanTwo.AsNoTracking().Join
|
|
(groupSpan, s => new { f1 = s.UnderlyingId, f2 = s.ValueDate }, u => new { f1 = u.UnderlyingId, f2 = u.ValueDate },
|
|
(s, u) => new { s, u })
|
|
.Select(g => new
|
|
{
|
|
id = g.s.id,
|
|
UnderlyingId = g.u.UnderlyingId,
|
|
SpanRate = g.s.SpanRate,
|
|
ValueDate = g.u.ValueDate
|
|
});
|
|
var query = span.AsNoTracking().Join(DbContext.underlying_manager, s => s.UnderlyingId, u => u.id, (s, u) => new UnderlyingSpanTwoDto
|
|
{
|
|
id = s.id,
|
|
SpanRate = s.SpanRate,
|
|
UnderlyingCode = u.UnderlyingCode,
|
|
UnderlyingName = u.UnderlyingName
|
|
});
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(req.UnderlyingCode))
|
|
{
|
|
query = query.Where(n => n.UnderlyingCode.Contains(req.UnderlyingCode));
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(req.UnderlyingName))
|
|
{
|
|
query = query.Where(n => n.UnderlyingName.Contains(req.UnderlyingName));
|
|
}
|
|
|
|
|
|
if (string.IsNullOrEmpty(req.sidx))
|
|
{
|
|
query = query.OrderBy(n => n.UnderlyingCode);
|
|
}
|
|
|
|
|
|
return query.ToSearchList(req);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取最新分红率
|
|
/// </summary>
|
|
public List<UnderlyingSpanTwoDto> GetLatestList(List<string> underlyingCodes, DateTime valueDate)
|
|
{
|
|
var groupSpan =
|
|
DbContext.UnderlyingSpanTwo.AsNoTracking()
|
|
.Where(O => O.ValueDate <= valueDate)
|
|
.GroupBy(s => s.UnderlyingId)
|
|
.Select(g => new
|
|
{
|
|
UnderlyingId = g.Key,
|
|
ValueDate = g.Max(s => s.ValueDate)
|
|
});
|
|
|
|
var span = DbContext.UnderlyingSpanTwo.AsNoTracking().Join
|
|
(groupSpan, s => new { f1 = s.UnderlyingId, f2 = s.ValueDate }, u => new { f1 = u.UnderlyingId, f2 = u.ValueDate },
|
|
(s, u) => new { s, u })
|
|
.Select(g => new
|
|
{
|
|
id = g.s.id,
|
|
UnderlyingId = g.u.UnderlyingId,
|
|
SpanRate = g.s.SpanRate,
|
|
ValueDate = g.u.ValueDate
|
|
});
|
|
var query = span.AsNoTracking().Join(DbContext.underlying_manager, s => s.UnderlyingId, u => u.id, (s, u) => new UnderlyingSpanTwoDto
|
|
{
|
|
id = s.id,
|
|
SpanRate = s.SpanRate,
|
|
UnderlyingCode = u.UnderlyingCode,
|
|
UnderlyingName = u.UnderlyingName
|
|
});
|
|
|
|
|
|
if (underlyingCodes?.Count() > 0)
|
|
{
|
|
query = query.Where(n => underlyingCodes.Contains(n.UnderlyingCode));
|
|
}
|
|
|
|
return query.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取历史分红率
|
|
/// </summary>
|
|
public IEnumerable<UnderlyingSpanTwoDto> GetHisList(string underlyingCode)
|
|
{
|
|
if (string.IsNullOrEmpty(underlyingCode))
|
|
{
|
|
return Enumerable.Empty<UnderlyingSpanTwoDto>();
|
|
}
|
|
|
|
var query = from m in DbContext.underlying_manager
|
|
join n in DbContext.UnderlyingSpanTwo on m.id equals n.UnderlyingId
|
|
where m.UnderlyingCode == underlyingCode
|
|
orderby n.ValueDate descending
|
|
select new UnderlyingSpanTwoDto
|
|
{
|
|
SpanRate = n.SpanRate,
|
|
ValueDate = n.ValueDate
|
|
};
|
|
return query.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取最新分红率
|
|
/// </summary>
|
|
public UnderlyingSpanTwoDto GetLatestDetail(string underlyingCode)
|
|
{
|
|
var span = DbContext.UnderlyingSpanTwo.AsNoTracking().Join
|
|
(DbContext.underlying_manager, s => s.UnderlyingId, u => u.id, (s, u) => new UnderlyingSpanTwoDto
|
|
{
|
|
UnderlyingCode = u.UnderlyingCode,
|
|
UnderlyingName = u.UnderlyingName,
|
|
SpanRate = s.SpanRate,
|
|
ValueDate = s.ValueDate
|
|
}).Where(s => s.UnderlyingCode == underlyingCode).OrderByDescending(x => x.ValueDate).FirstOrDefault();
|
|
|
|
if (span == null)
|
|
{
|
|
var variety = DataCacheProvider.GetVariety(underlyingCode);
|
|
var um = DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode);
|
|
|
|
if (um != null && variety != null && !variety.UpLimit.IsNullOrWhiteSpace())
|
|
{
|
|
span = new UnderlyingSpanTwoDto
|
|
{
|
|
UnderlyingCode = underlyingCode,
|
|
UnderlyingName = um.UnderlyingName,
|
|
SpanRate = variety.UpLimitValue
|
|
};
|
|
}
|
|
|
|
}
|
|
|
|
return span;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存分红率
|
|
/// </summary>
|
|
public UnderlyingSpanTwo SaveData(UnderlyingSpanTwoDto model)
|
|
{
|
|
if (string.IsNullOrEmpty(model?.UnderlyingCode))
|
|
{
|
|
throw new ServiceException("标的代码不能为空");
|
|
}
|
|
|
|
var udm = DbContext.underlying_manager.FirstOrDefault(n => n.UnderlyingCode == model.UnderlyingCode);
|
|
if (udm == null)
|
|
{
|
|
throw new ServiceException("标的信息不存在");
|
|
}
|
|
|
|
var valueDate = BLL.valuedateBLL.ValueDate;
|
|
if (valueDate < DateTime.Today) valueDate = DateTime.Today;
|
|
|
|
var span = DbContext.UnderlyingSpanTwo.FirstOrDefault(n => n.UnderlyingId == udm.id && n.ValueDate == valueDate);
|
|
|
|
AddOrSaveSpanTwo(span, udm.id, valueDate, model.SpanRate);
|
|
|
|
return span;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 导入Span涨幅度2
|
|
/// </summary>
|
|
/// <param name="streamIn"></param>
|
|
/// <param name="totalNum">当前文件中的目标期权总条数</param>
|
|
/// <param name="successNum">成功入库的数量</param>
|
|
public void ImportSpanTwoFromExcel(Stream streamIn, out int totalNum, out int successNum)
|
|
{
|
|
totalNum = 0;
|
|
successNum = 0;
|
|
|
|
var rowIndex = 0;
|
|
try
|
|
{
|
|
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 table = ds.Tables[0];
|
|
var reader = new DataRowReader(table, 0);
|
|
|
|
rowIndex = 1;
|
|
totalNum = table.Rows.Count - rowIndex;
|
|
|
|
//using (var trans = BeginTransaction())
|
|
//{
|
|
foreach (var row in table.Rows.Cast<DataRow>().Skip(1))
|
|
{
|
|
rowIndex++;
|
|
|
|
if (row.ItemArray.All(n => string.IsNullOrWhiteSpace(n?.ToString())))
|
|
{
|
|
totalNum--;
|
|
continue;
|
|
}
|
|
|
|
reader.SetDataRow(row);
|
|
|
|
var underlyingCode = reader.GetString("标的代码", true);
|
|
|
|
|
|
var um = DataCacheProvider.GetUnderlyingDataSource().GetData(underlyingCode);
|
|
if (um == null)
|
|
{
|
|
throw new ServiceException("该标的代码在系统中不存在");
|
|
}
|
|
|
|
var valueDate = reader.GetDate("变动日期", false);
|
|
var spanRate = reader.GetPercent("Span涨跌幅度2", false);
|
|
|
|
if (valueDate == null) valueDate = DateTime.Today;
|
|
if (spanRate == null) continue;
|
|
|
|
if(spanRate < 0) throw new ServiceException("只能填大于等于0的数字");
|
|
|
|
var span = DbContext.UnderlyingSpanTwo.FirstOrDefault(n => n.UnderlyingId == um.id && n.ValueDate == valueDate);
|
|
AddOrSaveSpanTwo(span, um.id, (DateTime)valueDate, spanRate);
|
|
|
|
successNum++;
|
|
}
|
|
|
|
//trans.Commit();
|
|
//}
|
|
}
|
|
catch (ServiceException se)
|
|
{
|
|
if (se.Tag != null)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
throw new ServiceException($"第{rowIndex}行,{se.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogFactory.GetLogger("导入Span涨跌幅2").Error(ex);
|
|
throw new ServiceException($"第{rowIndex}行,发生错误:{ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存Span涨跌幅2
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="valueDate"></param>
|
|
/// <param name="spanRate"></param>
|
|
private void AddOrSaveSpanTwo(UnderlyingSpanTwo span, int id, DateTime valueDate, double? spanRate)
|
|
{
|
|
|
|
if (span == null)
|
|
{
|
|
span = new UnderlyingSpanTwo
|
|
{
|
|
UnderlyingId = id,
|
|
ValueDate = valueDate
|
|
};
|
|
DbContext.UnderlyingSpanTwo.Add(span);
|
|
}
|
|
|
|
var remark = $"从{span.SpanRate}修改为";
|
|
|
|
if(spanRate < 0)
|
|
{
|
|
throw new ServiceException("Span涨跌幅2必须大于等于0");
|
|
}
|
|
span.SpanRate = spanRate;
|
|
span.OptId = UserId;
|
|
span.OptName = UserName;
|
|
span.OptDate = DateTime.Now;
|
|
|
|
remark = $"{span.OptDate}" + remark + $"{spanRate}";
|
|
|
|
|
|
var spanLog = new Processlog()
|
|
{
|
|
TypeId = span.UnderlyingId,
|
|
ProcessType = "Span2Setting",
|
|
ProcessStatus = "保存",
|
|
Log = "",
|
|
Remark = remark,
|
|
OptId = UserId,
|
|
CreateTime = DateTime.Now
|
|
};
|
|
DbContext.CreditProcessLog.Add(spanLog);
|
|
|
|
DbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class UnderlyingSpanTwoQueryModel : BaseSearchReq
|
|
{
|
|
public string UnderlyingName { get; set; }
|
|
|
|
public string UnderlyingCode { get; set; }
|
|
}
|
|
}
|