UserService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. namespace GxPress.Service.Implement
  26. {
  27. public class UserService : IUserService
  28. {
  29. private readonly IUserRepository _userRepository;
  30. private readonly IDepartmentRepository _departmentRepository;
  31. private readonly IAddressBookGroupRepository _addressBookGroupRepository;
  32. private IWebHostEnvironment _webHostEnvironment;
  33. private readonly IBlacklistUserRepository _blacklistUserRepository;
  34. private readonly IRoleRepository _roleRepository;
  35. private readonly IIMService _imService;
  36. private readonly IMapper _mapper;
  37. private readonly IFlowRepository _flowRepository;
  38. private readonly IFlowTodoRepository _flowTodoRepository;
  39. private readonly IWaitHandleRepository waitHandleRepository;
  40. public UserService(IUserRepository userRepository, IDepartmentRepository departmentRepository,
  41. IAddressBookGroupRepository addressBookGroupRepository, IBlacklistUserRepository blacklistUserRepository,
  42. IMapper mapper, IWebHostEnvironment webHostEnvironment, IRoleRepository roleRepository, IIMService imService, IFlowRepository flowRepository, IFlowTodoRepository flowTodoRepository, IWaitHandleRepository waitHandleRepository)
  43. {
  44. _userRepository = userRepository;
  45. _departmentRepository = departmentRepository;
  46. _addressBookGroupRepository = addressBookGroupRepository;
  47. _blacklistUserRepository = blacklistUserRepository;
  48. _mapper = mapper;
  49. _webHostEnvironment = webHostEnvironment;
  50. _roleRepository = roleRepository;
  51. _imService = imService;
  52. _flowRepository = flowRepository;
  53. _flowTodoRepository = flowTodoRepository;
  54. this.waitHandleRepository = waitHandleRepository;
  55. }
  56. /// <summary>
  57. /// 根据部门ID获取自建ID获取用户列表
  58. /// </summary>
  59. /// <param name="request"></param>
  60. /// <returns></returns>
  61. public async Task<IEnumerable<UserInfoResult>> FindUser(FindUserRequest request)
  62. {
  63. var userInfoResults = new List<UserInfoResult>();
  64. if (request.TypeId == 1)
  65. {
  66. //获取部门
  67. List<Entity.Department> departments = new List<Entity.Department>();
  68. foreach (var item in request.ItemId)
  69. {
  70. if (item > 0)
  71. {
  72. var users = await _userRepository.GetUserByDepartmentIdAsync(item);
  73. userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
  74. }
  75. departments = await _departmentRepository.GetDepartmentByPid(item, departments);
  76. }
  77. foreach (var department in departments)
  78. {
  79. //获取部门下面的员工
  80. var users = await _userRepository.GetUserByDepartmentIdAsync(department.Id);
  81. userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
  82. }
  83. }
  84. //查询自建用户
  85. if (request.TypeId == 2)
  86. {
  87. var users = new List<User>();
  88. foreach (var item in request.ItemId)
  89. {
  90. if (item > 0)
  91. {
  92. users.AddRange(await _addressBookGroupRepository.GetUserByAddressBookGroupIdAsync(item));
  93. }
  94. users = await _addressBookGroupRepository.GetUserByAddressBookGroupPidAsync(users, item,
  95. request.UserId);
  96. }
  97. userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
  98. }
  99. foreach (var item in userInfoResults)
  100. {
  101. if (item == null)
  102. continue;
  103. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  104. }
  105. return userInfoResults.Distinct();
  106. }
  107. /// <summary>
  108. /// 根据ID查询用户数据
  109. /// </summary>
  110. /// <param name="userId"></param>
  111. /// <param name="blackUserId"></param>
  112. /// <returns></returns>
  113. public async Task<UserDetail> GetUserByIdAsync(int userId, int blackUserId)
  114. {
  115. var user = await _userRepository.GetAsync(blackUserId);
  116. if (user == null)
  117. {
  118. throw new BusinessException("用户id有误");
  119. }
  120. var userDetail = _mapper.Map<UserDetail>(user);
  121. if (user.DepartmentId > 0)
  122. {
  123. var department = await _departmentRepository.GetAsync(user.DepartmentId);
  124. userDetail.DepartmentName = department.Name;
  125. }
  126. userDetail.AvatarUrl = StringUtils.AddDomainMin(userDetail.AvatarUrl);
  127. userDetail.HistoryAvatarUrl = StringUtils.AddDomainMin(userDetail.HistoryAvatarUrl);
  128. userDetail.IsBlack = await _blacklistUserRepository.FindUserIsBlack(userId, blackUserId);
  129. return userDetail;
  130. }
  131. /// <summary>
  132. /// 生成excel
  133. /// </summary>
  134. /// <returns></returns>
  135. public async Task<string> ExcelUserAsync(List<int> userIds)
  136. {
  137. string sWebRootFolder = _webHostEnvironment.WebRootPath;
  138. string sFileName = $"{Guid.NewGuid()}.xlsx";
  139. FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
  140. using (ExcelPackage package = new ExcelPackage(file))
  141. {
  142. // 添加worksheet
  143. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("员工信息"); //添加头
  144. worksheet.Cells[1, 1].Value = "姓名";
  145. worksheet.Cells[1, 2].Value = "手机号码";
  146. worksheet.Cells[1, 3].Value = "邮箱"; //添加值
  147. worksheet.Cells[1, 4].Value = "性别"; //添加值
  148. worksheet.Cells[1, 5].Value = "部门"; //添加值
  149. worksheet.Cells[1, 6].Value = "角色"; //添加值
  150. //获取用户
  151. var users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(User.Id), userIds));
  152. var enumerable = users as User[] ?? users.ToArray();
  153. for (int i = 0; i < enumerable.Count(); i++)
  154. {
  155. var index = i + 2;
  156. worksheet.Cells["A" + index].Value = enumerable[i].Name;
  157. worksheet.Cells["B" + index].Value = enumerable[i].Phone;
  158. worksheet.Cells["C" + index].Value = enumerable[i].Email;
  159. worksheet.Cells["D" + index].Value = enumerable[i].Gender == "Male" ? "男" : "女";
  160. //获取部门
  161. var department = await _departmentRepository.GetAsync(enumerable[i].DepartmentId);
  162. worksheet.Cells["E" + index].Value = department == null ? "" : department.Name;
  163. //获取角色
  164. var role = await _roleRepository.GetAsync(enumerable[i].RoleId);
  165. worksheet.Cells["F" + index].Value = role == null ? "" : role.Name;
  166. }
  167. package.Save();
  168. }
  169. return Path.Combine(sWebRootFolder, sFileName);
  170. }
  171. /// <summary>
  172. /// 用户分页列表
  173. /// </summary>
  174. /// <param name="request"></param>
  175. /// <returns></returns>
  176. public async Task<PagedList<UserResult>> GetPagedListAsync(UserPageSearchRequest request)
  177. {
  178. var pagedList = new PagedList<UserResult>
  179. {
  180. Total = await GetCountAsync(request.DepartmentId, request.RoleId, request.Keyword)
  181. };
  182. var list = await GetPageListAsync(request.Page, request.PerPage, request.DepartmentId, request.RoleId,
  183. request.Keyword);
  184. var items = list.Select(user => _mapper.Map<UserResult>(user)).ToList();
  185. foreach (var item in items)
  186. {
  187. var departmentName = string.Empty;
  188. //获取部门
  189. departmentName =
  190. await _departmentRepository.GetDepartmentFullPathAsync(item.DepartmentId, departmentName);
  191. item.DepartmentName = departmentName;
  192. var role = await _roleRepository.GetAsync(item.RoleId);
  193. if (role != null)
  194. item.RoleName = role.Name;
  195. }
  196. pagedList.Items = items;
  197. return pagedList;
  198. }
  199. public async Task<int> GetCountAsync(int departmentId, int roleId, string keyword)
  200. {
  201. var query = Q.NewQuery();
  202. if (departmentId > 0)
  203. {
  204. query.Where(nameof(User.DepartmentId), departmentId);
  205. }
  206. if (roleId > 0)
  207. {
  208. query.Where(nameof(User.RoleId), roleId);
  209. }
  210. if (!string.IsNullOrEmpty(keyword))
  211. {
  212. keyword = "%" + keyword + "%";
  213. query.Where(q =>
  214. q.WhereLike(nameof(User.Name), keyword)
  215. .OrWhereLike(nameof(User.Email), keyword)
  216. .OrWhereLike(nameof(User.Phone), keyword)
  217. );
  218. }
  219. return await _userRepository.CountAsync(query);
  220. }
  221. /// <summary>
  222. /// Set the limit and offset for a given page.
  223. /// </summary>
  224. /// <param name="page"></param>
  225. /// <param name="perPage"></param>
  226. /// <param name="departmentId"></param>
  227. /// <param name="roleId"></param>
  228. /// <param name="keyword"></param>
  229. /// <returns></returns>
  230. public async Task<IEnumerable<User>> GetPageListAsync(int page, int perPage, int departmentId, int roleId,
  231. string keyword)
  232. {
  233. var query = Q.ForPage(page, perPage);
  234. if (departmentId > 0)
  235. {
  236. query.Where(nameof(User.DepartmentId), departmentId);
  237. }
  238. if (roleId > 0)
  239. {
  240. query.Where(nameof(User.RoleId), roleId);
  241. }
  242. if (!string.IsNullOrEmpty(keyword))
  243. {
  244. keyword = "%" + keyword + "%";
  245. query.Where(q =>
  246. q.WhereLike(nameof(User.Name), keyword)
  247. .OrWhereLike(nameof(User.Email), keyword)
  248. .OrWhereLike(nameof(User.Phone), keyword)
  249. );
  250. }
  251. return await _userRepository.GetAllAsync(query);
  252. }
  253. public async Task<PagedList<User>> GetPageListAsync(UserPageSearchRequest request)
  254. {
  255. var result = new PagedList<User>();
  256. var query = Q.ForPage(request.Page, request.PerPage);
  257. if (request.DepartmentId > 0)
  258. {
  259. query.Where(nameof(User.DepartmentId), request.DepartmentId);
  260. }
  261. if (request.RoleId > 0)
  262. {
  263. query.Where(nameof(User.RoleId), request.RoleId);
  264. }
  265. if (!string.IsNullOrEmpty(request.Keyword))
  266. {
  267. request.Keyword = "%" + request.Keyword + "%";
  268. query.Where(q =>
  269. q.WhereLike(nameof(User.Name), request.Keyword)
  270. .OrWhereLike(nameof(User.Email), request.Keyword)
  271. .OrWhereLike(nameof(User.Phone), request.Keyword)
  272. );
  273. }
  274. result.Items = await _userRepository.GetAllAsync(query);
  275. result.Total = await _userRepository.CountAsync(query);
  276. return result;
  277. }
  278. /// <summary>
  279. /// 删除用户
  280. /// </summary>
  281. /// <param name="userIds"></param>
  282. /// <returns></returns>
  283. public async Task<bool> DeleteUsersAsync(IEnumerable<int> userIds)
  284. {
  285. var enumerable = userIds as int[] ?? userIds.ToArray();
  286. foreach (var userId in enumerable)
  287. {
  288. var user = await _userRepository.GetAsync(userId);
  289. if (user != null)
  290. {
  291. await _imService.DeleteUserAsync(user.ImId);
  292. }
  293. }
  294. return await _userRepository.DeleteUsersAsync(enumerable);
  295. }
  296. /// <summary>
  297. /// 获取用户未读数据
  298. /// </summary>
  299. /// <param name="userId"></param>
  300. /// <returns></returns>
  301. public async Task<UserCountResult> GetUserCountAsync(int userId)
  302. {
  303. var result = await _userRepository.GetUserCountAsync(userId);
  304. //工作我的审批未操作数量
  305. var request = new FlowListRequest();
  306. result.FlowAuditUReadCount = await _flowTodoRepository.GetCountAsync(userId, nameof(TodoTypeConst.ApproverCheck), false,
  307. request);
  308. // 工作我发起的已批复未读数量
  309. var flowState = new List<string> { FlowState.Checked.GetValue(), FlowState.Denied.GetValue() };
  310. 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));
  311. // 工作抄送我的未读数量
  312. result.FlowCcUReadCount = await _flowTodoRepository.FlowCcUReadCountAsync(userId);
  313. //待办事项未执行数量
  314. result.WaitHandleUFinishCount = await waitHandleRepository.GetUFinishAsync(userId);
  315. return result;
  316. }
  317. }
  318. }