DefaultBll.cs 3.11 KB
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, "发生异常");
            }
        }

    }
}