DefaultBll.cs
3.11 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using HHECS.Model;
namespace HHECS.Bll
{
public class DefaultBll : IBll
{
public string ConnectStr { get; set; }
public DefaultBll(string ConnecStr)
{
this.ConnectStr = ConnectStr;
}
private IDbConnection GetConnection()
{
return new SqlConnection(ConnectStr);
}
public BllResult<List<MenuOperation>> FindMenuOperation(List<Role> roles)
{
try
{
using (IDbConnection connection = GetConnection())
{
var list = connection.GetList<MenuOperation>("where id in (select menuOperationId from roleMenuOperation where roleId in @roleIds)", new
{
roleIds = roles.Select(t => new { t.Id }).ToArray()
}).ToList();
if (list.Count() == 0)
{
return BllResultFactory.Error<List<MenuOperation>>("未找到对应权限");
}
else
{
return BllResultFactory.Sucess<List<MenuOperation>>(list, "成功");
}
}
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
return BllResultFactory.Error<List<MenuOperation>>(null, "发生异常");
}
}
public BllResult<User> Login(string userCode, string password)
{
try
{
using (IDbConnection connection = GetConnection())
{
var lookup = new Dictionary<int, User>();
connection.Query<User, Role, User>("select u.*,rl.* from [user] u join userRole r on u.id = r.userId join [role] rl on r.roleId = rl.id where u.userName=@userCode and u.password=@password;", (user, role) =>
{
List<Role> roles = new List<Role>();
User u;
if (!lookup.TryGetValue(user.Id, out u))
{
u = user;
lookup.Add(u.Id, u);
}
u.Roles.Add(role);
return u;
}, new { userCode, password });
var list = lookup.Values.ToList();
if (list.Count() == 0)
{
return BllResultFactory.Error<User>("用户名或密码不正确");
}
else
{
return BllResultFactory.Sucess<User>(list[0], "登录成功");
}
}
}
catch (Exception ex)
{
LogExecute.WriteDBExceptionLog(ex);
return BllResultFactory.Error<User>(null, "发生异常");
}
}
}
}