懒羊羊
2023-12-28 e46d3baaf3e8d7d85f4bafec3aad75e52b078408
提交 | 用户 | 时间
e46d3b 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using YX.BLL;
10 using YX.Common.DotNetEncrypt;
11 using YX.Entity;
12
13 namespace YX
14 {
15     public partial class FrmUserInfoEdit : Form
16     {
17         FrmUserInfo _frmUserInfo;
18         SystemAppendProperty_Bll app_bll = new SystemAppendProperty_Bll();
19         SystemMenu_Bll menu_bll = new SystemMenu_Bll();
20         SystemUserInfo_Bll user_bll = new SystemUserInfo_Bll();
21         SystemOrganization_Bll org_bll = new SystemOrganization_Bll();
22         SystemRole_Bll role_bll = new SystemRole_Bll();
23         string User_ID = "";
24         OperationType type;
25         public FrmUserInfoEdit(FrmUserInfo frmUserInfo)
26         {
27            
28             this._frmUserInfo = frmUserInfo;
29             InitializeComponent();
30             this.Text = "用户信息-添加";
31             type = OperationType.Add;
32         }
33         public FrmUserInfoEdit(FrmUserInfo frmUserInfo,ref DataGridViewRow dvr)
34         {                 
35             InitializeComponent();
36             type = OperationType.Edit;
37             this._frmUserInfo = frmUserInfo;
38             User_ID = dvr.Cells["User_ID"].Value.ToString();
39             txt_Email.Text = dvr.Cells["邮箱"].Value.ToString();
40             txt_Title.Text = dvr.Cells["职称"].Value.ToString();
41             txt_User_Account.Text = dvr.Cells["登录账户"].Value.ToString();
42             txt_User_Code.Text = dvr.Cells["用户工号"].Value.ToString();
43             txt_User_Name.Text = dvr.Cells["用户名称"].Value.ToString();
44            // txt_User_Pwd.Text = dvr.Cells["登陆密码"].Value.ToString();
45             com_User_Sex.Text = dvr.Cells["性别"].Value.ToString();
46             richTextBox1.Text = dvr.Cells["备注"].Value.ToString();
47             this.Text = "用户信息-编辑";
48         }
49
50         private void FrmUserInfoEdit_Load(object sender, EventArgs e)
51         {
52           
53             GetAppendProperty();
54             BindUsertRightTreeView();
55             BindUserDeptTreeView();
56             BindRoleTreeView();
57             if (tree_Dept.Nodes.Count > 0)//展开一级节点
58             {
59                 tree_Dept.Nodes[0].Expand();
60             }
61             if (tree_UserRight.Nodes.Count > 0)//展开一级节点
62             {
63                 tree_UserRight.Nodes[0].Expand();
64             }
65             if (tree_UserRight.Nodes.Count > 0)//展开一级节点
66             {
67                 tree_UserRole.Nodes[0].Expand();
68             }
69         }
70         #region 用户权限
71         /// <summary>
72         /// 用户权限菜单树列表
73         /// </summary>
74         public void BindUsertRightTreeView()
75         {
76             try
77             {
78                 this.tree_UserRight.ImageList = imageList1;
79                 tree_UserRight.Nodes.Clear();
80                 var list = menu_bll.GetSysMenus();
81                 var user_right_list = menu_bll.GetUserRightByCondition(o=>o.Base_UserInfo.User_ID==User_ID);
82                 var parents = list.Where(o => o.ParentId == "0");
83                 foreach (var item in parents)
84                 {
85                     TreeNode tn = new TreeNode();
86                     tn.Text = item.Menu_Name;
87                     tn.Tag = item.Menu_Id;
88                     tn.ImageIndex = item.Menu_Img == null ? 0 : item.Menu_Img.Value;
89                     foreach (var user_right in user_right_list)
90                     {
91                         if (item.Menu_Id == user_right.Menu_Id)
92                         {
93                             tn.Checked = true;
94                         }
95                     }
96                     UserRightFillTree(tn, list, user_right_list);
97                     tree_UserRight.Nodes.Add(tn);
98                 }
99             }
100             catch (Exception ex)
101             {
102                 System_Bll.WriteLogToDB(new Entity.Base_Log
103                 {
104                     CreateUserID = FrmLogin.LoginUserID,
105                     CreateUserName = FrmLogin.loginUserName,
106                     LocalIP = FrmLogin.LocalIP,
107                     LogMessage = ex.Message,
108                     Type = "系统错误!",
109                     ClassName = typeof(FrmUserInfoEdit).ToString()
110                 });
111                 MessageBox.Show(ex.Message);
112             }
113         }
114
115         private void UserRightFillTree(TreeNode node, List<Base_SysMenu> list,List<Base_UserRight> user_right_list)
116         {
117
118             var childs = list.Where(o => o.ParentId == node.Tag.ToString());
119             if (childs.Count() > 0)
120             {
121                 foreach (var item in childs)
122                 {
123                     TreeNode tnn = new TreeNode();
124                     tnn.Text = item.Menu_Name;
125                     tnn.Tag = item.Menu_Id;
126                     tnn.ImageIndex = item.Menu_Img == null ? 0 : item.Menu_Img.Value;
127                     foreach (var user_right in user_right_list)
128                     {
129                         if (item.Menu_Id == user_right.Menu_Id)
130                         {
131                             tnn.Checked = true;
132                         }
133                     }
134                     if (item.ParentId == node.Tag.ToString())
135                     {
136                         UserRightFillTree(tnn, list, user_right_list);
137                     }
138                     node.Nodes.Add(tnn);
139                 }
140
141             }
142         } 
143         #endregion
144
145         #region 所属部门
146         /// <summary>
147         /// 用户权限菜单树列表
148         /// </summary>
149         public void BindUserDeptTreeView()
150         {
151             try
152             {
153                 tree_Dept.ImageList = imageList1;
154                 tree_Dept.Nodes.Clear();
155               
156                 var list = org_bll.GetOrganizations();
157                 var staff_list = org_bll.GetStaffOrganize(User_ID);
158                 var parents = list.Where(o => o.ParentId == "0");
159                 foreach (var item in parents)
160                 {
161                     TreeNode tn = new TreeNode();
162                     tn.Text = item.Organization_Name;
163                     tn.Tag = item.Organization_ID;
164                     tn.ImageIndex = 0;
165                     foreach (var staff in staff_list)
166                     {
167                         if (item.Organization_ID == staff.Organization_ID)
168                         {
169                             tn.Checked = true;
170                         }
171                     }
172
173                     UserDeptFillTree(tn, list,staff_list);
174                     tree_Dept.Nodes.Add(tn);
175                 }
176             }
177             catch (Exception ex)
178             {
179                 System_Bll.WriteLogToDB(new Entity.Base_Log
180                 {
181                     CreateUserID = FrmLogin.LoginUserID,
182                     CreateUserName = FrmLogin.loginUserName,
183                     LocalIP = FrmLogin.LocalIP,
184                     LogMessage = ex.Message,
185                     Type = "系统错误!",
186                     ClassName = typeof(FrmUserInfoEdit).ToString()
187                 });
188                 MessageBox.Show(ex.Message);
189             }
190         }
191
192         private void UserDeptFillTree(TreeNode node, List<Base_Organization> list,List<Base_StaffOrganize>staff_list)
193         {
194
195             var childs = list.Where(o => o.ParentId == node.Tag.ToString());
196             if (childs.Count() > 0)
197             {
198                 foreach (var item in childs)
199                 {
200                     TreeNode tnn = new TreeNode();
201                     tnn.Text = item.Organization_Name;
202                     tnn.Tag = item.Organization_ID;
203                     tnn.ImageIndex = 635;
204                     foreach(var staff in staff_list)
205                     {
206                         if (item.Organization_ID == staff.Organization_ID)
207                         {
208                             tnn.Checked = true;
209                         }
210                     }
211                    // tnn.Checked = true;
212                     if (item.ParentId == node.Tag.ToString())
213                     {
214                         UserDeptFillTree(tnn, list,staff_list);
215                     }
216                     node.Nodes.Add(tnn);
217                 }
218
219             }
220         }
221         #endregion
222
223         #region 所属角色
224         public void BindRoleTreeView()
225         {
226             try
227             {
228                 tree_UserRole.ImageList = imageList1;
229                 tree_UserRole.Nodes.Clear();
230                 var list = role_bll.GetRoles();
231                 var user_role_list = role_bll.GetUserRoleByUserID(User_ID);
232                 var parents = list.Where(o => o.ParentId == "0");
233                 foreach (var item in parents)
234                 {
235                     TreeNode tn = new TreeNode();
236                     tn.Text = item.Roles_Name;
237                     tn.Tag = item.Roles_ID;
238                     foreach (var user_role in user_role_list)
239                     {
240                         if (item.Roles_ID == user_role.Roles_ID)
241                         {
242                             tn.Checked = true;
243                         }
244                       
245                     }
246               
247                     tn.ImageIndex = 188;
248                     RoleFillTree(tn, list, user_role_list);
249                     tree_UserRole.Nodes.Add(tn);
250                 }
251             }
252             catch (Exception ex)
253             {
254
255                 System_Bll.WriteLogToDB(new Entity.Base_Log
256                 {
257                     CreateUserID = FrmLogin.LoginUserID,
258                     CreateUserName = FrmLogin.loginUserName,
259                     LocalIP = FrmLogin.LocalIP,
260                     LogMessage = ex.Message,
261                     Type = "系统错误!",
262                     ClassName = typeof(FrmUserInfoEdit).ToString()
263                 });
264                 MessageBox.Show(ex.Message);
265             }
266         }
267
268         private void RoleFillTree(TreeNode node, List<Base_Roles> list,List<Base_UserRole> user_role_list)
269         {
270
271             var childs = list.Where(o => o.ParentId == node.Tag.ToString());
272             if (childs.Count() > 0)
273             {
274                 foreach (var item in childs)
275                 {
276                     TreeNode tnn = new TreeNode();
277                     tnn.Text = item.Roles_Name;
278                     tnn.Tag = item.Roles_ID;
279                     tnn.ImageIndex = 188;
280                     foreach (var user_role in user_role_list)
281                     {
282                         if (item.Roles_ID == user_role.Roles_ID)
283                         {
284                             tnn.Checked = true;
285                         }
286
287                     }
288                     if (item.ParentId == node.Tag.ToString())
289                     {
290                         RoleFillTree(tnn, list, user_role_list);
291                     }
292                     node.Nodes.Add(tnn);
293                 }
294
295             }
296         }
297         #endregion
298
299         #region 附加属性
300         /// <summary>
301         /// 附加属性动态绘制
302         /// </summary>
303         public void GetAppendProperty()
304         {
305             try
306             {
307                 List<Base_AppendProperty> list = app_bll.AppendProperty_List("用户附加信息");
308                 List<Base_AppendPropertyInstance> listValue = app_bll.GetPropertyInstancepk("用户附加信息", User_ID);//获取附件字段值
309
310                 for (int i = 0; i < list.Count; i++)
311                 {
312
313                     Label lab = new Label();
314                     lab.Name = "lab_" + list[i].Property_Control_ID;
315                     lab.Text = list[i].Property_Name + ":";
316                     this.flowLayoutPanel1.Controls.Add(lab);
317                     lab.Size = new Size(65, 12);
318                     if (list[i].Property_Control_Style == "txt")
319                     {
320                         TextBox text = new TextBox();
321                         text.Name = list[i].Property_Control_ID;
322                         text.Size = new Size(list[i].Property_Control_Length, 21);
323                         foreach (var value in listValue)
324                         {
325                             if (text.Name == value.Property_Control_ID)
326                             {
327                                 text.Text = value.PropertyInstance_Value;
328                             }
329                         }
330                         this.flowLayoutPanel1.Controls.Add(text);
331                     }
332                     else if (list[i].Property_Control_Style == "select")
333                     {
334                         string[] strSource = list[i].Property_Control_DataSource.Split('|');
335                         ComboBox combox = new ComboBox();
336                         combox.Name = list[i].Property_Control_ID;
337                         combox.Size = new Size(list[i].Property_Control_Length, 21);
338                         combox.Text = strSource[0];
339                         foreach (var item in strSource)
340                         {
341                             combox.Items.Add(item);
342                         }
343                         foreach (var value in listValue)
344                         {
345                             if (combox.Name == value.Property_Control_ID)
346                             {
347                                 combox.Text = value.PropertyInstance_Value;
348                             }
349                         }
350                         this.flowLayoutPanel1.Controls.Add(combox);
351                     }
352                     else if (list[i].Property_Control_Style == "richText")
353                     {
354                         RichTextBox text = new RichTextBox();
355                         text.Name = list[i].Property_Control_ID;
356                         text.Size = new Size(list[i].Property_Control_Length, 100);
357                         foreach (var value in listValue)
358                         {
359                             if (text.Name == value.Property_Control_ID)
360                             {
361                                 text.Text = value.PropertyInstance_Value;
362                             }
363                         }
364                         this.flowLayoutPanel1.Controls.Add(text);
365                     }
366                     else if (list[i].Property_Control_Style == "date")
367                     {
368                         DateTimePicker date = new DateTimePicker();
369                         date.Name = list[i].Property_Control_ID;
370                         date.Size = new Size(list[i].Property_Control_Length, 21);
371                         foreach (var value in listValue)
372                         {
373                             if (date.Name == value.Property_Control_ID)
374                             {
375                                 date.Text = value.PropertyInstance_Value;
376                             }
377                         }
378                         this.flowLayoutPanel1.Controls.Add(date);
379                     }
380                 }
381             }
382             catch (Exception ex)
383             {
384                 System_Bll.WriteLogToDB(new Entity.Base_Log
385                 {
386                     CreateUserID = FrmLogin.LoginUserID,
387                     CreateUserName = FrmLogin.loginUserName,
388                     LocalIP = FrmLogin.LocalIP,
389                     LogMessage = ex.Message,
390                     Type = "系统错误!",
391                     ClassName = typeof(FrmUserInfoEdit).ToString()
392                 });
393                 MessageBox.Show(ex.Message);
394             }
395           
396         } 
397         #endregion
398
399         #region 保存事件
400         private void btn_save_Click(object sender, EventArgs e)
401         {
402             try
403             {
404                 Base_UserInfo userinfo = new Base_UserInfo();
405                 if (!string.IsNullOrEmpty(User_ID))
406                 {
407                     userinfo.ModifyDate = DateTime.Now;
408                     userinfo.ModifyUserId = FrmLogin.LoginUserID;
409                     userinfo.ModifyUserName = FrmLogin.loginUserName;
410
411                 }
412                 else
413                 {
414                     userinfo.CreateDate = DateTime.Now;
415                     userinfo.CreateUserId = FrmLogin.LoginUserID;
416                     userinfo.CreateUserName = FrmLogin.loginUserName;
417
418                     User_ID = Guid.NewGuid().ToString();
419                 }
420                 userinfo.DeleteMark = 1;
421                 userinfo.Email = txt_Email.Text;
422                 userinfo.Title = txt_Title.Text;
423                 userinfo.User_Account = txt_User_Account.Text;
424                 userinfo.User_Code = txt_User_Code.Text;
425                 userinfo.User_ID = User_ID;
426                 userinfo.User_Name = txt_User_Name.Text;
427                 //userinfo.User_Pwd =Md5Helper.Md5( txt_User_Pwd.Text);
428                 userinfo.User_Remark = richTextBox1.Text;
429                 userinfo.User_Sex = com_User_Sex.Text;
430                 StaffOrgList.Clear();
431                 UserRightList.Clear();
432                 RoleList.Clear();
433                 CheckRoleTreeViewNode(tree_UserRole.Nodes);
434                 CheckUsertRightTreeViewNode(this.tree_UserRight.Nodes);//获取勾选值
435                 CheckUsertDeptTreeViewNode(this.tree_Dept.Nodes);//获取勾选值
436                 GetAllAppendValue();//获取附加属性值
437                 userinfo.Base_UserRight = UserRightList;
438                 userinfo.Base_StaffOrganize = StaffOrgList;
439                 userinfo.Base_UserRole = RoleList;
440                 userinfo.Base_AppendPropertyInstance = appendList;
441                 int isOk = 0;
442                 if (type == OperationType.Add)
443                 {
444                     isOk = user_bll.AddUserInfo(userinfo);
445                 }
446                 else
447                 {
448                     isOk = user_bll.EditUserInfo(userinfo);
449                 }
450
451                 if (isOk > 0)
452                 {
453                     MessageBox.Show("保存成功!");
454                     this.Close();
455                 }
456                 else
457                 {
458                     MessageBox.Show("保存失败!");
459                 }
460             }
461             catch (Exception ex)
462             {
463
464                 System_Bll.WriteLogToDB(new Entity.Base_Log
465                 {
466                     CreateUserID = FrmLogin.LoginUserID,
467                     CreateUserName = FrmLogin.loginUserName,
468                     LocalIP = FrmLogin.LocalIP,
469                     LogMessage = ex.Message,
470                     Type = "系统错误!",
471                     ClassName = typeof(FrmUserInfoEdit).ToString()
472                 });
473                 MessageBox.Show(ex.Message);
474             }
475         }
476         List<Base_UserRight> UserRightList = new List<Base_UserRight>();
477         List<Base_StaffOrganize> StaffOrgList = new List<Base_StaffOrganize>();
478         List<Base_AppendPropertyInstance> appendList = new List<Base_AppendPropertyInstance>();
479         List<Base_UserRole> RoleList = new List<Base_UserRole>();
480         /// <summary>
481         /// 遍历用户权限选中节点
482         /// </summary>
483         /// <param name="node"></param>
484         public void CheckUsertRightTreeViewNode(TreeNodeCollection node)
485         {
486        
487             foreach (TreeNode n in node)
488             {
489                 if (n.Checked)
490                 {
491                     UserRightList.Add(new Base_UserRight { UserRight_ID = Guid.NewGuid().ToString(), Menu_Id = n.Tag.ToString(),
492                         CreateUserName=FrmLogin.loginUserName, CreateUserId=FrmLogin.LoginUserID, CreateDate=DateTime .Now });
493                 }
494                 CheckUsertRightTreeViewNode(n.Nodes);
495             }
496         }
497         /// <summary>
498         /// 遍历所属部门选中节点
499         /// </summary>
500         /// <param name="node"></param>
501         public void CheckUsertDeptTreeViewNode(TreeNodeCollection node)
502         {
503         
504             foreach (TreeNode n in node)
505             {
506                 if (n.Checked)
507                 {
508                     StaffOrgList.Add(new Base_StaffOrganize
509                     {
510                         StaffOrganize_Id = Guid.NewGuid().ToString(),
511                         Organization_ID = n.Tag.ToString(),
512                         CreateUserName = FrmLogin.loginUserName,
513                         CreateUserId = FrmLogin.LoginUserID,
514                         CreateDate = DateTime.Now 
515                       
516                     });
517                 }
518                 CheckUsertDeptTreeViewNode(n.Nodes);
519             }
520         } 
521         /// <summary>
522         /// 遍历附加属性
523         /// </summary>
524         public void GetAllAppendValue()
525         {
526             foreach(var control in this.flowLayoutPanel1.Controls)
527             {
528                 //遍历所有TextBox...
529                 if (control is TextBox)
530                 {
531                     TextBox t = (TextBox)control;
532                     if(!string.IsNullOrEmpty(t.Text))
533                     {
534                         appendList.Add(new Base_AppendPropertyInstance { PropertyInstance_ID = Guid.NewGuid().ToString(), Property_Control_ID = t.Name, PropertyInstance_Value = t.Text });
535                     }
536               
537                 }
538                 if(control is RichTextBox)
539                 {
540                     RichTextBox r = (RichTextBox)control;
541                     if (!string.IsNullOrEmpty(r.Text))
542                     {
543                         appendList.Add(new Base_AppendPropertyInstance { PropertyInstance_ID = Guid.NewGuid().ToString(), Property_Control_ID = r.Name, PropertyInstance_Value = r.Text });
544                     }
545                   
546                 }
547                 if(control is ComboBox)
548                 {
549                     ComboBox c = (ComboBox)control;
550                     if(!string.IsNullOrEmpty(c.Text)&& c.Text!= "==请选择==")
551                     {
552                         appendList.Add(new Base_AppendPropertyInstance { PropertyInstance_ID = Guid.NewGuid().ToString(), Property_Control_ID = c.Name, PropertyInstance_Value = c.Text });
553                     }
554                     
555                 }
556                // 遍历所有DateTimePicker...
557                 if (control is DateTimePicker)
558                 {
559                     DateTimePicker d = (DateTimePicker)control;
560                     if (!string.IsNullOrEmpty(d.Text))
561                     {
562                         appendList.Add(new Base_AppendPropertyInstance { PropertyInstance_ID = Guid.NewGuid().ToString(), Property_Control_ID = d.Name, PropertyInstance_Value = d.Text });
563                     }
564                 }
565             }
566         }
567         /// <summary>
568         /// 遍历角色权限选中节点
569         /// </summary>
570         /// <param name="node"></param>
571         public void CheckRoleTreeViewNode(TreeNodeCollection node)
572         {
573
574             foreach (TreeNode n in node)
575             {
576                 if (n.Checked)
577                 {
578                     RoleList.Add(new Base_UserRole
579                     {                        
580                         UserRole_ID = Guid.NewGuid().ToString(),
581                          
582                         //User_ID = User_ID,
583                         Roles_ID= n.Tag.ToString(),
584                         CreateUserName = FrmLogin.loginUserName,
585                         CreateUserId = FrmLogin.LoginUserID,
586                         CreateDate = DateTime.Now
587                     });
588                 }
589                 CheckRoleTreeViewNode(n.Nodes);
590             }
591         }
592         #endregion
593
594         private void btn_concel_Click(object sender, EventArgs e)
595         {
596             this.Close();
597         }
598     }
599 }