AddressBookGroupRepository.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Transactions;
  6. using AutoMapper;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Exceptions;
  9. using GxPress.Common.Tools;
  10. using GxPress.Entity;
  11. using GxPress.Repository.Interface;
  12. using GxPress.Request.AddressBookGroup;
  13. using GxPress.Result.AddressBook;
  14. using Microsoft.Extensions.Options;
  15. using Datory;
  16. using GxPress.Result.AddressBookGroup;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class AddressBookGroupRepository : IAddressBookGroupRepository
  20. {
  21. private readonly Repository<AddressBookGroup> _repository;
  22. private readonly Repository<AddressBookGroupUser> _addressBookGroupUserRepository;
  23. private readonly Repository<Department> _departmentRepository;
  24. private readonly Repository<User> _userRepository;
  25. private readonly IMapper _mapper;
  26. public AddressBookGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  27. {
  28. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<AddressBookGroup>(database);
  31. _addressBookGroupUserRepository = new Repository<AddressBookGroupUser>(database);
  32. _departmentRepository = new Repository<Department>(database);
  33. _userRepository = new Repository<User>(database);
  34. _mapper = mapper;
  35. }
  36. public IDatabase Database => _repository.Database;
  37. public string TableName => _repository.TableName;
  38. public List<TableColumn> TableColumns => _repository.TableColumns;
  39. /// <summary>
  40. /// 删除
  41. /// </summary>
  42. /// <param name="id"></param>
  43. /// <returns></returns>
  44. public async Task<bool> DeleteAsync(int id)
  45. {
  46. //删除组下面的
  47. await _addressBookGroupUserRepository.DeleteAsync(Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId),
  48. id));
  49. return await _repository.DeleteAsync(id);
  50. }
  51. /// <summary>
  52. /// 修改
  53. /// </summary>
  54. /// <param name="request"></param>
  55. /// <returns></returns>
  56. public async Task<bool> UpdateAsync(AddressBookGroupUpRequest request)
  57. {
  58. var addressBookGroup = await _repository.GetAsync(request.AddressBookGroupId);
  59. addressBookGroup.GroupName = request.GroupName;
  60. return await _repository.UpdateAsync(addressBookGroup);
  61. }
  62. /// <summary>
  63. /// 获取用户通讯录组
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <returns></returns>
  67. public async Task<IEnumerable<AddressBookGroupResult>> GetAddressBookGroupListAsync(
  68. AddressBookGroupSearchRequest request)
  69. {
  70. var result = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).Where(nameof(AddressBookGroup.ParentId), request.ParendId));
  71. var addressBookGroupResults = result.Select(n => _mapper.Map<AddressBookGroupResult>(n)).ToList();
  72. for (int i = 0; i < addressBookGroupResults.Count; i++)
  73. {
  74. if (request.FolderIds.Contains(addressBookGroupResults[i].Id))
  75. addressBookGroupResults.Remove(addressBookGroupResults[i]);
  76. else
  77. {
  78. addressBookGroupResults[i].IsChildren = await _repository.ExistsAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).Where(nameof(AddressBookGroup.ParentId), addressBookGroupResults[i].Id));
  79. }
  80. }
  81. return addressBookGroupResults;
  82. }
  83. /// <summary>
  84. /// 添加
  85. /// </summary>
  86. /// <param name="request"></param>
  87. /// <returns></returns>
  88. public async Task<bool> InsertAsync(AddressBookGroupInRequest request)
  89. {
  90. var addressBookGroup = new AddressBookGroup
  91. {
  92. GroupName = request.AddressBookGroupName,
  93. UserId = request.UserId,
  94. ParentId = request.ParentId
  95. };
  96. return await _repository.InsertAsync(addressBookGroup) > 0;
  97. }
  98. /// <summary>
  99. /// 获取通讯录用户组
  100. /// </summary>
  101. /// <param name="request"></param>
  102. /// <returns></returns>
  103. public async Task<AddressBookListResult> GetAddressBookListAsync(AddressBookListRequest request)
  104. {
  105. var result = new AddressBookListResult();
  106. //获取系统分配组
  107. var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), 0).OrderBy(nameof(Department.Sort)));
  108. result.Departments = departments.ToList();
  109. var addressBookGroups =
  110. await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId));
  111. var list = addressBookGroups.ToList();
  112. result.AddressBookGroups = list;
  113. return result;
  114. }
  115. /// <summary>
  116. /// 添加通讯录用户
  117. /// </summary>
  118. /// <param name="addressBookGroupId"></param>
  119. /// <param name="addressBookGroupUser"></param>
  120. /// <returns></returns>
  121. public async Task<bool> IsInsertAddressBookUserAsync(int addressBookGroupId,
  122. AddressBookGroupUser addressBookGroupUser)
  123. {
  124. if (await _addressBookGroupUserRepository.CountAsync(Q
  125. .Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId)
  126. .Where(nameof(AddressBookGroupUser.UserId), addressBookGroupUser.UserId)) == 0)
  127. {
  128. await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  129. }
  130. return true;
  131. }
  132. /// <summary>
  133. /// 添加联系人
  134. /// </summary>
  135. /// <param name="request"></param>
  136. /// <returns></returns>
  137. public async Task<bool> InsertAddressBookUserAsync(AddressBookInUserRequest request)
  138. {
  139. if (request.AddressBookGroupId <= 0)
  140. {
  141. throw new BusinessException("文件ID为空");
  142. }
  143. try
  144. {
  145. using (TransactionScope transactionScope = new TransactionScope())
  146. {
  147. //添加用户
  148. foreach (var userId in request.UserIds)
  149. {
  150. var addressBookGroupUser = new AddressBookGroupUser
  151. {
  152. AddressBookGroupId = request.AddressBookGroupId,
  153. UserId = userId
  154. };
  155. //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  156. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  157. }
  158. //添加部门人员
  159. foreach (var departmentId in request.DepartmentIds)
  160. {
  161. List<User> users = new List<User>();
  162. users = await GetUserByDepartmentIdAsync(users, departmentId);
  163. foreach (var user in users)
  164. {
  165. var addressBookGroupUser = new AddressBookGroupUser
  166. {
  167. AddressBookGroupId = request.AddressBookGroupId,
  168. UserId = user.Id
  169. };
  170. // await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  171. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  172. }
  173. }
  174. //添加组人员
  175. foreach (var addressBookGroupId in request.AddressBookGroupIds)
  176. {
  177. var addressBookGroupUserList = new List<AddressBookGroupUser>();
  178. if (addressBookGroupId == 0)
  179. {
  180. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync();
  181. addressBookGroupUserList.AddRange(addressBookGroupUsers);
  182. }
  183. else
  184. {
  185. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(
  186. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId));
  187. addressBookGroupUserList.AddRange(addressBookGroupUsers);
  188. }
  189. foreach (var item in addressBookGroupUserList)
  190. {
  191. var addressBookGroupUser = new AddressBookGroupUser
  192. {
  193. AddressBookGroupId = request.AddressBookGroupId,
  194. UserId = item.UserId
  195. };
  196. //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  197. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  198. }
  199. }
  200. transactionScope.Complete();
  201. }
  202. }
  203. catch (Exception e)
  204. {
  205. throw new BusinessException(e.Message);
  206. }
  207. return true;
  208. }
  209. /// <summary>
  210. /// 递归查询部门下级用户
  211. /// </summary>
  212. /// <param name="users"></param>
  213. /// <param name="departmentId"></param>
  214. /// <returns></returns>
  215. public async Task<List<User>> GetUserByDepartmentIdAsync(List<User> users, int departmentId)
  216. {
  217. var userList = await _userRepository.GetAllAsync(Q.Where(nameof(User.DepartmentId), departmentId));
  218. users.AddRange(userList);
  219. var departments =
  220. await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), departmentId));
  221. if (departments.Any())
  222. {
  223. foreach (var department in departments)
  224. {
  225. await GetUserByDepartmentIdAsync(users, department.Id);
  226. }
  227. }
  228. return users;
  229. }
  230. /// <summary>
  231. /// 根据通讯录PID获取用户
  232. /// </summary>
  233. /// <param name="users"></param>
  234. /// <param name="pid"></param>
  235. /// <param name="userId"></param>
  236. /// <returns></returns>
  237. public async Task<List<User>> GetUserByAddressBookGroupPidAsync(List<User> users, int pid, int userId)
  238. {
  239. var addressBookGroups = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.ParentId), pid).Where(nameof(AddressBookGroup.UserId), userId));
  240. if (!addressBookGroups.Any())
  241. {
  242. return users;
  243. }
  244. foreach (var addressBookGroup in addressBookGroups)
  245. {
  246. var addressBookGroupUsers =
  247. await _addressBookGroupUserRepository.GetAllAsync(
  248. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroup.Id));
  249. if (!addressBookGroupUsers.Any())
  250. continue;
  251. foreach (var item in addressBookGroupUsers)
  252. {
  253. var user = await _userRepository.GetAsync(Q.Where(nameof(User.Id), item.UserId).Where(nameof(User.IsDelete), false));
  254. users.Add(user);
  255. }
  256. await GetUserByAddressBookGroupPidAsync(users, addressBookGroup.Id, userId);
  257. }
  258. return users;
  259. }
  260. /// <summary>
  261. /// 根据组ID获取用户
  262. /// </summary>
  263. /// <param name="addressBookGroupId"></param>
  264. /// <returns></returns>
  265. public async Task<List<User>> GetUserByAddressBookGroupIdAsync(int addressBookGroupId)
  266. {
  267. var users = new List<User>();
  268. var addressBookGroupUserList =
  269. await _addressBookGroupUserRepository.GetAllAsync(
  270. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId));
  271. foreach (var item in addressBookGroupUserList)
  272. {
  273. var user = await _userRepository.GetAsync(item.UserId);
  274. users.Add(user);
  275. }
  276. return users;
  277. }
  278. public async Task<List<AddressBookGroup>> GetAllAsync(SqlKata.Query query)
  279. {
  280. return await _repository.GetAllAsync(query);
  281. }
  282. }
  283. }