Chr
2024-08-23 12acdf14fcddae8d16e00b7b981559da67ce60ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
{
    /// <summary>
    /// 分部类,主要是与web交互的方法
    /// </summary>
    public partial class MaterialSelectControl
    {
 
        private Timer _timerCache;
        public Timer TimerCache
        {
            get => _timerCache;
            set => RaiseAndSetIfChanged(ref _timerCache, value);
        }
 
        /// <summary>
        /// 业务类型字段变更后,查询AutoSelect
        /// </summary>
        /// <param name="value">变更后的字符</param>
        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<string>();
                        TimerCache = null;
                    }));
                    return;
                }
                HttpClient c = clientCreator.GetClient();
                Result<ObservableCollection<BusinessType>> result =
                    c.GetSyncAction<ObservableCollection<BusinessType>>("openApi/wpf/bt/list", new BusinessType()
                    {
                        no = value
                    });
                ObservableCollection<BusinessType> bts = result.result;
                if (bts != null && bts.Count > 0)
                {
                    ObservableCollection<string> btNames = new ObservableCollection<string>();
                    Dictionary<string, string> btDict = new Dictionary<string, string>();
                    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;
        }
 
        /// <summary>
        /// 查询物料异步函数
        /// </summary>
        /// <param name="pageNo">第几页</param>
        /// <param name="pageSize">查几条</param>
        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<Page<PdmPart>> result =
                     await c.PostAsyncAction<Page<PdmPart>>(url, new PdmPart()
                     {
                         partNo = PartNo,
                         partModel = PartModel,
                         partBrandName = PartBrandName,
                         partBussinesstype = partBussinesstype,
                         specs = (ObservableCollection<PartSpec>)specTable.ItemsSource,
                         column = Column,
                         order = Order
                     });
                Page<PdmPart> pageList = result.HandleResult();
                UpdatePage(result.result);
            }
            catch (Exception e)
            {
                MessageBox.Show("查询物料失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                Logger.Error("查询物料失败!异常:{0}", e);
            }
            finally
            {
                MaskAdorner.HideMask(table);
            }
        }
 
        /// <summary>
        /// 查询规格列表
        /// </summary>
        /// <param name="businessTypeName">业务类型拼接名称</param>
        private async void SelectBussinesstypeSpec()
        {
            string businessTypeName = SelectedBtName;
            if (businessTypeName == null)
            {
                specTable.ItemsSource = new ObservableCollection<PartSpec>();
                return;
            }
            try
            {
                HttpClient c = clientCreator.GetClient();
                Result<ObservableCollection<PartSpec>> result =
                    await c.GetAsyncAction<ObservableCollection<PartSpec>>("openApi/wpf/bt/spec/list", new BusinessType()
                    {
                        id = BtCache[businessTypeName]
                    });
                ObservableCollection<PartSpec> specs = result.HandleResult();
                specTable.ItemsSource = specs;
            }
            catch (Exception e)
            {
                MessageBox.Show("查询规格失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                Logger.Error("查询规格失败!异常:{0}", e);
            }
        }
    }
}