Beispiel #1
0
        public JsonResult EditTask(QuartzTaskEditModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new MessageData(false, "参数错误!")));
            }

            if (model.Id == 0 && _quartzTaskService.QuartzTasks.Any(n => n.ClassType == model.ClassType))
            {
                return(Json(new MessageData(false, "任务已存在!")));
            }

            try
            {
                QuartzTask task = model.AsQuartzTask();
                if (model.Id > 0)
                {
                    _quartzTaskService.Update(task);
                    DIContainer.Resolve <ITaskScheduler>().SaveTaskStatus(task);
                }
                else
                {
                    _quartzTaskService.Create(task);
                    DIContainer.Resolve <ITaskScheduler>().ResumeAll();
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                return(Json(new MessageData(false, "提交失败!")));
            }

            return(Json(new MessageData(true, "提交成功!")));
        }
Beispiel #2
0
        public ActionResult EditTask(int?id = null)
        {
            QuartzTask quartzTask = null;

            if (id.HasValue)
            {
                quartzTask = _quartzTaskService.FindById(id.Value);
            }
            else
            {
                quartzTask           = new QuartzTask();
                quartzTask.Enabled   = true;
                quartzTask.StartDate = DateTime.Now;
                quartzTask.TaskRule  = "* * * * * ?";
            }

            if (quartzTask == null)
            {
                return(HttpNotFound());
            }

            var classTypes = DIContainer.Resolve <ITypeFinder>().FindClassesOfType <ITask>();

            ViewData["classType"] = classTypes.Select(n => new SelectListItem {
                Text = n.Name, Value = n.FullName, Selected = n.FullName == quartzTask.ClassType
            });
            QuartzTaskEditModel editModel = quartzTask.AsEditModel();

            InitRules(editModel);
            return(View(editModel));
        }
Beispiel #3
0
        /// <summary>
        /// 初始化任务规则
        /// </summary>
        private void InitRules(QuartzTaskEditModel editModel)
        {
            List <SelectListItem> seconds    = new List <SelectListItem>();
            List <SelectListItem> minutes    = new List <SelectListItem>();
            List <SelectListItem> hours      = new List <SelectListItem>();
            List <SelectListItem> mouth      = new List <SelectListItem>();
            List <SelectListItem> day        = new List <SelectListItem>();
            List <SelectListItem> dayOfMouth = new List <SelectListItem>();

            for (int i = 0; i < 60; i++)
            {
                seconds.Add(new SelectListItem()
                {
                    Text = i.ToString(), Value = i.ToString(), Selected = editModel.Seconds == i.ToString()
                });
                minutes.Add(new SelectListItem()
                {
                    Text = i.ToString(), Value = i.ToString(), Selected = editModel.Minutes == i.ToString()
                });
                if (i > 0 && i <= 23)
                {
                    hours.Add(new SelectListItem()
                    {
                        Text = i.ToString(), Value = i.ToString(), Selected = editModel.Hours == i.ToString()
                    });
                }

                if (i > 0 && i <= 12)
                {
                    mouth.Add(new SelectListItem()
                    {
                        Text = i.ToString(), Value = i.ToString(), Selected = editModel.Mouth == i.ToString()
                    });
                }

                if (i > 0 && i <= 31)
                {
                    day.Add(new SelectListItem()
                    {
                        Text = i.ToString(), Value = i.ToString(), Selected = editModel.Day == i.ToString()
                    });
                    dayOfMouth.Add(new SelectListItem()
                    {
                        Text = i.ToString(), Value = i.ToString(), Selected = editModel.DayOfMouth == i.ToString()
                    });
                }
            }

            ViewData["Seconds"]    = seconds;
            ViewData["Minutes"]    = minutes;
            ViewData["Hours"]      = hours;
            ViewData["Mouth"]      = mouth;
            ViewData["Day"]        = day;
            ViewData["DayOfMouth"] = dayOfMouth;

            ViewData["Frequency"] = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "每天", Value = ((int)TaskFrequency.EveryDay).ToString(), Selected = TaskFrequency.EveryDay == editModel.Frequency
                },
                new SelectListItem()
                {
                    Text = "每周", Value = ((int)TaskFrequency.Weekly).ToString(), Selected = TaskFrequency.Weekly == editModel.Frequency
                },
                new SelectListItem()
                {
                    Text = "每月", Value = ((int)TaskFrequency.PerMonth).ToString(), Selected = TaskFrequency.PerMonth == editModel.Frequency
                }
            };
            ViewData["Number"] = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "第一周", Value = "1", Selected = editModel.Number == "1"
                },
                new SelectListItem()
                {
                    Text = "第二周", Value = "2", Selected = editModel.Number == "2"
                },
                new SelectListItem()
                {
                    Text = "第三周", Value = "3", Selected = editModel.Number == "3"
                },
                new SelectListItem()
                {
                    Text = "第四周", Value = "4", Selected = editModel.Number == "4"
                }
            };

            ViewData["DayOfWeek"] = new Dictionary <string, string>()
            {
                { "周一", "2" }, { "周二", "3" }, { "周三", "4" }, { "周四", "5" }, { "周五", "6" }, { "周六", "7" }, { "周日", "1" }
            };
            ViewData["WeeklyOfMouth"] = new List <SelectListItem>()
            {
                new SelectListItem()
                {
                    Text = "周一", Value = "2", Selected = editModel.WeeklyOfMouth == "2"
                },
                new SelectListItem()
                {
                    Text = "周二", Value = "3", Selected = editModel.WeeklyOfMouth == "3"
                },
                new SelectListItem()
                {
                    Text = "周三", Value = "4", Selected = editModel.WeeklyOfMouth == "4"
                },
                new SelectListItem()
                {
                    Text = "周四", Value = "5", Selected = editModel.WeeklyOfMouth == "5"
                },
                new SelectListItem()
                {
                    Text = "周五", Value = "6", Selected = editModel.WeeklyOfMouth == "6"
                },
                new SelectListItem()
                {
                    Text = "周六", Value = "7", Selected = editModel.WeeklyOfMouth == "7"
                },
                new SelectListItem()
                {
                    Text = "周日", Value = "1", Selected = editModel.WeeklyOfMouth == "1"
                }
            };
        }