/// <summary>
        /// 根据字典编码、主键获取实体
        /// </summary>
        /// <param name="dictionaryCode">字典编码</param>
        /// <param name="itemKey">字典项主键</param>
        /// <param name="itemValue">字典项值</param>
        /// <returns></returns>
        public BaseDictionaryItemEntity GetEntity(string dictionaryCode, string itemKey, string itemValue = null)
        {
            BaseDictionaryItemEntity entity = null;

            if (!string.IsNullOrEmpty(dictionaryCode) && !string.IsNullOrEmpty(itemKey))
            {
                var entityBaseDictionary = new BaseDictionaryManager(UserInfo).GetEntityByCode(dictionaryCode);
                if (entityBaseDictionary != null)
                {
                    var parameters = new List <KeyValuePair <string, object> >
                    {
                        new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldDictionaryId, entityBaseDictionary.Id),
                        new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldItemKey, itemKey),
                        new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldDeleted, 0),
                        new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldEnabled, 1)
                    };
                    if (!string.IsNullOrEmpty(itemValue))
                    {
                        parameters.Add(new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldItemValue, itemValue));
                    }
                    var cacheKey  = CurrentTableName + ".Entity." + dictionaryCode + "." + itemKey;
                    var cacheTime = TimeSpan.FromMilliseconds(86400000);
                    entity = CacheUtil.Cache <BaseDictionaryItemEntity>(cacheKey, () => BaseEntity.Create <BaseDictionaryItemEntity>(GetDataTable(parameters)), true, false, cacheTime);
                }
            }
            return(entity);
        }
示例#2
0
 /// <summary>
 /// 添加, 这里可以人工干预,提高程序的性能
 /// </summary>
 /// <param name="entity">实体</param>
 /// <param name="identity">自增量方式,表主键是否采用自增的策略</param>
 /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param>
 /// <returns>主键</returns>
 public string Add(BaseDictionaryItemEntity entity, bool identity = true, bool returnId = true)
 {
     Identity  = identity;
     ReturnId  = returnId;
     entity.Id = AddEntity(entity).ToInt();
     return(entity.Id.ToString());
 }
        /// <summary>
        /// 保存实体修改记录
        /// </summary>
        /// <param name="entityNew">修改后的实体对象</param>
        /// <param name="entityOld">修改前的实体对象</param>
        /// <param name="tableName">表名称</param>
        public void SaveEntityChangeLog(BaseDictionaryItemEntity entityNew, BaseDictionaryItemEntity entityOld, string tableName = null)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                //统一放在一个公共表 Troy.Cui 2016-08-17
                tableName = BaseChangeLogEntity.CurrentTableName;
            }
            var manager = new BaseChangeLogManager(UserInfo, tableName);

            foreach (var property in typeof(BaseDictionaryItemEntity).GetProperties())
            {
                var oldValue         = Convert.ToString(property.GetValue(entityOld, null));
                var newValue         = Convert.ToString(property.GetValue(entityNew, null));
                var fieldDescription = property.GetCustomAttributes(typeof(FieldDescription), false).FirstOrDefault() as FieldDescription;
                //不记录创建人、修改人、没有修改的记录
                if (!fieldDescription.NeedLog || oldValue == newValue)
                {
                    continue;
                }
                var entity = new BaseChangeLogEntity
                {
                    TableName         = CurrentTableName,
                    TableDescription  = FieldExtensions.ToDescription(typeof(BaseDictionaryItemEntity), "CurrentTableName"),
                    ColumnName        = property.Name,
                    ColumnDescription = fieldDescription.Text,
                    NewValue          = newValue,
                    OldValue          = oldValue,
                    RecordKey         = entityOld.Id.ToString()
                };
                manager.Add(entity, true, false);
            }
        }
        /// <summary>
        /// 检查唯一值式更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public int UniqueUpdate(BaseDictionaryItemEntity entity, out Status status)
        {
            var result = 0;

            //检查是否重复
            var parameters = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldDictionaryId, entity.DictionaryId),
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldItemKey, entity.ItemKey),
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldEnabled, 1),
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldDeleted, 0)
            };

            if (!IsUnique(parameters, entity.Id.ToString()))
            {
                //名称已重复
                Status        = Status.ErrorNameExist;
                StatusCode    = Status.ErrorNameExist.ToString();
                StatusMessage = Status.ErrorNameExist.ToDescription();
            }
            else
            {
                // 获取原始实体信息
                var entityOld = GetEntity(entity.Id);
                if (entityOld != null)
                {
                    // 保存修改记录,无论是否允许
                    SaveEntityChangeLog(entity, entityOld);

                    if (entityOld.AllowEdit == 1)
                    {
                        result = UpdateEntity(entity);
                        if (result == 1)
                        {
                            Status        = Status.OkUpdate;
                            StatusCode    = Status.OkUpdate.ToString();
                            StatusMessage = Status.OkUpdate.ToDescription();
                        }
                        else
                        {
                            Status        = Status.ErrorDeleted;
                            StatusCode    = Status.ErrorDeleted.ToString();
                            StatusMessage = Status.ErrorDeleted.ToDescription();
                        }
                    }
                    else
                    {
                        Status        = Status.NotAllowEdit;
                        StatusCode    = Status.NotAllowEdit.ToString();
                        StatusMessage = Status.NotAllowEdit.ToDescription();
                    }
                }
            }
            status = Status;
            return(result);
        }
示例#5
0
 /// <summary>
 /// 添加或更新(主键是否为0)
 /// </summary>
 /// <param name="entity">实体</param>
 /// <param name="identity">自增量方式,表主键是否采用自增的策略</param>
 /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param>
 /// <returns>主键</returns>
 public string AddOrUpdate(BaseDictionaryItemEntity entity, bool identity = true, bool returnId = true)
 {
     Identity = identity;
     ReturnId = returnId;
     if (entity.Id == 0)
     {
         entity.Id = AddEntity(entity).ToInt();
         return(entity.Id.ToString());
     }
     else
     {
         return(UpdateEntity(entity) > 0 ? entity.Id.ToString() : string.Empty);
     }
 }
示例#6
0
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="id">主键</param>
        /// <returns>数据表</returns>
        public BaseDictionaryItemEntity GetEntity(BaseUserInfo userInfo, string tableName, string id)
        {
            var dt = new DataTable(BaseDictionaryItemEntity.CurrentTableName);
            BaseDictionaryItemEntity entity = null;

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var itemDetailsManager = new BaseDictionaryItemManager(dbHelper, userInfo, tableName);
                entity = itemDetailsManager.GetEntity(id);
            });
            return(entity);
        }
示例#7
0
 /// <summary>
 /// 设置实体
 /// </summary>
 /// <param name="sqlBuilder">Sql语句生成器</param>
 /// <param name="entity">实体</param>
 private void SetEntity(SqlBuilder sqlBuilder, BaseDictionaryItemEntity entity)
 {
     SetEntityExtend(sqlBuilder, entity);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldDictionaryId, entity.DictionaryId);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldParentId, entity.ParentId);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldItemKey, entity.ItemKey);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldItemName, entity.ItemName);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldItemValue, entity.ItemValue);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldLanguage, entity.Language);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldAllowEdit, entity.AllowEdit);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldAllowDelete, entity.AllowDelete);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldDescription, entity.Description);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldSortCode, entity.SortCode);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldDeleted, entity.Deleted);
     sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldEnabled, entity.Enabled);
 }
示例#8
0
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="code">编码</param>
        /// <returns>数据表</returns>
        public BaseDictionaryItemEntity GetEntityByCode(BaseUserInfo userInfo, string tableName, string code)
        {
            var itemDetailsEntity = new BaseDictionaryItemEntity();

            var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) =>
            {
                var itemDetailsManager = new BaseDictionaryItemManager(dbHelper, userInfo, tableName);
                var dt = itemDetailsManager.GetDataTable(new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldItemKey, code), BaseDictionaryItemEntity.FieldSortCode);
                if ((dt != null) && (dt.Rows.Count > 0))
                {
                    itemDetailsEntity = (BaseDictionaryItemEntity)itemDetailsEntity.GetFrom(dt.Rows[0]);
                }
            });
            return(itemDetailsEntity);
        }
示例#9
0
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="entity">实体</param>
        /// <param name="status">状态</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>数据表</returns>
        public int Update(BaseUserInfo userInfo, string tableName, BaseDictionaryItemEntity entity, out Status status, out string statusMessage)
        {
            var result = 0;

            var returnCode    = Status.Ok;
            var returnMessage = string.Empty;
            var parameter     = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod());

            ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) =>
            {
                var itemDetailsManager = new BaseDictionaryItemManager(dbHelper, userInfo, tableName);
                // 编辑数据
                result        = itemDetailsManager.Update(entity);
                returnMessage = itemDetailsManager.GetStateMessage(returnCode);
            });
            status        = returnCode;
            statusMessage = returnMessage;
            return(result);
        }
        /// <summary>
        /// 检查唯一值式新增
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public string UniqueAdd(BaseDictionaryItemEntity entity, out Status status)
        {
            var result = string.Empty;
            //检查是否重复
            var parameters = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldDictionaryId, entity.DictionaryId),
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldItemKey, entity.ItemKey),
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldEnabled, 1),
                new KeyValuePair <string, object>(BaseDictionaryItemEntity.FieldDeleted, 0)
            };

            if (!IsUnique(parameters, entity.Id.ToString()))
            {
                //名称已重复
                Status        = Status.ErrorNameExist;
                StatusCode    = Status.ErrorNameExist.ToString();
                StatusMessage = Status.ErrorNameExist.ToDescription();
            }
            else
            {
                result = AddEntity(entity);
                if (!string.IsNullOrEmpty(result))
                {
                    Status        = Status.OkAdd;
                    StatusCode    = Status.OkAdd.ToString();
                    StatusMessage = Status.OkAdd.ToDescription();
                }
                else
                {
                    Status        = Status.Error;
                    StatusCode    = Status.Error.ToString();
                    StatusMessage = Status.Error.ToDescription();
                }
            }
            status = Status;
            return(result);
        }
示例#11
0
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">实体</param>
        public int UpdateEntity(BaseDictionaryItemEntity entity)
        {
            var sqlBuilder = new SqlBuilder(DbHelper);

            sqlBuilder.BeginUpdate(CurrentTableName);
            SetEntity(sqlBuilder, entity);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateBy, UserInfo.RealName);
            }
            sqlBuilder.SetDbNow(BaseDictionaryItemEntity.FieldUpdateTime);
            sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateIp, Utils.GetIp());
            sqlBuilder.SetWhere(PrimaryKey, entity.Id);
            //return sqlBuilder.EndUpdate();
            var result = sqlBuilder.EndUpdate();

            if (result > 0)
            {
                RemoveCache(entity.Id);
            }
            return(result);
        }
示例#12
0
 // 这个是声明扩展方法
 partial void SetEntityExtend(SqlBuilder sqlBuilder, BaseDictionaryItemEntity entity);
示例#13
0
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">实体</param>
        public string AddEntity(BaseDictionaryItemEntity entity)
        {
            var key = string.Empty;

            if (entity.SortCode == 0)
            {
                var managerSequence = new BaseSequenceManager(DbHelper, Identity);
                if (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2)
                {
                    key = managerSequence.Increment($"SC_{CurrentTableName}_SEQ");
                }
                else
                {
                    key = managerSequence.Increment(CurrentTableName);
                }
                entity.SortCode = key.ToInt();
            }
            var sqlBuilder = new SqlBuilder(DbHelper, Identity, ReturnId);

            sqlBuilder.BeginInsert(CurrentTableName, PrimaryKey);
            if (!Identity)
            {
                // 这里已经是指定了主键了,所以不需要返回主键了
                sqlBuilder.ReturnId = false;
                sqlBuilder.SetValue(PrimaryKey, entity.Id);
            }
            else
            {
                if (!ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2))
                {
                    if (DbHelper.CurrentDbType == CurrentDbType.Oracle)
                    {
                        sqlBuilder.SetFormula(PrimaryKey, $"{CurrentTableName}_SEQ.NEXTVAL");
                    }
                    if (DbHelper.CurrentDbType == CurrentDbType.Db2)
                    {
                        sqlBuilder.SetFormula(PrimaryKey, $"NEXT VALUE FOR {CurrentTableName}_SEQ");
                    }
                }
                else
                {
                    if (Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2))
                    {
                        var managerSequence = new BaseSequenceManager(DbHelper);
                        entity.Id = managerSequence.Increment($"{CurrentTableName}_SEQ").ToInt();
                        sqlBuilder.SetValue(PrimaryKey, entity.Id);
                    }
                }
            }
            SetEntity(sqlBuilder, entity);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldCreateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldCreateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldCreateBy, UserInfo.RealName);
            }
            else
            {
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldCreateBy, entity.CreateBy);
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldCreateUserName, entity.CreateUserName);
            }
            sqlBuilder.SetDbNow(BaseDictionaryItemEntity.FieldCreateTime);
            sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldCreateIp, Utils.GetIp());
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateUserId, UserInfo.UserId);
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateUserName, UserInfo.UserName);
                sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateBy, UserInfo.RealName);
            }
            sqlBuilder.SetDbNow(BaseDictionaryItemEntity.FieldUpdateTime);
            sqlBuilder.SetValue(BaseDictionaryItemEntity.FieldUpdateIp, Utils.GetIp());
            if (Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access))
            {
                key = sqlBuilder.EndInsert().ToString();
            }
            else
            {
                sqlBuilder.EndInsert();
            }
            if (Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2))
            {
                key = entity.Id.ToString();
            }
            if (!string.IsNullOrWhiteSpace(key))
            {
                RemoveCache();
            }
            return(key);
        }
示例#14
0
 /// <summary>
 /// 更新
 /// </summary>
 /// <param name="entity">实体</param>
 public int Update(BaseDictionaryItemEntity entity)
 {
     return(UpdateEntity(entity));
 }