示例#1
0
        private void btnOk_Click(object sender, EventArgs e)
        {
            btnOk.Enabled = false;
            try
            {
                DateTime nextStartTime = (cbSheduleType.SelectedIndex == 0 ?
                                          dtpDateOnce.Value.Date.Add(dtpTimeOnce.Value.TimeOfDay) :
                                          dtbDateFrequency.Value.Date.Add(dtbTimeFrequency.Value.TimeOfDay)).ToUniversalTime();

                #region InputChecks
                if (string.IsNullOrEmpty(tbTaskName.Text))
                {
                    MessageBox.Show($"Отсутствует имя задания.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _dontCloseForm = true;
                    return;
                }

                if (MQueryCommand.CheckTaskExistsByName(tbTaskName.Text, new int[1] {
                    _taskId
                }))
                {
                    MessageBox.Show($"Задание с именем [{tbTaskName.Text}] уже существует, введите другое имя.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _dontCloseForm = true;
                    return;
                }

                if (rgvSteps.dataGridView.Rows.Count == 0)
                {
                    MessageBox.Show($"Не создано ни одного шага.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _dontCloseForm = true;
                    return;
                }

                if (cbStartStep.SelectedItem == null)
                {
                    MessageBox.Show($"Не выбран стартовый шаг.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _dontCloseForm = true;
                    return;
                }

                if (cbSheduleType.SelectedItem == null)
                {
                    MessageBox.Show($"Не выбран тип расписания.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _dontCloseForm = true;
                    return;
                }

                if (rgvComputersList.SelectedValues.Count == 0)
                {
                    MessageBox.Show($"Нет выбранных компьютеров в списке компьютеров.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _dontCloseForm = true;
                    return;
                }

                foreach (DataGridViewRow row in rgvSteps.dataGridView.Rows)
                {
                    if (Convert.ToInt32(row.Cells["Step"].Value) == 1 &&
                        Convert.ToInt32(row.Cells["Id"].Value) != Convert.ToInt32(cbStartStep.SelectedValue))
                    {
                        if (MessageBox.Show($"Не все шаги будут выполнены, продолжить?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            _dontCloseForm = true;
                            return;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (Convert.ToInt32(row.Cells["Step"].Value) > 1 &&
                        !rgvSteps.dataGridView.Rows.OfType <DataGridViewRow>().Any(a =>
                                                                                   Convert.ToInt32(a.Cells["OnOperationCompleteStep"].Value) == Convert.ToInt32(row.Cells["Id"].Value) ||
                                                                                   Convert.ToInt32(a.Cells["OnOperationErrorStep"].Value) == Convert.ToInt32(row.Cells["Id"].Value)))
                    {
                        if (MessageBox.Show($"Не все шаги будут выполнены, продолжить?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            _dontCloseForm = true;
                            return;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                if (nextStartTime <= DateTime.UtcNow)
                {
                    MessageBox.Show($"Дата следующего выполнения должна быть больше текущей даты!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _dontCloseForm = true;
                    return;
                }

                if (nextStartTime > DateTime.UtcNow && nextStartTime.AddMinutes(-10) <= DateTime.UtcNow)
                {
                    if (MessageBox.Show($"Задание будет выполнено менее чем через 10 минут. Продолжить?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        _dontCloseForm = true;
                        return;
                    }
                }
                #endregion

                var computerIdList = rgvComputersList.SelectedValues.Select(s => Convert.ToInt32(s));

                var computerList = computerIdList.Select(s => new ShedulerTasksComputersList
                {
                    ComputerId = s
                });

                computerList.ForEach(f =>
                {
                    if (!_shedulerTask.ShedulerTasksComputersLists.Select(s => s.ComputerId).Contains(f.ComputerId))
                    {
                        _shedulerTask.ShedulerTasksComputersLists.Add(f);
                    }
                });

                var taskComputersList = _shedulerTask.ShedulerTasksComputersLists.ToArray();

                taskComputersList.ForEach(f =>
                {
                    if (!computerList.Select(s => s.ComputerId).Contains(f.ComputerId))
                    {
                        _shedulerTask.ShedulerTasksComputersLists.Remove(f);
                    }
                });

                _shedulerTask.Enabled     = chbTaskEnabled.Checked;
                _shedulerTask.Name        = tbTaskName.Text;
                _shedulerTask.Description = rtbTaskDescription.Text;
                _shedulerTask.Mode        = cbSheduleType.SelectedIndex == 0 ? 0 : Convert.ToInt32(cbOccurs.SelectedValue);

                _shedulerTask.StartTime = _shedulerTask.NextStartTime = nextStartTime;

                _shedulerTask.RepeatValue      = Convert.ToInt32(nudRecurs.Value);
                _shedulerTask.FirstStepId      = Convert.ToInt32(cbStartStep.SelectedValue);
                _shedulerTask.CreatedUserLogin = Environment.UserName;
                _shedulerTask.CreationDateUtc  = DateTime.UtcNow;


                //TODO

                var stepsGridList = rgvSteps.DataSource.OfType <ViewShedulerStepModel>().ToList();

                var stepsForModify = stepsGridList.Select(s => new ShedulerStep
                {
                    Id = s.Id,
                    OnOperationCompleteStep  = s.OnOperationCompleteStep,
                    OnOperationErrorStep     = s.OnOperationErrorStep,
                    OperationAttributes      = s.OperationAttributes,
                    OperationCheckIntervalMs = s.OperationCheckIntervalMs,
                    OperationType            = s.OperationType,
                    OrderId       = s.OrderId,
                    RepeatCount   = s.RepeatCount,
                    RepeatTimeout = s.RepeatTimeout,
                    TaskId        = _taskId
                });


                var tryDeleteInsertUpdateTaskFromFormRes = MQueryCommand.TryDeleteInsertUpdateTaskFromForm(_shedulerTask, stepsForModify);
                if (!tryDeleteInsertUpdateTaskFromFormRes.IsComplete)
                {
                    MessageBox.Show(tryDeleteInsertUpdateTaskFromFormRes.Message);
                }
            }
            finally
            {
                btnOk.Enabled = true;
            }
        }