/// <summary> /// 根据指定积分分类获取积分项目中的积分 /// </summary> /// <param name="pointItem">积分项目</param> /// <returns><remarks>key=PointCategory,value=Points</remarks>积分分类-积分字典</returns> private Dictionary <PointCategory, int> GetPointCategory2PointsDictionary(PointItem pointItem) { Dictionary <PointCategory, int> dictionary = new Dictionary <PointCategory, int>(); foreach (var category in GetPointCategories()) { int points = 0; if (category.CategoryKey == PointCategoryKeys.Instance().ExperiencePoints()) { points = pointItem.ExperiencePoints; } else if (category.CategoryKey == PointCategoryKeys.Instance().ReputationPoints()) { points = pointItem.ReputationPoints; } else if (category.CategoryKey == PointCategoryKeys.Instance().TradePoints()) { points = pointItem.TradePoints; } else if (category.CategoryKey == "TradePoints2") { points = pointItem.TradePoints2; } else if (category.CategoryKey == "TradePoints3") { points = pointItem.TradePoints3; } else if (category.CategoryKey == "TradePoints4") { points = pointItem.TradePoints4; } dictionary[category] = points; } return(dictionary); }
//关于缓存期限: //1、PointItem实体、列表 使用CachingExpirationType.RelativelyStable //2、PointCategory实体、列表 使用CachingExpirationType.RelativelyStable //3、PointRecord实体、列表 使用正常的缓存策略 //4、积分记录的所有积分类型都是0,则不创建 #region 积分变更及记录 /// <summary> /// 依据规则增减积分 /// </summary> /// <param name="userId">增减积分的UserId</param> /// <param name="pointItemKey">积分项目标识</param> /// <param name="description">积分记录描述</param> public void GenerateByRole(long userId, string pointItemKey, string description) { //1、依据pointItemKey查找积分项目,如果未找到则中断执行; PointItem pointItem = GetPointItem(pointItemKey); if (pointItem == null) { return; } if (pointItem.ExperiencePoints == 0 && pointItem.ReputationPoints == 0 && pointItem.TradePoints == 0) { return; } //2、检查用户当日各类积分是否达到限额,如果达到限额则不加积分,如果未达到则更新当日积分限额 Dictionary <string, int> dictionary = pointStatisticRepository.UpdateStatistic(userId, GetPointCategory2PointsDictionary(pointItem)); //如果用户当日各类积分都超出限额,则不产生积分 if (dictionary.Count(n => n.Value != 0) == 0) { return; } //3、按照pointItemKey对应的积分项目,生成积分记录,并对用户积分额进行增减; int experiencePoints = dictionary[PointCategoryKeys.Instance().ExperiencePoints()]; int reputationPoints = dictionary[PointCategoryKeys.Instance().ReputationPoints()]; int tradePoints = dictionary[PointCategoryKeys.Instance().TradePoints()]; int tradePoints2 = 0; int tradePoints3 = 0; int tradePoints4 = 0; if (dictionary.ContainsKey("TradePoints2")) { tradePoints2 = dictionary["TradePoints2"]; } if (dictionary.ContainsKey("TradePoints3")) { tradePoints3 = dictionary["TradePoints3"]; } if (dictionary.ContainsKey("TradePoints4")) { tradePoints4 = dictionary["TradePoints4"]; } PointRecord pointRecord = new PointRecord(userId, pointItem.ItemName, description, experiencePoints, reputationPoints, tradePoints); pointRecord.TradePoints2 = tradePoints2; pointRecord.TradePoints3 = tradePoints3; pointRecord.TradePoints4 = tradePoints4; pointRecordRepository.Insert(pointRecord); IUserService userService = DIContainer.Resolve <IUserService>(); userService.ChangePoints(userId, experiencePoints, reputationPoints, tradePoints, tradePoints2, tradePoints3, tradePoints4); CountService countService = new CountService(TenantTypeIds.Instance().User()); countService.ChangeCount(CountTypes.Instance().ReputationPointsCounts(), userId, userId, pointRecord.ReputationPoints); }
/// <summary> /// 用户和系统进行积分交易(例如:用户购买邀请码,礼品兑换) /// </summary> /// <param name="payerUserId">支付积分人UserId</param> /// <param name="points">交易积分额</param> /// <param name="description">交易描述</param> /// <param name="isImmediate">是否即时交易</param> public void TradeToSystem(long payerUserId, int points, string description, bool isImmediate) { //如果是即时交易,从支付方从交易积分扣除,否则从冻结的交易积分扣除(不足时抛出异常) if (points <= 0) { return; } //1、首先检查payerUserId是否可以支付积分交易额,如果余额不足抛出异常 IUserService userService = DIContainer.Resolve <IUserService>(); IUser payer = userService.GetUser(payerUserId); if (payer == null) { throw new ExceptionFacade(string.Format("用户“{0}”不存在或已被删除", payerUserId)); } PointCategory pointCategory = GetPointCategory(PointCategoryKeys.Instance().TradePoints()); if (pointCategory == null) { return; } if (isImmediate && payer.TradePoints < points) { throw new ExceptionFacade(string.Format("积分余额不足,仅有{0}{2}{3},不够支付{1}{2}{3}", payer.TradePoints, points, pointCategory.Unit, pointCategory.CategoryName)); } if (!isImmediate && payer.FrozenTradePoints < points) { throw new ExceptionFacade(string.Format("冻结积分余额不足,仅有{0}{2}{3},不够支付{1}{2}{3}", payer.FrozenTradePoints, points, pointCategory.Unit, pointCategory.CategoryName)); } //2、points去除交易税,分别变更交易双方的积分值,并生成积分记录 PointRecord payerPointRecord = new PointRecord(payerUserId, ResourceAccessor.GetString("Common_PointTrade"), description, 0, 0, -points); pointRecordRepository.Insert(payerPointRecord); if (isImmediate) { userService.ChangePoints(payerUserId, 0, 0, -points); } else { userService.ReduceFrozenTradePoints(payerUserId, points); } //用于积分提醒 TrackPointRecord(payerUserId, payerPointRecord); //变更系统积分 PointRecord pointRecord = new PointRecord(0, ResourceAccessor.GetString("Common_PointTrade"), description, 0, 0, points); pointRecordRepository.Insert(pointRecord); ChangeSystemTradePoints(points); }
/// <summary> /// 奖惩用户 /// </summary> /// <param name="userId">被奖惩用户</param> /// <param name="experiencePoints">经验</param> /// <param name="reputationPoints">威望</param> /// <param name="tradePoints">金币</param> /// <param name="description">奖惩理由</param> public void Reward(long userId, int experiencePoints, int reputationPoints, int tradePoints, string description) { if (experiencePoints == 0 && reputationPoints == 0 && tradePoints == 0) { return; } IUserService userService = DIContainer.Resolve <IUserService>(); IUser user = userService.GetUser(userId); if (user == null) { throw new ExceptionFacade(string.Format("用户“{0}”不存在或已被删除", userId)); } //1、增减用户积分额并生成用户积分记录; bool isIncome = experiencePoints > 0 || reputationPoints > 0 || tradePoints > 0; PointRecord pointRecord = new PointRecord(userId, isIncome ? ResourceAccessor.GetString("Common_AdminReward") : ResourceAccessor.GetString("Common_AdminPunish"), description, experiencePoints, reputationPoints, tradePoints); pointRecordRepository.Insert(pointRecord); userService.ChangePoints(userId, experiencePoints, reputationPoints, tradePoints); //2、增减系统积分额并生成系统积分记录; PointRecord systemPointRecord = new PointRecord(0, isIncome ? ResourceAccessor.GetString("Common_RewardUser") : ResourceAccessor.GetString("Common_PunishUser"), description, -experiencePoints, -reputationPoints, -tradePoints); pointRecordRepository.Insert(systemPointRecord); ChangeSystemTradePoints(-tradePoints); //3、生成操作文章 OperationLogService logService = Tunynet.DIContainer.Resolve <OperationLogService>(); IOperatorInfoGetter operatorInfoGetter = DIContainer.Resolve <IOperatorInfoGetter>(); if (operatorInfoGetter == null) { return; } OperationLogEntry logEntry = new OperationLogEntry(operatorInfoGetter.GetOperatorInfo()); logEntry.ApplicationId = 0; logEntry.Source = string.Empty; logEntry.OperationType = EventOperationType.Instance().Update(); logEntry.OperationObjectName = user.UserName; logEntry.OperationObjectId = userId; PointCategory experiencePointCategory = GetPointCategory(PointCategoryKeys.Instance().ExperiencePoints()); if (experiencePointCategory == null) { return; } PointCategory reputationPointCategory = GetPointCategory(PointCategoryKeys.Instance().ReputationPoints()); if (reputationPointCategory == null) { return; } PointCategory tradePointCategory = GetPointCategory(PointCategoryKeys.Instance().TradePoints()); if (tradePointCategory == null) { return; } logEntry.Description = string.Format("{0}“{1}”:{2}{3}{4},{5}{6}{7},{8}{9}{10}", isIncome ? ResourceAccessor.GetString("Common_RewardUser") : ResourceAccessor.GetString("Common_PunishUser"), user.UserName, experiencePoints, experiencePointCategory.Unit, experiencePointCategory.CategoryName, reputationPoints, reputationPointCategory.Unit, reputationPointCategory.CategoryName, tradePoints, tradePointCategory.Unit, tradePointCategory.CategoryName); logService.Create(logEntry); }
/// <summary> /// 积分交易 /// </summary> /// <param name="payerUserId">支付积分人UserId</param> /// <param name="payeeUserId">接收积分人UserId</param> /// <param name="points">交易积分额</param> /// <param name="description">交易描述</param> /// <param name="isImmediate">是否即时交易</param> /// <param name="needPointMessage">是否需要积分提醒</param> public void Trade(long payerUserId, long payeeUserId, int points, string description, bool isImmediate) { //如果是即时交易,从支付方从交易积分扣除,否则从冻结的交易积分扣除(不足时抛出异常) if (points <= 0) { return; } //1、首先检查payerUserId是否可以支付积分交易额,如果余额不足抛出异常 IUserService userService = DIContainer.Resolve <IUserService>(); IUser payer = userService.GetUser(payerUserId); if (payer == null) { throw new ExceptionFacade(string.Format("用户“{0}”不存在或已被删除", payerUserId)); } PointCategory pointCategory = GetPointCategory(PointCategoryKeys.Instance().TradePoints()); if (pointCategory == null) { return; } if (isImmediate && payer.TradePoints < points) { throw new ExceptionFacade(string.Format("积分余额不足,仅有{0}{2}{3},不够支付{1}{2}{3}", payer.TradePoints, points, pointCategory.Unit, pointCategory.CategoryName)); } if (!isImmediate && payer.FrozenTradePoints < points) { throw new ExceptionFacade(string.Format("冻结积分余额不足,仅有{0}{2}{3},不够支付{1}{2}{3}", payer.FrozenTradePoints, points, pointCategory.Unit, pointCategory.CategoryName)); } IUser payee = userService.GetUser(payeeUserId); if (payee == null) { throw new ExceptionFacade(string.Format("用户“{0}”不存在或已被删除", payeeUserId)); } //2、检查是否需要缴纳交易税,如果需要,则创建系统积分记录,变更系统积分总额 PointSettings pointSettings = pointSettingsManager.Get(); int realPoints = points; if (pointSettings.TransactionTax > 0 && pointSettings.TransactionTax < 100) { realPoints = points * (100 - pointSettings.TransactionTax) / 100; int taxPoints = points - realPoints; if (taxPoints > 0) { PointRecord pointRecord = new PointRecord(0, ResourceAccessor.GetString("Common_TransactionTax"), description, 0, 0, taxPoints); pointRecordRepository.Insert(pointRecord); ChangeSystemTradePoints(taxPoints); } } //3、points去除交易税,分别变更交易双方的积分值,并生成积分记录 PointRecord payerPointRecord = new PointRecord(payerUserId, ResourceAccessor.GetString("Common_PointTrade"), description, 0, 0, -points); pointRecordRepository.Insert(payerPointRecord); if (isImmediate) { userService.ChangePoints(payerUserId, 0, 0, -points); } else { userService.ReduceFrozenTradePoints(payerUserId, points); } //用于积分提醒 TrackPointRecord(payerUserId, payerPointRecord); PointRecord payeePointRecord = new PointRecord(payeeUserId, ResourceAccessor.GetString("Common_PointTrade"), description, 0, 0, realPoints); pointRecordRepository.Insert(payeePointRecord); userService.ChangePoints(payeeUserId, 0, 0, realPoints); }