public ServiceExceptionScheduleReportData(ServiceExceptionSchedule source)
     : base(source, source.ScheduleDate)
 {
     Service = source.Service.ToString();
 }
Beispiel #2
0
        public async Task<DTO.Schedule> AddServiceExceptionSchedule(Guid serviceId, DateTime scheduleDate)
        {
            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<ServiceExceptionSchedule>()
                        .Add(Expression.Eq("Service", service))
                        .Add(Expression.Eq("ScheduleDate", scheduleDate))
                        .SetMaxResults(1)
                        .UniqueResult<ServiceExceptionSchedule>();
                    if (schedule == null)
                    {
                        var template = session.CreateCriteria<DefaultExceptionSchedule>()
                            .Add(Expression.Eq("ScheduleDate", scheduleDate))
                            .SetMaxResults(1)
                            .UniqueResult<Schedule>();

                        if (template == null)
                        {
                            var dayOfWeek = scheduleDate.DayOfWeek;

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

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

                        schedule = new ServiceExceptionSchedule()
                        {
                            Service = service,
                            ScheduleDate = scheduleDate,

                            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);
                }
            });
        }