Esempio n. 1
0
        /// <summary>
        /// 存储修改数据日志
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="NewModel"></param>
        /// <param name="Dao"></param>
        /// <param name="sqlmap"></param>
        /// <returns></returns>
        public int Update <T>(T NewModel, DaoInterface <T> Dao, ISqlMapper sqlmap)
        {
            int result = 0;

            try
            {
                //检验此类是否开启更新数据时插入数据
                DataServiceLogConfig Config = DataServiceLogConfigDataHelper.GetConfigByClassName(NewModel.GetType().FullName, sqlmap);
                if (Config == null || Config.IsUpdate == 0)
                {
                    return(-1);
                }

                //定义日志内容
                Dictionary <string, string> LogDic = new Dictionary <string, string>();
                //获取Model的ID
                object id = NewModel.GetType().GetProperty("ID").GetValue(NewModel, null);
                if (id != null)
                {
                    //获取老的Model
                    T OldModel = Dao.GetObject(id);
                    if (OldModel != null)
                    {
                        //获取新的Model的属性列表
                        PropertyInfo[] propertyList = NewModel.GetType().GetProperties();
                        //获取老Model的Type
                        Type OldType = OldModel.GetType();
                        //新的Model和老的Model值做对比
                        foreach (PropertyInfo property in propertyList)
                        {
                            //新的属性
                            object newpro = property.GetValue(NewModel, null);
                            //老的属性
                            object oldpro = OldType.GetProperty(property.Name).GetValue(OldModel, null);
                            if (!newpro.Equals(oldpro))
                            {
                                LogDic.Add(property.Name, string.Format("\"{0}\"修改为:\"{1}\"", oldpro, newpro));
                            }
                        }
                        //保存日志
                        if (LogDic.Count > 0)
                        {
                            //存储日志
                            DataServiceLog Log = new DataServiceLog();
                            Log.ID             = new SysTableIdDao().generatorId("DataServiceLog", sqlmap);
                            Log.OperateContent = ToJson(LogDic);
                            Log.ClassName      = NewModel.GetType().FullName;
                            Log.DataID         = Convert.ToInt32(id);
                            Log.OperateType    = 2;
                            Log.UserID         = System.Web.HttpContext.Current.Session[SessionConstant.UserID] == null ? 0 : Convert.ToInt32(System.Web.HttpContext.Current.Session[SessionConstant.UserID]);
                            Log.OperateDate    = DateTime.Now;
                            sqlmap.Insert("insertDataServiceLog", Log);
                            result = 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result = -1;
            }

            return(result);
        }