Пример #1
0
        public ApiResult <bool> ContinueSelectSeat(long sessionId)
        {
            if (sessionId <= 0)
            {
                return(new ApiResult <bool>(ResultStatus.FAIL, "请选择正确的场次"));
            }

            Session session = _sessionRepository.Query(sessionId);

            if (session == null)
            {
                return(new ApiResult <bool>(ResultStatus.FAIL, "场次不存在"));
            }

            session.Continue();

            SelectSeatTask task = _selectSeatTaskRepository.QuerySessionNextTask(sessionId);

            task?.Begin();

            _transactionRepository.UseTransaction(IsolationLevel.ReadCommitted, () =>
            {
                if (task != null && _selectSeatTaskRepository.Start(task) <= 0)
                {
                    throw new DomainException("保存时异常0");
                }

                if (_sessionRepository.Update(session) <= 0)
                {
                    throw new DomainException("保存时异常1");
                }
            });

            return(new ApiResult <bool>(ResultStatus.SUCCESS, true));
        }
Пример #2
0
        public ApiResult <bool> SetOverdue(long sessionId, long taskId)
        {
            SelectSeatTask task = _selectSeatTaskRepository.Query(taskId);

            if (task == null)
            {
                return(new ApiResult <bool>(ResultStatus.FAIL, "任务不存在"));
            }

            if (task.SessionId != sessionId)
            {
                return(new ApiResult <bool>(ResultStatus.FAIL, "场次与任务不对应"));
            }

            task.Timedout();

            SelectSeatTask nextTask = _selectSeatTaskRepository.QueryNextTask(task);

            nextTask?.Begin();

            _transactionRepository.UseTransaction(IsolationLevel.ReadCommitted, () =>
            {
                if (nextTask != null && _selectSeatTaskRepository.Start(nextTask) <= 0)
                {
                    throw new Exception("保存时异常0");
                }

                if (_selectSeatTaskRepository.SetTimeout(task) <= 0)
                {
                    throw new Exception("保存时异常1");
                }
            });

            return(new ApiResult <bool>(ResultStatus.SUCCESS, true));
        }
Пример #3
0
        public ApiResult <bool> CheckIn(long wizardId, long sessionId)
        {
            Session session = _sessionRepository.Query(sessionId);

            if (session == null)
            {
                return(new ApiResult <bool>(ResultStatus.SUCCESS, "场次不存在"));
            }

            if (session.Status != SessionStatus.进行中)
            {
                return(new ApiResult <bool>(ResultStatus.FAIL, "场次" + session.Status.GetName()));
            }

            IEnumerable <SelectSeatTask> tasks = _selectSeatTaskRepository.QueryByWizardId(sessionId, wizardId);

            if (tasks.IsNullOrEmpty())
            {
                return(new ApiResult <bool>(ResultStatus.FAIL, "你不在排队中,请联系管理员"));
            }

            IEnumerable <SelectSeatTask> notInQueueTasks = tasks.Where(x => x.Status == SelectTaskStatus.未排队);

            IEnumerable <SelectSeatTask> overdueTasks = tasks.Where(x => x.Status == SelectTaskStatus.超时未重排);

            //if (notInQueueTasks.IsNullOrEmpty() && overdueTasks.IsNullOrEmpty())
            //    return new ApiResult<bool>(ResultStatus.FAIL, wipTask == null ? "全部都选完了" : "已经可以选了");

            //如果有未排队
            if (notInQueueTasks.Any())
            {
                var checkedInTasks = notInQueueTasks.Select(task =>
                {
                    task.CheckIn();
                    return(task);
                }).ToList();
                _selectSeatTaskRepository.CheckIn(checkedInTasks);
            }

            SelectSeatTask current = _selectSeatTaskRepository.QueryCurrent(sessionId)
                                     ?? _selectSeatTaskRepository.QuerySessionNextTask(sessionId);

            //有超时的任务,重新插入并排队
            if (overdueTasks.Any())
            {
                IEnumerable <SelectSeatTask> newTasks = overdueTasks.Select(x =>
                {
                    var newTask = new SelectSeatTask(NewId.GenerateId(), x, (current ?? x).SerialNo);
                    newTask.CheckIn();
                    return(newTask);
                }).ToList();

                IEnumerable <SelectSeatTask> oldTasks = overdueTasks.Select(x =>
                {
                    x.CheckInAgain();
                    return(x);
                }).ToList();

                _selectSeatTaskRepository.BatchInsert(newTasks);
                _selectSeatTaskRepository.CheckInAgain(oldTasks);
            }

            if (current == null)
            {
                SelectSeatTask task = _selectSeatTaskRepository.QuerySessionNextTask(sessionId);
                task?.Begin();
                if (task != null)
                {
                    _selectSeatTaskRepository.Start(task);
                }
            }
            else if (current.WizardId == wizardId && current.Status != SelectTaskStatus.进行中)
            {
                current.Begin();
                _selectSeatTaskRepository.Start(current);
            }

            return(new ApiResult <bool>(ResultStatus.SUCCESS, true));
        }
Пример #4
0
        public ApiResult <bool> Select(long sessionId, long wizardId, long taskId, string[] seatNos)
        {
            try
            {
                if (sessionId <= 0)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "sessionId必须大于0"));
                }

                if (wizardId <= 0)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "wizardId须大于0"));
                }

                if (taskId <= 0)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "taskId必须大于0"));
                }

                //if (session.Status != SessionStatus.进行中)
                //    return new ApiResult<bool>(ResultStatus.SUCCESS, "场次" + session.Status.GetName() + ",无法选座");

                Wizards wizard = _wizardRepository.Query(wizardId);
                if (wizard == null)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "巫师未注册"));
                }

                SelectSeatTask selectedTask = _selectSeatTaskRepository.Query(taskId);
                if (selectedTask == null)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "排队号不存在"));
                }

                if (selectedTask.Status != SelectTaskStatus.进行中)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "当前状态" + selectedTask.Status.GetName() + ",不能选座哦,请再等等或刷新!"));
                }

                if (selectedTask.WizardId != wizardId)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "请选择正确的任务"));
                }

                if (selectedTask.Total != seatNos.Length)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "选座数量必须为" + selectedTask.Total + "个"));
                }

                //IEnumerable<SelectSeatTask> tasks = _selectSeatTaskRepository.QueryByWizardId(sessionId, wizardId);
                //if (tasks.IsNullOrEmpty())
                //    return new ApiResult<bool>(ResultStatus.FAIL, "不在队列中");

                //SelectSeatTask canSelectTask = tasks.Where(x => x.Status == SelectTaskStatus.进行中).OrderBy(x => x.SerialNo).FirstOrDefault();
                //if (canSelectTask == null)
                //    return new ApiResult<bool>(ResultStatus.FAIL, "没有可选的名额了");

                //if (canSelectTask.Total != seatNos.Length)
                //    return new ApiResult<bool>(ResultStatus.FAIL, "选座数量必须为" + canSelectTask.Total);

                IEnumerable <Seat> seats = _seatRepository.Query(sessionId, seatNos);
                if (seats.Count() != seatNos.Length)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "seatNos传参错误"));
                }

                var selectedSeats = seats.Select(item =>
                {
                    item.Choose(wizard);
                    return(item);
                }).ToList();

                selectedTask.Select(seatNos);

                Session session = _sessionRepository.Query(sessionId);
                if (session == null)
                {
                    return(new ApiResult <bool>(ResultStatus.FAIL, "场次不存在"));
                }

                SelectSeatTask nextTask = null;
                if (session.Status == SessionStatus.进行中)
                {
                    nextTask = _selectSeatTaskRepository.QueryNextTask(selectedTask);
                    nextTask?.Begin();
                }

                _transactionRepository.UseTransaction(IsolationLevel.ReadUncommitted, () =>
                {
                    if (_seatRepository.BatchUpdate(selectedSeats.ToArray()) < 0)
                    {
                        throw new DomainException("保存时异常0");
                    }

                    if (_selectSeatTaskRepository.Select(selectedTask) < 0)
                    {
                        throw new DomainException("保存时异常1");
                    }

                    if (nextTask != null && _selectSeatTaskRepository.Start(nextTask) < 0)
                    {
                        throw new DomainException("保存时异常2");
                    }
                });

                return(new ApiResult <bool>(ResultStatus.SUCCESS, true));
            }
            catch (Exception ex)
            {
                _logger.LogError("选择座位异常", ex);
                return(new ApiResult <bool>(ResultStatus.EXCEPTION, ex.Message));
            }
        }