195 lines
8.4 KiB
C#
195 lines
8.4 KiB
C#
using YLErp.BLL;
|
|
using YLErp.DBModels.Consts;
|
|
|
|
namespace YLErp.Modules.TradeModule.DealModule
|
|
{
|
|
public class DbRecordChangesService<T> : TradeServiceBase where T : DbRecordChanges
|
|
{
|
|
public DbRecordChangesService(YLBaseService baseService) : base(baseService)
|
|
{
|
|
}
|
|
|
|
public DbRecordChangesService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
public DbRecordChangesService(OptUserInfo userInfo, YLContext dbContext) : base(userInfo, dbContext)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
/// <param name="objChanges"></param>
|
|
/// <returns></returns>
|
|
public int Save(T objChanges, bool useSave = true)
|
|
{
|
|
var obj = DbContext.Set<T>().Where(O => O.RecordId == objChanges.RecordId && O.OptDate == objChanges.OptDate && O.FieldName == objChanges.FieldName).FirstOrDefault();
|
|
if (obj == null)
|
|
{
|
|
obj = objChanges;
|
|
DbContext.Set<T>().Add(obj);
|
|
}
|
|
obj.FieldValue = objChanges.FieldValue;
|
|
obj.NewValue = objChanges.NewValue;
|
|
return useSave ? DbContext.SaveChanges() : 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
/// <param name="objChanges"></param>
|
|
/// <returns></returns>
|
|
public int Save(IEnumerable<T> objChanges, bool useSave = true)
|
|
{
|
|
var recordIds = objChanges.Select(O => O.RecordId);
|
|
var optDate = objChanges.Select(O => O.OptDate);
|
|
|
|
var arr = DbContext.Set<T>()
|
|
.Where(O =>
|
|
recordIds.Contains(O.RecordId) &&
|
|
optDate.Contains(O.OptDate))
|
|
.ToList();
|
|
foreach (var item in objChanges)
|
|
{
|
|
var obj = arr.Where(O => O.ChangeType == item.ChangeType && O.RecordId == item.RecordId && O.OptDate == item.OptDate && O.FieldName == item.FieldName).FirstOrDefault();
|
|
if (obj == null)
|
|
{
|
|
obj = item;
|
|
DbContext.Set<T>().Add(obj);
|
|
}
|
|
obj.FieldValue = item.FieldValue;
|
|
obj.NewValue = item.NewValue;
|
|
}
|
|
return useSave ? DbContext.SaveChanges() : 0;
|
|
}
|
|
|
|
public bool Delete(ConsInfoChangeType changeType, IEnumerable<int> recordIds, string fieldName, DateTime optDate)
|
|
{
|
|
bool result = false;
|
|
var query = DbContext.Set<T>()
|
|
.Where(O => recordIds.Contains(O.RecordId) && O.FieldName == fieldName && O.OptDate == optDate && O.ChangeType == changeType);
|
|
DbContext.Set<T>().RemoveRange(query);
|
|
DbContext.SaveChanges();
|
|
result = true;
|
|
return result;
|
|
}
|
|
|
|
public IEnumerable<T> GetValue(ConsInfoChangeType changeType, IEnumerable<int> recordIds, DateTime optDate)
|
|
{
|
|
IEnumerable<T> result = null;
|
|
var query = DbContext.Set<T>()
|
|
.Where(O => recordIds.Contains(O.RecordId) && O.OptDate == optDate && O.ChangeType == changeType);
|
|
result = query.ToList();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="changeType"></param>
|
|
/// <param name="recordIds"></param>
|
|
/// <param name="optDate">当 changeType 参数为除权除息时,该日期应该传目标日期的前一天,原因是除权除息修改记录对次日生效,其他类型对当日生效</param>
|
|
/// <returns></returns>
|
|
public IEnumerable<T> GetValueOfLatest(ConsInfoChangeType changeType, IEnumerable<int> recordIds, DateTime optDate)
|
|
{
|
|
IEnumerable<T> result = null;
|
|
var indexQuery = DbContext.Set<T>()
|
|
.Where(O => recordIds.Contains(O.RecordId) && O.OptDate > optDate && O.ChangeType == changeType)
|
|
.AsEnumerable()
|
|
.GroupBy(O => O.RecordId).Select(O => new { RecordId = O.Key, OptDate = O.Min(M => M.OptDate) });
|
|
var query = from index in indexQuery
|
|
join db in DbContext.Set<T>()
|
|
on index equals new { db.RecordId, db.OptDate }
|
|
select db;
|
|
result = query.ToList();
|
|
return result;
|
|
}
|
|
|
|
public T GetValue(ConsInfoChangeType changeType, int recordId, string fieldName, DateTime optDate)
|
|
{
|
|
T result = null;
|
|
var query = DbContext.Set<T>()
|
|
.Where(O => O.RecordId == recordId && O.FieldName == fieldName && O.ChangeType == changeType && O.OptDate == optDate);
|
|
result = query.FirstOrDefault();
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="changeType"></param>
|
|
/// <param name="recordId"></param>
|
|
/// <param name="fieldName"></param>
|
|
/// <param name="optDate">当 changeType 参数为除权除息时,该日期应该传目标日期的前一天,原因是除权除息修改记录对次日生效,其他类型对当日生效</param>
|
|
/// <returns></returns>
|
|
public T GetValueOfLatest(ConsInfoChangeType changeType, int recordId, string fieldName, DateTime optDate)
|
|
{
|
|
T result = null;
|
|
var query = DbContext.Set<T>()
|
|
.Where(O => O.RecordId == recordId && O.FieldName == fieldName && O.ChangeType == changeType && O.OptDate > optDate)
|
|
.OrderBy(O => O.OptDate);
|
|
result = query.FirstOrDefault();
|
|
return result;
|
|
}
|
|
|
|
public IEnumerable<T> GetValue(ConsInfoChangeType changeType, IEnumerable<int> recordIds, string fieldName, DateTime optDate)
|
|
{
|
|
IEnumerable<T> result = null;
|
|
try
|
|
{
|
|
var query = DbContext.Set<T>()
|
|
.Where(O => recordIds.Contains(O.RecordId) && O.FieldName == fieldName && O.OptDate == optDate && O.ChangeType == changeType);
|
|
result = query.ToList();
|
|
}
|
|
catch { }
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询未来的修改记录
|
|
/// </summary>
|
|
/// <param name="changeType"></param>
|
|
/// <param name="recordIds"></param>
|
|
/// <param name="fieldName"></param>
|
|
/// <param name="optDate">当 changeType 参数为除权除息时,该日期应该传目标日期的前一天,原因是除权除息修改记录对次日生效,其他类型对当日生效</param>
|
|
/// <returns></returns>
|
|
public IEnumerable<T> GetValueOfLatest(ConsInfoChangeType changeType, IEnumerable<int> recordIds, string fieldName, DateTime optDate)
|
|
{
|
|
IEnumerable<T> result = null;
|
|
try
|
|
{
|
|
var indexQuery = DbContext.Set<T>()
|
|
.Where(O => recordIds.Contains(O.RecordId) && O.FieldName == fieldName && O.OptDate > optDate && O.ChangeType == changeType)
|
|
.AsEnumerable()
|
|
.GroupBy(O => O.RecordId).Select(O => new { RecordId = O.Key, OptDate = O.Min(M => M.OptDate) });
|
|
var query = from index in indexQuery
|
|
join db in DbContext.Set<T>()
|
|
on index equals new { db.RecordId, db.OptDate }
|
|
select db;
|
|
result = query.ToList();
|
|
}
|
|
catch { }
|
|
return result;
|
|
}
|
|
|
|
public IEnumerable<T> GetValue(ConsInfoChangeType changeType, int recordId, string fieldName, DateTime optDateStart, DateTime optDateEnd)
|
|
{
|
|
List<T> result = null;
|
|
var query = DbContext.Set<T>()
|
|
.Where(O => O.RecordId == recordId && O.FieldName == fieldName && O.OptDate >= optDateStart && O.OptDate <= optDateEnd && O.ChangeType == changeType);
|
|
result = query.ToList();
|
|
return result ?? new List<T>();
|
|
}
|
|
|
|
public IEnumerable<T> GetValue(ConsInfoChangeType changeType, IEnumerable<int> recordIds, string fieldName, DateTime optDateStart, DateTime optDateEnd)
|
|
{
|
|
List<T> result = null;
|
|
var query = DbContext.Set<T>()
|
|
.Where(O => recordIds.Contains(O.RecordId) && O.FieldName == fieldName && O.OptDate >= optDateStart && O.OptDate <= optDateEnd && O.ChangeType == changeType);
|
|
result = query.ToList();
|
|
return result ?? new List<T>();
|
|
}
|
|
}
|
|
}
|