/// <summary> /// 新建实体时使用 /// </summary> public static InvitationCodeStatistic New() { InvitationCodeStatistic invitationCodeStatistic = new InvitationCodeStatistic() { }; return(invitationCodeStatistic); }
//关于缓存期限: //1、用户邀请码配额 使用CachingExpirationType.SingleObject //2、邀请码实体、列表 使用CachingExpirationType.SingleObject、ObjectCollection //3、邀请好友记录列表 使用CachingExpirationType.ObjectCollection #region 邀请码 //done:zhengw,by mazq 方法名不好理解 或者直接与上一个方法合并叫 GetInvitationCode(long userId) 有什么问题? //zhengw回复:已修改 /// <summary> /// 获取邀请码 /// </summary> /// <param name="userId">申请人</param> public string GetInvitationCode(long userId) { //回复:我本身也没有判断吧。 string code = EncryptionUtility.MD5_16(userId.ToString() + DateTime.UtcNow.Ticks.ToString()); IInviteFriendSettingsManager inviteFriendSettingsManager = DIContainer.Resolve <IInviteFriendSettingsManager>(); InviteFriendSettings inviteFriendSettings = inviteFriendSettingsManager.Get(); if (inviteFriendSettings.AllowInvitationCodeUseOnce) { InvitationCodeStatistic invitationCodeStatistic = GetUserInvitationCodeStatistic(userId); //如果配额够用的 if (ChangeUserInvitationCodeCount(userId, -1, 1, 1)) { InvitationCode invitationCode = new InvitationCode { Code = code, DateCreated = DateTime.UtcNow, ExpiredDate = DateTime.UtcNow.AddDays(inviteFriendSettings.InvitationCodeTimeLiness), IsMultiple = !inviteFriendSettings.AllowInvitationCodeUseOnce, UserId = userId }; invitationCodesRepository.Insert(invitationCode); } else { code = string.Empty; } //1.用户未使用邀请码配额减1,然后调用ChangeUserInvitationCodeCount进行更新 //2.过期时间根据InviteFriendSettings.InvitationCodeTimeLiness确定 } else { string todayCode = invitationCodesRepository.GetTodayCode(userId); if (string.IsNullOrEmpty(todayCode)) { ILinktimelinessSettingsManager manager = DIContainer.Resolve <ILinktimelinessSettingsManager>(); InvitationCode invitationCode = new InvitationCode { Code = code, DateCreated = DateTime.UtcNow, ExpiredDate = DateTime.UtcNow.AddDays(inviteFriendSettings.InvitationCodeTimeLiness), IsMultiple = !inviteFriendSettings.AllowInvitationCodeUseOnce, UserId = userId }; invitationCodesRepository.Insert(invitationCode); } else { code = todayCode; } //检查今日是否有生成过的可多次使用的邀请码,若没有,则生成;否则,直接返回 //过期时间根据LinktimelinessSettings.Lowlinktimeliness确定 //设置IsMultiple为true } //向邀请码表中插入数据库 return(code); }
/// <summary> /// 变更用户邀请码配额 /// </summary> /// <param name="userId">用户Id</param> /// <param name="userInvitationCodeUnUsedCount">用户未使用邀请码配额增量(若减少请使用负数)</param> /// <param name="userInvitationCodeUsedCount">用户使用的邀请码配额增量(若减少请使用负数)</param> /// <param name="userInvitationCodeBuyedCount">用户购买的邀请码配额增量(若减少请使用负数)</param> /// <returns>是否更新成功</returns> public bool ChangeUserInvitationCodeCount(long userId, int userInvitationCodeUnUsedCount, int userInvitationCodeUsedCount, int userInvitationCodeBuyedCount) { InvitationCodeStatistic invitationCodeStatistic = GetUserInvitationCodeStatistic(userId); //done:bianchx by libsh,等于0的时候也不行 //回复:已经修改了对应的逻辑,不允许在配额不足的时候更新。但是允许在配额不足的时候购买配额 if (invitationCodeStatistic.CodeUnUsedCount <= 0 && userInvitationCodeUnUsedCount <= 0) { return(false); } //1、需检查用户邀请码数额统计表是否有数据,若没有则创建,否则进行更新 //2、用户默认邀请码配额需要使用InviteFriendSettings.DefaultUserInvitationCodeCount return(invitationCodeStatisticsRepository.ChangeUserInvitationCodeCount(userId, userInvitationCodeUnUsedCount, userInvitationCodeUsedCount, userInvitationCodeBuyedCount)); }
/// <summary> /// 获取用户邀请码统计实体 /// </summary> /// <param name="userId">用户Id</param> /// <returns>用户邀请码统计实体</returns> public InvitationCodeStatistic GetUserInvitationCodeStatistic(long userId) { InvitationCodeStatistic invitationCodeStatistic = invitationCodeStatisticsRepository.Get(userId); if (invitationCodeStatistic == null) { InviteFriendSettings inviteFriendSettings = inviteFriendSettingsManager.Get(); invitationCodeStatistic = new InvitationCodeStatistic { CodeUnUsedCount = inviteFriendSettings.DefaultUserInvitationCodeCount, CodeUsedCount = 0, CodeBuyedCount = 0, UserId = userId }; } return(invitationCodeStatistic); }