using Mapster; using Prism.Commands; using Prism.Events; using Prism.Services.Dialogs; using ProjectManagementSystem.Common.Config; using ProjectManagementSystem.Common.Extenions; using ProjectManagementSystem.Common.Function; using ProjectManagementSystem.Common.Logger; using ProjectManagementSystem.Common.Models; using ProjectManagementSystem.Common.Models.Crms; using ProjectManagementSystem.Common.Service; using ProjectManagementSystem.Language; using ProjectManagementSystemView.Infrastructure; using ProjectManagementSystemView.Infrastructure.Events; using ProjectManagementSystemView.Infrastructure.Extensions; 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 { [AddINotifyPropertyChangedInterface] public class WmsQueryViewModel { private List wareHouses; protected readonly IDialogHostService dialogService; public bool IsVisible { get; set; } [OnChangedMethod(nameof(ReloadLayout))] public string WareHouseCode { get; set; } public VmLocationProperty SelectedVmData { get; set; } public ObservableCollection VmDatas { get; set; } = new ObservableCollection(); public ObservableCollection WareHouseCollection { get; set; } = new ObservableCollection(); public DelegateCommand LoadedCommand { get; private set; } public DelegateCommand ModifyCommand { get; private set; } public DelegateCommand TaskCallCommand { get; private set; } public DelegateCommand TaskTransportCommand { get; private set; } public DelegateCommand ReleaseAgvCommand { get; private set; } public WmsQueryViewModel(IDialogHostService dialogService, IEventAggregator eventAggregator) { this.dialogService = dialogService; var wareHouseFilters = AppSetting.TryGetValue("WareHouseFilter1").ToValueArray().ToList(); var wareHouseList = ExcelConfig.Instance.RouteConfigList .Where(d => !string.IsNullOrEmpty(d.Area)) .Where(d => { if (wareHouseFilters.Count == 0) return true; return wareHouseFilters.Contains(d.Area); }) .GroupBy(d => d.Area) .Select(d => d.Key) .OrderBy(d => wareHouseFilters.IndexOf(d)); wareHouses = wareHouseFilters.Count == 0 ? wareHouseList.OrderBy(d => d).ToList() : wareHouseList.ToList(); wareHouses.ForEach(p => WareHouseCollection.Add(p)); if (wareHouseFilters.Count == 0) { WareHouseCollection.Add(string.Empty); WareHouseCode = WareHouseCollection.Last(); } else { WareHouseCode = WareHouseCollection.First(); } LoadedCommand = new DelegateCommand(Loaded); ModifyCommand = new DelegateCommand(ModifyAsync); TaskCallCommand = new DelegateCommand((arg) => SimpleTaskAddAsync(arg, null, SelectedVmData)); TaskTransportCommand = new DelegateCommand((arg) => SimpleTaskAddAsync(arg, SelectedVmData, null)); ReleaseAgvCommand = new DelegateCommand(ReleaseAgv); ResourceService.Current.CultureChanged += d => ReloadLayout(); eventAggregator.GetEvent().Subscribe(Read); } private void Read() { if (!IsVisible) return; try { var res = Crms.PmsTaskService.GetLocationProperty(new LocationPropertyQueryDto() { Area = WareHouseCode }); var dataList = res?.data as List; UpdateVmDatas(dataList); } catch (Exception ex) { HandyControl.Controls.Growl.Error(ex.Message); } } protected virtual void UpdateVmDatas(List dataList) { VmDatas.Update(dataList, (s, v) => s.LocationCode == v.LocationCode); } protected virtual void Loaded() { } protected virtual void ReloadLayout() { VmDatas.Clear(); } private async void ModifyAsync() { try { if (SelectedVmData == null) return; DialogParameters param = new DialogParameters(); param.Add("Value", SelectedVmData); var dialogResult = await dialogService.ShowDialog("ModifyWmsView", param); if (dialogResult.Result == ButtonResult.OK) { var data = dialogResult.Parameters.GetValue("Value"); UpdateLocation(data); } } catch (Exception ex) { CLog.Instance.SystemLog.WriteExceptionCaller(ex); } } private async void SimpleTaskAddAsync(string arg, VmLocationProperty value1, VmLocationProperty value2) { try { if (value1 == null && value2 == null) return; if (value1 != null && value2 == null) { var temp = value1.LocationCode.GetLocationMember("目标仓库"); var temp2 = value1.LocationCode.GetLocationMember("目标仓位"); if (string.IsNullOrEmpty(temp) && string.IsNullOrEmpty(temp2)) { HandyControl.Controls.Growl.Info($"{value1.LocationCode} {Langs.TargetWarehouseNotConfigured}"); return; } } if (value1 == null && value2 != null) { var temp = value2.LocationCode.GetLocationMember("源仓库"); var temp2 = value2.LocationCode.GetLocationMember("源仓位"); if (string.IsNullOrEmpty(temp) && string.IsNullOrEmpty(temp2)) { HandyControl.Controls.Growl.Info($"{value2.LocationCode} {Langs.SourceWarehouseNotConfigured}"); return; } } DialogParameters param = new DialogParameters(); param.Add("Title", arg); param.Add("Value1", value1); param.Add("Value2", value2); var dialogResult = await dialogService.ShowDialog("SimpleTaskView", param); if (dialogResult.Result == ButtonResult.OK) { try { var data1 = dialogResult.Parameters.GetValue("Value1"); var data2 = dialogResult.Parameters.GetValue("Value2"); var dto = new PmsTaskInfoDto(); dto.WareHouseCode = data1.WareHouseCode; dto.LocationCode = data1.LocationCode; dto.MaterialId = data1.MaterialId; dto.TargetWareHouseCode = data2.WareHouseCode; dto.TargetLocationCode = data2.LocationCode; var result = PmsCustomApi.Post("TaskAdd", dto); if (result.code == 20000) { HandyControl.Controls.Growl.Success(result.message); } else { HandyControl.Controls.Growl.Warning(result.message); } } catch (Exception ex) { CLog.Instance.SystemLog.WriteExceptionCaller(ex); } } } catch (Exception ex) { CLog.Instance.SystemLog.WriteExceptionCaller(ex); } } private void UpdateLocation(VmLocationProperty locationData) { try { if (string.IsNullOrEmpty(locationData?.LocationCode)) return; var dto = locationData.Adapt(); var result = PmsCustomApi.Post("UpdateLocationProperty", dto); if (result.code == 20000) { HandyControl.Controls.Growl.Success(result.message); } else { HandyControl.Controls.Growl.Warning(result.message); } } catch (Exception ex) { CLog.Instance.SystemLog.WriteExceptionCaller(ex); } } private void ReleaseAgv() { try { var dto = new { LocationCode = SelectedVmData?.LocationCode }; var result = PmsCustomApi.Post("ReleaseAgv", dto); if (result.code == 20000) { HandyControl.Controls.Growl.Success(result.message); } else { HandyControl.Controls.Growl.Warning(result.message); } } catch (Exception ex) { CLog.Instance.SystemLog.WriteExceptionCaller(ex); } } } }