123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using IoTIntegrationPlatform.Common;
- using IoTIntegrationPlatform.Interface;
- using IoTIntegrationPlatform.Model.common;
- using IoTIntegrationPlatform.Model.Model;
- using SqlSugar;
- namespace IoTIntegrationPlatform.Services
- {
- public class AdminUserService : IAdminUserService
- {
- private ISqlSugarClient db { get; }
- public AdminUserService(ISqlSugarClient _db)
- {
- db = _db;
- }
- #region 类句柄操作
- private IntPtr handle;
- private bool disposed = false;
- //关闭句柄
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
- public extern static Boolean CloseHandle(IntPtr handle);
- /// <summary>
- /// 释放对象资源
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// 释放对象资源
- /// </summary>
- /// <param name="disposing"></param>
- protected virtual void Dispose(bool disposing)
- {
- if (!this.disposed)
- {
- if (handle != IntPtr.Zero)
- {
- CloseHandle(handle);
- handle = IntPtr.Zero;
- }
- }
- disposed = true;
- }
- ~AdminUserService()
- {
- Dispose(false);
- }
- #endregion
- /// <summary>
- /// 获取管理人员信息
- /// </summary>
- /// <param name="uuid"></param>
- /// <param name="account"></param>
- /// <param name="passWord"></param>
- /// <param name="mobile"></param>
- /// <returns></returns>
- public AdminUser GetAdminUserInfo(string uuid, string account, string passWord, string mobile)
- {
- AdminUser model = new AdminUser();
- try
- {
- model = db.Queryable<AdminUser>()
- .Where(it => it.IsValid == true)
- .WhereIF(!string.IsNullOrEmpty(account), it => it.Account == account)
- .WhereIF(!string.IsNullOrEmpty(passWord), it => it.Password == passWord)
- .WhereIF(!string.IsNullOrEmpty(mobile), it => it.Mobile == mobile)
- .First();
- }
- catch (Exception ex)
- {
- Logging.Error(ex, ex.Message);
- }
- return model;
- }
- /// <summary>
- /// 获取用户列表
- /// </summary>
- /// <param name="parm"></param>
- /// <param name="totalPage"></param>
- /// <returns></returns>
- public List<AdminUser> GetUserList(BaseParm parm, ref int totalPage)
- {
- List<AdminUser> list = new List<AdminUser>();
- try
- {
- list = db.CopyNew().Queryable<AdminUser>()
- .Where(it => it.IsValid == true && it.IsSuper != true)//隐藏到超级管理员
- .WhereIF(!string.IsNullOrEmpty(parm.Name), it => it.RealName.Contains(parm.Name) || it.Account.Contains(parm.Name))
- .WhereIF(!string.IsNullOrEmpty(parm.Param), it => it.Mobile == parm.Param )
- .ToPageList(parm.PageIndex, parm.PageSize, ref totalPage);
- }
- catch (Exception ex)
- {
- Logging.Error(ex, ex.Message);
- }
- return list;
- }
- /// <summary>
- /// 新增
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public bool Add(AdminUser model)
- {
- bool result = false;
- try
- {
- model.IsSuper = false;
- model.CreateTime = DateTime.Now;
- model.UpdateTime = DateTime.Now;
- model.IsValid = true;
- model.Id = db.Insertable(model).ExecuteReturnIdentity();
- if (model != null && model.Id > 0)
- {
- result = true;
- }
- }
- catch (Exception ex)
- {
- Logging.Error(ex, ex.Message);
- }
- return result;
- }
- /// <summary>
- /// 编辑管理员
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public bool UpdateUser(AdminUser model)
- {
- bool result = false;
- try
- {
- model.UpdateTime = DateTime.Now;
- int row = db.Updateable(model).ExecuteCommand();
- if (row > 0)
- {
- result = true;
- }
- }
- catch (Exception ex)
- {
- Logging.Error(ex, ex.Message);
- }
- return result;
- }
- /// <summary>
- /// 修改密码
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public bool UpdatePassWord(AdminUser model)
- {
- bool result = false;
- try
- {
- model.UpdateTime = DateTime.Now;
- int row = db.Updateable<AdminUser>()
- .SetColumns(it => new AdminUser { UpdateTime = DateTime.Now, Password = model.Password,SecretKey=model.SecretKey })
- .Where(it => it.Uuid == model.Uuid)
- .ExecuteCommand();
- if (row > 0)
- {
- result = true;
- }
- }
- catch (Exception ex)
- {
- Logging.Error(ex, ex.Message);
- }
- return result;
- }
- /// <summary>
- /// 删除管理员
- /// </summary>
- /// <param name="parm"></param>
- /// <returns></returns>
- public bool DeleteUser(BaseParm parm)
- {
- bool result = false;
- try
- {
- int row = db.Updateable<AdminUser>()
- .SetColumns(it => new AdminUser { UpdateTime = DateTime.Now, IsValid = true })
- .Where(it => it.Id == parm.Id)
- .ExecuteCommand();
- if (row > 0)
- {
- result = true;
- }
- }
- catch (Exception ex)
- {
- Logging.Error(ex, ex.Message);
- }
- return result;
- }
- /// <summary>
- /// 获取用户菜单权限
- /// </summary>
- /// <param name="userid"></param>
- /// <returns></returns>
- public List<AdminMenu> GetUserMenus(int userid)
- {
- List<AdminMenu> list = new List<AdminMenu>();
- try
- {
- var query= db.Queryable<AdminMenu>()
- .LeftJoin<AdminRoleMenu>((a, b) => a.IsValid == true && a.MenuCode == b.MenuCode)
- .LeftJoin<AdminRole>((a, b, c) => b.IsValid == true && c.IsValid == true && b.RoleId == c.Id)
- .LeftJoin<AdminUserRole>((a, b, c, d) => d.IsValid == true && c.Id== d.RoleId)
- .Where((a,b,c,d) => d.UserId == userid)
- .Select((a, b, c, d) => new AdminMenu { });
- list = query.ToList();
- }
- catch (Exception ex)
- {
- Logging.Error(ex, "GetUserMenus");
- }
- return list;
- }
- }
- }
|