/// <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> /// 彩票开始开奖 /// </summary> /// <returns></returns> public async Task <ResponseMessage> LotteryRunAsync(CancellationToken requestAborted) { var response = new ResponseMessage(); var periodInfo = await _allocateStore.GetScorePeriods().Where(w => w.State == 1 && !w.IsDelete).FirstOrDefaultAsync(); if (periodInfo == null) { response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "没有进行中的赛季,开奖失败"; return(response); } var nowTime = DateTime.Now; var numberList = new List <int>(); //开奖号码 var numberPeriods = await GetNumberPeriods(); //本次开奖期数 #region 生成中奖号码 //生成6个不相同的号码 while (numberList.Count < 6) { var rd = new Random(); var rdNext = rd.Next(0, 26); if (numberList.Any(a => a == rdNext)) { continue; } numberList.Add(rdNext); } numberList = numberList.OrderBy(o => o).ToList(); #endregion #region 计算中奖的人 var lotteryUser = await _giftStore.GetLotteryUsers().Where(w => w.NumberPeriods == numberPeriods && w.PeriodId == periodInfo.Id).ToListAsync(); //当期投注的人 var number6Count = 0; var number6Score = 0; //中6个的注数和总分数 var number5Count = 0; var number5Score = 0; //中5个的注数和总分数 var number4Count = 0; var number4Score = 0; //中4个的注数和总分数 var number3Count = 0; var number3Score = 0; //中3个的注数和总分数 var number2Count = 0; var number2Score = 0; //中2个的注数和总分数 var inputScore = lotteryUser.Sum(s => s.Score); //总投注K币 foreach (var lu in lotteryUser) { var winNumber = new List <int>(); //中奖的号码 var buyNumber = lu.Number.Split(','); //购买的号码 foreach (var bn in buyNumber) { if (numberList.Any(a => a == int.Parse(bn))) { winNumber.Add(int.Parse(bn)); } } winNumber = winNumber.OrderBy(o => o).ToList(); //中2个号,保本 if (winNumber.Count == 2) { lu.WinningNumber = string.Join(',', winNumber); lu.WinningScore = lu.Score; number2Count++; number2Score += lu.WinningScore ?? 0; } //中3个号,翻2倍 if (winNumber.Count == 3) { lu.WinningNumber = string.Join(',', winNumber); lu.WinningScore = lu.Score * 2; number3Count++; number3Score += lu.WinningScore ?? 0; } //中4个号,翻10倍 if (winNumber.Count == 4) { lu.WinningNumber = string.Join(',', winNumber); lu.WinningScore = lu.Score * 10; number4Count++; number4Score += lu.WinningScore ?? 0; } //中5个号,翻300倍 if (winNumber.Count == 5) { lu.WinningNumber = string.Join(',', winNumber); lu.WinningScore = lu.Score * 300; number5Count++; number5Score += lu.WinningScore ?? 0; } //中6个号,翻5000倍 if (winNumber.Count == 6) { lu.WinningNumber = string.Join(',', winNumber); lu.WinningScore = lu.Score * 5000; number6Count++; number6Score += lu.WinningScore ?? 0; } } lotteryUser = lotteryUser.Where(w => !string.IsNullOrEmpty(w.WinningNumber)).ToList();//筛选出中奖的人 #endregion var lotteryResult = new LotteryResult { Id = Guid.NewGuid().ToString(), CreateTime = nowTime, NumberPeriods = numberPeriods, Number = string.Join(',', numberList), WinResult = JsonHelper.ToJson(new { All = new { Count = lotteryUser.Count, OutScore = lotteryUser.Sum(s => s.WinningScore), InputScore = inputScore }, Number6 = new { Count = number6Count, Score = number6Score }, Number5 = new { Count = number5Count, Score = number5Score }, Number4 = new { Count = number4Count, Score = number4Score }, Number3 = new { Count = number3Count, Score = number3Score }, Number2 = new { Count = number2Count, Score = number2Score }, }) }; var userIds = lotteryUser.Select(s => s.UserId).ToList(); var userInfoList = await _allocateStore.GetUserInfos().Where(w => !w.IsDelete && userIds.Contains(w.Id)).ToListAsync(); //事务保存 using (var trans = await _transaction.BeginTransaction()) { try { //事务里面重新查一次 var scoreInfoList = await _allocateStore.GetScoreInfos().Where(w => !w.IsDelete && w.PeriodId == periodInfo.Id && userIds.Contains(w.UserId)).ToListAsync(); foreach (var lu in lotteryUser) { var storeInfo = scoreInfoList.FirstOrDefault(fi => fi.UserId == lu.UserId); if (storeInfo != null) { storeInfo.ConsumableScore = storeInfo.ConsumableScore + lu.WinningScore.Value; await _allocateStore.UpdateScoreInfo(storeInfo, requestAborted); await _giftStore.UpdateLotteryUser(lu, requestAborted); //添加中彩票动态 await _dynamicHelper.AddDynamicContent( DynamicType.WinLottery, periodInfo.Id, lu.Id, null, null, userInfoList.FirstOrDefault(fi => fi.Id == lu.UserId)?.UserName, userInfoList.FirstOrDefault(fi => fi.Id == lu.UserId)?.GroupName, lu.UserId, $"在K彩第《{lu.NumberPeriods}》期投中号码:{lu.WinningNumber}", lu.WinningScore.Value, null, null ); } } await _giftStore.AddLotteryResult(lotteryResult, requestAborted); //添加开奖动态 await _dynamicHelper.AddDynamicContent( DynamicType.LotteryResult, periodInfo.Id, lotteryResult.Id, null, null, "K先生", "K先生", "K先生", $"⭐K彩⭐第《{lotteryResult.NumberPeriods}》期开奖结果:{lotteryResult.Number}。共中出{lotteryUser.Count}注,共中出{lotteryUser.Sum(s => s.WinningScore)}K。" + $"{(number6Count > 0 ? "一等奖" + number6Count + "注,恭喜走上人生巅峰。" : "")}" + $"{(number5Count > 0?"二等奖"+ number5Count + "注,恭喜一夜暴富。" : "")}" + $"{(number4Count > 0 ? "三等奖共" + number4Count + $"注。" : "")}" + $"{(number3Count > 0 ? "四等奖共" + number3Count + "注。" : "")}" + $"{(number2Count > 0 ? "五等奖共" + number2Count + "注。" : "")}" + $"单注最高奖{lotteryUser.Max(m=>m.WinningScore)}K。", 0, null, null ); trans.Commit(); } catch (Exception e) { trans.Rollback(); throw new Exception("保存事务失败", e); } } // 触发信息变化 await _sendClientMessageManager.SendInfos(new List <Dto.Common.SendClientType>() { Dto.Common.SendClientType.Dynamic }); return(response); }