/// <summary> /// 更新完成度 /// </summary> /// <param name="userId">用户Id</param> public void UpdateIntegrity(long userId) { UserProfileSettings userProfileSettings = DIContainer.Resolve <IUserProfileSettingsManager>().GetUserProfileSettings(); int[] integrityItems = userProfileSettings.IntegrityProportions; int integrity = integrityItems[(int)ProfileIntegrityItems.Birthday]; PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); var sql = Sql.Builder; sql.Select("Count(*)") .From("spb_EducationExperiences") .Where("UserId = @0", userId); int countEducation = dao.ExecuteScalar <int>(sql); if (countEducation > 0) { integrity += integrityItems[(int)ProfileIntegrityItems.EducationExperience]; } sql = Sql.Builder; sql.Select("count(*)") .From("spb_WorkExperiences") .Where("UserId = @0", userId); int countWork = dao.ExecuteScalar <int>(sql); if (countWork > 0) { integrity += integrityItems[(int)ProfileIntegrityItems.WorkExperience]; } sql = Sql.Builder; sql.Where("userId = @0", userId); UserProfile userProfile = dao.FirstOrDefault <UserProfile>(sql); if (userProfile != null) { IUser user = new UserService().GetUser(userProfile.UserId); integrity += (user.HasAvatar ? integrityItems[(int)ProfileIntegrityItems.Avatar] : 0); integrity += (userProfile.HasHomeAreaCode ? integrityItems[(int)ProfileIntegrityItems.HomeArea] : 0); integrity += (userProfile.HasIM ? integrityItems[(int)ProfileIntegrityItems.IM] : 0); integrity += (userProfile.HasIntroduction ? integrityItems[(int)ProfileIntegrityItems.Introduction] : 0); integrity += (userProfile.HasMobile ? integrityItems[(int)ProfileIntegrityItems.Mobile] : 0); integrity += (userProfile.HasNowAreaCode ? integrityItems[(int)ProfileIntegrityItems.NowArea] : 0); userProfile.Integrity = integrity; Update(userProfile); } dao.CloseSharedConnection(); }
/// <summary> /// 添加标签与分组关联 /// </summary> /// <param name="tagName">标签名</param> /// <param name="groupId">拥有者Id</param> /// <param name="tenantTypeId">租户类型Id</param> public int AddTagInGroup(string tagName, long groupId, string tenantTypeId) { PetaPocoDatabase dao = CreateDAO(); int affectCount = 0; dao.OpenSharedConnection(); var sql = PetaPoco.Sql.Builder; sql.Select("count(*)") .From("tn_TagsInGroups") .Where("GroupId = @0", groupId) .Where("TagName = @0", tagName) .Where("TenantTypeId = @0", tenantTypeId); affectCount = dao.ExecuteScalar <int>(sql); if (affectCount == 0) { sql = PetaPoco.Sql.Builder; sql.Append("INSERT INTO tn_TagsInGroups (GroupId, TagName,TenantTypeId) VALUES (@0,@1,@2)", groupId, tagName, tenantTypeId); affectCount = dao.Execute(sql); if (affectCount > 0) { RealTimeCacheHelper.IncreaseAreaVersion("GroupId", groupId); } } dao.CloseSharedConnection(); return(affectCount); }
/// <summary> /// 获取标签实体 /// </summary> /// <param name="tagName">标签名</param> /// <param name="tenantTypeId">租户类型Id</param> /// <returns></returns> public T Get(string tagName, string tenantTypeId) { ICacheService cacheService = DIContainer.Resolve <ICacheService>(); string cacheKey = "TagIdToTagNames::TenantTypeId:" + tenantTypeId; Dictionary <string, long> tagNameToIds = cacheService.Get <Dictionary <string, long> >(cacheKey); long tagId = 0; if (tagNameToIds == null) { tagNameToIds = new Dictionary <string, long>(); } if (!tagNameToIds.ContainsKey(tagName) || tagNameToIds[tagName] == 0) { Sql sql = Sql.Builder; sql.Select("TagId") .From("tn_Tags") .Where("TagName = @0", StringUtility.StripSQLInjection(tagName)) .Where("TenantTypeId = @0", tenantTypeId); PetaPocoDatabase dao = CreateDAO(); tagId = dao.ExecuteScalar <long>(sql); tagNameToIds[tagName] = tagId; cacheService.Set(cacheKey, tagNameToIds, CachingExpirationType.UsualObjectCollection); } else { tagId = tagNameToIds[tagName]; } return(tagId == 0 ? null : Get(tagId)); }
/// <summary> /// 获取未使用的Url别名 /// </summary> /// <param name="aliases">Url别名集合</param> /// <param name="url">待处理的Url</param> /// <param name="urlExists">带处理Url是否已存在</param> public string GetUnusedAlias(string[] aliases, string url, out bool urlExists) { urlExists = false; if (aliases == null || aliases.Length == 0 || string.IsNullOrEmpty(url)) { return(string.Empty); } var sql = PetaPoco.Sql.Builder; string alias = string.Empty; PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); //判断Url是否存在 sql.Select("Alias") .From("tn_ShortUrls") .Where("Url = @0", url); alias = dao.FirstOrDefault <string>(sql); if (!string.IsNullOrEmpty(alias)) { urlExists = true; } if (!urlExists) { foreach (string item in aliases) { sql = Sql.Builder; sql.Select("count(Alias)") .From("tn_ShortUrls") .Where("Alias = @0", item); if (dao.ExecuteScalar <int>(sql) == 0) { alias = item; break; } } } dao.CloseSharedConnection(); return(alias); }
/// <summary> /// 创建实体 /// </summary> /// <param name="entity">待创建实体</param> /// <returns></returns> public override object Insert(T entity) { Sql sql = Sql.Builder; sql.Append("select count(*) from tn_Tags where TagName = @0 and TenantTypeId = @1", entity.TagName, entity.TenantTypeId); PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); object id = 0; if (dao.ExecuteScalar <int>(sql) == 0) { id = base.Insert(entity); } dao.CloseSharedConnection(); return(id); }
/// <summary> /// 添加标签与拥有者关联 /// </summary> /// <param name="tagInOwner">待创建实体</param> /// <returns>返回实体主键</returns> public long AddTagInOwner(TagInOwner tagInOwner) { if (tagInOwner == null || string.IsNullOrEmpty(tagInOwner.TagName) || string.IsNullOrEmpty(tagInOwner.TenantTypeId) || tagInOwner.OwnerId < 0) { return(0); } int affectCount = 0; long id = 0; PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); var sql = PetaPoco.Sql.Builder; sql.Select("count(*)") .From("tn_TagsInOwners") .Where("OwnerId = @0", tagInOwner.OwnerId) .Where("TagName = @0", tagInOwner.TagName) .Where("TenantTypeId = @0", tagInOwner.TenantTypeId); affectCount = dao.ExecuteScalar <int>(sql); if (affectCount == 0) { long.TryParse(base.Insert(tagInOwner).ToString(), out id); } dao.CloseSharedConnection(); if (id > 0) { RealTimeCacheHelper.IncreaseAreaVersion("OwnerId", tagInOwner.OwnerId); } return(id); }
/// <summary> /// 保存设置 /// </summary> /// <param name="settings">settings</param> public void Save(TSettingsEntity settings) { string classType = GetClassType(); string settingsXml = Serialize(settings); var sql = PetaPoco.Sql.Builder; sql.Select("count(ClassType)").From("tn_Settings").Where("ClassType=@0", classType); RWLock.EnterWriteLock(); PetaPocoDatabase database = CreateDAO(); try { database.OpenSharedConnection(); int count = database.ExecuteScalar <int>(sql); sql = PetaPoco.Sql.Builder; if (count > 0) { sql.Append("update tn_Settings set Settings=@0 where ClassType=@1", settingsXml, classType); } else { sql.Append("insert into tn_Settings (ClassType,Settings) values (@0,@1)", classType, settingsXml); } database.Execute(sql); } finally { database.CloseSharedConnection(); RWLock.ExitWriteLock(); } cacheService.Set(RealTimeCacheHelper.GetCacheKeyOfEntity(classType), settings, CachingExpirationType.RelativelyStable); RealTimeCacheHelper.IncreaseEntityCacheVersion(classType); }
/// <summary> /// 添加标签与拥有者关联 /// </summary> /// <param name="tagName">标签名</param> /// <param name="tenantTypeId">租户类型Id</param> /// <param name="ownerId">拥有者Id</param> /// <returns>返回影响行数</returns> public int AddTagInOwner(string tagName, string tenantTypeId, long ownerId) { int affectCount = 0; if (!string.IsNullOrEmpty(tagName) && !string.IsNullOrEmpty(tenantTypeId) && ownerId >= 0) { PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); var sql = PetaPoco.Sql.Builder; sql.Select("count(*)") .From("tn_TagsInOwners") .Where("OwnerId = @0", ownerId) .Where("TagName = @0", tagName) .Where("TenantTypeId = @0", tenantTypeId); affectCount = dao.ExecuteScalar <int>(sql); if (affectCount == 0) { sql = PetaPoco.Sql.Builder; sql.Append("INSERT INTO tn_TagsInOwners (OwnerId, TagName, TenantTypeId) VALUES (@0,@1,@2)", ownerId, tagName, tenantTypeId); affectCount = dao.Execute(sql); if (affectCount > 0) { RealTimeCacheHelper.IncreaseAreaVersion("OwnerId", ownerId); } } else { affectCount = 0; } dao.CloseSharedConnection(); } return(affectCount); }
/// <summary> /// 对操作对象进行星级评价操作 /// </summary> /// <param name="objectId">操作对象Id</param> /// <param name="tenantTypeId">租户类型Id</param> /// <param name="userId">用户的UserId</param> /// <param name="rateNumber">星级类型</param> /// <param name="ownerId">拥有者Id</param> public bool Rated(long objectId, string tenantTypeId, long userId, int rateNumber, long ownerId = 0) { RatingSettings ratingSettings = ratingSettingsManager.Get(); //执行Sql语句的集合 List <Sql> sqls = new List <Sql>(); //判断输入的等级数是否符合标准 if (rateNumber < 1 || rateNumber > 5) { return(false); } PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); //先判断是否存在评价信息 var sql = Sql.Builder; sql.Select("Count(*)") .From("tn_Ratings") .Where("ObjectId = @0 AND TenantTypeId = @1", objectId, tenantTypeId); if (ownerId > 0) { sql.Where("OwnerId = @0", ownerId); } int affectCount = dao.ExecuteScalar <int>(sql); //如果记录为0则不存在则先创建评价信息 if (affectCount == 0) { //创建 评价信息 sqls.Add(Sql.Builder.Append("INSERT INTO tn_Ratings (ObjectId, TenantTypeId, OwnerId, RateCount, Comprehensive , RateSum ) VALUES (@0, @1, @2,1,@3,@4)", objectId, tenantTypeId, ownerId, rateNumber, rateNumber)); //创建时直接添加新记录数据 for (int i = 1; i <= 5; i++) { if (rateNumber == i) { //如果等级是用户所选择的等级则直接插入数据 并且创建记录 sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingGrades (ObjectId, TenantTypeId, RateNumber, RateCount) VALUES (@0,@1,@2,1)", objectId, tenantTypeId, i)); sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingRecords (ObjectId, TenantTypeId, RateNumber, UserId, DateCreated) VALUES (@0,@1,@2,@3,@4)", objectId, tenantTypeId, i, userId, DateTime.UtcNow)); continue; } sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingGrades (ObjectId, TenantTypeId, RateNumber, RateCount) VALUES (@0,@1,@2,@3)", objectId, tenantTypeId, i, 0)); } } else { //判断是都有过评价记录 sql = Sql.Builder; sql.Select("*") .From("tn_RatingRecords") .Where("ObjectId = @0 AND TenantTypeId = @1 AND UserId = @2", objectId, tenantTypeId, userId); //获取评价的记录 RatingRecord ratingRecord = dao.FirstOrDefault <RatingRecord>(sql); if (ratingRecord == null) { sqls.Add(Sql.Builder.Append("INSERT INTO tn_RatingRecords (ObjectId, TenantTypeId, RateNumber, UserId, DateCreated) VALUES (@0,@1,@2,@3,@4)", objectId, tenantTypeId, rateNumber, userId, DateTime.UtcNow)); //更新信息评价数据 sqls.Add(Sql.Builder.Append("UPDATE tn_Ratings SET RateCount = RateCount + 1, Comprehensive=(RateSum + @0)/(RateCount + 1.0) , RateSum=RateSum + @0 where ObjectId = @1 and TenantTypeId = @2 and OwnerId = @3", rateNumber, objectId, tenantTypeId, ownerId)); //更新评价等级统计 sqls.Add(Sql.Builder.Append("UPDATE tn_RatingGrades SET RateCount = RateCount + 1 where ObjectId = @0 and TenantTypeId = @1 and RateNumber = @2", objectId, tenantTypeId, rateNumber)); } else { //用户 再次评价 先判断是否可修改 if (ratingSettings.IsModify) { //先检测是否是之前选过的等级 if (ratingRecord.RateNumber != rateNumber) { sqls.Add(Sql.Builder.Append("UPDATE tn_RatingRecords SET RateNumber = @0 WHERE ObjectId = @1 AND TenantTypeId = @2 AND UserId = @3", rateNumber, objectId, tenantTypeId, userId)); //先更新之前的等级统计 sqls.Add(Sql.Builder.Append("UPDATE tn_RatingGrades SET RateCount = RateCount - 1 where ObjectId = @0 and TenantTypeId = @1 and RateNumber = @2", objectId, tenantTypeId, ratingRecord.RateNumber)); //然后更新现在的最新等级统计 sqls.Add(Sql.Builder.Append("UPDATE tn_RatingGrades SET RateCount = RateCount + 1 where ObjectId = @0 and TenantTypeId = @1 and RateNumber = @2", objectId, tenantTypeId, rateNumber)); //之后更新评价信息表 sqls.Add(Sql.Builder.Append("UPDATE tn_Ratings SET Comprehensive = (RateSum + @0 - @1)/RateCount , RateSum=RateSum + @0 - @1 where ObjectId=@2 and TenantTypeId = @3 and OwnerId = @4", (float)rateNumber, (float)ratingRecord.RateNumber, objectId, tenantTypeId, ownerId)); } } } } if (sqls == null) { return(false); } using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(RatingRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); EntityData.ForType(typeof(RatingGrade)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); } } else { return(false); } dao.CloseSharedConnection(); return(true); }