public async Task <ResponseMessage> AllocateSubmit(Models.UserInfo user, [FromBody] AllocateSubmitRequest allocateSubmitRequest) { Logger.Trace($"用户{user?.UserName ?? ""}({user?.Id ?? ""})新增派发,请求参数为:\r\n" + (allocateSubmitRequest != null ? JsonHelper.ToJson(allocateSubmitRequest) : "")); var response = new ResponseMessage(); if (!ModelState.IsValid) { response.Code = ResponseCodeDefines.ArgumentNullError; response.Message = "模型验证失败" + ModelState.GetAllErrors(); Logger.Warn("新增派发模型验证失败:\r\n{0}", response.Message ?? ""); return(response); } var prefixs = new string[] { "AllocateController" }; var key = $"{user.Id}{allocateSubmitRequest.Theme}{allocateSubmitRequest.Score}"; try { // 防止重复提交 await _cache.LockSubmit(prefixs, key, "AllocateSubmit", HttpContext.RequestAborted); response = await _allocateManager.AllocateSubmitAsync(user, allocateSubmitRequest, HttpContext.RequestAborted); } catch (Exception e) { response.Code = ResponseCodeDefines.ServiceError; response.Message = e.Message; Logger.Error($"用户{user?.UserName ?? ""}({user?.Id ?? ""})新增派发,报错:{e.Message}\r\n{e.StackTrace}"); } finally { // 成功失败都要移除 await _cache.UnlockSubmit(prefixs, key); } return(response); }
/// <summary> /// 禅道签到彩蛋 /// </summary> /// <param name="zentaoLoginName"></param> /// <returns></returns> public async Task ZenTaoEggAsync(string zentaoLoginName) { //Egg3 if (DateTime.Now.Hour <= 10 && DateTime.Now.Hour >= 6) { var periodNow = await _scoreInfoStore.GetScorePeriods().FirstOrDefaultAsync(w => !w.IsDelete && w.State == 1); if (periodNow == null) { return; } //根据禅道帐号找到对应用户 var userInfo = await _userStore.GetUserInfos().FirstOrDefaultAsync(w => !w.IsDelete && w.ZenTao == zentaoLoginName); if (userInfo == null) { return; } //今天是否加过 var toDayAdd = await _scoreInfoStore.GetScoreInfoDetaileds().AnyAsync(a => a.PeriodId == periodNow.Id && !a.IsDelete && a.DealUserId == userInfo.Id && DateTime.Now.Date == a.CreateTime.Date && a.Memo == "Egg3"); if (!toDayAdd) { var allocateSubmitRequest = new AllocateSubmitRequest { AllocateUserId = userInfo.Id, PeriodId = periodNow.Id, Theme = "禅道早间签到", Memo = "Egg3", Score = DateTime.Now.Hour <= 8 ? 6 : 3, Labels = "" }; await _allocateManager.AllocateSubmitAsync(new UserInfo { Id = "K先生" }, allocateSubmitRequest, default(CancellationToken), true); } } //Egg4 if (DateTime.Now.Hour <= 23 && DateTime.Now.Hour >= 17) { var periodNow = await _scoreInfoStore.GetScorePeriods().FirstOrDefaultAsync(w => !w.IsDelete && w.State == 1); if (periodNow == null) { return; } //根据禅道帐号找到对应用户 var userInfo = await _userStore.GetUserInfos().FirstOrDefaultAsync(w => !w.IsDelete && w.ZenTao == zentaoLoginName); if (userInfo == null) { return; } //今天是否加过 var toDayAdd = await _scoreInfoStore.GetScoreInfoDetaileds().AnyAsync(a => a.PeriodId == periodNow.Id && !a.IsDelete && a.DealUserId == userInfo.Id && DateTime.Now.Date == a.CreateTime.Date && a.Memo == "Egg4"); if (!toDayAdd) { var allocateSubmitRequest = new AllocateSubmitRequest { AllocateUserId = userInfo.Id, PeriodId = periodNow.Id, Theme = "禅道晚间签到", Memo = "Egg4", Score = DateTime.Now.Hour < 20 ? 3 : 6, Labels = "" }; await _allocateManager.AllocateSubmitAsync(new UserInfo { Id = "K先生" }, allocateSubmitRequest, default(CancellationToken), true); } } }
/// <summary> /// 派发K币-提交 /// </summary> /// <param name="user"></param> /// <param name="allocateSubmitRequest"></param> /// <param name="requestAborted"></param> /// <param name="isEgg">是否彩蛋,默认不是,主要控制邮件发送</param> /// <returns></returns> public async Task <ResponseMessage> AllocateSubmitAsync(UserInfo user, AllocateSubmitRequest allocateSubmitRequest, CancellationToken requestAborted, bool isEgg = false) { var response = new ResponseMessage(); var periodInfo = await _iAllocateStore.GetScorePeriods().Where(w => !w.IsDelete && w.Id == allocateSubmitRequest.PeriodId).FirstOrDefaultAsync(); if (periodInfo == null || periodInfo.State != 1) { response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "只有进行中的赛季才能操作"; return(response); } var scoreInfo = await _iAllocateStore.GetScoreInfos().Where(w => !w.IsDelete && w.PeriodId == allocateSubmitRequest.PeriodId && w.UserId == allocateSubmitRequest.AllocateUserId).FirstOrDefaultAsync(); if (scoreInfo == null) { response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "没有找到派发对象的积分信息"; return(response); } var allocateUserInfo = await _iAllocateStore.GetUserInfos().Where(w => w.Id == allocateSubmitRequest.AllocateUserId).FirstOrDefaultAsync(); if (allocateUserInfo == null) { response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "派发对象用户不存在"; return(response); } #region 派发对象的收入信息 var scoreDetailed = new ScoreDetailed { Id = Guid.NewGuid().ToString(), PeriodId = allocateSubmitRequest.PeriodId, DealType = 4, UserId = user.Id,//派发人的ID DealUserId = allocateSubmitRequest.AllocateUserId, Theme = allocateSubmitRequest.Theme, Memo = allocateSubmitRequest.Memo, Score = allocateSubmitRequest.Score ?? 1, ScoreId = scoreInfo.Id, CreateTime = DateTime.Now, CreateUser = user.Id, IsDelete = false, Labels = allocateSubmitRequest.Labels }; scoreInfo.Score = scoreInfo.Score + allocateSubmitRequest.Score ?? 0; scoreInfo.ConsumableScore = scoreInfo.ConsumableScore + allocateSubmitRequest.Score ?? 0; #endregion #region 派发对象的印象标签 var userLabelList = new List <UserLabel>(); if (!string.IsNullOrWhiteSpace(allocateSubmitRequest.Labels)) { //标签处理,现在只是去重做存储 var labelList = allocateSubmitRequest.Labels.Replace(",", ",").Split(",").Where(w => !string.IsNullOrWhiteSpace(w)).Select(s => s.ToLower()).Distinct().ToList();//请求的标签 if (labelList.Where(w => w.Length > 12).Count() > 0) { response.Code = ResponseCodeDefines.NotAllow; response.Message = "单个标签必须小于6个字"; return(response); } foreach (var lable in labelList) { userLabelList.Add(new UserLabel { Id = Guid.NewGuid().ToString(), UserId = allocateUserInfo.Id, Label = lable, }); } } #endregion //事务保存 using (var trans = await _transaction.BeginTransaction()) { try { await _iAllocateStore.CreateScoreDetailed(scoreDetailed, requestAborted); await _iAllocateStore.UpdateScoreInfo(scoreInfo, requestAborted); if (userLabelList != null && userLabelList.Count != 0) { await _iAllocateStore.CreateUserLabelList(userLabelList, requestAborted); } //添加派发动态 await _dynamicHelper.AddDynamicContent( DynamicType.DistributeIncome, allocateSubmitRequest.PeriodId, scoreDetailed.Id, null, null, allocateUserInfo.UserName, allocateUserInfo.GroupName, allocateUserInfo.Id, allocateSubmitRequest.Theme, allocateSubmitRequest.Score ?? 1, null, null ); trans.Commit(); } catch (Exception e) { trans.Rollback(); throw new Exception("保存事务失败", e); } } #region 派发积分发送提醒 if (isEgg) { //一个彩蛋一个用户只会提醒一次 var isSend = await _iAllocateStore.GetScoreDetaileds().AnyAsync(w => !w.IsDelete && w.Memo == allocateSubmitRequest.Memo && w.DealUserId == allocateSubmitRequest.AllocateUserId); if (isSend) { return(response); } } _hellperEmail.SendEmpPush("您收到了新的积分,快去看看吧!", $"尊敬的勇士您好:您在《{periodInfo.Caption}》因为《{allocateSubmitRequest.Theme}》获得了{allocateSubmitRequest.Score ?? 1}K币,赶紧去看看吧。", new List <string> { allocateUserInfo.UserId }); #endregion // 触发排行榜,个人信息,动态变化 await _sendClientMessageManager.SendInfos(new List <Dto.Common.SendClientType>() { Dto.Common.SendClientType.Rank, Dto.Common.SendClientType.Dynamic, Dto.Common.SendClientType.User }); return(response); }
/// <summary> /// 2019-12-16取消彩蛋 /// </summary> /// <param name="userId"></param> /// <returns></returns> public async Task EasterEgg(string userId) { //Egg1 if (DateTime.Now.Hour <= 8 && DateTime.Now.Hour >= 6) { var periodNow = await _scoreInfoStore.GetScorePeriods().FirstOrDefaultAsync(w => !w.IsDelete && w.State == 1); if (periodNow == null) { return; } //今天是否加过 var toDayAdd = await _scoreInfoStore.GetScoreInfoDetaileds().AnyAsync(a => a.PeriodId == periodNow.Id && !a.IsDelete && a.DealUserId == userId && DateTime.Now.Date == a.CreateTime.Date && a.Memo == "Egg1"); if (!toDayAdd) { var egg1UserNumber = await _scoreInfoStore.GetScoreInfoDetaileds().Where(w => w.PeriodId == periodNow.Id && !w.IsDelete && w.Memo == "Egg1").Select(s => s.DealUserId).Distinct().CountAsync(); var userNumber = await _userStore.GetUserInfos().Where(w => !w.IsDelete).CountAsync(); var half = (egg1UserNumber > (userNumber / 2)) && ((userNumber / 2) > 10);//是否大于一半 var allocateSubmitRequest = new AllocateSubmitRequest { AllocateUserId = userId, PeriodId = periodNow.Id, Theme = !half ? "神秘彩蛋一" : "早间签到", Memo = "Egg1", Score = 10, Labels = _scoreInfoStore.GetScoreInfoDetaileds().Where(w => w.PeriodId == periodNow.Id && !w.IsDelete && w.Memo == "Egg1" && w.DealUserId == userId).Count() > 30 ? "不屈不挠" : "" }; await _allocateManager.AllocateSubmitAsync(new UserInfo { Id = "K先生" }, allocateSubmitRequest, default(CancellationToken), true); //大于一半人的时候向所有人公布Egg1 if ((userNumber / 2) + 1 == egg1UserNumber) { //var allUserEmail = await _userStore.GetUserInfos().Where(w => !w.IsDelete && !string.IsNullOrEmpty(w.Email)).Select(s => s.Email).ToListAsync(); //_hellperEmail.SendEmail("积分系统紧急通知【神秘彩蛋一】", // $"尊敬的勇士您好:", // $"目前已有超过一半的人触发了《神秘彩蛋一》,为了公平,K先生现在向所有人公布《神秘彩蛋一》:为了鼓励大家早上早到,在每天的早上6点到9点之间重新登录积分系统可获得10K币,保持登录不算哦。--K先生", // allUserEmail); } } } //Egg2 if (DateTime.Now.Hour <= 23 && DateTime.Now.Hour >= 18) { var periodNow = await _scoreInfoStore.GetScorePeriods().FirstOrDefaultAsync(w => !w.IsDelete && w.State == 1); if (periodNow == null) { return; } //今天是否加过 var toDayAdd = await _scoreInfoStore.GetScoreInfoDetaileds().AnyAsync(a => a.PeriodId == periodNow.Id && !a.IsDelete && a.DealUserId == userId && DateTime.Now.Date == a.CreateTime.Date && a.Memo == "Egg2"); if (!toDayAdd) { var egg2UserNumber = await _scoreInfoStore.GetScoreInfoDetaileds().Where(w => w.PeriodId == periodNow.Id && !w.IsDelete && w.Memo == "Egg2").Select(s => s.DealUserId).Distinct().CountAsync(); var userNumber = await _userStore.GetUserInfos().Where(w => !w.IsDelete).CountAsync(); var half = (egg2UserNumber > (userNumber / 2)) && ((userNumber / 2) > 10);//是否大于一半 var allocateSubmitRequest = new AllocateSubmitRequest { AllocateUserId = userId, PeriodId = periodNow.Id, Theme = !half ? "神秘彩蛋二" : "晚间签到", Memo = "Egg2", Score = 5, Labels = _scoreInfoStore.GetScoreInfoDetaileds().Where(w => w.PeriodId == periodNow.Id && !w.IsDelete && w.Memo == "Egg2" && w.DealUserId == userId).Count() > 20 ? "持之以恒" : "" }; await _allocateManager.AllocateSubmitAsync(new UserInfo { Id = "K先生" }, allocateSubmitRequest, default(CancellationToken), true); //大于一半人的时候向所有人公布Egg2 if ((userNumber / 2) + 1 == egg2UserNumber) { //var allUserEmail = await _userStore.GetUserInfos().Where(w => !w.IsDelete && !string.IsNullOrEmpty(w.Email)).Select(s => s.Email).ToListAsync(); //_hellperEmail.SendEmail("积分系统紧急通知【神秘彩蛋二】", // $"尊敬的勇士您好:", // $"目前已有超过一半的人触发了《神秘彩蛋二》,为了公平,K先生现在向所有人公布《神秘彩蛋二》:为了感谢大家辛苦的工作,在每天晚上17点到次日零点之间重新登录积分系统可获得5K币,保持登录不算哦。--K先生", // allUserEmail); } } } }