public async Task <ResponseMessage> Update(Models.UserInfo user, [FromBody] UpdatePeriodState updatePeriodState) { Logger.Trace($"用户{user?.UserName ?? ""}({user?.Id ?? ""})更改赛季信息,请求参数为:\r\n" + (updatePeriodState != null ? JsonHelper.ToJson(updatePeriodState) : "")); var response = new ResponseMessage(); if (!ModelState.IsValid) { response.Code = ResponseCodeDefines.ArgumentNullError; response.Message = "模型验证失败" + ModelState.GetAllErrors(); Logger.Warn("添加赛季验证失败:\r\n{0}", response.Message ?? ""); } if (user.IsAdmin == false) { response.Code = ResponseCodeDefines.NotAllow; response.Message = "没有权限"; Logger.Trace("没有权限:\r\n{0}", response.Message ?? ""); } try { response = await _scorePeriodManager.UapdatePeriodState(updatePeriodState); } catch (Exception e) { response.Code = ResponseCodeDefines.ServiceError; response.Message = e.Message; Logger.Error($"用户{user?.UserName ?? ""}({user?.Id ?? ""})更改赛季状态,报错:{e.Message}\r\n{e.StackTrace}"); } return(response); }
/// <summary> /// 开始/结束赛季 /// </summary> /// <param name="updatePeriodState"></param> /// <returns></returns> public async Task <ResponseMessage> UapdatePeriodState(UpdatePeriodState updatePeriodState) { var response = new ResponseMessage(); var scorePeriod = await _scorePeriodStore.GetScorePeriods().FirstOrDefaultAsync(a => a.Id == updatePeriodState.PeriodId); if (scorePeriod == null) { response.Code = ResponseCodeDefines.NotFound; response.Message = "未找到赛季信息"; return(response); } //开始赛季时需要初始化的用户积分信息 var scoreInfoList = new List <ScoreInfo>(); switch (updatePeriodState.State) { case 1: if (scorePeriod.State != 0) { response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "只有未开始的赛季才能操作[开始]"; return(response); } var temp = await _scorePeriodStore.GetScorePeriods().FirstOrDefaultAsync(a => a.State == 1 && a.IsDelete == false); if (temp != null) { response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "还有赛季未结束,不能开始新的赛季"; return(response); } scorePeriod.State = 1; //查询当前赛季需要初始化积分信息的用户 var userInfos = _scorePeriodStore.GetNotScoreInfoUsers(scorePeriod.Id); var nowTime = DateTime.Now; scoreInfoList = await userInfos.Select(s => new ScoreInfo { Id = Guid.NewGuid().ToString(), UserId = s.Id, CreateUser = s.Id, CreateTime = nowTime, IsDelete = false, Score = 0, ConsumableScore = 0, PeriodId = scorePeriod.Id, UpdateTime = nowTime, UpdateUser = s.Id }).ToListAsync(); break; case 2: if (scorePeriod.State != 1) { response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "需要结束的赛季必须是进行中的"; return(response); } scorePeriod.State = 2; break; default: response.Code = ResponseCodeDefines.ModelStateInvalid; response.Message = "请求修改的状态不符合。"; return(response); } using (var trans = await _transaction.BeginTransaction()) { try { if (scoreInfoList.Any()) { await _scoreInfoStore.CreateScoreInfoList(scoreInfoList); } await _scorePeriodStore.UpdatePeriodAsync(scorePeriod); trans.Commit(); } catch (Exception) { trans.Rollback(); throw; } } // 触发赛季 await _sendClientMessageManager.SendInfo(Dto.Common.SendClientType.Season); return(response); }