UserService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Common;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Threading.Tasks;
  7. using YiSha.Data;
  8. using YiSha.Data.Repository;
  9. using YiSha.Entity.OrganizationManage;
  10. using YiSha.Enum;
  11. using YiSha.Enum.OrganizationManage;
  12. using YiSha.IService.OrganizationManage;
  13. using YiSha.Model.Param.OrganizationManage;
  14. using YiSha.Util;
  15. using YiSha.Util.Extension;
  16. using YiSha.Util.Model;
  17. namespace YiSha.Service.OrganizationManage
  18. {
  19. public partial class UserService
  20. {
  21. private IRepositoryFactory _baseRepository;
  22. private IUserBelongService _userBelongService;
  23. public UserService(IRepositoryFactory baseRepository, IUserBelongService userBelongService)
  24. {
  25. _baseRepository = baseRepository;
  26. _userBelongService = userBelongService;
  27. }
  28. #region 获取数据
  29. public async Task<List<UserEntity>> GetListPartial(UserListParam param)
  30. {
  31. var expression = ListFilterPartial(param);
  32. var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression);
  33. return list.ToList();
  34. }
  35. public async Task<List<UserEntity>> GetPageListPartial(UserListParam param, Pagination pagination)
  36. {
  37. var expression = ListFilterPartial(param);
  38. var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression, pagination);
  39. return list.ToList();
  40. }
  41. public async Task<UserEntity> GetEntityPartial(int id)
  42. {
  43. return await _baseRepository.BaseRepository(dbConnectType).FindEntity<UserEntity>(id);
  44. }
  45. public async Task<UserEntity> GetEntityByUserNamePartial(string userName)
  46. {
  47. return await _baseRepository.BaseRepository(dbConnectType).FindEntity<UserEntity>(p => p.UserName == userName);
  48. }
  49. public async Task<UserEntity> CheckLoginPartial(string userName)
  50. {
  51. var expression = LinqExtensions.True<UserEntity>();
  52. expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
  53. expression = expression.And(t => t.UserName == userName);
  54. expression = expression.Or(t => t.Mobile == userName);
  55. expression = expression.Or(t => t.Email == userName);
  56. return await _baseRepository.BaseRepository(dbConnectType).FindEntity(expression);
  57. }
  58. public bool ExistUserNamePartial(UserEntity entity)
  59. {
  60. var expression = LinqExtensions.True<UserEntity>();
  61. expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
  62. if (entity.Id.IsNullOrZero())
  63. {
  64. expression = expression.And(t => t.UserName == entity.UserName);
  65. }
  66. else
  67. {
  68. expression = expression.And(t => t.UserName == entity.UserName && t.Id != entity.Id);
  69. }
  70. return _baseRepository.BaseRepository(dbConnectType).IQueryable(expression).Count() > 0 ? true : false;
  71. }
  72. #endregion
  73. #region 提交数据
  74. public async Task UpdateUserPartial(UserEntity entity)
  75. {
  76. await _baseRepository.BaseRepository(dbConnectType).Update(entity);
  77. }
  78. public async Task SaveFormPartial(UserEntity entity)
  79. {
  80. var isAdd = false;
  81. try
  82. {
  83. //如果id为0,先在数据库中新增用户信息,获取用户的userid便于职位、角色信息的关联(事务中无法获取到用户的userid)
  84. if (entity.Id.IsNullOrZero())
  85. {
  86. isAdd = true;
  87. //新增用户
  88. await entity.Create();
  89. await _baseRepository.BaseRepository(dbConnectType).Insert(entity);
  90. }
  91. // 职位
  92. var userBelongPositionList = new List<UserBelongEntity>();
  93. if (!string.IsNullOrEmpty(entity.PositionIds))
  94. {
  95. foreach (int positionId in TextHelper.SplitToArray<int>(entity.PositionIds, ','))
  96. {
  97. UserBelongEntity positionBelongEntity = new UserBelongEntity();
  98. positionBelongEntity.UserId = entity.Id;
  99. positionBelongEntity.BelongId = positionId;
  100. positionBelongEntity.BelongType = UserBelongTypeEnum.Position.ParseToInt();
  101. await positionBelongEntity.Create();
  102. userBelongPositionList.Add(positionBelongEntity);
  103. }
  104. }
  105. // 角色
  106. var userBelongRoleList = new List<UserBelongEntity>();
  107. if (!string.IsNullOrEmpty(entity.RoleIds))
  108. {
  109. foreach (int roleId in TextHelper.SplitToArray<int>(entity.RoleIds, ','))
  110. {
  111. UserBelongEntity departmentBelongEntity = new UserBelongEntity();
  112. departmentBelongEntity.UserId = entity.Id;
  113. departmentBelongEntity.BelongId = roleId;
  114. departmentBelongEntity.BelongType = UserBelongTypeEnum.Role.ParseToInt();
  115. await departmentBelongEntity.Create();
  116. userBelongRoleList.Add(departmentBelongEntity);
  117. }
  118. }
  119. //开启事务
  120. var db = await _baseRepository.BaseRepository(dbConnectType).BeginTrans();
  121. try
  122. {
  123. if (!isAdd)
  124. {
  125. //修改用户
  126. await db.Delete<UserBelongEntity>(t => t.UserId == entity.Id);
  127. // 密码不进行更新,有单独的方法更新密码
  128. entity.Password = null;
  129. await entity.Modify();
  130. await db.Update(entity);
  131. }
  132. if (userBelongPositionList.Any())
  133. await _userBelongService.InsertMany(userBelongPositionList);
  134. if (userBelongRoleList.Any())
  135. await _userBelongService.InsertMany(userBelongRoleList);
  136. await db.CommitTrans();
  137. }
  138. catch
  139. {
  140. await db.RollbackTrans();
  141. throw;
  142. }
  143. }
  144. catch
  145. {
  146. //如果id为0且用户的userid不为零,则需要删除新增的用户信息
  147. if (isAdd)
  148. {
  149. await _baseRepository.BaseRepository(dbConnectType).Delete(entity);
  150. }
  151. }
  152. }
  153. public async Task DeleteFormPartial(string ids)
  154. {
  155. var db = await _baseRepository.BaseRepository(dbConnectType).BeginTrans();
  156. try
  157. {
  158. //删除用户信息
  159. await DeleteFormById(ids);
  160. //删除所属信息
  161. ids = $"{ string.Join(",", ids.Split(","))}";
  162. List<DbParameter> parameters = new List<DbParameter>();
  163. parameters.Add(DbParameterExtension.CreateDbParameter("@userid", ids, dbConnectType));
  164. await _userBelongService.DeleteFormByWhere(" FIND_IN_SET (userid,@userid) ", parameters);
  165. await db.CommitTrans();
  166. }
  167. catch
  168. {
  169. await db.RollbackTrans();
  170. throw;
  171. }
  172. }
  173. public async Task ResetPasswordPartial(UserEntity entity)
  174. {
  175. await entity.Modify();
  176. await _baseRepository.BaseRepository(dbConnectType).Update(entity);
  177. }
  178. public async Task ChangeUserPartial(UserEntity entity)
  179. {
  180. await entity.Modify();
  181. await _baseRepository.BaseRepository(dbConnectType).Update(entity);
  182. }
  183. #endregion
  184. #region 私有方法
  185. private Expression<Func<UserEntity, bool>> ListFilterPartial(UserListParam param)
  186. {
  187. var expression = LinqExtensions.True<UserEntity>();
  188. expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);
  189. if (param != null)
  190. {
  191. if (!string.IsNullOrEmpty(param.UserName))
  192. {
  193. expression = expression.And(t => t.UserName.Contains(param.UserName));
  194. }
  195. if (!string.IsNullOrEmpty(param.UserIds))
  196. {
  197. int[] userIdList = TextHelper.SplitToArray<int>(param.UserIds, ',');
  198. expression = expression.And(t => userIdList.Contains(t.Id));
  199. }
  200. if (!string.IsNullOrEmpty(param.Mobile))
  201. {
  202. expression = expression.And(t => t.Mobile.Contains(param.Mobile));
  203. }
  204. if (param.UserStatus > -1)
  205. {
  206. expression = expression.And(t => t.UserStatus == param.UserStatus);
  207. }
  208. if (!string.IsNullOrEmpty(param.StartTime.ParseToString()))
  209. {
  210. expression = expression.And(t => t.BaseModifyTime >= param.StartTime);
  211. }
  212. if (!string.IsNullOrEmpty(param.EndTime.ParseToString()))
  213. {
  214. param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
  215. expression = expression.And(t => t.BaseModifyTime <= param.EndTime);
  216. }
  217. if (param.ChildrenDepartmentIdList != null && param.ChildrenDepartmentIdList.Count > 0)
  218. {
  219. expression = expression.And(t => param.ChildrenDepartmentIdList.Contains((int)t.DepartmentId));
  220. }
  221. }
  222. return expression;
  223. }
  224. #endregion
  225. }
  226. }