UserService.cs 17 KB

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