UserService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using YLShipBuildLandMap.Entity;
  6. using YLShipBuildLandMap.Entity.ViewModel;
  7. using System.Linq.Expressions;
  8. using Bowin.Common.Linq.Entity;
  9. using YLShipBuildLandMap.Entity.ViewModel.SystemSetting;
  10. using Bowin.Common.Linq;
  11. using System.Threading.Tasks;
  12. using Microsoft.EntityFrameworkCore;
  13. using System.Security.Cryptography;
  14. using Microsoft.VisualBasic;
  15. using Z.EntityFramework.Plus;
  16. using YLShipBuildLandMap.Entity.Extensions;
  17. using Bowin.Common.Utility;
  18. namespace YLShipBuildLandMap.Services.SystemSetting
  19. {
  20. public class UserService : IUserService
  21. {
  22. private YLShipBuildLandMapContext DbContext { get; set; }
  23. public UserService(YLShipBuildLandMapContext dbContext)
  24. {
  25. DbContext = dbContext;
  26. }
  27. #region 生成相关View的查询
  28. private IQueryable<LoginUser> GetLoginUserQuerable(Expression<Func<SysUser, bool>> userExp)
  29. {
  30. var sql = from u in DbContext.SysUser.Where(userExp)
  31. from r in (
  32. from role in DbContext.SysRole
  33. from userRole in role.SysUserSysRole
  34. where userRole.UserId == u.UserId
  35. select role
  36. ).Take(1).DefaultIfEmpty()
  37. select new LoginUser
  38. {
  39. UserID = u.UserId,
  40. LoginID = u.LoginId,
  41. UserName = u.Name,
  42. CurrentRoleID = r.RoleId,
  43. RoleName = r.RoleName,
  44. };
  45. return sql;
  46. }
  47. private IQueryable<UserView> GetUserViewQueryable(Expression<Func<SysUser, bool>> exp)
  48. {
  49. var sql = from u in DbContext.SysUser.Where(exp)
  50. select new UserView
  51. {
  52. UserID = u.UserId,
  53. CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid,
  54. LoginID = u.LoginId,
  55. Name = u.Name,
  56. RecordStatus = u.RecordStatus,
  57. FirstCode = YLShipBuildLandMapContext.GetPyFirst(u.Name)
  58. };
  59. return sql;
  60. }
  61. private IQueryable<UserView> GetAllUsersQueryable(Expression<Func<SysUser, bool>> exp)
  62. {
  63. var sql = from u in DbContext.SysUser.Where(exp)
  64. select new UserView
  65. {
  66. UserID = u.UserId,
  67. CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid,
  68. LoginID = u.LoginId,
  69. Name = u.Name,
  70. RecordStatus = u.RecordStatus,
  71. FirstCode = YLShipBuildLandMapContext.GetPyFirst(u.Name)
  72. };
  73. return sql;
  74. }
  75. private IQueryable<UserView> GetSysUserQueryable(Expression<Func<SysUser, bool>> exp)
  76. {
  77. var sql = from u in DbContext.SysUser.Where(exp)
  78. select new UserView
  79. {
  80. UserID = u.UserId,
  81. CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid,
  82. LoginID = u.LoginId,
  83. Name = u.Name,
  84. RecordStatus = u.RecordStatus,
  85. FirstCode = YLShipBuildLandMapContext.GetPyFirst(u.Name)
  86. };
  87. return sql;
  88. }
  89. private IQueryable<UserView> GetUserRoleViewQueryable(Expression<Func<SysUser, bool>> exp, Expression<Func<SysUserSysRole, bool>> dex)
  90. {
  91. var sql = from u in DbContext.SysUser.Where(exp)
  92. select new UserView
  93. {
  94. UserID = u.UserId,
  95. LoginID = u.LoginId,
  96. CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid,
  97. Name = u.Name,
  98. RecordStatus = u.RecordStatus,
  99. //RoleID=res.RoleId
  100. //RoleList = u.SysUserSysRole.Select(x => new RoleView
  101. //{
  102. // RoleID = x.RoleId,
  103. // RoleName = x.Role.RoleName
  104. //})
  105. };
  106. return sql;
  107. }
  108. #endregion
  109. public IGridResultSet<UserView> GetSysUserForSelect(Guid? prinicipalUserId, string key, int? pageIndex = 1, int? pageSize = 999, Guid?[] departIds = null, string roleName = null)
  110. {
  111. Expression<Func<SysUser, bool>> exp = (x => x.RecordStatus > 0);
  112. bool existUser = true;
  113. if (!string.IsNullOrEmpty(key))
  114. exp = exp.And(x => x.Name.Contains(key));
  115. if (prinicipalUserId != null && prinicipalUserId != Guid.Empty)
  116. {
  117. exp = exp.And(x => x.UserId == prinicipalUserId);
  118. existUser = false;
  119. }
  120. if (!string.IsNullOrEmpty(roleName))
  121. {
  122. exp = exp.And(x => x.SysUserSysRole.Any(w => w.Role.RoleName.Contains(roleName)));
  123. }
  124. var sql = GetSysUserQueryable(exp);
  125. if (departIds != null && departIds.Length > 0 && existUser)
  126. sql = sql.Where(x => departIds.Contains(x.DepartmentID));
  127. return sql.OrderBy(x => x.FirstCode).ToGridResultSet(pageIndex, pageSize);
  128. }
  129. public LoginUser Login(string loginID, string passMd5)
  130. {
  131. var sql = this.GetLoginUserQuerable(u => u.LoginId == loginID && u.Password == passMd5
  132. && u.RecordStatus > 0);
  133. var idsql = this.GetLoginUserQuerable(u => u.CmWorkLinkAccountPsid == loginID && u.Password == passMd5
  134. && u.RecordStatus > 0);
  135. var loginuser = sql.FirstOrDefault() != null ? sql.FirstOrDefault() : idsql.FirstOrDefault();
  136. return loginuser;
  137. }
  138. public LoginUser GetLoginUserById(Guid userID)
  139. {
  140. var sql = this.GetLoginUserQuerable(u => u.UserId == userID);
  141. return sql.FirstOrDefault();
  142. }
  143. public IGridResultSet<UserView> GetUserViewsForSelect(string loginID, string name, int? pageIndex = null, int? pageSize = null, Guid?[] departIds = null, bool isAllUser = false)
  144. {
  145. Expression<Func<SysUser, bool>> exp = (x => x.RecordStatus > 0);
  146. if (!string.IsNullOrEmpty(loginID))
  147. {
  148. exp = exp.And(x => x.LoginId.Contains(loginID));
  149. }
  150. if (!string.IsNullOrEmpty(name))
  151. {
  152. exp = exp.And(x => x.Name.Contains(name));
  153. }
  154. IGridResultSet<UserView> result = null;
  155. if (isAllUser)
  156. {
  157. result = GetAllUsersQueryable(exp).OrderBy(x => x.UserID).ToGridResultSet(pageIndex, pageSize);
  158. }
  159. return result;
  160. }
  161. public IGridResultSet<UserView> GetUserViewList(string userName, Guid? roleID, int? recordStatus, List<Guid> useridList, int? pageIndex = null, int? pageSize = null)
  162. {
  163. Expression<Func<SysUser, bool>> exp = (x => true);
  164. Expression<Func<SysUserSysRole, bool>> dex = (x => true);
  165. exp = exp.AndIf(useridList != null && useridList.Count > 0, e => useridList.Contains(e.UserId));
  166. if (!string.IsNullOrEmpty(userName))
  167. {
  168. exp = exp.And(x => x.Name.Contains(userName));
  169. }
  170. if (roleID != null)
  171. {
  172. dex = dex.And(x => x.RoleId == roleID);
  173. var userIds = DbContext.SysUserSysRole.Where(dex).Select(x => x.UserId).ToList();
  174. if (userIds.Count > 0)
  175. {
  176. exp = exp.And(x => userIds.Contains(x.UserId));
  177. }
  178. }
  179. if (recordStatus.HasValue)
  180. {
  181. exp = exp.And(x => x.RecordStatus == recordStatus);
  182. }
  183. var query = GetUserRoleViewQueryable(exp, dex);
  184. var userList = query.Distinct().OrderBy(x => x.Name)
  185. .ToGridResultSet(pageIndex, pageSize);
  186. var userIDList = userList.rows.Select(x => (Guid?)x.UserID).ToList();
  187. var roleList = DbContext.SysUserSysRole.Where(x => userIDList.Contains(x.UserId)).Select(x => new { UserID = x.UserId, RoleID = x.RoleId, RoleName = x.Role.RoleName })
  188. .ToList();
  189. foreach (var user in userList.rows)
  190. {
  191. user.RoleList = roleList.Where(x => x.UserID == user.UserID).Select(x => new RoleView { RoleID = x.RoleID, RoleName = x.RoleName }).ToList();
  192. }
  193. return userList;
  194. }
  195. public UserView GetUserView(Guid userID)
  196. {
  197. return this.GetUserViewQueryable(x => x.UserId == userID).FirstOrDefault();
  198. }
  199. public async Task Save(UserView userView, Guid operatorID)
  200. {
  201. DateTime nowTime = DateTime.Now;
  202. SysUser user = new SysUser();
  203. if (userView.UserID.HasValue)
  204. {
  205. user = DbContext.SysUser
  206. .FirstOrDefault(x => x.UserId == userView.UserID);
  207. user.LoginId = userView.LoginID;
  208. user.Name = userView.Name;
  209. user.CmWorkLinkAccountPsid = userView.CmWorkLinkAccountPsid;
  210. user.RecordStatus = userView.RecordStatus;
  211. user.ModifyUserId = operatorID;
  212. user.ModifyTime = nowTime;
  213. this.DbContext.Update(user);
  214. }
  215. else
  216. {
  217. string pwd = "yldy@2211";
  218. MD5 md5 = MD5.Create();
  219. user.Password = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(pwd.Trim()))).Replace("-", null);
  220. user.UserId = Guid.NewGuid();
  221. user.LoginId = userView.LoginID;
  222. user.Name = userView.Name;
  223. user.CmWorkLinkAccountPsid = userView.CmWorkLinkAccountPsid;
  224. user.RecordStatus = userView.RecordStatus;
  225. user.CreateUserId = operatorID;
  226. user.CreateTime = nowTime;
  227. this.DbContext.Add(user);
  228. }
  229. await this.DbContext.SaveChangesAsync();
  230. }
  231. public async Task Delete(List<Guid> userIDList)
  232. {
  233. using (var trans = DbContext.Database.BeginTransaction())
  234. {
  235. try
  236. {
  237. foreach (Guid userId in userIDList)
  238. {
  239. }
  240. DbContext.SysUser.Where(x => userIDList.Contains(x.UserId)).Delete();
  241. await this.DbContext.SaveChangesAsync();
  242. trans.Commit();
  243. }
  244. catch (Exception)
  245. {
  246. trans.Rollback();
  247. throw new Exception("该账号已用于其他业务功能,不可删除,建议禁用该账户");
  248. }
  249. }
  250. }
  251. public UserView GetUserViewByName(string loginID, string name)
  252. {
  253. //return this.GetUserViewQueryable(x => x.LoginId == loginID || x.Name == name.Trim()).FirstOrDefault();
  254. //取消用户名唯一限制
  255. return this.GetUserViewQueryable(x => x.LoginId == loginID).FirstOrDefault();
  256. }
  257. public async Task UpdatePwd(Guid userID, string newPassword)
  258. {
  259. SysUser user = DbContext.SysUser.Where(x => x.UserId == userID).FirstOrDefault();
  260. if (user.Password.Equals(newPassword))
  261. {
  262. throw new Exception("新密码不可和旧密码相同!");
  263. }
  264. user.Password = newPassword.ToUpper();
  265. user.ModifyTime = DateTime.Now;
  266. user.ModifyUserId = userID;
  267. DbContext.Update(user);
  268. await DbContext.SaveChangesAsync();
  269. }
  270. public async Task ResetPwd(Guid Id, Guid userID)
  271. {
  272. SysUser user = DbContext.SysUser.Where(x => x.UserId == Id).FirstOrDefault();
  273. if (user == null)
  274. {
  275. throw new Exception("用户不存在");
  276. }
  277. string pwd = "yldy@2211";
  278. MD5 md5 = MD5.Create();
  279. user.Password = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(pwd.Trim()))).Replace("-", null);
  280. user.ModifyUserId = userID;
  281. user.ModifyTime = DateTime.Now;
  282. DbContext.Update(user);
  283. await DbContext.SaveChangesAsync();
  284. }
  285. public List<UserView> GetUserViewListByRoleName(string roleName)
  286. {
  287. var roleNames = roleName.Split(",");
  288. var curUserList = this.GetUserViewQueryable(x => x.RecordStatus > 0 && x.SysUserSysRole.Any(w => roleNames.Contains(w.Role.RoleName))).ToList();
  289. return curUserList;
  290. }
  291. public LoginUser GetLoginUserByWeChatUserId(string weChatUserId)
  292. {
  293. var sql = this.GetLoginUserQuerable(u => u.WeChatUserId == weChatUserId);
  294. return sql.FirstOrDefault();
  295. }
  296. public int SetWechatUser(Guid userId, string weChatUserId)
  297. {
  298. this.DbContext.SysUser.Where(u => u.UserId == userId && (string.IsNullOrEmpty(u.WeChatUserId) || u.WeChatUserId == weChatUserId)).Update(e => new SysUser { WeChatUserId = weChatUserId, WeChatLoginTime = DateTime.Now });
  299. return this.DbContext.SaveChanges();
  300. }
  301. public IGridResultSet<UserRoleView> GetUserRoleList()
  302. {
  303. var query = from a in DbContext.SysRole
  304. select new UserRoleView
  305. {
  306. RoleID = a.RoleId,
  307. RoleName = a.RoleName
  308. };
  309. return query.Distinct().OrderBy(a => a.RoleName).ToGridResultSet();
  310. }
  311. public void ChangePassword(string loginID, string password, string newPassword)
  312. {
  313. var md5Password = password.MD5();
  314. var user = this.DbContext.SysUser.FirstOrDefault(x => x.LoginId == loginID && x.Password == md5Password && x.RecordStatus > 0);
  315. if (user != null)
  316. {
  317. user.Password = newPassword.MD5();
  318. this.DbContext.SaveChanges();
  319. }
  320. else
  321. {
  322. throw new Exception("原密码输入错误,请重新输入。");
  323. }
  324. }
  325. public LoginUser GetLoginUserByCmOAId(string oaID)
  326. {
  327. var sql = this.GetLoginUserQuerable(u => u.CmWorkLinkAccountPsid == oaID
  328. && u.RecordStatus > 0);
  329. var loginuser = sql.FirstOrDefault();
  330. return loginuser;
  331. }
  332. }
  333. }