WinUserAddOrEdit.xaml.cs
5.4 KB
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
using HHECS.Bll;
using HHECS.Model;
using HHECS.Model.BllModel;
using HHECS.Model.Entities;
using HHECS.View.Win;
using HHECS.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace HHECS.View.UserOperation
{
/// <summary>
/// UserAddOrEdit.xaml 的交互逻辑
/// </summary>
public partial class WinUserAddOrEdit : BaseWindow
{
public int? Id { get; set; }
public User CurrentUser { get; set; } = new User();
public List<UserEditVM> RoleList { get; set; }
public WinUserAddOrEdit(int? id)
{
this.Id = id;
if (id == null)
{
this.Title = "用户新增";
}
else
{
this.Title = "用户编辑";
}
InitializeComponent();
Init();
this.GridMain.DataContext = CurrentUser;
}
private void Init()
{
//获取所有角色
var result = AppSession.Bll.GetAllRole();
if (result.Success)
{
RoleList = result.Data.Select(t => new UserEditVM()
{
Selected = false,
Id = t.Id.Value,
RoleName = t.RoleName
}).ToList();
if (Id == null)
{
//说明是新增
ListViewMain.ItemsSource = RoleList;
}
else
{
//说明是更新
TxtUserCode.IsReadOnly = true;
TxtUserName.IsReadOnly = true;
BllResult<List<User>> result2 = AppSession.Bll.GetUserByCondition($"where id = {Id}");
if (result2.Success)
{
User tempUser = result2.Data[0];
CurrentUser.Id = tempUser.Id;
CurrentUser.UserCode = tempUser.UserCode;
CurrentUser.UserName = tempUser.UserName;
CurrentUser.Password = tempUser.Password;
CurrentUser.Partment = tempUser.Partment;
CurrentUser.Address = tempUser.Address;
CurrentUser.Phone = tempUser.Phone;
CurrentUser.Remark = tempUser.Remark;
CurrentUser.Disable = tempUser.Disable;
CurrentUser.Created = tempUser.Created;
CurrentUser.CreatedBy = tempUser.CreatedBy;
CurrentUser.Updated = tempUser.Updated;
CurrentUser.UpdatedBy = tempUser.UpdatedBy;
//获取他现在所拥有的角色
var result3 = AppSession.Bll.GetUserWithRoles(tempUser.UserCode, tempUser.Password, AppSession.WarehouseId, AppSession.WarehouseCode, AppSession.Client, AppSession.Urls);
if (result3.Success)
{
RoleList.ForEach(t =>
{
t.Selected = result3.Data.Roles.Count(i => i.Id == t.Id) > 0;
});
ListViewMain.ItemsSource = RoleList;
}
else
{
MessageBox.Show($"获取用户角色失败:{result3.Msg}");
}
}
else
{
MessageBox.Show($"查询用户失败:{result2.Msg}");
}
}
}
else
{
MessageBox.Show("获取角色信息出错");
}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
//用户名、编码、密码和角色必要
if (String.IsNullOrWhiteSpace(CurrentUser.UserName) || String.IsNullOrWhiteSpace(CurrentUser.UserCode) || String.IsNullOrWhiteSpace(CurrentUser.Password) || RoleList.Count(t => t.Selected == true) == 0)
{
MessageBox.Show("要求必须提供用户名、用户编码、密码和至少一种角色");
return;
}
BllResult<User> result = null;
if (CurrentUser.Id == null)
{
//新增
result = AppSession.Bll.SaveUserWithRoles(CurrentUser, RoleList.Where(t => t.Selected == true).Select(t => t.Id).ToList());
}
else
{
//更新
result = AppSession.Bll.UpdateUserWithRoles(CurrentUser, RoleList.Where(t => t.Selected == true).Select(t => t.Id).ToList());
}
if (result.Success)
{
MessageBox.Show("保存成功");
CurrentUser.Id = result.Data.Id;
}
else
{
MessageBox.Show($"保存失败:{result.Msg}");
}
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}