UserAddOrEditVM.cs
4.63 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
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HHECS.RobotTool.Model;
using RobotTool;
using System.Collections.ObjectModel;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;
namespace HHECS.RobotTool.ViewModel.AccountVM
{
public partial class UserAddOrEditVM : ObservableObject
{
[ObservableProperty]
private User user = new User();
[ObservableProperty]
private ObservableCollection<Role> roles = new ObservableCollection<Role>();
public Window Owner { get; set; } = null!;
private bool IsEdit = false;
private readonly IFreeSql _freeSql;
public UserAddOrEditVM(IFreeSql freeSql)
{
_freeSql = freeSql;
}
public void InitialData(int? userId = null)
{
try
{
//所有的角色
var roles = _freeSql.Queryable<Role>().ToList();
User.Enable = true;
if (userId != null)
{
Owner.Title = "修改用户信息";
IsEdit = true;
User = _freeSql.Queryable<User>().Where(x => x.Id.Equals(userId)).First();
var roleIds = _freeSql.Queryable<UserRole>().Where(x => x.UserId == userId).ToList(x => x.RoleId);
roles.ForEach(x => x.IsSelected = roleIds.Contains(x.Id));
}
Roles = new ObservableCollection<Role>(roles);
}
catch (Exception ex)
{
MessageBox.Error($"初始化数据失败.{ex.Message}");
}
}
[RelayCommand]
public void Save()
{
try
{
if (string.IsNullOrWhiteSpace(User.Account))
{
MessageBox.Warning($"用户账号不能为空!");
return;
}
if (string.IsNullOrWhiteSpace(User.UserName))
{
MessageBox.Warning($"用户名称不能为空!");
return;
}
if (string.IsNullOrWhiteSpace(User.Password))
{
MessageBox.Warning($"用户密码不能为空!");
return;
}
if (User.Password.Length < 6 || User.Password.Length > 16)
{
MessageBox.Warning($"用户密码长度必须在6~16位之间!");
return;
}
var userId = User.Id;
if (!IsEdit)
{
var temp = _freeSql.Queryable<User>().Where(x => x.Account.ToLower() == User.Account.ToLower()).First(x => x.Account);
if (!string.IsNullOrWhiteSpace(temp))
{
MessageBox.Error($"操作失败:新增“{User.Account}”失败,系统已存在同名账号“{temp}”,请改用其他账号重试!");
return;
}
User.Created = DateTime.Now;
User.CreatedBy = App.User.Account;
userId = (int)_freeSql.Insert(User).ExecuteIdentity();
}
else
{
User.Updated = DateTime.Now;
User.UpdatedBy = App.User.Account;
_freeSql.Update<User>().SetSource(User).ExecuteAffrows();
}
var userRoleIds = _freeSql.Queryable<UserRole>().Where(x => x.UserId == userId).ToList(x => x.RoleId);
//新增
var userRoleTemp1 = Roles.Where(x => x.IsSelected && !userRoleIds.Contains(x.Id)).Select(x => new UserRole
{
UserId = userId,
RoleId = x.Id,
Created = DateTime.Now,
CreatedBy = App.User.Account
}).ToList();
_freeSql.Insert(userRoleTemp1).ExecuteAffrows();
//删除
var userRoleTemp2 = Roles.Where(x => !x.IsSelected && userRoleIds.Contains(x.Id)).Select(x => x.Id).ToList();
_freeSql.Delete<UserRole>().Where(x => userRoleTemp2.Contains(x.RoleId) && x.UserId == userId).ExecuteAffrows();
Owner.DialogResult = true;
MessageBox.Success("操作成功");
}
catch (Exception ex)
{
MessageBox.Error($"操作失败.{ex.Message}");
}
}
[RelayCommand]
public void Cancel()
{
Owner.Close();
}
}
}