AdminUserService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using IoTIntegrationPlatform.Common;
  5. using IoTIntegrationPlatform.Interface;
  6. using IoTIntegrationPlatform.Model.common;
  7. using IoTIntegrationPlatform.Model.Model;
  8. using SqlSugar;
  9. namespace IoTIntegrationPlatform.Services
  10. {
  11. public class AdminUserService : IAdminUserService
  12. {
  13. private ISqlSugarClient db { get; }
  14. public AdminUserService(ISqlSugarClient _db)
  15. {
  16. db = _db;
  17. }
  18. #region 类句柄操作
  19. private IntPtr handle;
  20. private bool disposed = false;
  21. //关闭句柄
  22. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
  23. public extern static Boolean CloseHandle(IntPtr handle);
  24. /// <summary>
  25. /// 释放对象资源
  26. /// </summary>
  27. public void Dispose()
  28. {
  29. Dispose(true);
  30. GC.SuppressFinalize(this);
  31. }
  32. /// <summary>
  33. /// 释放对象资源
  34. /// </summary>
  35. /// <param name="disposing"></param>
  36. protected virtual void Dispose(bool disposing)
  37. {
  38. if (!this.disposed)
  39. {
  40. if (handle != IntPtr.Zero)
  41. {
  42. CloseHandle(handle);
  43. handle = IntPtr.Zero;
  44. }
  45. }
  46. disposed = true;
  47. }
  48. ~AdminUserService()
  49. {
  50. Dispose(false);
  51. }
  52. #endregion
  53. /// <summary>
  54. /// 获取管理人员信息
  55. /// </summary>
  56. /// <param name="uuid"></param>
  57. /// <param name="account"></param>
  58. /// <param name="passWord"></param>
  59. /// <param name="mobile"></param>
  60. /// <returns></returns>
  61. public AdminUser GetAdminUserInfo(string uuid, string account, string passWord, string mobile)
  62. {
  63. AdminUser model = new AdminUser();
  64. try
  65. {
  66. model = db.Queryable<AdminUser>()
  67. .Where(it => it.IsValid == true)
  68. .WhereIF(!string.IsNullOrEmpty(account), it => it.Account == account)
  69. .WhereIF(!string.IsNullOrEmpty(passWord), it => it.Password == passWord)
  70. .WhereIF(!string.IsNullOrEmpty(mobile), it => it.Mobile == mobile)
  71. .First();
  72. }
  73. catch (Exception ex)
  74. {
  75. Logging.Error(ex, ex.Message);
  76. }
  77. return model;
  78. }
  79. /// <summary>
  80. /// 获取用户列表
  81. /// </summary>
  82. /// <param name="parm"></param>
  83. /// <param name="totalPage"></param>
  84. /// <returns></returns>
  85. public List<AdminUser> GetUserList(BaseParm parm, ref int totalPage)
  86. {
  87. List<AdminUser> list = new List<AdminUser>();
  88. try
  89. {
  90. list = db.CopyNew().Queryable<AdminUser>()
  91. .Where(it => it.IsValid == true && it.IsSuper != true)//隐藏到超级管理员
  92. .WhereIF(!string.IsNullOrEmpty(parm.Name), it => it.RealName.Contains(parm.Name) || it.Account.Contains(parm.Name))
  93. .WhereIF(!string.IsNullOrEmpty(parm.Param), it => it.Mobile == parm.Param )
  94. .ToPageList(parm.PageIndex, parm.PageSize, ref totalPage);
  95. }
  96. catch (Exception ex)
  97. {
  98. Logging.Error(ex, ex.Message);
  99. }
  100. return list;
  101. }
  102. /// <summary>
  103. /// 新增
  104. /// </summary>
  105. /// <param name="model"></param>
  106. /// <returns></returns>
  107. public bool Add(AdminUser model)
  108. {
  109. bool result = false;
  110. try
  111. {
  112. model.IsSuper = false;
  113. model.CreateTime = DateTime.Now;
  114. model.UpdateTime = DateTime.Now;
  115. model.IsValid = true;
  116. model.Id = db.Insertable(model).ExecuteReturnIdentity();
  117. if (model != null && model.Id > 0)
  118. {
  119. result = true;
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. Logging.Error(ex, ex.Message);
  125. }
  126. return result;
  127. }
  128. /// <summary>
  129. /// 编辑管理员
  130. /// </summary>
  131. /// <param name="model"></param>
  132. /// <returns></returns>
  133. public bool UpdateUser(AdminUser model)
  134. {
  135. bool result = false;
  136. try
  137. {
  138. model.UpdateTime = DateTime.Now;
  139. int row = db.Updateable(model).ExecuteCommand();
  140. if (row > 0)
  141. {
  142. result = true;
  143. }
  144. }
  145. catch (Exception ex)
  146. {
  147. Logging.Error(ex, ex.Message);
  148. }
  149. return result;
  150. }
  151. /// <summary>
  152. /// 修改密码
  153. /// </summary>
  154. /// <param name="model"></param>
  155. /// <returns></returns>
  156. public bool UpdatePassWord(AdminUser model)
  157. {
  158. bool result = false;
  159. try
  160. {
  161. model.UpdateTime = DateTime.Now;
  162. int row = db.Updateable<AdminUser>()
  163. .SetColumns(it => new AdminUser { UpdateTime = DateTime.Now, Password = model.Password,SecretKey=model.SecretKey })
  164. .Where(it => it.Uuid == model.Uuid)
  165. .ExecuteCommand();
  166. if (row > 0)
  167. {
  168. result = true;
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. Logging.Error(ex, ex.Message);
  174. }
  175. return result;
  176. }
  177. /// <summary>
  178. /// 删除管理员
  179. /// </summary>
  180. /// <param name="parm"></param>
  181. /// <returns></returns>
  182. public bool DeleteUser(BaseParm parm)
  183. {
  184. bool result = false;
  185. try
  186. {
  187. int row = db.Updateable<AdminUser>()
  188. .SetColumns(it => new AdminUser { UpdateTime = DateTime.Now, IsValid = true })
  189. .Where(it => it.Id == parm.Id)
  190. .ExecuteCommand();
  191. if (row > 0)
  192. {
  193. result = true;
  194. }
  195. }
  196. catch (Exception ex)
  197. {
  198. Logging.Error(ex, ex.Message);
  199. }
  200. return result;
  201. }
  202. /// <summary>
  203. /// 获取用户菜单权限
  204. /// </summary>
  205. /// <param name="userid"></param>
  206. /// <returns></returns>
  207. public List<AdminMenu> GetUserMenus(int userid)
  208. {
  209. List<AdminMenu> list = new List<AdminMenu>();
  210. try
  211. {
  212. var query= db.Queryable<AdminMenu>()
  213. .LeftJoin<AdminRoleMenu>((a, b) => a.IsValid == true && a.MenuCode == b.MenuCode)
  214. .LeftJoin<AdminRole>((a, b, c) => b.IsValid == true && c.IsValid == true && b.RoleId == c.Id)
  215. .LeftJoin<AdminUserRole>((a, b, c, d) => d.IsValid == true && c.Id== d.RoleId)
  216. .Where((a,b,c,d) => d.UserId == userid)
  217. .Select((a, b, c, d) => new AdminMenu { });
  218. list = query.ToList();
  219. }
  220. catch (Exception ex)
  221. {
  222. Logging.Error(ex, "GetUserMenus");
  223. }
  224. return list;
  225. }
  226. }
  227. }