Beispiel #1
0
        public static string WorkoutDeltaStringForUpdate(FinanceAccountExtASViewModel oldAcnt,
                                                         FinanceAccountExtASViewModel newAcnt)
        {
            var diffs = WorkoutDeltaForUpdate(oldAcnt, newAcnt);

            List <String> listHeaderSqls = new List <string>();

            foreach (var diff in diffs)
            {
                var dbfield = newAcnt.GetDBFieldName(diff.Key);

                if (diff.Value == null)
                {
                    listHeaderSqls.Add("[" + dbfield + "] = NULL");
                }
                else
                {
                    if (diff.Value is DateTime)
                    {
                        listHeaderSqls.Add("[" + dbfield + "] = '" + ((DateTime)diff.Value).ToString("yyyy-MM-dd") + "'");
                    }
                    else if (diff.Value is Boolean)
                    {
                        listHeaderSqls.Add("[" + dbfield + "] = " + (((Boolean)diff.Value) ? "1" : "NULL"));
                    }
                    else if (diff.Value is String)
                    {
                        if (String.IsNullOrEmpty((string)diff.Value))
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = NULL");
                        }
                        else
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = N'" + diff.Value + "'");
                        }
                    }
                    else if (diff.Value is Decimal)
                    {
                        if (Decimal.Compare((Decimal)diff.Value, 0) == 0)
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = NULL");
                        }
                        else
                        {
                            listHeaderSqls.Add("[" + dbfield + "] = " + diff.Value.ToString());
                        }
                    }
                    else
                    {
                        listHeaderSqls.Add("[" + dbfield + "] = " + diff.Value.ToString());
                    }
                }
            }

            return(listHeaderSqls.Count == 0 ?
                   String.Empty :
                   (@"UPDATE [dbo].[t_fin_account_ext_as] SET " + string.Join(",", listHeaderSqls) + " WHERE [ACCOUNTID] = " + oldAcnt.AccountID.ToString()));
        }
Beispiel #2
0
        public static Dictionary <String, Object> WorkoutDeltaForUpdate(FinanceAccountExtASViewModel oldAcnt,
                                                                        FinanceAccountExtASViewModel newAcnt)
        {
            Dictionary <String, Object> dictDelta = new Dictionary <string, object>();

            if (oldAcnt == null || newAcnt == null || Object.ReferenceEquals(oldAcnt, newAcnt) ||
                oldAcnt.AccountID != newAcnt.AccountID)
            {
                throw new ArgumentException("Invalid inputted parameter Or AccountID change is not allowed");
            }
            if (!oldAcnt.IsValid() || !newAcnt.IsValid())
            {
                throw new Exception("Account info is invalid");
            }

            Type t = typeof(FinanceAccountExtASViewModel);

            PropertyInfo[] listProperties       = t.GetProperties();
            var            listSortedProperties = listProperties.OrderBy(o => o.Name);

            foreach (PropertyInfo item in listSortedProperties)
            {
                if (item.Name == "RefDocForSold")
                {
                    if (oldAcnt.RefDocForSold.HasValue)
                    {
                        if (newAcnt.RefDocForSold.HasValue)
                        {
                            if (oldAcnt.RefDocForSold.Value != newAcnt.RefDocForSold.Value)
                            {
                                dictDelta.Add(item.Name, newAcnt.RefDocForSold.Value);
                            }
                        }
                        else
                        {
                            dictDelta.Add(item.Name, null);
                        }
                    }
                    else
                    {
                        if (newAcnt.RefDocForSold.HasValue)
                        {
                            dictDelta.Add(item.Name, newAcnt.RefDocForSold.Value);
                        }
                    }
                }
                else
                {
                    object oldValue = item.GetValue(oldAcnt, null);
                    object newValue = item.GetValue(newAcnt, null);
                    if (item.PropertyType == typeof(Decimal))
                    {
                        if (Decimal.Compare((Decimal)oldValue, (Decimal)newValue) != 0)
                        {
                            dictDelta.Add(item.Name, newValue);
                        }
                    }
                    else if (item.PropertyType == typeof(String))
                    {
                        if (String.CompareOrdinal((string)oldValue, (string)newValue) != 0)
                        {
                            dictDelta.Add(item.Name, newValue);
                        }
                    }
                    else if (item.PropertyType == typeof(DateTime))
                    {
                        if (DateTime.Compare(((DateTime)oldValue).Date, ((DateTime)newValue).Date) != 0)
                        {
                            dictDelta.Add(item.Name, newValue);
                        }
                    }
                    else
                    {
                        if (!Object.Equals(oldValue, newValue))
                        {
                            dictDelta.Add(item.Name, newValue);
                        }
                    }
                }
            }

            return(dictDelta);
        }