Example #1
0
        /// <summary>
        /// 完结任务
        /// </summary>
        /// <param name="user"></param>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public async Task <ResponseMessage> OverTaskAsync(UserInfo user, string taskId)
        {
            var response = new ResponseMessage();
            var taskInfo = await _iTaskStore.GetTaskInfos().Where(w => !w.IsDelete && w.Id == taskId).FirstOrDefaultAsync();

            if (taskInfo == null)
            {
                response.Code    = ResponseCodeDefines.NotFound;
                response.Message = "未找到相关任务";
                return(response);
            }
            var periodInfo = await _iTaskStore.GetScorePeriods().Where(w => !w.IsDelete && w.Id == taskInfo.PeriodId).FirstOrDefaultAsync();

            if (periodInfo == null || periodInfo.State != 1)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "只有进行中的赛季才能操作";
                return(response);
            }
            if (taskInfo.CreateUser != user.Id)
            {
                response.Code    = ResponseCodeDefines.NotAllow;
                response.Message = "不能操作其他人创建的任务";
                return(response);
            }

            //印象标签
            var userLabelList = new List <UserLabel>();
            var labelList     = new List <string>();

            if (!string.IsNullOrWhiteSpace(taskInfo.Labels))
            {
                //标签处理,现在只是去重做存储
                labelList = taskInfo.Labels.Replace(",", ",").Split(",").Where(w => !string.IsNullOrWhiteSpace(w)).Distinct().ToList();//请求的标签
                if (labelList.Where(w => w.Length > 12).Count() > 0)
                {
                    response.Code    = ResponseCodeDefines.NotAllow;
                    response.Message = "标签必须小于6个字";
                    return(response);
                }
            }
            //分配积分
            var taskUserList = await _iTaskStore.GetTaskUsers().Where(w => w.TaskId == taskId).Select(s => s.UserId).ToListAsync();

            var taskUserInfoList = await _iUserStore.GetUserInfos().Where(w => !w.IsDelete && taskUserList.Contains(w.Id)).ToListAsync();

            //平均每人分多少分
            int userScore = 0;

            //事务保存
            using (var trans = await _transaction.BeginTransaction())
            {
                try
                {
                    //查询拿到事务里面防止并发
                    var scoreInfoList = await _iTaskStore.GetScoreInfos().Where(w => !w.IsDelete && w.PeriodId == taskInfo.PeriodId && taskUserList.Contains(w.UserId)).ToListAsync();

                    if (!scoreInfoList.Any())
                    {
                        response.Code    = ResponseCodeDefines.NotFound;
                        response.Message = "未找到相关完成人";
                        return(response);
                    }
                    userScore = taskInfo.Score / scoreInfoList.Count;
                    var scoreDetailedList = new List <ScoreDetailed>();
                    foreach (var scoreInfo in scoreInfoList)
                    {
                        scoreInfo.Score           = scoreInfo.Score + userScore;
                        scoreInfo.ConsumableScore = scoreInfo.ConsumableScore + userScore;
                        //积分明细
                        scoreDetailedList.Add(new ScoreDetailed
                        {
                            Id         = Guid.NewGuid().ToString(),
                            PeriodId   = scoreInfo.PeriodId,
                            DealType   = 3,
                            UserId     = taskInfo.CreateUser,
                            TaskId     = taskInfo.Id,
                            DealUserId = user.Id,
                            Theme      = taskInfo.TaskName,
                            Memo       = taskInfo.TaskTntro,
                            Score      = userScore,
                            ScoreId    = scoreInfo.Id,
                            CreateTime = DateTime.Now,
                            CreateUser = user.Id,
                            IsDelete   = false,
                            Labels     = taskInfo.Labels
                        });
                        //印象标签
                        foreach (var lable in labelList)
                        {
                            userLabelList.Add(new UserLabel
                            {
                                Id     = Guid.NewGuid().ToString(),
                                UserId = scoreInfo.UserId,
                                Label  = lable,
                            });
                        }
                        //添加收入动态
                        await _dynamicHelper.AddDynamicContent(
                            DynamicType.TaskIncome,
                            taskInfo.PeriodId,
                            scoreInfo.Id,
                            null,
                            null,
                            taskUserInfoList.FirstOrDefault(fi => fi.Id == scoreInfo.UserId)?.UserName,
                            taskUserInfoList.FirstOrDefault(fi => fi.Id == scoreInfo.UserId)?.GroupName,
                            scoreInfo.UserId,
                            taskInfo.TaskName,
                            userScore,
                            null,
                            null
                            );
                    }
                    //更新任务
                    taskInfo.TaskState = 3;
                    await _iTaskStore.UpdateTaskInfoAsync(taskInfo);

                    //更新积分信息
                    await _iTaskStore.UpdateScoreInfoList(scoreInfoList);

                    //添加积分明细
                    if (scoreDetailedList != null && scoreDetailedList.Count != 0)
                    {
                        await _iTaskStore.CreateScoreDetailedList(scoreDetailedList);
                    }
                    //添加用户标签
                    if (userLabelList != null && userLabelList.Count != 0)
                    {
                        await _iTaskStore.CreateUserLabelList(userLabelList);
                    }
                    trans.Commit();
                }
                catch (Exception e)
                {
                    trans.Rollback();
                    throw new Exception("保存事务失败", e);
                }

                #region 任务完结发送提醒
                var usersIds = taskUserInfoList.Where(w => !string.IsNullOrWhiteSpace(w.UserId)).Select(s => s.UserId).ToList();
                if (usersIds.Any())
                {
                    _hellperEmail.SendEmpPush($"您在{periodInfo.Caption}中有任务已完结,快去看看吧!",
                                              $"尊敬的勇士您好:您在《{periodInfo.Caption}》中完成了任务《{taskInfo.TaskName}》获得了{userScore}K币,赶紧去看看吧。",
                                              usersIds);
                }
                #endregion
            }

            // 触发排行榜,个人信息,动态变化
            await _sendClientMessageManager.SendInfos(new List <Dto.Common.SendClientType>() {
                Dto.Common.SendClientType.Rank,
                Dto.Common.SendClientType.Dynamic,
                Dto.Common.SendClientType.User
            });

            return(response);
        }