UserService.cs 20 KB

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