Пример #1
0
        public void Execute(IJobExecutionContext context)
        {
            DateTime now     = DateTime.Now;
            DateTime time    = default(DateTime).Add(now.TimeOfDay);
            DateTime newTime = DateTime.Parse(time.TimeOfDay.ToString(), CultureInfo.InvariantCulture);
            List <ShowHideProduct> listSH = Service.showHideDAO.findShowHideProducts(now, now, now.DayOfWeek).ToList();

            if (listSH.Count() > 0)
            {
                for (var i = 0; i < listSH.Count(); i++)
                {
                    ShowHideProduct showHideProduct = new ShowHideProduct();
                    Product         product         = new Product();
                    showHideProduct = listSH[i];
                    product         = Service.productDAO.getProduct(showHideProduct.productId);

                    string   dateTimeFromTime = showHideProduct.fromTime.ToString();
                    string   timeFromTime     = dateTimeFromTime.Split(' ')[1] + " " + dateTimeFromTime.Split(' ')[2];
                    DateTime newFromTime      = DateTime.Parse(timeFromTime, CultureInfo.InvariantCulture);

                    string   dateTimeToTime = showHideProduct.toTime.ToString();
                    string   timeToTime     = dateTimeToTime.Split(' ')[1] + " " + dateTimeToTime.Split(' ')[2];
                    DateTime newToTime      = DateTime.Parse(timeToTime, CultureInfo.InvariantCulture);

                    var compareTime  = newTime < newFromTime;
                    var compareTime2 = newTime.CompareTo(newFromTime);
                    if (newTime.CompareTo(newFromTime) >= 0 && newTime.CompareTo(newToTime) <= 0 && (showHideProduct.show != product.status))
                    {
                        product.status = listSH[i].show;
                        Service.productDAO.updateProduct(product);
                    }
                    else if (newTime.CompareTo(newFromTime) >= 0 && newTime.CompareTo(newToTime) >= 0 && (showHideProduct.show == product.status))
                    {
                        switch (showHideProduct.show)
                        {
                        case 0:
                            product.status = 1;
                            break;

                        default:
                            product.status = 0;
                            break;
                        }
                        Service.productDAO.updateProduct(product);
                    }
                }
            }
        }
        public IHttpActionResult createShowHideProduct([FromBody] ModelCreate modelCreate)
        {
            Response response = new Response();

            if (modelCreate.fromDate == null || modelCreate.endDate == null)
            {
                response.code   = "400";
                response.status = "chưa chọn khoản ngày tháng";
                return(Content <Response>(System.Net.HttpStatusCode.OK, response));
            }

            if (modelCreate.DaysOfWeek.Count == 0)
            {
                response.code   = "400";
                response.status = "chưa chọn thứ";
                return(Content <Response>(System.Net.HttpStatusCode.OK, response));
            }

            if (modelCreate.times.Count == 0)
            {
                response.code   = "400";
                response.status = "chưa chọn khoản giờ";
                return(Content <Response>(System.Net.HttpStatusCode.OK, response));
            }

            if (modelCreate.productId.Count == 0)
            {
                response.code   = "400";
                response.status = "chưa chọn sản phẩm";
                return(Content <Response>(System.Net.HttpStatusCode.OK, response));
            }

            foreach (var day in modelCreate.DaysOfWeek)
            {
                for (var i = 0; i < modelCreate.times.Count; i++)
                {
                    var time = modelCreate.times[i];
                    foreach (var product in modelCreate.productId)
                    {
                        IEnumerable <ShowHideProduct> showHideProduct = Service.showHideDAO.findShowHideProducts(modelCreate.fromDate, modelCreate.endDate, day, time.fromTime, time.toTime, product, time.show);
                        if (showHideProduct.Count() > 0)
                        {
                            response.code   = "409";
                            response.status = "Sản phẩm này đã bị trùng lịch";
                            return(Content <Response>(System.Net.HttpStatusCode.OK, response));
                        }
                    }
                }
            }

            foreach (var day in modelCreate.DaysOfWeek)
            {
                for (var i = 0; i < modelCreate.times.Count; i++)
                {
                    var             time   = modelCreate.times[i];
                    ShowHideProduct create = new ShowHideProduct();
                    create.startDate = modelCreate.fromDate;
                    create.endDate   = modelCreate.endDate;
                    create.dayOfWeek = day;
                    create.fromTime  = time.fromTime;
                    create.toTime    = time.toTime;
                    create.show      = time.show;
                    foreach (var id in modelCreate.productId)
                    {
                        create.productId = id;
                        Service.showHideDAO.insertShowHideProduct(create);
                    }
                }
            }

            response.code   = "200";
            response.status = "Tạo lịch thành công";
            return(Content <Response>(System.Net.HttpStatusCode.OK, response));
        }