Beispiel #1
0
        /// <summary>
        /// 新增/修改我的任务
        /// </summary>
        /// <returns></returns>
        public async Task <ResponseMessage> SaveTaskInfo(Models.UserInfo user, TaskSaveRequest request, CancellationToken cancellationToken)
        {
            var response = new ResponseMessage();

            if (request.Score <= 0)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "分数必须大于零";
                return(response);
            }
            if (request.UserNumber == 0)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "所需人数必须大于零";
                return(response);
            }
            var periodInfo = await _iTaskStore.GetScorePeriods().Where(w => !w.IsDelete && w.Id == request.PeriodId).FirstOrDefaultAsync();

            if (periodInfo == null || periodInfo.State != 1)
            {
                response.Code    = ResponseCodeDefines.ModelStateInvalid;
                response.Message = "只有进行中的赛季才能操作";
                return(response);
            }
            var labelList = new List <string>();

            if (!string.IsNullOrWhiteSpace(request.Labels))
            {
                //标签处理,现在只是去重做存储
                labelList = request.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.ModelStateInvalid;
                    response.Message = "单个标签必须小于6个字";
                    return(response);
                }
            }
            if (string.IsNullOrEmpty(request.Id))
            {
                var saveInfo = new TaskInfo()
                {
                    Id           = Guid.NewGuid().ToString(),
                    CreateUser   = user.Id,
                    CreateTime   = DateTime.Now,
                    ExamineMemo  = string.Empty,
                    ExamineState = 1,
                    ExamineTime  = null,
                    TaskState    = 0,
                    IsDelete     = false,
                    ExamineUser  = string.Empty,
                    Labels       = labelList.Count > 0 ? string.Join(",", labelList) : "",
                    LootTime     = request.LootTime.GetValueOrDefault(DateTime.Now),
                    PeriodId     = request.PeriodId,
                    Score        = request.Score ?? 0,
                    TaskName     = request.TaskName,
                    TaskTntro    = request.TaskTntro,
                    UserNumber   = request.UserNumber ?? 0
                };
                await _iTaskStore.AddTaskInfoAsync(saveInfo, cancellationToken);

                #region 发送任务审核提醒
                var adminUserIds = await _iUserStore.GetUserInfos().Where(w => w.IsAdmin && !w.IsDelete && !string.IsNullOrWhiteSpace(w.UserId)).Select(s => s.UserId).ToListAsync();

                if (adminUserIds.Any())
                {
                    _hellperEmail.SendEmpPush($"{periodInfo.Caption}中有新任务需要您审核",
                                              $"尊敬的系统管理员您好:有一条任务需要您审核:《{request.TaskName}》。",
                                              adminUserIds);
                }
                #endregion
            }
            else
            {
                var oldTaskInfo = await _iTaskStore.GetTaskInfos().FirstOrDefaultAsync(item => item.Id == request.Id, cancellationToken);

                if (oldTaskInfo == null || user.Id != oldTaskInfo.CreateUser)
                {
                    response.Code    = ResponseCodeDefines.NotFound;
                    response.Message = "原任务信息已不存在";
                    return(response);
                }
                if (oldTaskInfo.TaskState >= 2)
                {
                    response.Code    = ResponseCodeDefines.ModelStateInvalid;
                    response.Message = "进行中和已完结的任务不能修改";
                    return(response);
                }
                if (oldTaskInfo.ExamineState == 3)
                {
                    response.Code    = ResponseCodeDefines.ModelStateInvalid;
                    response.Message = "审核驳回的任务不能修改";
                    return(response);
                }
                var oldTaskUserCount = await _iTaskStore.GetTaskUsers().Where(w => w.TaskId == oldTaskInfo.Id).CountAsync();

                if (request.UserNumber < oldTaskUserCount)
                {
                    response.Code    = ResponseCodeDefines.ModelStateInvalid;
                    response.Message = $"需要人数不能小于当前已抢的人数:{oldTaskUserCount}";
                    return(response);
                }
                else if (request.UserNumber == oldTaskUserCount)
                {
                    oldTaskInfo.TaskState = 2;//如果修改的人数等于了已领取的人数,就开始任务
                }
                //简介,人数,印象标签(如果是已抢中 则人数只能该大不能改小)
                oldTaskInfo.TaskTntro  = request.TaskTntro;
                oldTaskInfo.UserNumber = request.UserNumber ?? 1;
                oldTaskInfo.Labels     = labelList.Count > 0 ? string.Join(",", labelList) : "";

                await _iTaskStore.UpdateTaskInfoAsync(oldTaskInfo, cancellationToken);
            }
            return(response);
        }