using Mapster; using MaterialDesignThemes.Wpf; using Prism.Commands; using Prism.Services.Dialogs; using ProjectManagementSystem.Common.Config; using ProjectManagementSystem.Common.Extenions; using ProjectManagementSystem.Common.Models; using ProjectManagementSystem.Common.Service; using ProjectManagementSystemView.Infrastructure; using ProjectManagementSystemView.Infrastructure.Models; using PropertyChanged; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProjectManagementSystemView.ViewModels.Dialogs { [AddINotifyPropertyChangedInterface] public class SimpleTaskViewModel : IDialogHostAware { public ObservableCollection WareHouseCodeCollection1 { get; set; } public ObservableCollection LocationCodeCollection1 { get; set; } public ObservableCollection MaterialIdCollection { get; set; } public ObservableCollection WareHouseCodeCollection2 { get; set; } public ObservableCollection LocationCodeCollection2 { get; set; } public string Title { get; set; } public bool IsTaskCall { get; set; } public bool IsTaskTransport { get; set; } [OnChangedMethod(nameof(OnSourceWareHouseCodeChanged))] public string SourceWareHouseCode { get; set; } [OnChangedMethod(nameof(OnSourceLocationCodeChanged))] public string SourceLocationCode { get; set; } public string SourceMaterialId { get; set; } [OnChangedMethod(nameof(OnTargetWareHouseCodeChanged))] public string TargetWareHouseCode { get; set; } public string TargetLocationCode { get; set; } public string DialogHostName { get; set; } public DelegateCommand SaveCommand { get; set; } public DelegateCommand CancelCommand { get; set; } public SimpleTaskViewModel() { WareHouseCodeCollection1 = new ObservableCollection(); LocationCodeCollection1 = new ObservableCollection(); MaterialIdCollection = new ObservableCollection(); WareHouseCodeCollection2 = new ObservableCollection(); LocationCodeCollection2 = new ObservableCollection(); SaveCommand = new DelegateCommand(Save); CancelCommand = new DelegateCommand(Cancel); } private void OnSourceWareHouseCodeChanged() { LocationCodeCollection1.Clear(); var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { WareHouseCode = SourceWareHouseCode }); var dataList = res?.data as List; dataList?.ForEach(p => LocationCodeCollection1.Add(p.LocationCode)); LocationCodeCollection1.Add(""); MaterialIdCollection.Clear(); var mDataList = dataList.Where(d => !string.IsNullOrEmpty(d.MaterialId)).Select(d => d.MaterialId).Distinct().ToList(); mDataList.ForEach(p => MaterialIdCollection.Add(p)); MaterialIdCollection.Add(""); } private void OnTargetWareHouseCodeChanged() { LocationCodeCollection2.Clear(); var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { WareHouseCode = TargetWareHouseCode }); var dataList = res?.data as List; dataList?.ForEach(p => LocationCodeCollection2.Add(p.LocationCode)); LocationCodeCollection2.Add(""); } private void OnSourceLocationCodeChanged() { if (string.IsNullOrEmpty(SourceLocationCode)) { OnSourceWareHouseCodeChanged(); return; } var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { LocationCode = SourceLocationCode }); var dataList = res?.data as List; var data = dataList?.FirstOrDefault(); if (data != null) { MaterialIdCollection.Clear(); MaterialIdCollection.Add(data.MaterialId); SourceMaterialId = data.MaterialId; } } /// /// 取消 /// private void Cancel() { if (DialogHost.IsDialogOpen(DialogHostName)) DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No)); //取消返回NO告诉操作结束 } /// /// 确定 /// private void Save() { if (DialogHost.IsDialogOpen(DialogHostName)) { var model1 = new VmLocationProperty() { WareHouseCode = SourceWareHouseCode, LocationCode = SourceLocationCode, MaterialId = SourceMaterialId }; var model2 = new VmLocationProperty() { WareHouseCode = TargetWareHouseCode, LocationCode = TargetLocationCode }; //确定时,把编辑的实体返回并且返回OK DialogParameters param = new DialogParameters(); param.Add("Value1", model1); param.Add("Value2", model2); DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.OK, param)); } } public void OnDialogOpend(IDialogParameters parameters) { if (parameters.ContainsKey("Value1") && parameters.ContainsKey("Value2")) { Title = parameters.GetValue("Title"); var data1 = parameters.GetValue("Value1"); var data2 = parameters.GetValue("Value2"); WareHouseCodeCollection1.Clear(); LocationCodeCollection1.Clear(); MaterialIdCollection.Clear(); WareHouseCodeCollection2.Clear(); LocationCodeCollection2.Clear(); IsTaskCall = data1 == null && data2 != null; IsTaskTransport = data1 != null && data2 == null; if (IsTaskTransport) { WareHouseCodeCollection1.Add(data1.WareHouseCode); LocationCodeCollection1.Add(data1.LocationCode); MaterialIdCollection.Add(data1.MaterialId); SourceWareHouseCode = data1.WareHouseCode; SourceLocationCode = data1.LocationCode; SourceMaterialId = data1.MaterialId; var targetLocations = data1.LocationCode.GetLocationMember("目标仓位").ToValueArray(); if (targetLocations.Length > 0) { foreach (var location in targetLocations) { LocationCodeCollection2.Add(location); } TargetLocationCode = targetLocations.First(); } else { var arr = data1.LocationCode.GetLocationMember("目标仓库").ToValueArray(); for (int i = 0; i < arr.Length; i++) { WareHouseCodeCollection2.Add(arr[i]); } if (arr.Length > 0) { TargetWareHouseCode = arr[0]; } } } if (IsTaskCall) { var sourceLocations = data2.LocationCode.GetLocationMember("源仓位").ToValueArray(); if (sourceLocations.Length > 0) { foreach (var location in sourceLocations) { LocationCodeCollection1.Add(location); } SourceLocationCode = sourceLocations.First(); } else { var arr = data2.LocationCode.GetLocationMember("源仓库").ToValueArray(); for (int i = 0; i < arr.Length; i++) { WareHouseCodeCollection1.Add(arr[i]); } if (arr.Length > 0) { SourceWareHouseCode = arr[0]; } } WareHouseCodeCollection2.Add(data2.WareHouseCode); LocationCodeCollection2.Add(data2.LocationCode); TargetWareHouseCode = data2.WareHouseCode; TargetLocationCode = data2.LocationCode; } } } } }