BaseDataWindow: implement area commitment type list

This commit is contained in:
2023-11-06 19:35:45 +01:00
parent 7e6aaf514b
commit 4fff89029d
5 changed files with 275 additions and 8 deletions

View File

@ -0,0 +1,154 @@
using Elwig.Helpers;
using Elwig.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
namespace Elwig.Windows {
public partial class BaseDataWindow {
private Dictionary<string, string?>? _acts = null;
private Dictionary<AreaComType, string>? _actIds = null;
private ObservableCollection<AreaComType>? _actList = null;
private bool _actChanged = false;
private bool _actUpdate = false;
private void AreaCommitmentTypesInitEditing() {
_actList = new(Context.AreaCommitmentTypes.OrderBy(v => v.VtrgId).ToList());
_acts = _actList.ToDictionary(v => v.VtrgId, v => (string?)v.VtrgId);
_actIds = _actList.ToDictionary(v => v, v => v.VtrgId);
ControlUtils.RenewItemsSource(AreaCommitmentTypeList, _actList, a => (a as AreaComType)?.VtrgId);
AreaCommitmentTypeList_SelectionChanged(null, null);
}
private void AreaCommitmentTypesFinishEditing() {
ControlUtils.RenewItemsSource(AreaCommitmentTypeList, Context.AreaCommitmentTypes.OrderBy(v => v.SortId).ToList(), v => (v as AreaComType)?.VtrgId);
_actList = null;
_acts = null;
_actIds = null;
_actChanged = false;
AreaCommitmentTypeAddButton.IsEnabled = false;
AreaCommitmentTypeDeleteButton.IsEnabled = false;
}
private async Task AreaCommitmentTypesSave() {
if (!_actChanged || _actList == null || _acts == null || _actIds == null)
return;
foreach (var (vtrgid, _) in _acts.Where(a => a.Value == null)) {
Context.Remove(Context.AreaCommitmentTypes.Find(vtrgid));
}
foreach (var (attr, old) in _actIds) {
attr.VtrgId = old;
}
foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) {
Context.Update(Context.AreaCommitmentTypes.Find(old));
}
await Context.SaveChangesAsync();
foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) {
await Context.Database.ExecuteSqlAsync($"UPDATE area_commitment_type SET vtrgid = {vtrgid} WHERE vtrgid = {old}");
}
await Context.SaveChangesAsync();
foreach (var type in _actList.Where(a => !_actIds.ContainsKey(a))) {
if (type.VtrgId == null) continue;
await Context.AddAsync(type);
}
await Context.SaveChangesAsync();
}
private void AreaCommitmentTypeList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {
UpdateButtons();
_actUpdate = true;
if (AreaCommitmentTypeList.SelectedItem is AreaComType type) {
AreaCommitmentTypeIdInput.Text = $"{type.SortId}{type.AttrId}";
ControlUtils.SelectComboBoxItem(AreaCommitmentTypeWineVariantInput, s => (s as WineVar)?.SortId, type.SortId);
ControlUtils.SelectComboBoxItem(AreaCommitmentTypeWineAttributeInput, a => (a as WineAttr)?.AttrId, type.AttrId);
AreaCommitmentTypeMinKgPerHaInput.Text = $"{type.MinKgPerHa}";
AreaCommitmentTypePenaltyPerKgInput.Text = $"{type.PenaltyPerKg}";
AreaCommitmentTypePenaltyInput.Text = $"{type.PenaltyAmount}";
AreaCommitmentTypePenaltyNoneInput.Text = $"{type.PenaltyNone}";
} else {
AreaCommitmentTypeIdInput.Text = "";
AreaCommitmentTypeWineVariantInput.SelectedItem = null;
AreaCommitmentTypeWineAttributeInput.SelectedItem = null;
AreaCommitmentTypeMinKgPerHaInput.Text = "";
AreaCommitmentTypePenaltyPerKgInput.Text = "";
AreaCommitmentTypePenaltyInput.Text = "";
AreaCommitmentTypePenaltyNoneInput.Text = "";
}
_actUpdate = false;
}
private void AreaCommitmentTypeAddButton_Click(object sender, RoutedEventArgs evt) {
if (_actList == null) return;
_actChanged = true;
var item = Context.CreateProxy<AreaComType>();
_actList.Add(item);
AreaCommitmentTypeList.SelectedItem = item;
UpdateButtons();
}
private void AreaCommitmentTypeDeleteButton_Click(object sender, RoutedEventArgs evt) {
if (_actList == null || _acts == null) return;
_actChanged = true;
var idx = AreaCommitmentTypeList.SelectedIndex;
var item = _actList[idx];
_acts[item.VtrgId] = null;
_actList.RemoveAt(idx);
AreaCommitmentTypeList.SelectedIndex = idx < _actList.Count ? idx : idx - 1;
UpdateButtons();
}
private void AreaCommitmentType_Changed(object? sender, RoutedEventArgs? evt) {
if (_actUpdate || (!IsEditing && !IsCreating) || AreaCommitmentTypeList.SelectedItem is not AreaComType attr || _acts == null || _actIds == null) return;
_actChanged = true;
var v = AreaCommitmentTypeWineVariantInput.SelectedItem as WineVar;
var a = AreaCommitmentTypeWineAttributeInput.SelectedItem as WineAttr;
AreaCommitmentTypeIdInput.Text = $"{v?.SortId}{a?.AttrId}";
var old = _actIds.GetValueOrDefault(attr);
var id = AreaCommitmentTypeIdInput.Text;
if (old != null) _acts[old] = id;
attr.VtrgId = id;
attr.SortId = v?.SortId;
attr.AttrId = a?.AttrId;
attr.MinKgPerHa = AreaCommitmentTypeMinKgPerHaInput.Text.Length > 0 ? int.Parse(AreaCommitmentTypeMinKgPerHaInput.Text) : null;
attr.PenaltyPerKg = AreaCommitmentTypePenaltyPerKgInput.Text.Length > 0 ? decimal.Parse(AreaCommitmentTypePenaltyPerKgInput.Text) : null;
attr.PenaltyAmount = AreaCommitmentTypePenaltyInput.Text.Length > 0 ? decimal.Parse(AreaCommitmentTypePenaltyInput.Text) : null;
attr.PenaltyNone = AreaCommitmentTypePenaltyNoneInput.Text.Length > 0 ? decimal.Parse(AreaCommitmentTypePenaltyNoneInput.Text) : null;
CollectionViewSource.GetDefaultView(_actList).Refresh();
UpdateButtons();
}
private void AreaCommitmentTypeIdInput_TextChanged(object sender, TextChangedEventArgs evt) {
UpperCaseInput_TextChanged(sender, evt);
AreaCommitmentType_Changed(sender, evt);
}
private void AreaCommitmentTypeMinKgPerHa_TextChanged(object sender, TextChangedEventArgs evt) {
InputTextChanged((TextBox)sender, Validator.CheckInteger((TextBox)sender, false, 5));
AreaCommitmentType_Changed(sender, evt);
}
private void AreaCommitmentTypePenaltyPerKgInput_TextChanged(object sender, TextChangedEventArgs evt) {
if (SeasonList.SelectedItem is not Season s) return;
InputTextChanged((TextBox)sender, Validator.CheckDecimal((TextBox)sender, false, 2, s.Precision));
AreaCommitmentType_Changed(sender, evt);
}
private void AreaCommitmentTypePenaltyInput_TextChanged(object sender, TextChangedEventArgs evt) {
InputTextChanged((TextBox)sender, Validator.CheckDecimal((TextBox)sender, false, 4, 2));
AreaCommitmentType_Changed(sender, evt);
}
}
}