Beispiel #1
0
        public void GenerateSlot(int salonId, byte dayOfWeek, TimeSpan start, TimeSpan end)
        {
            if (start > end)
            {
                return;
            }
            // Get next month
            var nextMonth      = DateTime.Now.Month; // UPDATE CURRENT MONTH
            var currYear       = DateTime.Now.Year;
            var startSlot      = (int)start.TotalMinutes / 15;
            var endSlot        = (int)end.TotalMinutes / 15;
            var betweenMinutes = (end - start).TotalMinutes;

            var dates = DateUtil.GetDates(currYear, nextMonth, dayOfWeek);

            foreach (var date in dates)
            {
                Console.WriteLine($"GENERATE DATE: {date.ToShortDateString()}");
                var existedSalon = _slotRepo.Gets().Where(s => s.SalonId == salonId && s.SlotDate == date).FirstOrDefault();
                if (existedSalon == null)
                {
                    SlotTime newSlot = new SlotTime
                    {
                        SalonId  = salonId,
                        SlotDate = date
                    };
                    _slotRepo.Insert(newSlot);
                    _unitOfWork.SaveChanges();
                }
                else
                {
                    for (int i = 1; i <= 96; i++)
                    {
                        existedSalon.GetType().GetProperty($"Slot{i}").SetValue(existedSalon, null);
                        _slotRepo.Edit(existedSalon);
                    }
                    _unitOfWork.SaveChanges();
                }

                var sql = "UPDATE [SlotTime] SET ";
                for (int i = startSlot; i <= endSlot; i++)
                {
                    Console.WriteLine($"SLOT: {i}");
                    var slotName = "Slot" + i;
                    sql += $"{slotName}=0,";
                }
                sql = sql.TrimEnd(',');
                sql = sql + $" WHERE SalonId=@salonId and SlotDate=@slotDate";
                object[] parameters = new object[] {
                    new SqlParameter("@salonId", salonId),
                    new SqlParameter("@slotDate", date)
                };
                int result = _unitOfWork.ExecuteSqlCommand(sql, parameters);

                _unitOfWork.SaveChanges();
            }
        }
        private List <SlotTimeViewModel> ParseToSlotList(SlotTime slotdate)
        {
            var list = new List <SlotTimeViewModel>();

            for (int i = 1; i <= 96; i++)
            {
                var converted = slotdate.GetType().GetProperty($"Slot{i}").GetValue(slotdate);
                int capacity  = -1;
                if (converted != null)
                {
                    capacity = Convert.ToInt32(converted);
                }
                list.Add(new SlotTimeViewModel
                {
                    Id       = slotdate.Id,
                    SalonId  = slotdate.SalonId,
                    SlotDate = slotdate.SlotDate,
                    Time     = TimeSpan.FromMinutes(i * 15),
                    Capacity = capacity,
                    Index    = i
                });
            }
            return(list);
        }