private List <SectionWithDepartment> GetPrepareSaveData()
        {
            var deleteList = new List <SectionWithDepartment>();

            if (ModifySectionWithDepList.Any())
            {
                for (int i = 0; i < ModifySectionWithDepList.Count; i++)
                {
                    ModifySectionWithDepList[i].SectionId = SectionId.Value;
                    ModifySectionWithDepList[i].UpdateBy  = Login.UserId;
                    ModifySectionWithDepList[i].UpdateAt  = DateTime.Today.ToLocalTime();
                    ModifySectionWithDepList[i].CreateBy  = Login.UserId;
                    ModifySectionWithDepList[i].CreateAt  = DateTime.Today.ToLocalTime();
                }
            }
            for (int i = 0; i < OriginSectionWithDepList.Count; i++)
            {
                var isExists = ModifySectionWithDepList.Exists(x => x.DepartmentCode == OriginSectionWithDepList[i].DepartmentCode);
                if (!isExists)
                {
                    deleteList.Add(OriginSectionWithDepList[i]);
                }
            }
            return(deleteList);
        }
        /// <summary> Gridにある全データを削除 </summary>
        private void btnDeleteAll_Click(object sender, EventArgs e)
        {
            try
            {
                this.ButtonClicked(btnDeleteAll);
                ClearStatusMessage();

                if (!ModifySectionWithDepList.Any())
                {
                    ShowWarningDialog(MsgWngNoData, "削除");
                    return;
                }

                if (ShowConfirmDialog(MsgQstConfirmDeleteAll))
                {
                    ModifySectionWithDepList.Clear();
                    grdDepartmentModify.DataSource = new BindingSource(ModifySectionWithDepList, null);

                    Task <List <SectionWithDepartment> > task = LoadGridListAsync();
                    ProgressDialog.Start(ParentForm, task, false, SessionKey);
                    OriginSectionWithDepList       = task.Result;
                    grdDepartmentOrigin.DataSource = new BindingSource(OriginSectionWithDepList, null);

                    Modified = true;
                    ClearInput();
                }
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
                NLogHandler.WriteErrorLog(this, ex, SessionKey);
            }
        }
        /// <summary> From/To Rangeにある請求部門データをSectionWithDepartmetListに追加 </summary>
        private async Task <bool> AddSectionWithDepartment()
        {
            var departmentListToCheck = await GetDepartmentDataForCheckAsync();

            var codeFrom = string.IsNullOrWhiteSpace(txtDepartmentFrom.Text) ? txtDepartmentTo.Text : txtDepartmentFrom.Text;
            var codeTo   = string.IsNullOrWhiteSpace(txtDepartmentTo.Text) ? txtDepartmentFrom.Text : txtDepartmentTo.Text;

            Func <Department, bool> range = d => (string.Compare(codeFrom, d.Code) <= 0 && string.Compare(d.Code, codeTo) <= 0);

            var newDepartments = (departmentListToCheck).Where(range)
                                 .Where(d => !ModifySectionWithDepList.Any(swd => swd.DepartmentCode == d.Code));

            if (!newDepartments.Any())
            {
                return(false);
            }

            ModifySectionWithDepList.AddRange(newDepartments.Select(d => new SectionWithDepartment
            {
                DepartmentId   = d.Id,
                DepartmentCode = d.Code,
                DepartmentName = d.Name
            }));
            ModifySectionWithDepList = ModifySectionWithDepList.OrderBy(d => d.DepartmentCode).ToList();
            return(true);
        }
        private void SaveSectionWithDepartment()
        {
            CloseFlg = true;

            if (!string.IsNullOrWhiteSpace(txtDepartmentFrom.Text) ||
                !string.IsNullOrWhiteSpace(txtDepartmentTo.Text))
            {
                Task <bool> containsNew = AddSectionWithDepartment();
                ProgressDialog.Start(ParentForm, containsNew, false, SessionKey);
                if (!containsNew.Result)
                {
                    ShowWarningDialog(MsgWngNoData, "追加");
                    ClearInput();
                    CloseFlg = false;
                    return;
                }
            }

            bool success = false;
            List <SectionWithDepartment> list = null;
            var task = ServiceProxyFactory.LifeTime(async factory =>
            {
                var deleteList = GetPrepareSaveData();
                var service    = factory.Create <SectionWithDepartmentMasterClient>();
                SectionWithDepartmentResult result = await service.SaveAsync(SessionKey, ModifySectionWithDepList.ToArray(), deleteList.ToArray());

                success = result?.ProcessResult.Result ?? false;

                if (success)
                {
                    list = await LoadGridListAsync();
                }
            });

            ProgressDialog.Start(ParentForm, task, false, SessionKey);

            if (success)
            {
                DispStatusMessage(MsgInfSaveSuccess);
                ModifySectionWithDepList.Clear();

                OriginSectionWithDepList       = list;
                ModifySectionWithDepList       = OriginSectionWithDepList;
                grdDepartmentModify.DataSource = new BindingSource(OriginSectionWithDepList, null);
                grdDepartmentOrigin.DataSource = new BindingSource(ModifySectionWithDepList, null);
                txtDepartmentFrom.Focus();
                ClearInput();
                Modified = false;
            }
            else
            {
                ShowWarningDialog(MsgErrSaveError);
            }
        }
        /// <summary> From/To RangeにあるデータをGridから削除 </summary>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                this.ButtonClicked(btnDelete);
                if (string.IsNullOrWhiteSpace(txtDepartmentFrom.Text) && string.IsNullOrWhiteSpace(txtDepartmentTo.Text))
                {
                    ShowWarningDialog(MsgWngInputRequired, "請求部門コード");
                    txtDepartmentFrom.Focus();
                    return;
                }
                if (!string.IsNullOrWhiteSpace(txtDepartmentFrom.Text) && !string.IsNullOrWhiteSpace(txtDepartmentTo.Text) &&
                    !txtDepartmentFrom.ValidateRange(txtDepartmentTo, () => ShowWarningDialog(MsgWngInputRangeChecked, "請求部門コード")))
                {
                    txtDepartmentFrom.Focus();
                    return;
                }

                var codeFrom = string.IsNullOrWhiteSpace(txtDepartmentFrom.Text) ? txtDepartmentTo.Text : txtDepartmentFrom.Text;
                var codeTo   = string.IsNullOrWhiteSpace(txtDepartmentTo.Text) ? txtDepartmentFrom.Text : txtDepartmentTo.Text;

                Func <SectionWithDepartment, bool> range = d => (string.Compare(codeFrom, d.DepartmentCode) <= 0 && string.Compare(d.DepartmentCode, codeTo) <= 0);

                if (!ModifySectionWithDepList.Any(range))
                {
                    ShowWarningDialog(MsgWngNoData, "削除");
                    ClearInput();
                    return;
                }

                ModifySectionWithDepList = ModifySectionWithDepList
                                           .Except(ModifySectionWithDepList.Where(range))
                                           .OrderBy(d => d.DepartmentCode).ToList();
                grdDepartmentModify.DataSource = new BindingSource(ModifySectionWithDepList, null);

                Task <List <SectionWithDepartment> > task = LoadGridListAsync();
                ProgressDialog.Start(ParentForm, task, false, SessionKey);
                OriginSectionWithDepList       = task.Result;
                grdDepartmentOrigin.DataSource = new BindingSource(OriginSectionWithDepList, null);

                Modified = true;
                ClearInput();
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString());
                NLogHandler.WriteErrorLog(this, ex, SessionKey);
            }
        }
        private void btnDepartmentFrom_Click(object sender, EventArgs e)
        {
            var departmentId = ModifySectionWithDepList.Select(d => d.DepartmentId).ToArray();
            var department   = this.ShowDepartmentSearchDialog(loader: new DepartmentGridLoader(ApplicationContext)
            {
                Key          = DepartmentGridLoader.SearchDepartment.WithSection,
                SectionId    = SectionId.Value,
                DepartmentId = departmentId
            });

            if (department != null)
            {
                SetDepartmentData(department, txtDepartmentFrom, lblDepartmentFromName, ref DepartmentFromId);
                ClearStatusMessage();
            }
        }