lg
2024-09-22 c115ac388af5a6928716f0146a75a2a42fb27d8e
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using DevExpress.XtraBars;
using Gs.DevApp.Entity;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
 
namespace Gs.DevApp.UserControl
{
    public partial class ToolBarMenu : DevExpress.XtraEditors.XtraUserControl
    {
        public bool isSetBtn { get; set; }
        public List<string> actions { get; }
        /// <summary>
        /// 新增
        /// </summary>
        public event EventHandler btnAddClick;
 
        /// <summary>
        /// 删除
        /// </summary>
        public event EventHandler btnDelClick;
 
        /// <summary>
        /// 修改
        /// </summary>
        public event EventHandler btnEdtClick;
 
        /// <summary>
        /// 刷新
        /// </summary>
        public event EventHandler btnLoadClick;
 
        /// <summary>
        /// 保存
        /// </summary>
        public event EventHandler btnSaveClick;
 
        /// <summary>
        /// 取消
        /// </summary>
        public event EventHandler btnEscClick;
 
        /// <summary>
        /// 开工
        /// </summary>
        public event EventHandler btnKGClick;
 
        /// <summary>
        /// 权限
        /// </summary>
        public event EventHandler btnRoleClick;
 
        /// <summary>
        /// 权限
        /// </summary>
        public event EventHandler btnReportClick;
 
        /// <summary>
        /// 权限
        /// </summary>
        public event EventHandler btnQueryClick;
 
 
        public ToolBarMenu()
        {
            InitializeComponent();
            this.btnAdd.ItemClick += BtnAdd_ItemClick;
            this.btnDel.ItemClick += BtnDel_ItemClick;
            this.btnEdit.ItemClick += BtnEdit_ItemClick;
            this.btnLoad.ItemClick += BtnLoad_ItemClick;
            this.btnSave.ItemClick += BtnSave_ItemClick;
            this.btnKG.ItemClick += BtnKG_ItemClick;
            this.btnEsc.ItemClick += BtnEsc_ItemClick;
            this.btnRole.ItemClick += BtnRole_ItemClick;
            this.btnReport.ItemClick += BtnReport_ItemClick;
            this.btnQuery.ItemClick += BtnQuery_ItemClick;
            List<string> lst = new List<string>();
            foreach (BarItem item in barManager1.Items)
            {
                if (item is BarLargeButtonItem largeButtonItem)
                {
                    if (!string.IsNullOrEmpty(item.Caption))
                        lst.Add(item.Caption);
                }
            }
            this.actions = lst;
            //初始化按钮状态
            // initialization();
        }
 
        private void BtnQuery_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (btnQueryClick != null)
            {
                btnQueryClick(this, e);
            }
        }
 
        private void BtnReport_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (btnReportClick != null)
            {
                btnReportClick(this, e);
            }
        }
        /// <summary>
        /// 初始化按钮状态
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (this.Parent != null)
            {
                getNamespace();
            }
        }
 
        private void BtnEsc_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (MsgHelper.AskQuestion("确定要要取消吗?"))
            {
                List<string> lst = new List<string>();
                lst.Add(btnAdd.Name);
                lst.Add(btnEdit.Name);
                lst.Add(btnLoad.Name);
                lst.Add(btnDel.Name);
                lst.Add(btnRole.Name);
                _enabledBtn(lst);
                if (btnEscClick != null)
                {
                    btnEscClick(this, e);
                }
            }
        }
 
        private void BtnKG_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (btnKGClick != null)
            {
                btnKGClick(this, e);
            }
        }
 
        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (btnSaveClick != null)
            {
                btnSaveClick(this, e);
                if (isSetBtn == true)
                {
                    List<string> lst = new List<string>();
                    lst.Add(btnAdd.Name);
                    lst.Add(btnEdit.Name);
                    lst.Add(btnEsc.Name);
                    _enabledBtn(lst);
                }
            }
        }
 
        private void BtnLoad_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
 
            if (btnLoadClick != null)
            {
                //List<string> lst = new List<string>();
                //lst.Add(btnRole.Name);
                //setBtn(lst);
                btnLoadClick(this, e);
            }
 
        }
 
        private void BtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<string> lst = new List<string>();
            lst.Add(btnSave.Name);
            lst.Add(btnEsc.Name);
 
            _enabledBtn(lst);
            if (btnEdtClick != null)
            {
                btnEdtClick(this, e);
            }
        }
 
        private void BtnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (btnDelClick != null)
            {
                btnDelClick(this, e);
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<string> lst = new List<string>();
            lst.Add(btnSave.Name);
            lst.Add(btnEsc.Name);
            _enabledBtn(lst);
            if (btnAddClick != null)
            {
                btnAddClick(this, e);
            }
 
        }
 
        private void BtnRole_ItemClick(object sender, ItemClickEventArgs e)
        {
            //List<string> lst = new List<string>();
            //lst.Add(btnSave.Name);
            //lst.Add(btnEsc.Name);
            //setBtn(lst);
            if (btnRoleClick != null)
            {
                btnRoleClick(this, e);
            }
        }
        private void _enabledBtn(List<string> lstBtn)
        {
            foreach (BarItem item in barManager1.Items)
            {
                if (item is BarLargeButtonItem largeButtonItem)
                {
                    if (lstBtn.Contains(item.Name))
                        largeButtonItem.Enabled = true;
                    else
                        largeButtonItem.Enabled = false;
                }
            }
        }
 
        /// <summary>
        /// 根据命名空间和权限,读取该窗体下面的按钮集合
        /// </summary>
        private void getNamespace()
        {
            string _formNamespace = "";
            Control parentControl = this;
            while (parentControl.Parent != null)
            {
                if (parentControl.Parent is Form parentForm)
                {
                    Type formType = parentForm.GetType();
                    _formNamespace = formType.FullName;
                    break;
                }
                parentControl = parentControl.Parent;
            }
            var _obj = new
            {
                userGuid = LoginInfoModel.CurrentUser.LoginUserGuid,
                orgGuid = LoginInfoModel.CurrentUser.LoginOrgGuid,
                formNamespace = _formNamespace,
            };
            string strReturn = "";
            try
            {
                strReturn = UtilityHelper.HttpPost("", "Role/GetListByUserOrgSpace", JsonConvert.SerializeObject(_obj));
                ReturnModel<DataTable> dd = UtilityHelper.ReturnToList(strReturn);
                DataTable dt = dd.rtnData;
                List<string> _enabledList = new List<string>();
                foreach (BarItem item in barManager1.Items)
                {
                    ///具有联动关系的按钮(增加),
                    if (item.Caption == "增加")
                    {
                        bool ld1 = dt.AsEnumerable().Any(row => row.Field<string>("name") == "增加"
                      //|| row.Field<string>("name") == "修改"
                      );
                        if (ld1 == true)
                        {
                            btnEsc.Visibility = btnSave.Visibility = btnLoad.Visibility = BarItemVisibility.Always;
                            btnAdd.Visibility = BarItemVisibility.Always;
                            _enabledList.Add(item.Name);
                            continue;
                        }
                    }
                    ///具有联动关系的按钮(修改),
                    if (item.Caption == "修改")
                    {
                        bool ld1 = dt.AsEnumerable().Any(row => row.Field<string>("name") == "修改"
                      );
                        if (ld1 == true)
                        {
                            btnEsc.Visibility = btnSave.Visibility = btnLoad.Visibility = BarItemVisibility.Always;
                            btnEdit.Visibility = BarItemVisibility.Always;
                            _enabledList.Add(item.Name);
                            continue;
                        }
                    }
                    ///具有联动关系的按钮(查询),
                    bool ld2 = dt.AsEnumerable().Any(row => row.Field<string>("name") == "查询"
                    );
                    if (ld2 == true)
                    {
                        if (item.Caption == "刷新")
                        {
                            item.Visibility = BarItemVisibility.Always;
                            //准备启用
                            _enabledList.Add(item.Name);
                            continue;
                        }
                    }
                    //按钮是否在权限表中
                    bool exists = dt.AsEnumerable().Any(row => row.Field<string>("name") == item.Caption);
                    if (exists == true)
                    {
                        item.Visibility = BarItemVisibility.Always;
                        //准备启用
                        string _btnName = _FindBarLargeButtonItemByCaption(item.Caption);
                        if (!string.IsNullOrEmpty(_btnName))
                            _enabledList.Add(_btnName);
                    }
                }
                _enabledBtn(_enabledList);
            }
            catch (Exception ex)
            {
                ToolBox.MsgHelper.Warning("提示:" + ex.Message);
            }
        }
 
 
        private string _FindBarLargeButtonItemByCaption(string caption)
        {
            foreach (BarItem item in barManager1.Items)
            {
                if (item is BarLargeButtonItem largeButtonItem && largeButtonItem.Caption == caption)
                {
                    return largeButtonItem.Name;
                }
            }
            return null;
        }
    }
}