using PdmSwPlugin.Commmon.Util.UI; using PdmSwPlugin.Common.Entity; using PdmSwPlugin.Common.Util.Http; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Net.Http; using System.Timers; using System.Windows; namespace PdmSwPlugin.MaterialSelect { /// /// 分部类,主要是与web交互的方法 /// public partial class MaterialSelectControl { private Timer _timerCache; public Timer TimerCache { get => _timerCache; set => RaiseAndSetIfChanged(ref _timerCache, value); } /// /// 业务类型字段变更后,查询AutoSelect /// /// 变更后的字符 private void SelectBussinessType() { /// 使用延时函数,避免太过频繁的向web发送查询请求 long interval = 500; Timer timer = TimerCache; if (timer != null) { timer.Interval = interval; return; } TimerCache = new Timer(interval); timer = TimerCache; timer.Elapsed += (sender, e) => { timer.Enabled = false; string value = PartBussinesstypeName; if (string.IsNullOrEmpty(value)) { Dispatcher.Invoke(new Action(delegate { btInput.IsDropDownOpen = false; btInput.ItemsSource = new ObservableCollection(); TimerCache = null; })); return; } HttpClient c = clientCreator.GetClient(); Result> result = c.GetSyncAction>("openApi/wpf/bt/list", new BusinessType() { no = value }); ObservableCollection bts = result.result; if (bts != null && bts.Count > 0) { ObservableCollection btNames = new ObservableCollection(); Dictionary btDict = new Dictionary(); foreach (BusinessType bt in bts) { btDict.Add(bt.name, bt.id); btNames.Add(bt.name); } Dispatcher.Invoke(new Action(delegate { btInput.ItemsSource = btNames; BtCache = btDict; btInput.IsDropDownOpen = true; TimerCache = null; })); } else { Dispatcher.Invoke(new Action(delegate { TimerCache = null; })); } }; timer.Enabled = true; } /// /// 查询物料异步函数 /// /// 第几页 /// 查几条 private async void HttpSelectPartWithSpec(long pageNo, long pageSize) { if (Loading) { MessageBox.Show("请等待...", "提示"); return; } MaskAdorner.ShowMask(table); try { string url = $"openApi/wpf/material/list-with-spec/{pageNo}/{pageSize}"; string partBussinesstype = string.IsNullOrWhiteSpace(PartBussinesstypeName) ? null : (BtCache == null ? "" : BtCache.ContainsKey(PartBussinesstypeName) ? BtCache[PartBussinesstypeName] : ""); HttpClient c = clientCreator.GetClient(); Result> result = await c.PostAsyncAction>(url, new PdmPart() { partNo = PartNo, partModel = PartModel, partBrandName = PartBrandName, partBussinesstype = partBussinesstype, specs = (ObservableCollection)specTable.ItemsSource, column = Column, order = Order }); Page pageList = result.HandleResult(); UpdatePage(result.result); } catch (Exception e) { MessageBox.Show("查询物料失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); Logger.Error("查询物料失败!异常:{0}", e); } finally { MaskAdorner.HideMask(table); } } /// /// 查询规格列表 /// /// 业务类型拼接名称 private async void SelectBussinesstypeSpec() { string businessTypeName = SelectedBtName; if (businessTypeName == null) { specTable.ItemsSource = new ObservableCollection(); return; } try { HttpClient c = clientCreator.GetClient(); Result> result = await c.GetAsyncAction>("openApi/wpf/bt/spec/list", new BusinessType() { id = BtCache[businessTypeName] }); ObservableCollection specs = result.HandleResult(); specTable.ItemsSource = specs; } catch (Exception e) { MessageBox.Show("查询规格失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); Logger.Error("查询规格失败!异常:{0}", e); } } } }