Example #1
0
        public async Task FillServiceWeekdaySchedule(Guid serviceId, DayOfWeek targetDay)
        {
            await Task.Run(() =>
            {
                CheckPermission(UserRole.Administrator, AdministratorPermissions.Services);

                using (var session = SessionProvider.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var service = session.Get<Service>(serviceId);
                    if (service == null)
                    {
                        throw new FaultException<ObjectNotFoundFault>(new ObjectNotFoundFault(serviceId),
                            string.Format("Услуга [{0}] не найдена", serviceId));
                    }

                    var template = session.CreateCriteria<ServiceWeekdaySchedule>()
                        .Add(Expression.Eq("Service", service))
                        .Add(Expression.Eq("DayOfWeek", targetDay))
                        .SetMaxResults(1)
                        .UniqueResult<ServiceWeekdaySchedule>();
                    if (template == null)
                    {
                        throw new FaultException<ObjectNotFoundFault>(new ObjectNotFoundFault(targetDay),
                            string.Format("Расписание не найдено [{0}] день недели", targetDay));
                    }

                    foreach (var day in new DayOfWeek[] {
                        DayOfWeek.Monday,
                        DayOfWeek.Tuesday,
                        DayOfWeek.Wednesday,
                        DayOfWeek.Thursday,
                        DayOfWeek.Friday,
                        DayOfWeek.Saturday})
                    {
                        if (day != targetDay)
                        {
                            var schedule = session.CreateCriteria<ServiceWeekdaySchedule>()
                                .Add(Expression.Eq("Service", service))
                                .Add(Expression.Eq("DayOfWeek", day))
                                .SetMaxResults(1)
                                .UniqueResult<ServiceWeekdaySchedule>();
                            if (schedule == null)
                            {
                                schedule = new ServiceWeekdaySchedule()
                                {
                                    Service = service,
                                    DayOfWeek = day
                                };
                            }

                            schedule.StartTime = template.StartTime;
                            schedule.FinishTime = template.FinishTime;
                            schedule.IsWorked = template.IsWorked;
                            schedule.LiveClientInterval = template.LiveClientInterval;
                            schedule.Intersection = template.Intersection;
                            schedule.MaxClientRequests = template.MaxClientRequests;
                            schedule.RenderingMode = template.RenderingMode;
                            schedule.EarlyStartTime = template.EarlyStartTime;
                            schedule.EarlyFinishTime = template.EarlyFinishTime;
                            schedule.EarlyReservation = template.EarlyReservation;
                            schedule.EarlyClientInterval = template.EarlyClientInterval;
                            schedule.MaxOnlineOperators = template.MaxOnlineOperators;
                            schedule.OnlineOperatorsOnly = template.OnlineOperatorsOnly;
                            session.Save(schedule);

                            foreach (var r in session.CreateCriteria<ServiceRendering>()
                                .Add(Expression.Eq("Schedule", schedule))
                                .List<ServiceRendering>())
                            {
                                session.Delete(r);
                            }

                            foreach (var r in session.CreateCriteria<ServiceRendering>()
                                .Add(Expression.Eq("Schedule", template))
                                .List<ServiceRendering>())
                            {
                                var rendering = new ServiceRendering()
                                {
                                    Schedule = schedule,
                                    Operator = r.Operator,
                                    Mode = r.Mode,
                                    Priority = r.Priority
                                };

                                session.Save(rendering);
                            }

                            session.Save(schedule);
                        }
                    }

                    var todayQueuePlan = QueueInstance.TodayQueuePlan;
                    using (var locker = todayQueuePlan.WriteLock())
                    {
                        transaction.Commit();

                        todayQueuePlan.Flush(QueuePlanFlushMode.ServiceSchedule);
                        todayQueuePlan.Build(DateTime.Now.TimeOfDay);
                    }
                }
            });
        }
Example #2
0
        public async Task<DTO.Schedule> AddServiceWeekdaySchedule(Guid serviceId, DayOfWeek dayOfWeek)
        {
            return await Task.Run(() =>
            {
                CheckPermission(UserRole.Administrator, AdministratorPermissions.Services);

                using (var session = SessionProvider.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var service = session.Get<Service>(serviceId);
                    if (service == null)
                    {
                        throw new FaultException<ObjectNotFoundFault>(new ObjectNotFoundFault(serviceId), string.Format("Услуга [{0}] не найдена", serviceId));
                    }

                    var schedule = session.CreateCriteria<ServiceWeekdaySchedule>()
                        .Add(Expression.Eq("Service", service))
                        .Add(Expression.Eq("DayOfWeek", dayOfWeek))
                        .SetMaxResults(1)
                        .UniqueResult<ServiceWeekdaySchedule>();
                    if (schedule == null)
                    {
                        var template = session.CreateCriteria<DefaultWeekdaySchedule>()
                             .Add(Expression.Eq("DayOfWeek", dayOfWeek))
                             .SetMaxResults(1)
                             .UniqueResult<Schedule>();

                        schedule = new ServiceWeekdaySchedule()
                        {
                            Service = service,
                            DayOfWeek = dayOfWeek,

                            StartTime = template.StartTime,
                            FinishTime = template.FinishTime,
                            IsWorked = template.IsWorked,
                            LiveClientInterval = template.LiveClientInterval,
                            Intersection = template.Intersection,
                            MaxClientRequests = template.MaxClientRequests,
                            RenderingMode = template.RenderingMode,
                            EarlyStartTime = template.EarlyStartTime,
                            EarlyFinishTime = template.EarlyFinishTime,
                            EarlyReservation = template.EarlyReservation,
                            EarlyClientInterval = template.EarlyClientInterval,
                            MaxOnlineOperators = template.MaxOnlineOperators,
                            OnlineOperatorsOnly = template.OnlineOperatorsOnly
                        };
                        session.Save(schedule);

                        foreach (var r in session.CreateCriteria<ServiceRendering>()
                                                     .Add(Expression.Eq("Schedule", template))
                                                     .List<ServiceRendering>().ToArray())
                        {
                            var rendering = new ServiceRendering()
                            {
                                Schedule = schedule,
                                Operator = r.Operator,
                                Mode = r.Mode,
                                Priority = r.Priority
                            };

                            session.Save(rendering);
                        }
                    }

                    session.Save(schedule);

                    var todayQueuePlan = QueueInstance.TodayQueuePlan;
                    using (var locker = todayQueuePlan.WriteLock())
                    {
                        transaction.Commit();

                        todayQueuePlan.Flush(QueuePlanFlushMode.ServiceSchedule);
                        todayQueuePlan.Build(DateTime.Now.TimeOfDay);
                    }

                    return Mapper.Map<Schedule, DTO.Schedule>(schedule);
                }
            });
        }