/// <summary> /// 编辑之后,需要重新刷新缓存,否则其他读取数据的地方会乱了,或者不及时了 /// </summary> /// <param name="entity">实体</param> /// <returns>影响行数</returns> public int AfterUpdate(BaseOrganizationEntity entity) { var result = 0; SetCache(entity); return(result); }
/// <summary> /// 保存实体修改记录 /// </summary> /// <param name="newEntity">修改前的实体对象</param> /// <param name="oldEntity">修改后的实体对象</param> /// <param name="tableName">表名称</param> public void SaveEntityChangeLog(BaseOrganizationEntity newEntity, BaseOrganizationEntity oldEntity, 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(BaseOrganizationEntity).GetProperties()) { var oldValue = Convert.ToString(property.GetValue(oldEntity, null)); var newValue = Convert.ToString(property.GetValue(newEntity, null)); var fieldDescription = property.GetCustomAttributes(typeof(FieldDescription), false).FirstOrDefault() as FieldDescription; //不记录创建人、修改人、没有修改的记录 if (!fieldDescription.NeedLog || oldValue == newValue) { continue; } var record = new BaseChangeLogEntity { TableName = CurrentTableName, TableDescription = FieldExtensions.ToDescription(typeof(BaseOrganizationEntity), "CurrentTableName"), ColumnName = property.Name, ColumnDescription = fieldDescription.Text, RecordKey = oldEntity.Id.ToString(), NewValue = newValue, OldValue = oldValue }; manager.Add(record, true, false); } }
/// <summary> /// 检查唯一值式新增 /// </summary> /// <param name="entity"></param> /// <param name="status"></param> /// <returns></returns> public string UniqueAdd(BaseOrganizationEntity entity, out Status status) { var result = string.Empty; //检查是否重复 var parameters = new List <KeyValuePair <string, object> >(); if (entity.ParentId > 0) { //父项不等于空的时候,才检查名称重复 parameters.Add(new KeyValuePair <string, object>(BaseOrganizationEntity.FieldParentId, entity.ParentId)); parameters.Add(new KeyValuePair <string, object>(BaseOrganizationEntity.FieldName, entity.Name)); parameters.Add(new KeyValuePair <string, object>(BaseOrganizationEntity.FieldEnabled, 1)); parameters.Add(new KeyValuePair <string, object>(BaseOrganizationEntity.FieldDeleted, 0)); } if ((entity.ParentId > 0) && Exists(parameters)) { //名称已重复 Status = Status.ErrorNameExist; StatusCode = Status.ErrorNameExist.ToString(); StatusMessage = Status.ErrorNameExist.ToDescription(); } else { parameters = new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(BaseOrganizationEntity.FieldCode, entity.Code), new KeyValuePair <string, object>(BaseOrganizationEntity.FieldEnabled, 1), new KeyValuePair <string, object>(BaseOrganizationEntity.FieldDeleted, 0) }; if (entity.Code.Length > 0 && Exists(parameters)) { //编号已重复 Status = Status.ErrorCodeExist; StatusCode = Status.ErrorCodeExist.ToString(); StatusMessage = Status.ErrorCodeExist.ToDescription(); } else { result = AddEntity(entity); if (!string.IsNullOrEmpty(result)) { AfterAdd(entity); 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); }
/// <summary> /// 添加, 这里可以人工干预,提高程序的性能 /// </summary> /// <param name="entity">实体</param> /// <param name="identity">自增量方式,表主键是否采用自增的策略</param> /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param> /// <returns>主键</returns> public string Add(BaseOrganizationEntity entity, bool identity = true, bool returnId = true) { Identity = identity; ReturnId = returnId; entity.Id = AddEntity(entity).ToInt(); return(entity.Id.ToString()); }
/// <summary> /// 添加实体 /// </summary> /// <param name="userInfo">用户</param> /// <param name="entity">实体</param> /// <param name="status">状态</param> /// <param name="statusMessage">状态信息</param> /// <returns>主键</returns> public string Add(BaseUserInfo userInfo, BaseOrganizationEntity entity, out Status status, out string statusMessage) { var result = string.Empty; var returnCode = Status.Ok; var returnMessage = string.Empty; var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod()); ServiceUtil.ProcessUserCenterWriteDb(userInfo, parameter, (dbHelper) => { var manager = new BaseOrganizationManager(dbHelper, userInfo); result = manager.UniqueAdd(entity, out returnCode); //增加返回状态和信息Troy.Cui 2018-10-03 returnMessage = manager.StatusMessage; //returnMessage = manager.GetStateMessage(returnCode); if (returnCode.Equals(Status.OkAdd.ToString())) { entity.Id = int.Parse(result); //Troy.Cui 2018-10-18去掉这里的Folder检查 //var folderManager = new BaseFolderManager(dbHelper, userInfo); //folderManager.FolderCheck(entity.Id.ToString(), entity.Name); } }); status = returnCode; statusMessage = returnMessage; return(result); }
/// <summary> /// 更新组织机构 /// </summary> /// <param name="userInfo">用户</param> /// <param name="entity">实体</param> /// <param name="status">状态</param> /// <param name="statusMessage">状态信息</param> /// <returns>影响行数</returns> public int Update(BaseUserInfo userInfo, BaseOrganizationEntity 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) => { // 2015-12-19 吉日嘎拉 网络不稳定,数据获取不完整时,异常时,会引起重大隐患 var manager = new BaseOrganizationManager(dbHelper, userInfo); // result = manager.Update(entity); if (manager.StatusCode.Equals(Status.OkUpdate.ToString())) { // var folderManager = new BaseFolderManager(dbHelper, userInfo); // result = folderManager.SetProperty(entity.Id.ToString(), new KeyValuePair<string, object>(BaseFolderEntity.FieldFolderName, entity.Name)); } returnCode = manager.Status; returnMessage = manager.StatusMessage; }); status = returnCode; statusMessage = returnMessage; return(result); }
/// <summary> /// 添加之后,需要重新刷新缓存,否则其他读取数据的地方会乱了,或者不及时了 /// </summary> /// <param name="entity">实体</param> /// <returns>影响行数</returns> public int AfterAdd(BaseOrganizationEntity entity) { var result = 0; CachePreheatingSpelling01(entity); CachePreheatingSpelling02(entity); CachePreheatingSpelling03(entity); CachePreheatingSpelling04(entity); return(result); }
/// <summary> /// 从缓存获取 /// </summary> /// <param name="key"></param> /// <returns></returns> public static BaseOrganizationEntity GetCacheByKey(string key) { BaseOrganizationEntity result = null; if (!string.IsNullOrWhiteSpace(key)) { CacheUtil.Get <BaseOrganizationEntity>(key); } return(result); }
/// <summary> /// 设置实体 /// </summary> /// <param name="sqlBuilder">Sql语句生成器</param> /// <param name="entity">实体</param> private void SetEntity(SqlBuilder sqlBuilder, BaseOrganizationEntity entity) { SetEntityExtend(sqlBuilder, entity); sqlBuilder.SetValue(BaseOrganizationEntity.FieldParentId, entity.ParentId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldParentName, entity.ParentName); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCode, entity.Code); sqlBuilder.SetValue(BaseOrganizationEntity.FieldName, entity.Name); sqlBuilder.SetValue(BaseOrganizationEntity.FieldShortName, entity.ShortName); sqlBuilder.SetValue(BaseOrganizationEntity.FieldStandardName, entity.StandardName); sqlBuilder.SetValue(BaseOrganizationEntity.FieldStandardCode, entity.StandardCode); sqlBuilder.SetValue(BaseOrganizationEntity.FieldQuickQuery, entity.QuickQuery); sqlBuilder.SetValue(BaseOrganizationEntity.FieldSimpleSpelling, entity.SimpleSpelling); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCategoryCode, entity.CategoryCode); sqlBuilder.SetValue(BaseOrganizationEntity.FieldOuterPhone, entity.OuterPhone); sqlBuilder.SetValue(BaseOrganizationEntity.FieldInnerPhone, entity.InnerPhone); sqlBuilder.SetValue(BaseOrganizationEntity.FieldFax, entity.Fax); sqlBuilder.SetValue(BaseOrganizationEntity.FieldPostalCode, entity.PostalCode); sqlBuilder.SetValue(BaseOrganizationEntity.FieldProvince, entity.Province); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCity, entity.City); sqlBuilder.SetValue(BaseOrganizationEntity.FieldDistrict, entity.District); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCompanyId, entity.CompanyId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCompanyCode, entity.CompanyCode); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCompanyName, entity.CompanyName); sqlBuilder.SetValue(BaseOrganizationEntity.FieldArea, entity.Area); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCostCenter, entity.CostCenter); sqlBuilder.SetValue(BaseOrganizationEntity.FieldFinancialCenter, entity.FinancialCenter); sqlBuilder.SetValue(BaseOrganizationEntity.FieldAddress, entity.Address); sqlBuilder.SetValue(BaseOrganizationEntity.FieldWeb, entity.Web); sqlBuilder.SetValue(BaseOrganizationEntity.FieldBank, entity.Bank); sqlBuilder.SetValue(BaseOrganizationEntity.FieldBankAccount, entity.BankAccount); sqlBuilder.SetValue(BaseOrganizationEntity.FieldLayer, entity.Layer); sqlBuilder.SetValue(BaseOrganizationEntity.FieldLongitude, entity.Longitude); sqlBuilder.SetValue(BaseOrganizationEntity.FieldLatitude, entity.Latitude); sqlBuilder.SetValue(BaseOrganizationEntity.FieldContainChildNodes, entity.ContainChildNodes); sqlBuilder.SetValue(BaseOrganizationEntity.FieldIsInnerOrganization, entity.IsInnerOrganization); sqlBuilder.SetValue(BaseOrganizationEntity.FieldProvinceId, entity.ProvinceId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCityId, entity.CityId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldDistrictId, entity.DistrictId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldStreetId, entity.StreetId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldStreet, entity.Street); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCostCenterId, entity.CostCenterId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldFinancialCenterId, entity.FinancialCenterId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldLeader, entity.Leader); sqlBuilder.SetValue(BaseOrganizationEntity.FieldLeaderMobile, entity.LeaderMobile); sqlBuilder.SetValue(BaseOrganizationEntity.FieldManager, entity.Manager); sqlBuilder.SetValue(BaseOrganizationEntity.FieldManagerMobile, entity.ManagerMobile); sqlBuilder.SetValue(BaseOrganizationEntity.FieldEmergencyCall, entity.EmergencyCall); sqlBuilder.SetValue(BaseOrganizationEntity.FieldBusinessPhone, entity.BusinessPhone); sqlBuilder.SetValue(BaseOrganizationEntity.FieldDescription, entity.Description); sqlBuilder.SetValue(BaseOrganizationEntity.FieldSortCode, entity.SortCode); sqlBuilder.SetValue(BaseOrganizationEntity.FieldDeleted, entity.Deleted); sqlBuilder.SetValue(BaseOrganizationEntity.FieldEnabled, entity.Enabled); }
/// <summary> /// 从缓存获取 /// </summary> /// <param name="code"></param> /// <returns></returns> public static BaseOrganizationEntity GetEntityByCodeByCache(string code) { BaseOrganizationEntity result = null; if (!string.IsNullOrEmpty(code)) { // string key = "OrganizationByCode:" + code; var key = "OBC:" + code; result = CacheUtil.Cache(key, () => new BaseOrganizationManager().GetEntityByCode(code), true); } return(result); }
/// <summary> /// 按名称获取实体 /// </summary> /// <param name="userInfo">用户</param> /// <param name="name">名称</param> /// <returns>实体</returns> public BaseOrganizationEntity GetEntityByName(BaseUserInfo userInfo, string name) { BaseOrganizationEntity entity = null; var parameter = ServiceInfo.Create(userInfo, MethodBase.GetCurrentMethod()); ServiceUtil.ProcessUserCenterReadDb(userInfo, parameter, (dbHelper) => { var manager = new BaseOrganizationManager(dbHelper, userInfo); entity = manager.GetEntityByName(name); }); return(entity); }
/// <summary> /// 从缓存获取 /// </summary> /// <param name="id"></param> /// <param name="refreshCache"></param> /// <returns></returns> public static BaseOrganizationEntity GetEntityByCache(string id, bool refreshCache = false) { BaseOrganizationEntity result = null; if (!string.IsNullOrEmpty(id)) { var cacheKey = "O:"; cacheKey += id; result = CacheUtil.Cache(cacheKey, () => new BaseOrganizationManager().GetEntity(id), true, refreshCache); } return(result); }
/// <summary> /// 添加或更新(主键是否为0) /// </summary> /// <param name="entity">实体</param> /// <param name="identity">自增量方式,表主键是否采用自增的策略</param> /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param> /// <returns>主键</returns> public string AddOrUpdate(BaseOrganizationEntity 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); } }
/// <summary> /// 重新设置缓存(重新强制设置缓存)可以提供外部调用的 /// </summary> /// <param name="id">主键</param> /// <returns>用户信息</returns> public static BaseOrganizationEntity SetCache(string id) { BaseOrganizationEntity result = null; var manager = new BaseOrganizationManager(); result = manager.GetEntity(id); if (result != null) { SetCache(result); } return(result); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="entity">实体</param> public static void SetCache(BaseOrganizationEntity entity) { if (entity != null) { var key = string.Empty; // key = "Organization:" + entity.Id; key = "O:" + entity.Id; CacheUtil.Set <BaseOrganizationEntity>(key, entity); // key = "OrganizationByCode:" + entity.Code; key = "OBC:" + entity.Code; CacheUtil.Set <string>(key, entity.Id.ToString()); //key = "OrganizationByName:" + entity.Name; key = "OBN:" + entity.Name; CacheUtil.Set <string>(key, entity.Id.ToString()); } }
/// <summary> /// 批量保存 /// </summary> /// <param name="dt"></param> /// <returns></returns> public override int BatchSave(DataTable dt) { var result = 0; var entity = new BaseOrganizationEntity(); foreach (DataRow dr in dt.Rows) { // 删除状态 if (dr.RowState == DataRowState.Deleted) { var id = dr[BaseOrganizationEntity.FieldId, DataRowVersion.Original].ToString(); if (id.Length > 0) { result += DeleteEntity(id); } } // 被修改过 if (dr.RowState == DataRowState.Modified) { var id = dr[BaseOrganizationEntity.FieldId, DataRowVersion.Original].ToString(); if (id.Length > 0) { entity.GetFrom(dr); result += UpdateEntity(entity); } } // 添加状态 if (dr.RowState == DataRowState.Added) { entity.GetFrom(dr); result += AddEntity(entity).Length > 0 ? 1 : 0; } if (dr.RowState == DataRowState.Unchanged) { continue; } if (dr.RowState == DataRowState.Detached) { continue; } } return(result); }
/// <summary> /// 缓存预热 /// </summary> /// <param name="organizationEntity"></param> /// <param name="score"></param> public static void CachePreheatingSpelling02(BaseOrganizationEntity organizationEntity, double score = 0) { // 读取到的数据直接强制设置到缓存里 var id = organizationEntity.Id; // 2016-01-06 吉日嘎拉 网点编号不能大小写转换,否则查询就乱套了,不能改变原样 var code = organizationEntity.Code; var name = organizationEntity.Name; var simpleSpelling = organizationEntity.SimpleSpelling; var enabled = organizationEntity.Enabled.ToString(); var deletionStateCode = organizationEntity.Deleted.ToString(); var organization = id + ";" + code + ";" + name + ";" + enabled + ";" + deletionStateCode; // 2016-04-11 吉日嘎拉 已经被删除的网点不需要缓存了 if (organizationEntity.Deleted == 1) { // organization += " 已删除"; return; } var key = string.Empty; // 02:父级主键缓存数据方法 var parentId = organizationEntity.ParentId; if (!string.IsNullOrEmpty(parentId.ToString())) { if (!string.IsNullOrEmpty(code)) { // 2016-01-06 吉日嘎拉 这里需要小写,提高效率,提高有善度 key = "ParentId:" + parentId + ":" + code.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(name)) { key = "ParentId:" + parentId + ":" + name.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(simpleSpelling)) { key = "ParentId:" + parentId + ":" + simpleSpelling.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } } }
private static BaseOrganizationEntity GetOrganizationEntity(IDataReader dataReader) { var entity = new BaseOrganizationEntity { Id = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldId].ToString()), Code = dataReader[BaseOrganizationEntity.FieldCode].ToString(), Name = dataReader[BaseOrganizationEntity.FieldName].ToString(), SimpleSpelling = dataReader[BaseOrganizationEntity.FieldSimpleSpelling].ToString().ToLower(), CostCenterId = dataReader[BaseOrganizationEntity.FieldCostCenterId].ToString(), ProvinceId = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldProvinceId].ToString()), CompanyId = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldCompanyId].ToString()), CityId = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldCityId].ToString()), ParentId = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldParentId].ToString()), Enabled = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldEnabled], 1), Deleted = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldDeleted], 0), SortCode = BaseUtil.ConvertToInt(dataReader[BaseOrganizationEntity.FieldSortCode]) }; return(entity); }
/// <summary> /// 更新实体 /// </summary> /// <param name="entity">实体</param> public int UpdateEntity(BaseOrganizationEntity entity) { var sqlBuilder = new SqlBuilder(DbHelper); sqlBuilder.BeginUpdate(CurrentTableName); SetEntity(sqlBuilder, entity); if (UserInfo != null) { sqlBuilder.SetValue(BaseOrganizationEntity.FieldUpdateUserId, UserInfo.UserId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldUpdateUserName, UserInfo.UserName); sqlBuilder.SetValue(BaseOrganizationEntity.FieldUpdateBy, UserInfo.RealName); } sqlBuilder.SetDbNow(BaseOrganizationEntity.FieldUpdateTime); sqlBuilder.SetValue(BaseOrganizationEntity.FieldUpdateIp, Utils.GetIp()); sqlBuilder.SetWhere(PrimaryKey, entity.Id); //return sqlBuilder.EndUpdate(); var result = sqlBuilder.EndUpdate(); if (result > 0) { RemoveCache(entity.Id); } return(result); }
/// <summary> /// 添加实体 /// </summary> /// <param name="entity">实体</param> public string AddEntity(BaseOrganizationEntity 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(BaseOrganizationEntity.FieldCreateUserId, UserInfo.UserId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCreateUserName, UserInfo.UserName); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCreateBy, UserInfo.RealName); } else { sqlBuilder.SetValue(BaseOrganizationEntity.FieldCreateBy, entity.CreateBy); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCreateUserName, entity.CreateUserName); } sqlBuilder.SetDbNow(BaseOrganizationEntity.FieldCreateTime); sqlBuilder.SetValue(BaseOrganizationEntity.FieldCreateIp, Utils.GetIp()); if (UserInfo != null) { sqlBuilder.SetValue(BaseOrganizationEntity.FieldUpdateUserId, UserInfo.UserId); sqlBuilder.SetValue(BaseOrganizationEntity.FieldUpdateUserName, UserInfo.UserName); sqlBuilder.SetValue(BaseOrganizationEntity.FieldUpdateBy, UserInfo.RealName); } sqlBuilder.SetDbNow(BaseOrganizationEntity.FieldUpdateTime); sqlBuilder.SetValue(BaseOrganizationEntity.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); }
/// <summary> /// 转换为UserInfo用户信息 /// </summary> /// <param name="userInfo"></param> /// <param name="userEntity"></param> /// <param name="userLogonEntity"></param> /// <param name="validateUserOnly"></param> /// <returns></returns> public BaseUserInfo ConvertToUserInfo(BaseUserInfo userInfo, BaseUserEntity userEntity, BaseUserLogonEntity userLogonEntity = null, bool validateUserOnly = false) { if (userEntity == null) { return(null); } userInfo.Id = userEntity.Id.ToString(); userInfo.UserId = userEntity.Id; userInfo.IsAdministrator = userEntity.IsAdministrator == 1; userInfo.Code = userEntity.Code; userInfo.UserName = userEntity.UserName; userInfo.RealName = userEntity.RealName; userInfo.NickName = userEntity.NickName; if (userLogonEntity != null) { userInfo.OpenId = userLogonEntity.OpenId; } userInfo.CompanyId = userEntity.CompanyId.ToString(); userInfo.CompanyName = userEntity.CompanyName; userInfo.CompanyCode = BaseOrganizationManager.GetEntityByCache(userEntity.CompanyId.ToString())?.Code; //Troy.Cui 2018.08.04 增加Sub的转换 userInfo.SubCompanyId = userEntity.SubCompanyId.ToString(); userInfo.SubCompanyName = userEntity.SubCompanyName; userInfo.SubCompanyCode = BaseOrganizationManager.GetEntityByCache(userEntity.SubCompanyId.ToString())?.Code; userInfo.DepartmentId = userEntity.DepartmentId.ToString(); userInfo.DepartmentName = userEntity.DepartmentName; userInfo.DepartmentCode = BaseOrganizationManager.GetEntityByCache(userEntity.DepartmentId.ToString())?.Code; userInfo.SubDepartmentId = userEntity.SubDepartmentId.ToString(); userInfo.SubDepartmentName = userEntity.SubDepartmentName; userInfo.SubDepartmentCode = BaseOrganizationManager.GetEntityByCache(userEntity.SubDepartmentId.ToString())?.Code; userInfo.WorkgroupId = userEntity.WorkgroupId.ToString(); userInfo.WorkgroupName = userEntity.WorkgroupName; //2016-11-23 欧腾飞加入 companyCode 和数字签名 userInfo.Signature = userEntity.Signature; BaseOrganizationEntity organizationEntity = null; if (!string.IsNullOrEmpty(userInfo.CompanyId)) { try { organizationEntity = BaseOrganizationManager.GetEntityByCache(userInfo.CompanyId.ToString()); } catch (Exception ex) { var writeMessage = "BaseOrganizationManager.GetEntityByCache:发生时间:" + DateTime.Now + Environment.NewLine + "CompanyId 无法缓存获取:" + userInfo.CompanyId + Environment.NewLine + "Message:" + ex.Message + Environment.NewLine + "Source:" + ex.Source + Environment.NewLine + "StackTrace:" + ex.StackTrace + Environment.NewLine + "TargetSite:" + ex.TargetSite + Environment.NewLine; LogUtil.WriteLog(writeMessage, "Exception"); } if (organizationEntity == null) { var organizationManager = new BaseOrganizationManager(); organizationEntity = organizationManager.GetEntity(userInfo.CompanyId); // 2015-12-06 吉日嘎拉 进行记录日志功能改进 if (organizationEntity == null) { var writeMessage = "BaseOrganizationManager.GetEntity:发生时间:" + DateTime.Now + Environment.NewLine + "CompanyId 无法缓存获取:" + userInfo.CompanyId + Environment.NewLine + "BaseUserInfo:" + userInfo.Serialize(); LogUtil.WriteLog(writeMessage, "Log"); } } if (organizationEntity != null) { userInfo.CompanyCode = organizationEntity.Code; } } ////部门数据需要从部门表里读取 //if (!validateUserOnly && !string.IsNullOrEmpty(userInfo.DepartmentId)) //{ // organizationEntity = BaseOrganizationManager.GetEntityByCache(userInfo.DepartmentId); //} //else //{ // if (organizationManager == null) // { // organizationManager = new Business.BaseOrganizationManager(); // } // organizationEntity = organizationManager.GetEntity(userInfo.DepartmentId); //} //if (organizationEntity != null) //{ // userInfo.DepartmentCode = organizationEntity.Code; //} return(userInfo); }
/// <summary> /// 更新 /// </summary> /// <param name="entity">实体</param> /// <param name="status">状态</param> /// <returns></returns> public int UniqueUpdate(BaseOrganizationEntity entity, out Status status) { var result = 0; // 检查是否已被其他人修改 //if (DbUtil.IsModifed(DbHelper, BaseOrganizationEntity.CurrentTableName, entity.Id, entity.UpdateUserId, entity.UpdateTime)) //{ // // 数据已经被修改 // status = StatusCode.ErrorChanged.ToString(); //} var parameters = new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(BaseOrganizationEntity.FieldParentId, entity.ParentId), new KeyValuePair <string, object>(BaseOrganizationEntity.FieldName, entity.Name), new KeyValuePair <string, object>(BaseOrganizationEntity.FieldDeleted, 0) }; if (Exists(parameters, entity.Id)) { // 名称已重复 Status = Status.ErrorNameExist; StatusCode = Status.ErrorNameExist.ToString(); StatusMessage = Status.ErrorNameExist.ToDescription(); } else { // 检查编号是否重复 parameters = new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>(BaseOrganizationEntity.FieldCode, entity.Code), new KeyValuePair <string, object>(BaseOrganizationEntity.FieldDeleted, 0) }; if (entity.Code.Length > 0 && Exists(parameters, entity.Id)) { // 编号已重复 Status = Status.ErrorCodeExist; StatusCode = Status.ErrorCodeExist.ToString(); StatusMessage = Status.ErrorCodeExist.ToDescription(); } else { if (string.IsNullOrEmpty(entity.QuickQuery)) { // 2015-12-11 吉日嘎拉 全部小写,提高Oracle的效率 entity.QuickQuery = StringUtil.GetPinyin(entity.Name).ToLower(); } if (string.IsNullOrEmpty(entity.SimpleSpelling)) { // 2015-12-11 吉日嘎拉 全部小写,提高Oracle的效率 entity.SimpleSpelling = StringUtil.GetSimpleSpelling(entity.Name).ToLower(); } // 获取原始实体信息 var entityOld = GetEntity(entity.Id); if (entityOld != null) { // 保存修改记录,无论是否允许 SaveEntityChangeLog(entity, entityOld); // 1:更新部门的信息 result = UpdateEntity(entity); // 2:组织机构修改时,用户表的公司,部门,工作组数据给同步更新。 var userManager = new BaseUserManager(DbHelper, UserInfo); switch (entity.CategoryCode) { case "Company": userManager.SetProperty(new KeyValuePair <string, object>(BaseUserEntity.FieldCompanyId, entity.Id), new KeyValuePair <string, object>(BaseUserEntity.FieldCompanyName, entity.Name)); break; case "SubCompany": userManager.SetProperty(new KeyValuePair <string, object>(BaseUserEntity.FieldSubCompanyId, entity.Id), new KeyValuePair <string, object>(BaseUserEntity.FieldSubCompanyName, entity.Name)); break; case "Department": userManager.SetProperty(new KeyValuePair <string, object>(BaseUserEntity.FieldDepartmentId, entity.Id), new KeyValuePair <string, object>(BaseUserEntity.FieldDepartmentName, entity.Name)); break; case "SubDepartment": userManager.SetProperty(new KeyValuePair <string, object>(BaseUserEntity.FieldSubDepartmentId, entity.Id), new KeyValuePair <string, object>(BaseUserEntity.FieldSubDepartmentName, entity.Name)); break; case "Workgroup": userManager.SetProperty(new KeyValuePair <string, object>(BaseUserEntity.FieldWorkgroupId, entity.Id), new KeyValuePair <string, object>(BaseUserEntity.FieldWorkgroupName, entity.Name)); break; } // 03:组织机构修改时,文件夹同步更新 // BaseFolderManager folderManager = new BaseFolderManager(this.DbHelper, this.UserInfo); // folderManager.SetProperty(new KeyValuePair<string, object>(BaseFolderEntity.FieldFolderName, entity.Name), new KeyValuePair<string, object>(BaseFolderEntity.FieldId, entity.Id)); if (result == 1) { // AfterUpdate(entity); SetCache(entity.Id.ToString()); Status = Status.OkUpdate; StatusCode = Status.OkUpdate.ToString(); StatusMessage = Status.OkUpdate.ToDescription(); } else { Status = Status.ErrorDeleted; StatusCode = Status.ErrorDeleted.ToString(); StatusMessage = Status.ErrorDeleted.ToDescription(); } } } } status = Status; return(result); }
/// <summary> /// 更新 /// </summary> /// <param name="entity">实体</param> public int Update(BaseOrganizationEntity entity) { return(UpdateEntity(entity)); }
/// <summary> /// 缓存预热 /// </summary> /// <param name="organizationEntity"></param> /// <param name="score"></param> public static void CachePreheatingSpelling04(BaseOrganizationEntity organizationEntity, double score = 0) { // 读取到的数据直接强制设置到缓存里 var id = organizationEntity.Id; // 2016-01-06 吉日嘎拉 网点编号不能大小写转换,否则查询就乱套了,不能改变原样 var code = organizationEntity.Code; var name = organizationEntity.Name; var simpleSpelling = organizationEntity.SimpleSpelling; var enabled = organizationEntity.Enabled.ToString(); var deletionStateCode = organizationEntity.Deleted.ToString(); var organization = id + ";" + code + ";" + name + ";" + enabled + ";" + deletionStateCode; // 2016-04-11 吉日嘎拉 已经被删除的网点不需要缓存了 if (organizationEntity.Deleted == 1) { // organization += " 已删除"; return; } var key = string.Empty; // 04:结算中心主键缓存数据方法 var costCenterId = organizationEntity.CostCenterId; if (!string.IsNullOrEmpty(costCenterId)) { if (!string.IsNullOrEmpty(code)) { // 2016-01-06 吉日嘎拉 这里需要小写,提高效率,提高有善度 key = "CostCenterId:" + costCenterId + ":" + code.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(name)) { key = "CostCenterId:" + costCenterId + ":" + name.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(simpleSpelling)) { key = "CostCenterId:" + costCenterId + ":" + simpleSpelling.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } } // 05:按省缓存数据方法 var provinceId = organizationEntity.ProvinceId; if (!string.IsNullOrEmpty(provinceId?.ToString())) { if (!string.IsNullOrEmpty(code)) { // 2016-01-06 吉日嘎拉 这里需要小写,提高效率,提高有善度 key = "ProvinceId:" + provinceId + ":" + code.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(name)) { key = "ProvinceId:" + provinceId + ":" + name.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(simpleSpelling)) { key = "ProvinceId:" + provinceId + ":" + simpleSpelling.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } } // 06:按市缓存数据方法 var cityId = organizationEntity.CityId; if (!string.IsNullOrEmpty(cityId?.ToString())) { if (!string.IsNullOrEmpty(code)) { // 2016-01-06 吉日嘎拉 这里需要小写,提高效率,提高有善度 key = "CityId:" + cityId + ":" + code.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(name)) { key = "CityId:" + cityId + ":" + name.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(simpleSpelling)) { key = "CityId:" + cityId + ":" + simpleSpelling.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } } // 07:所有下属递归的方式进行快速缓存检索(START WITH CONNECT BY PRIOR) 包括自己 var startId = organizationEntity.Id.ToString(); while (!string.IsNullOrEmpty(startId)) { if (!string.IsNullOrEmpty(code)) { // 2016-01-06 吉日嘎拉 这里需要小写,提高效率,提高有善度 key = "StartId:" + startId + ":" + code.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(name)) { key = "StartId:" + startId + ":" + name.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } if (!string.IsNullOrEmpty(simpleSpelling)) { key = "StartId:" + startId + ":" + simpleSpelling.ToLower(); CacheUtil.Set(key, organization, new TimeSpan(15, 0, 0, 0)); } // 获取上级的上级,一直进行循环,在缓存里进行计算,提高效率 startId = GetParentIdByCache(startId); } // 输出到屏幕看看运行效果如何?心里有个数 Console.WriteLine(score + " " + organization); }
/// <summary> /// 导入K8系统网点信息 /// </summary> /// <param name="connectionString">数据库连接</param> /// <param name="conditional">条件,不需要同步所有的数据</param> /// <returns>影响行数</returns> public int ImportK8Organization(string connectionString = null, string conditional = null) { // delete from baseorganization where id < 1000000 var result = 0; if (string.IsNullOrEmpty(connectionString)) { connectionString = ConfigurationUtil.AppSettings("K8Connection", BaseSystemInfo.EncryptDbConnection); } if (!string.IsNullOrEmpty(connectionString)) { // 01:可以从k8里读取公司、用户、密码的。 var dbHelper = DbHelperFactory.Create(CurrentDbType.Oracle, connectionString); var organizationManager = new Business.BaseOrganizationManager(this.DbHelper, this.UserInfo); // 不不存在的组织机构删除掉TAB_SITE是远程试图 var commandText = "DELETE FROM BASEORGANIZE WHERE id < 1000000 AND id NOT IN (SELECT id FROM TAB_SITE)"; organizationManager.ExecuteNonQuery(commandText); // 同步数据 commandText = "SELECT * FROM TAB_SITE WHERE BL_NOT_INPUT IS NULL OR BL_NOT_INPUT = 0 "; if (!string.IsNullOrEmpty(conditional)) { commandText += conditional; } var dataReader = dbHelper.ExecuteReader(commandText); if (dataReader != null && !dataReader.IsClosed) { while (dataReader.Read()) { // 这里需要从数据库读取、否则容易造成丢失数据 var entity = organizationManager.GetEntity(dataReader["ID"].ToString()); if (entity == null) { entity = new BaseOrganizationEntity(); //entity.Id = dr["ID"].ToString(); } entity.Code = dataReader["SITE_CODE"].ToString(); if (string.IsNullOrEmpty(entity.ParentName) || !entity.ParentName.Equals(dataReader["SUPERIOR_SITE"].ToString())) { entity.ParentName = dataReader["SUPERIOR_SITE"].ToString(); entity.ParentId = 0; } entity.Name = dataReader["SITE_NAME"].ToString(); entity.ShortName = dataReader["SITE_NAME"].ToString(); entity.CategoryCode = dataReader["TYPE"].ToString(); entity.OuterPhone = dataReader["PHONE"].ToString(); entity.Fax = dataReader["FAX"].ToString(); entity.Province = dataReader["PROVINCE"].ToString(); entity.City = dataReader["CITY"].ToString(); entity.District = dataReader["RANGE_NAME"].ToString(); entity.CostCenter = dataReader["SUPERIOR_FINANCE_CENTER"].ToString(); entity.Area = dataReader["BIG_AREA_NAME"].ToString(); entity.CompanyName = dataReader["SITE1_NAME"].ToString(); if (!string.IsNullOrEmpty(dataReader["ORDER_BY"].ToString())) { entity.SortCode = int.Parse(dataReader["ORDER_BY"].ToString()); } // 02:可以把读取到的数据能写入到用户中心的。 result = organizationManager.UpdateEntity(entity); if (result == 0) { organizationManager.AddEntity(entity); } } dataReader.Close(); } // 填充 parentname // select * from baseorganization where parentname is null commandText = @"update baseorganization set parentname = (select fullname from baseorganization t where t.id = baseorganization.parentId) where parentname is null"; ExecuteNonQuery(commandText); // 填充 parentId // select * from baseorganization where parentId is null commandText = @"UPDATE baseorganization SET parentId = (SELECT Id FROM baseorganization t WHERE t.fullname = baseorganization.parentname) WHERE parentId IS NULL"; // 100000 以下是基础数据的,100000 以上是通用权限管理系统的 // UPDATE baseorganization SET parentId = (SELECT Id FROM baseorganization t WHERE t.fullname = baseorganization.parentname) WHERE parentId < 100000 ExecuteNonQuery(commandText); // 更新错误数据 commandText = @"UPDATE baseorganization SET parentId = null WHERE id = parentId"; ExecuteNonQuery(commandText); // 设置员工的公司主键 commandText = @"UPDATE baseuser SET companyid = (SELECT MAX(Id) FROM baseorganization WHERE baseorganization.fullname = baseuser.companyname AND baseorganization.Id < 1000000) WHERE companyId IS NULL OR companyId = ''"; ExecuteNonQuery(commandText); } return(result); }
// 这个是声明扩展方法 partial void SetEntityExtend(SqlBuilder sqlBuilder, BaseOrganizationEntity entity);