public async Task <ActionResult> GetCustomerHistoryBookingAsync(Guid id)
        {
            var customer = _customerService.GetCustomer(id);

            if (customer == null)
            {
                return(NotFound());
            }

            List <TicketVM> listTicketVM = new List <TicketVM>();
            var             item         = new CustomerTicketHistoryVM();

            item.CustomerId   = customer.Id;
            item.CustomerName = customer.FullName;

            var tickets = _ticketService
                          .GetTickets(_ => _.CustomerId == customer.Id.ToString())
                          .OrderByDescending(_ => _.BookingDate).ToList();

            if (tickets.Count != 0)
            {
                foreach (var ticket in tickets)
                {
                    var ticketVM = ticket.Adapt <TicketVM>();
                    ticketVM.StartTime  = ticket.Block.StartTime;
                    ticketVM.DoctorName = _doctorService.GetDoctorBasic(ticket.Block.DoctorId).FullName;
                    ticketVM.Date       = ticket.Block.Date;
                    listTicketVM.Add(ticketVM);
                }
                item.Tickets = listTicketVM;
            }
            return(Ok(item));
        }
        public ActionResult GetDoctorInfoById(string id)
        {
            var doctor = _doctorBasicService.GetDoctorBasic(id);

            if (doctor == null)
            {
                return(NotFound());
            }
            ////xu ly view model
            var result = doctor.Adapt <DoctorDetailVM>();

            result = doctor.DoctorPro.Adapt(result);
            result.SpecialityName = GetListSpecialityName(result.Id);
            return(Ok(result));
        }
 public ActionResult Time([FromQuery] Guid id, [FromQuery] DateTime date)
 {
     try
     {
         var schedulingIds = _schedulingService.GetSchedulings(s => s.Date.Date == date.Date &&
                                                               s.SpecialityId.Contains(id.ToString())).ToList();
         var group  = schedulingIds.GroupBy(_ => _.Doctor.Id);
         var result = new List <BlockOfDoctorViewModel>();
         foreach (var data in group)
         {
             var item = new BlockOfDoctorViewModel()
             {
                 FullName = _doctorService.GetDoctorBasic(data.Key).FullName
             };
             foreach (var scheduling in data)
             {
                 item.Blocks.AddRange(scheduling.Blocks.Select(_ => _.Adapt <BlockBookingViewModel>()));
                 foreach (var block in item.Blocks)
                 {
                     if (TimeSpan.Compare(TimeSpan.Parse(block.Time), DateTime.Now.TimeOfDay) < 0)
                     {
                         block.IsFull = true;
                     }
                 }
             }
             result.Add(item);
         }
         return(Ok(result));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
 public ActionResult AllBlock([FromQuery] DateTime date, string specialityId, string doctorId)
 {
     try
     {
         doctorId     = doctorId == null ? "" : doctorId;
         specialityId = specialityId == null ? "" : specialityId;
         var blocks                  = _blockService.GetBlocks(b => b.Date == date && b.IsAvailable && b.DoctorId.Contains(doctorId) && b.Scheduling.SpecialityId.Contains(specialityId));
         var groupBySpeciality       = blocks.GroupBy(s => s.Scheduling.SpecialityName);
         List <ManageBlockVM> result = new List <ManageBlockVM>();
         //Lấy block theo speciality
         foreach (var speciality in groupBySpeciality)
         {
             var itemSpeciality = new ManageBlockVM()
             {
                 SpecialityName = speciality.Key,
                 SpecialityId   = speciality.ToList()[0].Scheduling.SpecialityId
             };
             var groupByDoctor = speciality.GroupBy(_ => _.DoctorId);
             //Lấy block theo doctor
             foreach (var doctor in groupByDoctor)
             {
                 var itemDoctor = new DoctorBookingVM()
                 {
                     FullName = _doctorService.GetDoctorBasic(doctor.Key).FullName, Id = doctor.Key
                 };
                 itemDoctor.Blocks.AddRange(doctor.OrderByDescending(s => s.Date).Select(s => s.Adapt <BlockVM>()));
                 //Chay tất cả các block của 1 bác sĩ
                 foreach (var item in itemDoctor.Blocks)
                 {
                     //lấy ra các Ticket của block
                     foreach (var ticket in _ticketService.GetTickets(_ => _.BlockId == item.Id))
                     {
                         if (ticket.CustomerId != null)
                         {
                             var cus = _customerService.GetCustomer(Guid.Parse(ticket.CustomerId)).Adapt <TicketBookingVM>();
                             cus.Note     = ticket.Note;
                             cus.TicketId = ticket.Id;
                             item.Customers.Add(cus);
                         }
                         else
                         {
                             item.Customers.Add(null);
                         }
                     }
                 }
                 itemDoctor.Blocks = itemDoctor.Blocks.OrderBy(_ => _.StartTime).ToList();
                 itemSpeciality.Doctors.Add(itemDoctor);
             }
             result.Add(itemSpeciality);
         }
         return(Ok(result));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
        public async Task <ActionResult> ActiveTicketAsync([FromBody] List <string> Schedulings)
        {
            try
            {
                foreach (var schedulingId in Schedulings)
                {
                    var scheduling = _schedulingService.GetScheduling(Guid.Parse(schedulingId));
                    if (!scheduling.IsAvailable)
                    {
                        scheduling.IsAvailable = true;
                        var blocks = _blockService.GetBlocks(_ => _.SchedulingId == Guid.Parse(schedulingId));
                        foreach (var block in blocks)
                        {
                            block.IsAvailable = true;
                            //create tickets for each block (số lượng ticket dựa vào số lượng totalticket trong block)
                            int count = 0;//index của ticket
                            for (int j = 0; j < block.TotalTicket; j++)
                            {
                                Ticket ticket = new Ticket();
                                ticket.BlockId     = block.Id;
                                ticket.DateCreated = DateTime.Now;
                                ticket.Index       = (++count).ToString();
                                _ticketService.CreateTicket(ticket, (await _userManager.GetUserAsync(User)).UserName);
                            }
                        }
                    }
                }
                _schedulingService.Save();

                //lấy những bác sĩ được xếp lịch để gửi mail
                List <string> doctorIds = new List <string>();
                foreach (var schedulingId in Schedulings)
                {
                    var doctorId = _schedulingService.GetScheduling(Guid.Parse(schedulingId)).DoctorId;
                    doctorIds.Add(doctorId);
                }

                foreach (var data in doctorIds.GroupBy(_ => _))
                {
                    var        doctor = _doctorService.GetDoctorBasic(data.Key);
                    EmailModel email  = new EmailModel();
                    email.ToMail  = doctor.Email;
                    email.Subject = "This is subject";
                    email.Message = "Vô coi lịch trực của mày kìa";
                    _emailService.SendEmail(email);
                }
                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
        public ActionResult GetAllTicketsToday(DateTime?time)
        {
            var now = time == null ? DateTime.Now : time.Value;

            try
            {
                var tickets = _ticketService.GetTickets(_ => _.Block.Date.Date == now.Date && _.CustomerId != null)
                              .OrderByDescending(_ => _.Block.StartTime);
                var ticketsVM = new List <AllTicketTodayVM>();
                var result    = new List <PastNowFutureTicketVM>();
                foreach (var ticket in tickets)
                {
                    var ticketVM = ticket.Adapt <AllTicketTodayVM>();
                    //block now
                    if (ticket.Block.StartTime >= now.TimeOfDay.Subtract(new TimeSpan(0, 30, 0)) &&
                        ticket.Block.StartTime <= now.TimeOfDay)
                    {
                        ticketVM.TicketType = "Now";
                    }
                    if (ticket.Block.StartTime < now.TimeOfDay.Subtract(new TimeSpan(0, 30, 0)))
                    {
                        ticketVM.TicketType = "Past";
                    }
                    if (ticket.Block.StartTime > now.TimeOfDay)
                    {
                        ticketVM.TicketType = "Future";
                    }
                    var cus = _customerService.GetCustomer(Guid.Parse(ticket.CustomerId));
                    ticketVM.CustomerName        = cus.FullName;
                    ticketVM.CustomerCMDN        = cus.CMND;
                    ticketVM.CustomerPhoneNumber = cus.PhoneNumber;
                    ticketVM.DoctorId            = ticket.Block.DoctorId;
                    ticketVM.DoctorName          = _doctorService.GetDoctorBasic(ticket.Block.DoctorId).FullName;
                    ticketVM.StartTime           = ticket.Block.StartTime;
                    ticketsVM.Add(ticketVM);
                }

                var groupTicketType = ticketsVM.GroupBy(_ => _.TicketType);
                foreach (var data in groupTicketType)
                {
                    PastNowFutureTicketVM passNowFuture = new PastNowFutureTicketVM();
                    passNowFuture.TicketType = data.Key;
                    passNowFuture.Tickets.AddRange(data.Select(_ => _.Adapt <AllTicketTodayVM>()));
                    result.Add(passNowFuture);
                }
                return(Ok(result));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }