通用权限管理系统底层的软删除数据的处理方法介绍

一、单条记录设置软删除:可同时设置记录是否可启用

        /// <summary>
        /// 设置删除标志
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="changeEnabled">修改有效状态</param>
        /// <param name="recordModifiedUser">记录修改者</param>
        /// <returns>影响行数</returns>
        public virtual int SetDeleted(object id, bool changeEnabled = false, bool recordModifiedUser = false)
        {
            List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
            parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldDeletionStateCode, 1));
            if (changeEnabled)
            {
                parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldEnabled, 0));
            }
            if (recordModifiedUser && this.UserInfo != null)
            {
                parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldModifiedUserId, this.UserInfo.Id));
                //宋彪发现这里的错误 文字与格式字符串错误
                //parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldModifiedOn, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldModifiedOn, DateTime.Now));
            }
            return this.SetProperty(new KeyValuePair<string, object>(this.PrimaryKey, id), parameters);
        }
        public virtual int SetProperty(KeyValuePair<string, object> whereParameter, List<KeyValuePair<string, object>> parameters)
        {
            List<KeyValuePair<string, object>> whereParameters = new List<KeyValuePair<string, object>>();
            whereParameters.Add(whereParameter);
            return DbLogic.SetProperty(DbHelper, this.CurrentTableName, whereParameters, parameters);
        }

二、多条记录设置软删除标志:可同时设置记录是否可启用

        /// <summary>
        /// 批量删除标志
        /// </summary>
        /// <param name="ids">主键数组</param>
        /// <param name="enabled">有效</param>
        /// <param name="modifiedUser">修改者</param>
        /// <returns>影响行数</returns>
        public virtual int SetDeleted(object[] ids, bool enabled = false, bool modifiedUser = false)
        {
            List<KeyValuePair<string, object>> parameters = new List<KeyValuePair<string, object>>();
            parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldDeletionStateCode, 1));
            if (enabled)
            {
                parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldEnabled, 0));
            }
            if (modifiedUser && this.UserInfo != null)
            {
                parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldModifiedUserId, this.UserInfo.Id));
                parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldModifiedBy, this.UserInfo.RealName));
                parameters.Add(new KeyValuePair<string, object>(BaseBusinessLogic.FieldModifiedOn, DateTime.Now));
            }
            return this.SetProperty(ids, parameters);
        }
        public virtual int SetProperty(object[] ids, List<KeyValuePair<string, object>> parameters)
        {
            return this.SetProperty(this.PrimaryKey, ids, parameters);
        }
        public virtual int SetProperty(string name, object[] values, List<KeyValuePair<string, object>> parameters)
        {
            int result = 0;
            if (values == null)
            {
                result += this.SetProperty(new KeyValuePair<string, object>(name, string.Empty), parameters);
            }
            else
            {
                for (int i = 0; i < values.Length; i++)
                {
                    result += this.SetProperty(new KeyValuePair<string, object>(name, values[i]), parameters);
                }
            }
            return result;
        }
        public virtual int SetProperty(KeyValuePair<string, object> whereParameter, List<KeyValuePair<string, object>> parameters)
        {
            List<KeyValuePair<string, object>> whereParameters = new List<KeyValuePair<string, object>>();
            whereParameters.Add(whereParameter);
            return DbLogic.SetProperty(DbHelper, this.CurrentTableName, whereParameters, parameters);
        }