yyk
2024-08-22 1310b47a24f0cc70f0128c820bd490dca6a1a921
Wms/WMS.BLL/SysServer/UserInforServer.cs
@@ -11,90 +11,73 @@
using OracleInternal.Secure.Network;
using Utility.Tools;
using System.Linq;
using SqlSugar;
using WMS.Entity.Context;
using WMS.Entity.BllTaskEntity;
using WMS.DAL;
using Utility;
namespace WMS.BLL.SysServer
{
    public class UserInforServer : IUserInforServer
    {
        public IUserInforRepository UserSvc { get; set; }
        readonly IMapper _mapper;
        private readonly IFunSettingServer _setting;
        public UserInforServer(IUserInforRepository userSvc, IMapper mapper, IFunSettingServer setting)
        private static readonly SqlSugarScope Db = DataContext.Db;
        private readonly UserManager _userManager;
        public UserInforServer(IMapper mapper, IFunSettingServer setting, UserManager userManager)
        {
            UserSvc = userSvc;
            _mapper = mapper;
            _setting = setting;
            _userManager = userManager;
        }
        public bool LoginAdmin(string loginName, string loginPwd, out int adminId)
        public async Task<int> LoginAdmin(string loginName, string loginPwd)
        {
            //loginPwd = Md5Tools.CalcMd5(loginPwd);
            var date = UserSvc.GetAllAsync().First(m => m.UserName == loginName && m.PassWord == loginPwd);
            if (date != null) //账号密码是否正确
            var modUser = await Db.Queryable<SysUserInfor>().FirstAsync(m => m.UserName == loginName && m.PassWord == loginPwd &&m.IsDel=="0");
            if (modUser != null) //账号密码是否正确
            {
                if (date.Status == "0") //当前账号是否正常启用
                if (modUser.Status == "0") //当前账号是否正常启用
                {
                    //最后登陆时间
                    UserSvc.UptUserLoginTime(date.Id);
                    adminId = date.Id;
                    return true;
                    await Db.Updateable(modUser).UpdateColumns(s => s.LoginTime == DateTime.Now).ExecuteCommandAsync();
                    return modUser.Id;
                }
                else //当前账号被禁用
                {
                    adminId = 0;
                    return true;
                    return 0;
                }
            }
            else //账号密码不正确或没有此账号
            {
                adminId = 0;
                return false;
                return -1;
            }
        }
        public SysUserInfor CeShi()
        {
            var date = UserSvc.ceshi();
            return date;
        }
        /// <summary>
        /// 获取用户角色信息
        /// </summary>
        /// <returns></returns>
        public List<UserInfoDto> GetUserRoleList(string UserName, string DepartmentNo, string RoleNo, string Status)
        public async Task<List<UserInfoDto>> GetUserRoleList(string UserName, string DepartmentNo, string RoleNo, string Status)
        {
            string str = "select user1.Id Id,user1.UserNo UserNo,user1.UserName UserName,user1.RealName RealName,user1.PassWord PassWord,user1.JobNo JobNo,user1.Status Status,user1.Sex Sex,user1.Nationality Nationality,user1.CreateTime CreateTime,user1.CreateUser CreateUser,user1.Paper Paper,user1.Phone Phone,user1.Email Email,user1.Addres Addres,user1.Demo Demo,role.RoleName,user2.RealName CreateUserName,depa.DepartmentName DepartmentName from SysUserInfor user1 left join SysRoles role on user1.RoleNo = role.RoleNo left join SysUserInfor user2 on user1.CreateUser = user2.Id left join SysDepartment depa on user1.DepartmentNo = depa.DepartmentNo where user1.IsDel = '0'";
            //判断登录名称是否为空
            if (!string.IsNullOrEmpty(UserName))
            {
                str += $" and user1.UserName like '%{UserName}%'";
            }
            //判断部门号是否为0
            if (!string.IsNullOrEmpty(DepartmentNo))
            {
                str += $" and user1.DepartmentNo = '{DepartmentNo}'";
            }
            //判断角色号是否为空
            if (!string.IsNullOrEmpty(RoleNo))
            {
                str += $" and user1.RoleNo = '{RoleNo}'";
            }
            //判断状态是否为空
            if (!string.IsNullOrEmpty(Status))
            {
                str += $" and user1.Status = '{Status}'";
            }
            List<UserInfoDto> userInfoDtos = UserSvc.GetUserRoleList(str);
            //var arr = Md5Tools.CalcMd5(userInfoDtos[3].PassWord);
            return userInfoDtos;
            return await Db.Queryable<SysUserInfor>()
                                        .LeftJoin<SysUserInfor>((a, b) => a.CreateUser == b.Id)
                                        .LeftJoin<SysDepartment>((a, b, c) => a.DepartmentNo == c.DepartmentNo)
                                        .LeftJoin<SysRoles>((a, b, c, d) => a.RoleNo == d.RoleNo)
                                        .WhereIF(!string.IsNullOrEmpty(UserName), a => a.UserName.Contains(UserName))
                                        .WhereIF(!string.IsNullOrEmpty(DepartmentNo), a => a.DepartmentNo == DepartmentNo)
                                        .WhereIF(!string.IsNullOrEmpty(RoleNo), a => a.RoleNo == RoleNo)
                                        .WhereIF(!string.IsNullOrEmpty(Status), a => a.Status == Status)
                                        .Where(a => a.IsDel == "0")
                                        .Select<UserInfoDto>((a, b, c, d) => new UserInfoDto()
                                        {
                                            RealName = a.RealName,
                                            CreateUserName = b.RealName,
                                            DepartmentName = c.DepartmentName,
                                            RoleName = d.RoleName,
                                        }, true)
                                        .ToListAsync();
        }
        /// <summary>
@@ -104,15 +87,13 @@
        /// <param name="userName">用户登录名</param>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        public int GetUserInfoByNo(string userNo, string userName,int id)
        public async Task<int> GetUserInfoByNo(string userNo, string userName, int id)
        {
            string str = $"select * from SysUserInfor where IsDel=0 and (UserNo = '{userNo}' or UserName = '{userName}') ";
            if (id!=0)
            {
                str += $"and Id != {id} ";
            }
            List<SysUserInfor> user = UserSvc.GetUserInfoByNo(str);
            return user.Count;
            return await Db.Queryable<SysUserInfor>()
                        .Where(s => s.IsDel == "0")
                        .Where(s => s.UserNo == userNo || s.UserName == userName)
                        .WhereIF(id != 0, a => a.Id != id)
                        .CountAsync();
        }
        /// <summary>
@@ -123,30 +104,26 @@
        /// <exception cref="Exception">异常</exception>
        public async Task<int> InsertUserInfo(UserInfoDto UserInfoDto)
        {
            //异常
            try
            //数据模型映射
            SysUserInfor userinfo = _mapper.Map<SysUserInfor>(UserInfoDto);
            //判断用户号是否唯一
            int count = await GetUserInfoByNo(userinfo.UserNo, userinfo.UserName, 0);
            int i = 0;
            if (count > 0)
            {
                //数据模型映射
                SysUserInfor userinfo = _mapper.Map<SysUserInfor>(UserInfoDto);
                //判断用户号是否唯一
                int count = GetUserInfoByNo(userinfo.UserNo,userinfo.UserName,0);
                int i = 0;
                if (count > 0)
                {
                    i = 3;
                }
                else if (count ==0)
                {
                    //新增用户
                    i = await UserSvc.InsertUserInfo(userinfo);
                }
                return i;
                throw Oops.Bah("用户名或登录名重复");
            }
            catch (Exception ex)
            else if (count == 0)
            {
                //抛出异常
                throw new Exception("新增用户信息异常", ex);
                userinfo.PassWord = Md5Tools.CalcMd5("boxline");
                userinfo.SetPasswordTime = DateTime.Now;
                userinfo.CreateTime = DateTime.Now;
                userinfo.CreateUser = _userManager.UserId;
                i = await Db.Insertable(userinfo).ExecuteCommandAsync();
                if (i <= 0)
                    throw Oops.Bah("新增用户信息失败");
            }
            return i;
        }
@@ -156,21 +133,15 @@
        /// <param name="user">用户实体模型</param>
        /// <returns></returns>
        /// <exception cref="Exception">异常</exception>
        public async Task<int> DeleteUserinfo(SysUserInfor user)
        public async Task DeleteUserinfo(SysUserInfor user)
        {
            //异常
            try
            {
                //删除
                int i = await UserSvc.DeleteUserinfo(user);
                //返回数据
                return i;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("删除用户信息异常", ex);
            }
            await Db.Updateable<SysUserInfor>()
                           .Where(s => s.Id == user.Id)
                           .SetColumns(s => s.IsDel == "1")
                           .SetColumns(s => s.UpdateTime == DateTime.Now)
                           .SetColumns(s => s.UpdateUser == _userManager.UserId)
                           .ExecuteCommandAsync();
        }
        /// <summary>
@@ -181,31 +152,26 @@
        /// <exception cref="Exception">异常exception>
        public async Task<int> UpdateUserinfo(UserInfoDto userdto)
        {
            //异常
            try
            //映射数据模型
            SysUserInfor userlist = _mapper.Map<SysUserInfor>(userdto);
            //判断用户号是否唯一
            int count = await GetUserInfoByNo(userdto.UserNo, userdto.UserName, userdto.Id);
            int i = 0;
            if (count > 0)
            {
                //映射数据模型
                SysUserInfor userlist = _mapper.Map<SysUserInfor>(userdto);
                //判断用户号是否唯一
                int count = GetUserInfoByNo(userdto.UserNo, userdto.UserName,userdto.Id);
                int i = 0;
                if (count > 0)
                {
                    i = 3;
                }
                else if (count == 0)
                {
                    //编辑
                    i = await UserSvc.UpdateUserinfo(userlist);
                }
                return i;
                throw Oops.Bah("用户名或登录名重复");
            }
            catch (Exception ex)
            else if (count == 0)
            {
                //抛出异常
                throw new Exception("编辑用户信息异常", ex);
                userdto.UpdateUser = _userManager.UserId;
                userdto.UpdateTime = DateTime.Now;
                i = await Db.Updateable(userlist)
                    .UpdateColumns(s => new { s.UserName, s.UserNo, s.Sex, s.RoleNo, s.RealName, s.Phone, s.Paper, s.Nationality, s.JobNo, s.Email, s.Demo, s.Addres, s.UpdateUser, s.UpdateTime })
                    .ExecuteCommandAsync();
                if (i <= 0)
                    throw Oops.Bah("编辑用户信息失败");
            }
            return i;
        }
        /// <summary>
@@ -213,12 +179,9 @@
        /// </summary>
        /// <param name="userids">用户id</param>
        /// <returns></returns>
        public SysUserInfor GetUserInfoById(int userids)
        public async Task<SysUserInfor> GetUserInfoById(int userid)
        {
            //获取信息列表
            SysUserInfor user= UserSvc.GetUserInfoById(userids);
            //返回数据
            return user;
            return await Db.Queryable<SysUserInfor>().FirstAsync(s => s.Id == userid && s.IsDel == "0");
        }
        /// <summary>
@@ -230,59 +193,37 @@
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        /// <exception cref="Exception">捕获异常</exception>
        public async Task<int> UptUserPassWord(string pwdOld, string pwdNew, string pwdNewTwo, int userId)
        public async Task<int> UptUserPassWord(string pwdNew, int userId)
        {
            //捕获异常
            try
            {
                //验证两次新密码是否一致
                if (pwdNew == pwdNewTwo)
                {
                    int i = await UserSvc.UptUserPassWord(pwdOld, pwdNew, pwdNewTwo, userId);
                    return i;
                }
                else
                {
                    return 3;
                }
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("修改密码异常",ex);
            }
            return await Db.Updateable<SysUserInfor>()
                            .Where(s => s.Id == userId)
                            .SetColumns(s => s.UpdateTime == DateTime.Now)
                            .SetColumns(s => s.UpdateUser == _userManager.UserId)
                            .SetColumns(s => s.SetPasswordTime == DateTime.Now)
                            .ExecuteCommandAsync();
        }
        /// <summary>
        /// x天后提醒用户修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int IsPassWordTime(int userId)
        public async Task<int> IsPassWordTime(int userId)
        {
            //捕获异常
            try
            SysFunSetting funSettings = await Db.Queryable<SysFunSetting>().FirstAsync(a => a.IsDel == "0" && a.FunSetNo == "Fun026");
            if (funSettings != null && funSettings.IsEnable == "NO")
            {
                SysFunSetting funSettings = _setting.GetFunSettingByNo("Fun026");
                if (funSettings != null && funSettings.IsEnable == "NO")
                SysUserInfor userinfo = await Db.Queryable<SysUserInfor>().FirstAsync(s => s.Id == userId && s.IsDel == "0");
                if (userinfo.SetPasswordTime != null)
                {
                    SysUserInfor userinfo = UserSvc.GetAllAsync().First(m => m.Id == userId);
                    if (userinfo.SetPasswordTime != null)
                    int days = string.IsNullOrEmpty(funSettings.SetValue) ? 30 : Convert.ToInt32(funSettings.SetValue);
                    bool isTime = userinfo.SetPasswordTime.Value.AddDays(days) < DateTime.Now;
                    if (isTime)
                    {
                        int days = string.IsNullOrEmpty(funSettings.SetValue) ? 30 : Convert.ToInt32(funSettings.SetValue);
                        bool isTime = userinfo.SetPasswordTime.Value.AddDays(days) < DateTime.Now;
                        if (isTime)
                        {
                            return 1;
                        }
                        return 1;
                    }
                }
                return 0;
            }
            catch (Exception ex)
            {
                //抛出异常
                throw new Exception("修改密码异常", ex);
            }
            return 0;
        }
    }
}