using YLErp.BLL; using YLErp.DBModels.Consts; namespace YLErp.Modules.TradeModule.DealModule { public class DbRecordChangesService : 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) { } /// /// 保存 /// /// /// public int Save(T objChanges, bool useSave = true) { var obj = DbContext.Set().Where(O => O.RecordId == objChanges.RecordId && O.OptDate == objChanges.OptDate && O.FieldName == objChanges.FieldName).FirstOrDefault(); if (obj == null) { obj = objChanges; DbContext.Set().Add(obj); } obj.FieldValue = objChanges.FieldValue; obj.NewValue = objChanges.NewValue; return useSave ? DbContext.SaveChanges() : 0; } /// /// 保存 /// /// /// public int Save(IEnumerable objChanges, bool useSave = true) { var recordIds = objChanges.Select(O => O.RecordId); var optDate = objChanges.Select(O => O.OptDate); var arr = DbContext.Set() .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().Add(obj); } obj.FieldValue = item.FieldValue; obj.NewValue = item.NewValue; } return useSave ? DbContext.SaveChanges() : 0; } public bool Delete(ConsInfoChangeType changeType, IEnumerable recordIds, string fieldName, DateTime optDate) { bool result = false; var query = DbContext.Set() .Where(O => recordIds.Contains(O.RecordId) && O.FieldName == fieldName && O.OptDate == optDate && O.ChangeType == changeType); DbContext.Set().RemoveRange(query); DbContext.SaveChanges(); result = true; return result; } public IEnumerable GetValue(ConsInfoChangeType changeType, IEnumerable recordIds, DateTime optDate) { IEnumerable result = null; var query = DbContext.Set() .Where(O => recordIds.Contains(O.RecordId) && O.OptDate == optDate && O.ChangeType == changeType); result = query.ToList(); return result; } /// /// /// /// /// /// 当 changeType 参数为除权除息时,该日期应该传目标日期的前一天,原因是除权除息修改记录对次日生效,其他类型对当日生效 /// public IEnumerable GetValueOfLatest(ConsInfoChangeType changeType, IEnumerable recordIds, DateTime optDate) { IEnumerable result = null; var indexQuery = DbContext.Set() .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() 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() .Where(O => O.RecordId == recordId && O.FieldName == fieldName && O.ChangeType == changeType && O.OptDate == optDate); result = query.FirstOrDefault(); return result; } /// /// /// /// /// /// /// 当 changeType 参数为除权除息时,该日期应该传目标日期的前一天,原因是除权除息修改记录对次日生效,其他类型对当日生效 /// public T GetValueOfLatest(ConsInfoChangeType changeType, int recordId, string fieldName, DateTime optDate) { T result = null; var query = DbContext.Set() .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 GetValue(ConsInfoChangeType changeType, IEnumerable recordIds, string fieldName, DateTime optDate) { IEnumerable result = null; try { var query = DbContext.Set() .Where(O => recordIds.Contains(O.RecordId) && O.FieldName == fieldName && O.OptDate == optDate && O.ChangeType == changeType); result = query.ToList(); } catch { } return result; } /// /// 查询未来的修改记录 /// /// /// /// /// 当 changeType 参数为除权除息时,该日期应该传目标日期的前一天,原因是除权除息修改记录对次日生效,其他类型对当日生效 /// public IEnumerable GetValueOfLatest(ConsInfoChangeType changeType, IEnumerable recordIds, string fieldName, DateTime optDate) { IEnumerable result = null; try { var indexQuery = DbContext.Set() .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() on index equals new { db.RecordId, db.OptDate } select db; result = query.ToList(); } catch { } return result; } public IEnumerable GetValue(ConsInfoChangeType changeType, int recordId, string fieldName, DateTime optDateStart, DateTime optDateEnd) { List result = null; var query = DbContext.Set() .Where(O => O.RecordId == recordId && O.FieldName == fieldName && O.OptDate >= optDateStart && O.OptDate <= optDateEnd && O.ChangeType == changeType); result = query.ToList(); return result ?? new List(); } public IEnumerable GetValue(ConsInfoChangeType changeType, IEnumerable recordIds, string fieldName, DateTime optDateStart, DateTime optDateEnd) { List result = null; var query = DbContext.Set() .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(); } } }