139 lines
4.8 KiB
C#
139 lines
4.8 KiB
C#
|
|
using HandyControl.Controls;
|
||
|
|
using HandyControl.Tools.Extension;
|
||
|
|
using System.Collections.ObjectModel;
|
||
|
|
using YY.Admin.Core;
|
||
|
|
using YY.Admin.Core.Entity;
|
||
|
|
using YY.Admin.Core.Services;
|
||
|
|
using YY.Admin.Services.Service;
|
||
|
|
|
||
|
|
namespace YY.Admin.ViewModels.Customer;
|
||
|
|
|
||
|
|
public class CustomerEditDialogViewModel : BaseViewModel, IDialogResultable<bool>
|
||
|
|
{
|
||
|
|
private readonly ICustomerService _customerService;
|
||
|
|
private readonly IJeecgDictSyncService _dictSyncService;
|
||
|
|
|
||
|
|
private MesXslCustomer? _customer;
|
||
|
|
public MesXslCustomer? Customer
|
||
|
|
{
|
||
|
|
get => _customer;
|
||
|
|
set => SetProperty(ref _customer, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsAddMode => string.IsNullOrWhiteSpace(Customer?.Id) || (Customer?.Id?.StartsWith("local-") ?? false);
|
||
|
|
public string DialogTitle => IsAddMode ? "新增客户" : "编辑客户";
|
||
|
|
|
||
|
|
public ObservableCollection<KeyValuePair<string, string>> StatusOptions { get; } = new();
|
||
|
|
public ObservableCollection<KeyValuePair<string, string>> CustomerRegionOptions { get; } = new();
|
||
|
|
|
||
|
|
private bool _result;
|
||
|
|
public bool Result { get => _result; set => SetProperty(ref _result, value); }
|
||
|
|
public Action? CloseAction { get; set; }
|
||
|
|
|
||
|
|
public DelegateCommand SaveCommand { get; }
|
||
|
|
public DelegateCommand CancelCommand { get; }
|
||
|
|
|
||
|
|
public CustomerEditDialogViewModel(
|
||
|
|
ICustomerService customerService,
|
||
|
|
IJeecgDictSyncService dictSyncService,
|
||
|
|
IContainerExtension container,
|
||
|
|
IRegionManager regionManager) : base(container, regionManager)
|
||
|
|
{
|
||
|
|
_customerService = customerService;
|
||
|
|
_dictSyncService = dictSyncService;
|
||
|
|
SaveCommand = new DelegateCommand(async () => await SaveAsync());
|
||
|
|
CancelCommand = new DelegateCommand(() => CloseAction?.Invoke());
|
||
|
|
_ = LoadDictOptionsAsync();
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task LoadDictOptionsAsync()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var statusOpts = await _dictSyncService.GetDictOptionsAsync("xslmes_customer_status");
|
||
|
|
var regionOpts = await _dictSyncService.GetDictOptionsAsync("xslmes_customer_region");
|
||
|
|
|
||
|
|
StatusOptions.Clear();
|
||
|
|
foreach (var item in statusOpts) StatusOptions.Add(item);
|
||
|
|
CustomerRegionOptions.Clear();
|
||
|
|
foreach (var item in regionOpts) CustomerRegionOptions.Add(item);
|
||
|
|
|
||
|
|
if (StatusOptions.Count == 0)
|
||
|
|
{
|
||
|
|
StatusOptions.Add(new KeyValuePair<string, string>("启用", "0"));
|
||
|
|
StatusOptions.Add(new KeyValuePair<string, string>("停用", "1"));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
StatusOptions.Clear();
|
||
|
|
StatusOptions.Add(new KeyValuePair<string, string>("启用", "0"));
|
||
|
|
StatusOptions.Add(new KeyValuePair<string, string>("停用", "1"));
|
||
|
|
CustomerRegionOptions.Clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void InitializeForAdd()
|
||
|
|
{
|
||
|
|
Customer = new MesXslCustomer { Status = "0" };
|
||
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
||
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void InitializeForEdit(MesXslCustomer customer)
|
||
|
|
{
|
||
|
|
Customer = new MesXslCustomer
|
||
|
|
{
|
||
|
|
Id = customer.Id,
|
||
|
|
CustomerCode = customer.CustomerCode,
|
||
|
|
CustomerName = customer.CustomerName,
|
||
|
|
CustomerShortName = customer.CustomerShortName,
|
||
|
|
CustomerRegion = customer.CustomerRegion,
|
||
|
|
ErpCode = customer.ErpCode,
|
||
|
|
Status = customer.Status,
|
||
|
|
IzEnable = customer.IzEnable,
|
||
|
|
CustomerDesc = customer.CustomerDesc,
|
||
|
|
TenantId = customer.TenantId,
|
||
|
|
};
|
||
|
|
RaisePropertyChanged(nameof(IsAddMode));
|
||
|
|
RaisePropertyChanged(nameof(DialogTitle));
|
||
|
|
}
|
||
|
|
|
||
|
|
private async Task SaveAsync()
|
||
|
|
{
|
||
|
|
if (Customer == null) return;
|
||
|
|
if (string.IsNullOrWhiteSpace(Customer.CustomerCode))
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("客户编码不能为空!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (string.IsNullOrWhiteSpace(Customer.CustomerName))
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Warning("客户名称不能为空!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
bool ok;
|
||
|
|
if (IsAddMode)
|
||
|
|
{
|
||
|
|
ok = await _customerService.AddAsync(Customer);
|
||
|
|
if (ok) HandyControl.Controls.MessageBox.Success("新增客户成功!");
|
||
|
|
else { HandyControl.Controls.MessageBox.Error("新增客户失败!"); return; }
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
ok = await _customerService.EditAsync(Customer);
|
||
|
|
if (!ok) { HandyControl.Controls.MessageBox.Error("编辑客户失败!"); return; }
|
||
|
|
}
|
||
|
|
Result = ok;
|
||
|
|
CloseAction?.Invoke();
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
HandyControl.Controls.MessageBox.Error($"操作失败:{ex.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|