/// <summary> /// 创建提醒记录 /// </summary> /// <param name="userId">被提醒用户Id</param> /// <param name="reminderMode">提醒方式</param> /// <param name="reminderInfoType">提醒信息类型</param> /// <param name="objectIds">提醒对象Id集合</param> public void CreateRecords(long userId, int reminderMode, int reminderInfoType, IEnumerable <long> objectIds) { PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); var sql = Sql.Builder; sql.Select("ObjectId") .From("tn_ReminderRecords") .Where("UserId=@0", userId) .Where("ReminderModeId=@0", reminderMode) .Where("ReminderInfoTypeId=@0", reminderInfoType); IEnumerable <object> oldObjectIds_object = dao.FetchFirstColumn(sql); IEnumerable <long> oldObjectIds = oldObjectIds_object.Cast <long>(); foreach (var objectId in objectIds) { if (!oldObjectIds.Contains(objectId)) { ReminderRecord record = ReminderRecord.New(); record.UserId = userId; record.ReminderModeId = reminderMode; record.ReminderInfoTypeId = reminderInfoType; record.ObjectId = objectId; dao.Insert(record); } } dao.CloseSharedConnection(); //更新缓存 RealTimeCacheHelper.IncreaseAreaVersion("UserId", userId); }
/// <summary> /// 执行队列 /// </summary> public void ExecQueue() { PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); var tempCreateVisitQueue = new Queue <long>(CreateVisitQueue); CreateVisitQueue.Clear(); while (tempCreateVisitQueue.Count > 0) { Visit visit = Get(tempCreateVisitQueue.Dequeue()); if (visit == null) { continue; } dao.Insert(visit); } //将队列中的数据更新到数据库 var tempUpdateVisitQueue = new Queue <long>(UpdateVisitQueue); UpdateVisitQueue.Clear(); while (tempUpdateVisitQueue.Count > 0) { Visit visit = Get(tempUpdateVisitQueue.Dequeue()); if (visit == null) { continue; } dao.Update(visit); } dao.CloseSharedConnection(); }
/// <summary> /// 创建分类 /// </summary> /// <param name="category">待创建分类</param> /// <returns></returns> public override object Insert(T category) { PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); long primaryKey = Convert.ToInt64(dao.Insert(category)); if (primaryKey > 0) { List <Sql> sqls = new List <Sql>(); if (category.ParentId > 0) { sqls.Add(Sql.Builder.Append("Update tn_Categories set ChildCount = ChildCount + 1 Where CategoryId = @0", category.ParentId)); } sqls.Add(Sql.Builder.Append("update tn_Categories set DisplayOrder = @0 where CategoryId = @0", category.CategoryId)); int affectCount = dao.Execute(sqls); category.DisplayOrder = category.CategoryId; base.OnInserted(category); } dao.CloseSharedConnection(); return(primaryKey); }
/// <summary> /// 保存用户自定义风格 /// </summary> /// <param name="presentAreaKey">呈现区域标识</param> /// <param name="ownerId">OwnerId</param> /// <param name="customStyle">自定义风格实体</param> public void Save(string presentAreaKey, long ownerId, CustomStyle customStyle) { PetaPocoDatabase database = CreateDAO(); database.OpenSharedConnection(); CustomStyleEntity entity = Get(presentAreaKey, ownerId); string customStyleXml = Serialize(customStyle); if (entity != null) { customStyle.LastModified = DateTime.UtcNow; entity.CustomStyle = customStyle; entity.SerializedCustomStyle = customStyleXml; entity.LastModified = DateTime.UtcNow; database.Update(entity); } else { entity = CustomStyleEntity.New(); entity.CustomStyle = customStyle; entity.PresentAreaKey = presentAreaKey; entity.OwnerId = ownerId; entity.SerializedCustomStyle = customStyleXml; database.Insert(entity); } database.CloseSharedConnection(); cacheService.Set(GetCacheKey_CustomStyleEntity(presentAreaKey, ownerId), entity, CachingExpirationType.UsualSingleObject); }
/// <summary> /// 更新内容隐私设置 /// </summary> /// <param name="privacyable">可隐私接口</param> /// <param name="specifyObjects"><remarks>key=specifyObjectTypeId,value=内容指定对象集合</remarks></param> public void UpdatePrivacySettings(IPrivacyable privacyable, Dictionary <int, IEnumerable <ContentPrivacySpecifyObject> > specifyObjects) { //回复:已修改 var sql = Sql.Builder; //List<ContentPrivacySpecifyObject> userPrivacySpecifyObjects; //IEnumerable<ContentPrivacySpecifyObject> pairs; //IEnumerable<ContentPrivacySpecifyObject> deletePairs; //IEnumerable<ContentPrivacySpecifyObject> insertPairs; PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); sql.Append("delete from tn_ContentPrivacySpecifyObjects") .Where("ContentId = @0", privacyable.ContentId) .Where("TenantTypeId = @0", privacyable.TenantTypeId); dao.Execute(sql); if (specifyObjects == null) { specifyObjects = new Dictionary <int, IEnumerable <ContentPrivacySpecifyObject> >(); } foreach (var item in specifyObjects) { //sql = Sql.Builder; //done:zhangp,by zhengw:SpecifyObjectId=>SpecifyObjectTypeId //回复:已修改 foreach (var insertPair in item.Value) { dao.Insert(insertPair); } //sql.Select("*") // .From("tn_ContentPrivacySpecifyObjects") // .Where("ContentId = @0", privacyable.ContentId) // .Where("TenantTypeId = @0", privacyable.TenantTypeId) // .Where("SpecifyObjectTypeId = @0", item.Key); //userPrivacySpecifyObjects = dao.Fetch<ContentPrivacySpecifyObject>(sql); //done:zhangp,by zhengw:userPrivacySpecifyObjects.Count为0时就不更新了?我觉得可以把这个判断去掉,两个边界情况都可以使用求交集的办法 //回复:已修改 //pairs = userPrivacySpecifyObjects.Intersect(item.Value); //deletePairs = userPrivacySpecifyObjects.Except(pairs); //foreach (var deletePair in deletePairs) // dao.Delete(deletePair); //insertPairs = item.Value.Except(pairs); //foreach (var insertPair in insertPairs) // dao.Insert(insertPair); } dao.CloseSharedConnection(); RealTimeCacheHelper.IncreaseAreaVersion("TenantTypeId", privacyable.TenantTypeId); }
/// <summary> /// 用户批量更新提醒设置 /// </summary> /// <param name="userId">用户Id</param> /// <param name="userReminderSettings">用户提醒设置集合</param> public void BatchUpdateUserReminderSettings(long userId, IEnumerable <UserReminderSettings> userReminderSettings) { PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); bool hasSetting = false; var sql = PetaPoco.Sql.Builder; sql.Select("Id") .From("tn_UserReminderSettings") .Where("userId=@0", userId); List <long> ids = dao.Fetch <long>(sql); IEnumerable <UserReminderSettings> oldUserReminderSettings = PopulateEntitiesByEntityIds(ids); foreach (var userReminderSetting in userReminderSettings) { foreach (var oldUserReminderSetting in oldUserReminderSettings) { if (oldUserReminderSetting.ReminderInfoTypeId == userReminderSetting.ReminderInfoTypeId && oldUserReminderSetting.ReminderModeId == userReminderSetting.ReminderModeId) { hasSetting = true; if (oldUserReminderSetting.ReminderThreshold == userReminderSetting.ReminderThreshold && oldUserReminderSetting.IsEnabled == userReminderSetting.IsEnabled && oldUserReminderSetting.IsRepeated == userReminderSetting.IsRepeated && oldUserReminderSetting.RepeatInterval == userReminderSetting.RepeatInterval) { break; } else { oldUserReminderSetting.IsEnabled = userReminderSetting.IsEnabled; oldUserReminderSetting.IsRepeated = userReminderSetting.IsRepeated; oldUserReminderSetting.ReminderThreshold = userReminderSetting.ReminderThreshold; oldUserReminderSetting.RepeatInterval = userReminderSetting.RepeatInterval; dao.Update(oldUserReminderSetting); break; } } } if (!hasSetting) { dao.Insert(userReminderSetting); } hasSetting = false; } dao.CloseSharedConnection(); RealTimeCacheHelper.IncreaseAreaVersion("UserId", userId); }
/// <summary> /// 把用户加入到一组角色中 /// </summary> /// <param name="userId">用户Id</param> /// <param name="roleNames">赋予用户的用户角色</param> public void AddUserToRoles(long userId, List <string> roleNames) { PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); RemoveUserRoles(userId); var sqlInsert = Sql.Builder; UserInRole userInRole = new UserInRole(); userInRole.UserId = userId; foreach (var roleName in roleNames) { userInRole.RoleName = roleName; dao.Insert(userInRole); } dao.CloseSharedConnection(); //增加版本 RealTimeCacheHelper.IncreaseAreaVersion("UserId", userId); }
/// <summary> /// 变更应用数据 /// </summary> /// <param name="applicationId">应用Id</param> /// <param name="dataKey">数据标识</param> /// <param name="value">待变更的值</param> public void Change(int applicationId, string dataKey, string value) { PetaPocoDatabase dao = CreateDAO(); ApplicationData applicationData = Get(applicationId, dataKey); if (applicationData == null) { applicationData = new ApplicationData(); applicationData.ApplicationId = applicationId; applicationData.Datakey = dataKey; applicationData.TenantTypeId = string.Empty; applicationData.StringValue = value; dao.Insert(applicationData); } else { applicationData.StringValue = value; dao.Update(applicationData); } RealTimeCacheHelper.IncreaseEntityCacheVersion(applicationData); }
/// <summary> /// 变更系统数据 /// </summary> /// <param name="dataKey">数据标识</param> /// <param name="number">待变更的数值</param> public void Change(string dataKey, long number) { //当DataKey不存在时,插入新数据 PetaPocoDatabase dao = CreateDAO(); SystemData systemData = Get(dataKey); if (systemData == null) { systemData = new SystemData(); systemData.Datakey = dataKey; systemData.LongValue = number; dao.Insert(systemData); } else { systemData.LongValue += number; dao.Update(systemData); } RealTimeCacheHelper.IncreaseEntityCacheVersion(systemData); }
/// <summary> /// 变更系统数据 /// </summary> /// <param name="ownerId">ownerId</param> /// <param name="tenantTypeId">租户类型Id</param> /// <param name="dataKey">数据标识</param> /// <param name="value">待变更的数值</param> public void Change(long ownerId, string tenantTypeId, string dataKey, decimal value) { PetaPocoDatabase dao = CreateDAO(); OwnerData ownerData = Get(ownerId, tenantTypeId, dataKey); if (ownerData == null || ownerData.Id == 0) { ownerData = OwnerData.New(); ownerData.OwnerId = ownerId; ownerData.Datakey = dataKey; ownerData.DecimalValue = value; ownerData.TenantTypeId = tenantTypeId; Sql sql = Sql.Builder; sql.Append("update tn_OwnerData set DecimalValue = @3 where DataKey = @0 and OwnerId = @1 and TenantTypeId = @2", dataKey, ownerId, tenantTypeId, value); int affectCount = dao.Execute(sql); if (affectCount == 0) { dao.Insert(ownerData); } } else { ownerData.DecimalValue = ownerData.DecimalValue + value > 0 ? ownerData.DecimalValue + value : 0; dao.Update(ownerData); } //done:libsh,by zhengw: 同上 //replay:已修改 string cacheKey = GetCacheKey_GetOwnerData(ownerId, dataKey, tenantTypeId); OwnerData cacheOwnerdata = cacheService.Get <OwnerData>(cacheKey); if (cacheOwnerdata != null) { cacheService.Set(cacheKey, ownerData, CachingExpirationType.SingleObject); } }
/// <summary> /// 刷新数据库 /// </summary> /// <remarks> /// 通过Task调用 /// </remarks> public void Refresh(ConcurrentDictionary <string, OnlineUser> OnlineUsersForProcess) { //设计要点: //1、把OnlineUsersForProcess更新到数据库,如果UserName存在则更新LastActivityTime,否则添加新在线用户 // 可参考CountRepository.ExecQueue() //2、把超期未活动的用户移除 //3、更新tn_OnlineUserStatistics : 每日一条记录保留最高记录(依据UserCount) PetaPocoDatabase dao = CreateDAO(); try { dao.OpenSharedConnection(); foreach (string key in OnlineUsersForProcess.Keys) { OnlineUser onlineUserInDic = null; OnlineUsersForProcess.TryRemove(key, out onlineUserInDic); var sql = Sql.Builder; sql.Select("*") .From("tn_OnlineUsers") .Where("UserName = @0", key); OnlineUser onlineUserInDB = dao.FirstOrDefault <OnlineUser>(sql); if (onlineUserInDB == null) { dao.Insert(onlineUserInDic); } else { sql = Sql.Builder; sql.Append("Update tn_OnlineUsers set LastActivityTime=@0,LastAction=@1", onlineUserInDic.LastActivityTime, onlineUserInDic.LastAction) .Where("UserName=@0", key); dao.Execute(sql); } dao.Execute(Sql.Builder.Append("Update tn_Users set LastActivityTime=@0,LastAction=@1,IpLastActivity=@2", onlineUserInDic.LastActivityTime, onlineUserInDic.LastAction, onlineUserInDic.Ip)); } //移除超期未活动的用户 UserSettings userSetting = DIContainer.Resolve <IUserSettingsManager>().Get(); var deleteSql = Sql.Builder; deleteSql.Append("delete from tn_OnlineUsers") .Where("LastActivityTime < @0", DateTime.UtcNow.AddMinutes(-userSetting.UserOnlineTimeWindow)); dao.Execute(deleteSql); //更新tn_OnlineUserStatistics : 每日一条记录保留最高记录(依据UserCount) int loggedUserCount = GetLoggedUsers().Count(); int anonymousUserCount = GetAnonymousUsers().Count(); int total = loggedUserCount + anonymousUserCount; var updateSql = Sql.Builder; updateSql.Append("update tn_OnlineUserStatistics set LoggedUserCount=@0,AnonymousCount=@1,UserCount = @2", loggedUserCount, anonymousUserCount, total) .Where("UserCount < @0", total) .Where("DateCreated>@0", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day)); int num = dao.Execute(updateSql); if (num == 0) { var selectSql = Sql.Builder; selectSql.Select("*") .From("tn_OnlineUserStatistics") .Where("DateCreated>@0", new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day)); OnlineUserStatistic statistic = dao.FirstOrDefault <OnlineUserStatistic>(selectSql); if (statistic == null) { OnlineUserStatistic userStatistic = OnlineUserStatistic.New(); userStatistic.DateCreated = DateTime.UtcNow; userStatistic.LoggedUserCount = loggedUserCount; userStatistic.AnonymousCount = anonymousUserCount; userStatistic.UserCount = total; dao.Insert(userStatistic); } } } finally { dao.CloseSharedConnection(); } }
/// <summary> /// 对操作对象进行顶操作 /// </summary> /// <param name="objectId">操作对象Id</param> /// <param name="tenantTypeId">租户类型Id</param> /// <param name="userId">操作用户Id</param> /// <param name="mode">顶踩的操作模式</param> /// <returns>是否操作成功,Ture-成功</returns> public bool Support(long objectId, string tenantTypeId, long userId, AttitudeMode mode = AttitudeMode.Bidirection) { PetaPocoDatabase dao = CreateDAO(); dao.OpenSharedConnection(); AttitudeSettings attitudeSettings = attitudeSettingsManager.Get(); AttitudeOnlySupportSettings attitudeOnlySupportSettings = attitudeOnlySupportSettingsManager.Get(); bool returnValue = false; var sql = Sql.Builder; sql.Where("ObjectId = @0 and TenantTypeId = @1", objectId, tenantTypeId); Attitude entity = dao.FirstOrDefault <Attitude>(sql); if (entity == null) { entity = Attitude.New(); entity.TenantTypeId = tenantTypeId; entity.ObjectId = objectId; dao.Insert(entity); } #region 判断是否双向 if (AttitudeMode.Bidirection == mode) { #region 判断是否可修改 //判断是否可修改 if (attitudeSettings.IsModify) { #region 判断是否取消 //判断是否可取消 if (attitudeSettings.EnableCancel) { IList <Sql> sqls = new List <Sql>(); int affectCount = 0; //判断是否有过操作记录 bool?isSupport = IsSupport(objectId, tenantTypeId, userId); switch (isSupport) { //顶过记录 case true: returnValue = false; break; //踩过记录 case false: sqls.Add(Sql.Builder.Append(@"DELETE FROM tn_AttitudeRecords WHERE (UserId = @0) AND (TenantTypeId = @1) and (ObjectId = @2)" , userId, tenantTypeId, objectId)); sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET OpposeCount = OpposeCount - 1, Comprehensive = Comprehensive - @0 WHERE (ObjectId = @1) AND (TenantTypeId = @2)" , attitudeSettings.OpposeWeights, objectId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); //清除顶踩状态缓存 cacheService.Remove(GetCacheKey_IsSupport(objectId, tenantTypeId, userId)); } returnValue = true; } else { returnValue = false; } break; default: sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET SupportCount = SupportCount + 1, Comprehensive = Comprehensive + @0 WHERE (ObjectId = @1) AND (TenantTypeId = @2)" , attitudeSettings.SupportWeights, objectId, tenantTypeId)); sqls.Add(Sql.Builder.Append(@"INSERT INTO tn_AttitudeRecords (ObjectId, UserId, TenantTypeId, IsSupport) VALUES (@0, @1, @2, 1)" , objectId, userId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); cacheService.Set(GetCacheKey_IsSupport(objectId, tenantTypeId, userId), true, CachingExpirationType.SingleObject); } returnValue = true; } else { returnValue = false; } break; } } else { IList <Sql> sqls = new List <Sql>(); int affectCount = 0; //判断是否有过操作记录 bool?isSupport = IsSupport(objectId, tenantTypeId, userId); switch (isSupport) { //已经顶过 case true: returnValue = false; break; //已经踩过 case false: sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET OpposeCount = OpposeCount - 1 ,SupportCount = SupportCount + 1, Comprehensive = Comprehensive + @0 - @1 WHERE (ObjectId = @2) AND (TenantTypeId = @3)" , attitudeSettings.SupportWeights, attitudeSettings.OpposeWeights, objectId, tenantTypeId )); sqls.Add(Sql.Builder.Append(@"UPDATE tn_AttitudeRecords SET IsSupport =1 WHERE(ObjectId = @0) AND (UserId = @1) AND (TenantTypeId = @2)" , objectId, userId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); cacheService.Set(GetCacheKey_IsSupport(objectId, tenantTypeId, userId), true, CachingExpirationType.SingleObject); } returnValue = true; } else { returnValue = false; } break; default: //没有操作过的记录就 添加记录 sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET SupportCount = SupportCount + 1, Comprehensive = Comprehensive + @0 WHERE (ObjectId = @1) AND (TenantTypeId = @2)" , attitudeSettings.SupportWeights, objectId, tenantTypeId)); sqls.Add(Sql.Builder.Append(@"INSERT INTO tn_AttitudeRecords (ObjectId, UserId, TenantTypeId, IsSupport) VALUES (@0, @1, @2, 1)" , objectId, userId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); cacheService.Set(GetCacheKey_IsSupport(objectId, tenantTypeId, userId), true, CachingExpirationType.SingleObject); } returnValue = true; } else { returnValue = false; } break; } } #endregion 判断是否取消 } else { //先判断一下是否有顶踩记录 IList <Sql> sqls = new List <Sql>(); int affectCount = 0; //判断是否有过操作记录 bool?isSupport = IsSupport(objectId, tenantTypeId, userId); if (isSupport == null) { //没有记录就 添加记录 sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET SupportCount = SupportCount + 1, Comprehensive = Comprehensive + @0 WHERE (ObjectId = @2) AND (TenantTypeId = @3)" , attitudeSettings.SupportWeights, attitudeSettings.OpposeWeights, objectId, tenantTypeId )); sqls.Add(Sql.Builder.Append(@"INSERT INTO tn_AttitudeRecords (ObjectId, UserId, TenantTypeId, IsSupport) VALUES (@0, @1, @2, 1)" , objectId, userId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); cacheService.Set(GetCacheKey_IsSupport(objectId, tenantTypeId, userId), true, CachingExpirationType.SingleObject); } returnValue = true; } else { returnValue = false; } } } #endregion 判断是否可修改 } else { //单向 #region 是否可取消操作 //是否可取消操作 if (attitudeOnlySupportSettings.IsCancel) { IList <Sql> sqls = new List <Sql>(); int affectCount = 0; //判断是否有过操作记录 bool?isSupport = IsSupport(objectId, tenantTypeId, userId); switch (isSupport) { //顶过记录 case true: sqls.Add(Sql.Builder.Append(@"DELETE FROM tn_AttitudeRecords WHERE UserId = @0 and TenantTypeId = @1 and ObjectId = @2" , userId, tenantTypeId, objectId)); sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET SupportCount = SupportCount-1, Comprehensive = SupportCount-1 WHERE ObjectId = @0 and TenantTypeId = @1", objectId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); //清除顶踩状态缓存 cacheService.Remove(GetCacheKey_IsSupport(objectId, tenantTypeId, userId)); } returnValue = true; } else { returnValue = false; } break; default: sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET SupportCount = SupportCount+1, Comprehensive = SupportCount+1 WHERE (ObjectId = @0) AND (TenantTypeId = @1)" , objectId, tenantTypeId)); sqls.Add(Sql.Builder.Append(@"INSERT INTO tn_AttitudeRecords (ObjectId, UserId, TenantTypeId, IsSupport) VALUES (@0, @1, @2, 1)" , objectId, userId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); cacheService.Set(GetCacheKey_IsSupport(objectId, tenantTypeId, userId), true, CachingExpirationType.SingleObject); } returnValue = true; } else { returnValue = false; } break; } } else { //不可取消的单向操作 IList <Sql> sqls = new List <Sql>(); int affectCount = 0; //判断是否有过操作记录 bool?isSupport = IsSupport(objectId, tenantTypeId, userId); if (isSupport == null) { sqls.Add(Sql.Builder.Append(@"UPDATE tn_Attitudes SET SupportCount = SupportCount + 1, Comprehensive = SupportCount + 1 WHERE (ObjectId = @0) AND (TenantTypeId = @1)" , objectId, tenantTypeId)); sqls.Add(Sql.Builder.Append(@"INSERT INTO tn_AttitudeRecords (ObjectId, UserId, TenantTypeId, IsSupport) VALUES (@0, @1, @2, 1)" , objectId, userId, tenantTypeId)); using (var transaction = dao.GetTransaction()) { affectCount = dao.Execute(sqls); transaction.Complete(); } if (affectCount > 0) { if (RealTimeCacheHelper.EnableCache) { EntityData.ForType(typeof(AttitudeRecord)).RealTimeCacheHelper.IncreaseAreaVersion("ObjectId", objectId); //更新缓存 Get(objectId, tenantTypeId, false); cacheService.Set(GetCacheKey_IsSupport(objectId, tenantTypeId, userId), true, CachingExpirationType.SingleObject); } returnValue = true; } else { returnValue = false; } } } #endregion 是否可取消操作 } #endregion 判断是否双向 dao.CloseSharedConnection(); return(returnValue); }
/// <summary> /// 更新积分统计 /// </summary> /// <param name="userId">用户Id</param> /// <param name="pointCategory2PointsDictionary"><remarks>key=PointCategory,value=Points</remarks>积分分类-积分字典</param> /// <returns>修订后应获取到的积分值</returns> public Dictionary <string, int> UpdateStatistic(long userId, Dictionary <PointCategory, int> pointCategory2PointsDictionary) { Dictionary <string, int> dictionary = new Dictionary <string, int>(); RWLock.EnterWriteLock(); PetaPocoDatabase dao = CreateDAO(); try { dao.OpenSharedConnection(); //1、检查当日积分统计是否存在,不存在创建 //2、检查是否超过当日限额,如果未超过更新当日积分累计 var sql = Sql.Builder; sql.Select("*") .From("tn_PointStatistics") .Where("UserId = @0", userId) .Where("StatisticalYear = @0", DateTime.UtcNow.Year) .Where("StatisticalMonth = @0", DateTime.UtcNow.Month) .Where("StatisticalDay = @0", DateTime.UtcNow.Day); IEnumerable <PointStatistic> pointStatistices = dao.Fetch <PointStatistic>(sql); //初始化 foreach (var pair in pointCategory2PointsDictionary) { dictionary[pair.Key.CategoryKey] = pair.Value; } //当日积分统计不存在 if (pointStatistices == null || pointStatistices.Count() == 0) { //创建当日积分统计 foreach (var pair in pointCategory2PointsDictionary) { if (pair.Key.QuotaPerDay <= 0) { continue; } var pointStatistic = PointStatistic.New(); pointStatistic.UserId = userId; pointStatistic.PointCategoryKey = pair.Key.CategoryKey; pointStatistic.Points = pair.Value; dao.Insert(pointStatistic); } } else { //检查是积分限额,调整用户最终获取到的积分 foreach (var pair in pointCategory2PointsDictionary) { if (pair.Key.QuotaPerDay <= 0) { continue; } var category = pair.Key; var pointStatistic = pointStatistices.FirstOrDefault(n => n.PointCategoryKey == category.CategoryKey); if (pointStatistic == null) { continue; } if (pair.Value > 0 && pointStatistic.Points + pair.Value > category.QuotaPerDay)//超过限额 { dictionary[pair.Key.CategoryKey] = 0; } else { pointStatistic.Points += pair.Value; dao.Update(pointStatistic); } } } } finally { dao.CloseSharedConnection(); RWLock.ExitWriteLock(); } //更新用户分区缓存 RealTimeCacheHelper.IncreaseAreaVersion("UserId", userId); return(dictionary); }