UserService.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using AutoMapper;
  7. using GxPress.Common.Exceptions;
  8. using GxPress.Common.Page;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.App.User;
  12. using GxPress.Request.User;
  13. using GxPress.Result.User;
  14. using GxPress.Service.Interface;
  15. using GxPress.Service.Interface.IM;
  16. using Microsoft.AspNetCore.Hosting;
  17. using OfficeOpenXml;
  18. using Datory;
  19. using GxPress.Common.Tools;
  20. using GxPress.Result.App.User;
  21. using GxPress.Repository.Interface.WorkFlow;
  22. using GxPress.EnumConst;
  23. using GxPress.Request.App.Flow;
  24. using GxPress.Repository.Interface.WaitHandle;
  25. using GxPress.Request.AddressBookGroup;
  26. using GxPress.Repository.Interface.DepartmentUser;
  27. using GxPress.Repository.Interface.Friends;
  28. namespace GxPress.Service.Implement
  29. {
  30. public class UserService : IUserService
  31. {
  32. private readonly IUserRepository _userRepository;
  33. private readonly IDepartmentRepository _departmentRepository;
  34. private readonly IAddressBookGroupRepository _addressBookGroupRepository;
  35. private IWebHostEnvironment _webHostEnvironment;
  36. private readonly IBlacklistUserRepository _blacklistUserRepository;
  37. private readonly IRoleRepository _roleRepository;
  38. private readonly IIMService _imService;
  39. private readonly IMapper _mapper;
  40. private readonly IFlowRepository _flowRepository;
  41. private readonly IFlowTodoRepository _flowTodoRepository;
  42. private readonly IWaitHandleRepository waitHandleRepository;
  43. private readonly IDepartmentUserRepository departmentUserRepository;
  44. private readonly IFriendsRepository friendsRepository;
  45. public UserService(IUserRepository userRepository, IDepartmentRepository departmentRepository,
  46. IAddressBookGroupRepository addressBookGroupRepository, IBlacklistUserRepository blacklistUserRepository,
  47. IMapper mapper, IWebHostEnvironment webHostEnvironment, IRoleRepository roleRepository, IIMService imService, IFlowRepository flowRepository, IFlowTodoRepository flowTodoRepository, IWaitHandleRepository waitHandleRepository, IDepartmentUserRepository departmentUserRepository, IFriendsRepository friendsRepository)
  48. {
  49. _userRepository = userRepository;
  50. _departmentRepository = departmentRepository;
  51. _addressBookGroupRepository = addressBookGroupRepository;
  52. _blacklistUserRepository = blacklistUserRepository;
  53. _mapper = mapper;
  54. _webHostEnvironment = webHostEnvironment;
  55. _roleRepository = roleRepository;
  56. _imService = imService;
  57. _flowRepository = flowRepository;
  58. _flowTodoRepository = flowTodoRepository;
  59. this.waitHandleRepository = waitHandleRepository;
  60. this.departmentUserRepository = departmentUserRepository;
  61. this.friendsRepository = friendsRepository;
  62. }
  63. /// <summary>
  64. /// 根据部门ID获取自建ID获取用户列表
  65. /// </summary>
  66. /// <param name="request"></param>
  67. /// <returns></returns>
  68. public async Task<IEnumerable<UserInfoResult>> FindUser(FindUserRequest request)
  69. {
  70. var userInfoResults = new List<UserInfoResult>();
  71. if (request.TypeId == 1)
  72. {
  73. //获取部门
  74. List<Entity.Department> departments = new List<Entity.Department>();
  75. foreach (var item in request.ItemId)
  76. {
  77. if (item > 0)
  78. {
  79. var users = await _userRepository.GetUserByDepartmentIdAsync(item);
  80. userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
  81. }
  82. departments = await _departmentRepository.GetDepartmentByPid(item, departments);
  83. }
  84. foreach (var department in departments)
  85. {
  86. //获取部门下面的员工
  87. var users = await _userRepository.GetUserByDepartmentIdAsync(department.Id);
  88. userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
  89. }
  90. }
  91. //查询自建用户
  92. if (request.TypeId == 2)
  93. {
  94. var users = new List<User>();
  95. foreach (var item in request.ItemId)
  96. {
  97. if (item > 0)
  98. {
  99. users.AddRange(await _addressBookGroupRepository.GetUserByAddressBookGroupIdAsync(item));
  100. }
  101. users = await _addressBookGroupRepository.GetUserByAddressBookGroupPidAsync(users, item,
  102. request.UserId);
  103. }
  104. userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
  105. }
  106. foreach (var item in userInfoResults)
  107. {
  108. if (item == null)
  109. continue;
  110. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  111. }
  112. return userInfoResults.Distinct();
  113. }
  114. /// <summary>
  115. /// 根据ID查询用户数据
  116. /// </summary>
  117. /// <param name="userId"></param>
  118. /// <param name="blackUserId"></param>
  119. /// <returns></returns>
  120. public async Task<UserDetail> GetUserByIdAsync(int userId, int blackUserId)
  121. {
  122. var user = await _userRepository.GetAsync(blackUserId);
  123. if (user == null)
  124. {
  125. throw new BusinessException("用户id有误");
  126. }
  127. var userDetail = _mapper.Map<UserDetail>(user);
  128. // if (user.DepartmentId > 0)
  129. // {
  130. // var department = await _departmentRepository.GetAsync(user.DepartmentId);
  131. // userDetail.DepartmentName = department.Name;
  132. // }
  133. userDetail.AvatarUrl = StringUtils.AddDomainMin(userDetail.AvatarUrl);
  134. userDetail.HistoryAvatarUrl = StringUtils.AddDomainMin(userDetail.HistoryAvatarUrl);
  135. userDetail.IsBlack = await _blacklistUserRepository.FindUserIsBlack(userId, blackUserId);
  136. return userDetail;
  137. }
  138. /// <summary>
  139. /// 生成excel
  140. /// </summary>
  141. /// <returns></returns>
  142. public async Task<string> ExcelUserAsync(List<int> userIds)
  143. {
  144. string sWebRootFolder = _webHostEnvironment.WebRootPath;
  145. string sFileName = $"{Guid.NewGuid()}.xlsx";
  146. FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
  147. using (ExcelPackage package = new ExcelPackage(file))
  148. {
  149. // 添加worksheet
  150. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("员工信息"); //添加头
  151. worksheet.Cells[1, 1].Value = "姓名";
  152. worksheet.Cells[1, 2].Value = "手机号码";
  153. worksheet.Cells[1, 3].Value = "邮箱"; //添加值
  154. worksheet.Cells[1, 4].Value = "性别"; //添加值
  155. worksheet.Cells[1, 5].Value = "部门"; //添加值
  156. worksheet.Cells[1, 6].Value = "角色"; //添加值
  157. //获取用户
  158. var users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(User.Id), userIds));
  159. var enumerable = users as User[] ?? users.ToArray();
  160. for (int i = 0; i < enumerable.Count(); i++)
  161. {
  162. var index = i + 2;
  163. worksheet.Cells["A" + index].Value = enumerable[i].Name;
  164. worksheet.Cells["B" + index].Value = enumerable[i].Phone;
  165. worksheet.Cells["C" + index].Value = enumerable[i].Email;
  166. worksheet.Cells["D" + index].Value = enumerable[i].Gender == "Male" ? "男" : "女";
  167. //获取部门
  168. var department = await _departmentRepository.GetAsync(enumerable[i].DepartmentId);
  169. worksheet.Cells["E" + index].Value = department == null ? "" : department.Name;
  170. //获取角色
  171. var role = await _roleRepository.GetAsync(enumerable[i].RoleId);
  172. worksheet.Cells["F" + index].Value = role == null ? "" : role.Name;
  173. }
  174. package.Save();
  175. }
  176. return Path.Combine(sWebRootFolder, sFileName);
  177. }
  178. /// <summary>
  179. /// 用户分页列表
  180. /// </summary>
  181. /// <param name="request"></param>
  182. /// <returns></returns>
  183. public async Task<PagedList<UserResult>> GetPagedListAsync(UserPageSearchRequest request)
  184. {
  185. var pagedList = new PagedList<UserResult>
  186. {
  187. Total = await GetCountAsync(request.DepartmentId, request.RoleId, request.Keyword)
  188. };
  189. var list = await GetPageListAsync(request.Page, request.PerPage, request.DepartmentId, request.RoleId,
  190. request.Keyword);
  191. var items = list.Select(user => _mapper.Map<UserResult>(user)).ToList();
  192. foreach (var item in items)
  193. {
  194. item.AvatarUrl = StringUtils.AddDomain(item.AvatarUrl);
  195. // var departmentName = string.Empty;
  196. // //获取部门
  197. // departmentName =
  198. // await _departmentRepository.GetDepartmentFullPathAsync(item.DepartmentId, departmentName);
  199. // item.DepartmentName = departmentName;
  200. item.DepartmentIds = await departmentUserRepository.GetDepartmentIdsAsync(item.Id);
  201. var role = await _roleRepository.GetAsync(item.RoleId);
  202. if (role != null)
  203. item.RoleName = role.Name;
  204. }
  205. pagedList.Items = items;
  206. return pagedList;
  207. }
  208. public async Task<int> GetCountAsync(int departmentId, int roleId, string keyword)
  209. {
  210. var query = Q.NewQuery();
  211. if (departmentId > 0)
  212. {
  213. var userIds = await departmentUserRepository.GetUserIdsAsync(departmentId);
  214. query.WhereIn(nameof(User.Id), userIds);
  215. query.Where(nameof(User.DepartmentId), departmentId);
  216. }
  217. if (roleId > 0)
  218. {
  219. query.Where(nameof(User.RoleId), roleId);
  220. }
  221. if (!string.IsNullOrEmpty(keyword))
  222. {
  223. keyword = "%" + keyword + "%";
  224. query.Where(q =>
  225. q.WhereLike(nameof(User.Name), keyword)
  226. .OrWhereLike(nameof(User.Email), keyword)
  227. .OrWhereLike(nameof(User.Phone), keyword)
  228. );
  229. }
  230. return await _userRepository.CountAsync(query);
  231. }
  232. /// <summary>
  233. /// Set the limit and offset for a given page.
  234. /// </summary>
  235. /// <param name="page"></param>
  236. /// <param name="perPage"></param>
  237. /// <param name="departmentId"></param>
  238. /// <param name="roleId"></param>
  239. /// <param name="keyword"></param>
  240. /// <returns></returns>
  241. public async Task<IEnumerable<User>> GetPageListAsync(int page, int perPage, int departmentId, int roleId,
  242. string keyword)
  243. {
  244. var query = Q.ForPage(page, perPage);
  245. if (departmentId > 0)
  246. {
  247. var userIds = await departmentUserRepository.GetUserIdsAsync(departmentId);
  248. query.WhereIn(nameof(User.Id), userIds);
  249. //query.Where(nameof(User.DepartmentId), departmentId);
  250. }
  251. if (roleId > 0)
  252. {
  253. query.Where(nameof(User.RoleId), roleId);
  254. }
  255. if (!string.IsNullOrEmpty(keyword))
  256. {
  257. keyword = "%" + keyword + "%";
  258. query.Where(q =>
  259. q.WhereLike(nameof(User.Name), keyword)
  260. .OrWhereLike(nameof(User.Email), keyword)
  261. .OrWhereLike(nameof(User.Phone), keyword)
  262. );
  263. }
  264. return await _userRepository.GetAllAsync(query);
  265. }
  266. public async Task<PagedList<User>> GetPageListAsync(UserPageSearchRequest request)
  267. {
  268. var result = new PagedList<User>();
  269. var query = Q.ForPage(request.Page, request.PerPage);
  270. if (request.DepartmentId > 0)
  271. {
  272. var userIds = await departmentUserRepository.GetUserIdsAsync(request.DepartmentId);
  273. query.WhereIn(nameof(User.Id), userIds);
  274. //query.Where(nameof(User.DepartmentId), request.DepartmentId);
  275. }
  276. if (request.RoleId > 0)
  277. {
  278. query.Where(nameof(User.RoleId), request.RoleId);
  279. }
  280. if (!string.IsNullOrEmpty(request.Keyword))
  281. {
  282. request.Keyword = "%" + request.Keyword + "%";
  283. query.Where(q =>
  284. q.WhereLike(nameof(User.Name), request.Keyword)
  285. .OrWhereLike(nameof(User.Email), request.Keyword)
  286. .OrWhereLike(nameof(User.Phone), request.Keyword)
  287. );
  288. }
  289. result.Items = await _userRepository.GetAllAsync(query);
  290. result.Total = await _userRepository.CountAsync(query);
  291. return result;
  292. }
  293. /// <summary>
  294. /// 删除用户
  295. /// </summary>
  296. /// <param name="userIds"></param>
  297. /// <returns></returns>
  298. public async Task<bool> DeleteUsersAsync(IEnumerable<int> userIds)
  299. {
  300. var enumerable = userIds as int[] ?? userIds.ToArray();
  301. foreach (var userId in enumerable)
  302. {
  303. var user = await _userRepository.GetAsync(userId);
  304. if (user != null)
  305. {
  306. await _imService.DeleteUserAsync(user.ImId);
  307. }
  308. }
  309. return await _userRepository.DeleteUsersAsync(enumerable);
  310. }
  311. /// <summary>
  312. /// 获取用户未读数据
  313. /// </summary>
  314. /// <param name="userId"></param>
  315. /// <returns></returns>
  316. public async Task<UserCountResult> GetUserCountAsync(int userId)
  317. {
  318. var result = await _userRepository.GetUserCountAsync(userId);
  319. //工作我的审批未操作数量
  320. var request = new FlowListRequest();
  321. result.FlowAuditUReadCount = await _flowTodoRepository.GetCountAsync(userId, nameof(TodoTypeConst.ApproverCheck), false,
  322. request);
  323. // 工作我发起的已批复未读数量
  324. var flowState = new List<string> { FlowState.Checked.GetValue(), FlowState.Denied.GetValue() };
  325. result.FlowSendUReadCount = await _flowRepository.CountAsync(Q.Where(nameof(Entity.WorkFlow.Flow.UserId), userId).WhereIn(nameof(Entity.WorkFlow.Flow.State), flowState).Where(nameof(Entity.WorkFlow.Flow.IsRead), false).Where(nameof(Entity.WorkFlow.Flow.ProcessId), ">", 0));
  326. // 工作抄送我的未读数量
  327. result.FlowCcUReadCount = await _flowTodoRepository.FlowCcUReadCountAsync(userId);
  328. //待办事项未执行数量
  329. result.WaitHandleUFinishCount = await waitHandleRepository.GetUFinishAsync(userId);
  330. return result;
  331. }
  332. /// <summary>
  333. /// 获取用户的通讯录
  334. /// </summary>
  335. /// <param name="userId"></param>
  336. /// <returns></returns>
  337. public async Task<UserLinkResult> GetUserLinkResultAsync(int userId)
  338. {
  339. //获取用户
  340. var user = await _userRepository.GetAsync(userId);
  341. //单位通讯录
  342. var userLinkResult = new List<UserLinkModelResult>();
  343. if (user.DepartmentId > 0)
  344. {
  345. var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Entity.Department.ParentId), 0));
  346. var userLinkModelAttributeResult = new List<UserLinkModelAttributeResult>();
  347. foreach (var item in departments)
  348. userLinkModelAttributeResult.Add(new UserLinkModelAttributeResult { Id = item.Id, Name = item.Name });
  349. userLinkResult.Add(new UserLinkModelResult
  350. {
  351. TypeId = 1,
  352. Name = "单位通讯录",
  353. Items = userLinkModelAttributeResult
  354. });
  355. }
  356. //系统通讯录
  357. userLinkResult.Add(
  358. new UserLinkModelResult
  359. {
  360. TypeId = 2,
  361. Name = "系统通讯录",
  362. Items = new List<UserLinkModelAttributeResult>{
  363. new UserLinkModelAttributeResult{Id=0,Name="我的小组",TypeValue=1},
  364. new UserLinkModelAttributeResult{Id=0,Name="我的群聊",TypeValue=2},
  365. new UserLinkModelAttributeResult{Id=0,Name="我的课程",TypeValue=3}
  366. }
  367. }
  368. );
  369. //自建通讯录
  370. var addressBookGroupList = await _addressBookGroupRepository.GetAddressBookGroupListAsync(
  371. new AddressBookGroupSearchRequest { ParendId = 0, UserId = userId });
  372. var systemLinkModelAttributeResults = new List<UserLinkModelAttributeResult>();
  373. foreach (var item in addressBookGroupList)
  374. systemLinkModelAttributeResults.Add(new UserLinkModelAttributeResult { Id = item.Id, Name = item.GroupName });
  375. userLinkResult.Add(
  376. new UserLinkModelResult
  377. {
  378. TypeId = 3,
  379. Name = "自建通讯录",
  380. Items = systemLinkModelAttributeResults
  381. }
  382. );
  383. //获取用户好友
  384. var userIds = await friendsRepository.GetFriendsIdsAsync(userId, 0);
  385. var result = new UserLinkResult()
  386. {
  387. Items = userLinkResult,
  388. FriendUsers = await _userRepository.GetUserInfoResultsAsync(userIds)
  389. };
  390. if (result.FriendUsers != null && result.FriendUsers.Count() > 0)
  391. foreach (var item in result.FriendUsers)
  392. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  393. return result;
  394. }
  395. /// <summary>
  396. /// 更新用户信息
  397. /// </summary>
  398. /// <param name="id"></param>
  399. /// <param name="request"></param>
  400. /// <returns></returns>
  401. public async Task<bool> UpdateAsync(int id, UserInfoRequest request)
  402. {
  403. var user = await _userRepository.GetAsync(id);
  404. if (user == null) throw new BusinessException("该用户不存在");
  405. if (request.DepartmentId.Count() > 0)
  406. {
  407. await departmentUserRepository.AddDepartmentUserAsync(request.DepartmentId, id);
  408. }
  409. if (!string.IsNullOrEmpty(request.DepartmentName))
  410. {
  411. user.DepartmentName = request.DepartmentName;
  412. }
  413. if (!string.IsNullOrEmpty(request.Nick))
  414. user.Nick = request.Nick;
  415. if (!string.IsNullOrEmpty(request.Description))
  416. user.Description = request.Description;
  417. if (!string.IsNullOrEmpty(request.Gender))
  418. user.Gender = request.Gender;
  419. if (!string.IsNullOrEmpty(request.Name))
  420. user.Name = request.Name;
  421. if (!string.IsNullOrEmpty(request.Position))
  422. user.Position = request.Position;
  423. if (request.RoleId > 0)
  424. user.RoleId = request.RoleId;
  425. if (!string.IsNullOrEmpty(request.Signature))
  426. user.Signature = request.Signature;
  427. //用户头像
  428. if (!string.IsNullOrEmpty(request.AvatarUrl))
  429. {
  430. user.HistoryAvatarUrl = user.AvatarUrl;
  431. user.AvatarUrl = StringUtils.RemoveDomain(request.AvatarUrl);
  432. }
  433. user.Email = request.Email;
  434. if (!string.IsNullOrWhiteSpace(request.Phone))
  435. user.Phone = request.Phone;
  436. user.QRCoder = StringUtils.RemoveDomain(user.QRCoder);
  437. if (request.IsDisable > 0)
  438. user.IsDisable = request.IsDisable == 1;
  439. if (request.IsFreeze > 0)
  440. user.IsFreeze = request.IsFreeze == 1;
  441. if (request.IsVip > 0)
  442. user.IsVip = request.IsVip == 1;
  443. if (request.IsLeader > 0)
  444. user.IsLeader = request.IsLeader == 1;
  445. if (request.IsMute > 0)
  446. user.IsMute = request.IsMute == 1;
  447. if (!string.IsNullOrWhiteSpace(request.DisableTiem))
  448. user.DisableTiem = Convert.ToDateTime(request.DisableTiem);
  449. return await _userRepository.UpdateAsync(user);
  450. }
  451. public async Task<UserInfoResult> GetUserInfoAsync(int id)
  452. {
  453. var result = await _userRepository.GetUserInfoAsync(id);
  454. result.DepartmentIds = await departmentUserRepository.GetDepartmentIdsAsync(id);
  455. return result;
  456. }
  457. /// <summary>
  458. /// 查询不是好友的用户
  459. /// </summary>
  460. /// <returns></returns>
  461. public async Task<IEnumerable<UserInfoResult>> FindUserInfoNoFriendsResultAsync(int userId, string keyword)
  462. {
  463. var userIds = await friendsRepository.GetFriendsIdsAsync(userId, -1);
  464. var userIdsList = userIds.ToList();
  465. userIdsList.Add(userId);
  466. //判断是否有部门
  467. if (await departmentUserRepository.IsExistsAsync(userId))
  468. //获取部门数据
  469. userIdsList.AddRange(await departmentUserRepository.GetUserIdsAsync());
  470. var result = await _userRepository.GetUserInfoNoFriendsResults(keyword, userIdsList);
  471. return result;
  472. }
  473. }
  474. }