public virtual T Last() { var obj = Current; string tableName = typeof(T).Name; string keyName = MasterKey.IsNullOrEmpty() ? GetKeys[0] : MasterKey; StringBuilder sqlStr = new StringBuilder(); sqlStr.Append($"Select Top 1 * from {tableName} Where 1=1 "); if (GetKeys.Length > 4 && obj.GetType().GetProperties().Any(p => p.Name.Equals("PkId")) && GetKeys.Contains("TrxTypeId") && GetKeys.Contains("CompanyCode") && GetKeys.Contains("BranchCode")) { sqlStr.Append($" And CompanyCode ='{obj.GetValue("CompanyCode")}'"); sqlStr.Append($" And BranchCode ='{obj.GetValue("BranchCode")}'"); sqlStr.Append(" Order by PkId Desc"); } else if (GetKeys.Contains("CompanyCode")) { if (GetKeys.Length > 1 && GetKeys.Length <= 4) { for (int i = 1; i < GetKeys.Length; i++) { sqlStr.Append($" And {GetKeys[i]} ='{obj.GetValue(GetKeys[i])}'"); } } sqlStr.Append($" Order by {keyName} Desc"); } else { sqlStr.Append($" Order by {keyName} Desc"); } T entity = Context.Set <T>().SqlQuery(sqlStr.ToString()).FirstOrDefault() ?? new T(); return(entity); }
public virtual TransactionResult Delete(T entity, string headerkeyvalue = null, bool useLog = true) { if (Context.Database.Connection.State != ConnectionState.Open) { Context.Database.Connection.Open(); } TransactionScope scope = new TransactionScope(); try { var keyvalue = entity.GetValue(MasterKey.IsNullOrEmpty() ? GetKeys[0] : MasterKey); var row = Find(entity); entity = row; Context.Entry(entity).State = EntityState.Deleted; if (BeforeDelete != null) { var eventUseData = new EventUseData(); BeforeDelete.Invoke(entity, Context, eventUseData); if (eventUseData.Cancel) { scope.Dispose(); return(eventUseData.Message); } } //if (_user.FollowUp && useLog) //{ // Context.LogEntity(entity, _user, GetKeys[0], _formType, headerkeyvalue, headerkeyvalue); //} Context.SaveChanges(); //var lkpWebControllers = Context.lkp_WebControllers.FirstOrDefault(c => c.formType == _formType); //c.ControllerName.Equals(_controllername, StringComparison.OrdinalIgnoreCase) || //if (lkpWebControllers != null) //{ // int formType = lkpWebControllers.formType; // RemoveAttachments(formType, keyvalue.ToString()); //} scope.Complete(); if (AfterDelete != null) { EventBase eventMessage = new EventBase { Message = TransactionResult.None }; AfterDelete.Invoke(entity, Context, eventMessage); if (eventMessage.Message != TransactionResult.None) { return(eventMessage.Message); } } return(TransactionResult.DeleteSuccess); } catch (Exception ex) { HandleException(ex); scope.Dispose(); Context.Database.Connection.Close(); //Logger.LogSystemError(_user.UserId.ToString(), ex); return(TransactionResult.DeleteFailed); } finally { scope.Dispose(); } }
public virtual T Previous() { var obj = Current; if (obj == null) { return(null); } T entity; string tableName = typeof(T).Name; string keyName = MasterKey.IsNullOrEmpty() ? GetKeys[0] : MasterKey; string keyValue = obj.GetValue(GetKeys[0]).IsNull(); StringBuilder sqlStr = new StringBuilder(); StringBuilder sqlWhere = new StringBuilder(); sqlStr.Append($"Select * from {tableName} Where 1=1 "); if (GetKeys.Length > 4 && obj.GetType().GetProperties().Any(p => p.Name.Equals("PkId")) && GetKeys.Contains("TrxTypeId") && GetKeys.Contains("CompanyCode") && GetKeys.Contains("BranchCode")) { string s = $"Select * From {tableName} Where " + $"TrxTypeId='{obj.GetValue("TrxTypeId")}' And " + $"YearId='{obj.GetValue("YearId")}' And " + $"PeriodId='{obj.GetValue("PeriodId")}' And " + $"CompanyCode='{obj.GetValue("CompanyCode")}' And " + $"BranchCode='{obj.GetValue("BranchCode")}'"; if (obj.GetValue("TrxNo") != null) { s += $"And TrxNo = '{obj.GetValue("TrxNo")}'"; } keyValue = Context.Set <T>().SqlQuery(s).FirstOrDefault().GetValue("PkId").IsNull(); //obj.GetValue(GetKeys[0]).IsNull(); sqlWhere.Append($" And CompanyCode ='{obj.GetValue("CompanyCode")}'"); sqlWhere.Append($" And BranchCode ='{obj.GetValue("BranchCode")}'"); if (obj.GetValue("TrxNo") != null) { sqlStr.Append($"{sqlWhere} And PkId=(Select Max(PkId) from {tableName} Where PkId < '{keyValue}' {sqlWhere})"); } entity = Context.Set <T>().SqlQuery(sqlStr.ToString()).FirstOrDefault() ?? obj; } else if (GetKeys.Contains("CompanyCode")) { if (GetKeys.Length > 1 && GetKeys.Length <= 4) { for (int i = 1; i < GetKeys.Length; i++) { sqlWhere.Append($" And {GetKeys[i]} ='{obj.GetValue(GetKeys[i])}'"); } } if (keyValue != "") { sqlStr.Append($"{sqlWhere} And {keyName}=(Select Max({keyName}) from {tableName} Where {keyName} < '{keyValue}' {sqlWhere})"); } entity = Context.Set <T>().SqlQuery(sqlStr.ToString()).FirstOrDefault(); } else { if (keyValue != "") { sqlStr.Append($"{sqlWhere} And {keyName}=(Select Max({keyName}) from {tableName} Where {keyName} < '{keyValue}' {sqlWhere})"); } entity = Context.Set <T>().SqlQuery(sqlStr.ToString()).FirstOrDefault(); } return(entity ?? obj); }