AddressBookGroupRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. using Dapper;
  18. namespace GxPress.Repository.Implement
  19. {
  20. public class AddressBookGroupRepository : IAddressBookGroupRepository
  21. {
  22. private readonly Repository<AddressBookGroup> _repository;
  23. private readonly Repository<AddressBookGroupUser> _addressBookGroupUserRepository;
  24. private readonly Repository<Department> _departmentRepository;
  25. private readonly Repository<User> _userRepository;
  26. private readonly IMapper _mapper;
  27. private readonly string _connectionString;
  28. private readonly string _databaseTypeStr;
  29. public AddressBookGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  30. {
  31. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  32. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  33. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  34. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  35. _repository = new Repository<AddressBookGroup>(database);
  36. _addressBookGroupUserRepository = new Repository<AddressBookGroupUser>(database);
  37. _departmentRepository = new Repository<Department>(database);
  38. _userRepository = new Repository<User>(database);
  39. _mapper = mapper;
  40. }
  41. public IDatabase Database => _repository.Database;
  42. public string TableName => _repository.TableName;
  43. public List<TableColumn> TableColumns => _repository.TableColumns;
  44. /// <summary>
  45. /// 删除
  46. /// </summary>
  47. /// <param name="id"></param>
  48. /// <returns></returns>
  49. public async Task<bool> DeleteAsync(int id)
  50. {
  51. //删除组下面的
  52. await _addressBookGroupUserRepository.DeleteAsync(Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId),
  53. id));
  54. return await _repository.DeleteAsync(id);
  55. }
  56. /// <summary>
  57. /// 修改
  58. /// </summary>
  59. /// <param name="request"></param>
  60. /// <returns></returns>
  61. public async Task<bool> UpdateAsync(AddressBookGroupUpRequest request)
  62. {
  63. var addressBookGroup = await _repository.GetAsync(request.AddressBookGroupId);
  64. addressBookGroup.GroupName = request.GroupName;
  65. return await _repository.UpdateAsync(addressBookGroup);
  66. }
  67. /// <summary>
  68. /// 获取用户通讯录组
  69. /// </summary>
  70. /// <param name="request"></param>
  71. /// <returns></returns>
  72. public async Task<IEnumerable<AddressBookGroupResult>> GetAddressBookGroupListAsync(
  73. AddressBookGroupSearchRequest request)
  74. {
  75. var result = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).Where(nameof(AddressBookGroup.ParentId), request.ParendId).OrderByDesc(nameof(AddressBookGroup.Sort)));
  76. var addressBookGroupResults = result.Select(n => _mapper.Map<AddressBookGroupResult>(n)).ToList();
  77. for (int i = 0; i < addressBookGroupResults.Count; i++)
  78. {
  79. if (request.FolderIds.Contains(addressBookGroupResults[i].Id))
  80. addressBookGroupResults.Remove(addressBookGroupResults[i]);
  81. else
  82. {
  83. addressBookGroupResults[i].IsChildren = await _repository.ExistsAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).Where(nameof(AddressBookGroup.ParentId), addressBookGroupResults[i].Id));
  84. }
  85. addressBookGroupResults[i].UserCount = await _addressBookGroupUserRepository.CountAsync(Q.Where(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), addressBookGroupResults[i].Id));
  86. }
  87. return addressBookGroupResults;
  88. }
  89. /// <summary>
  90. /// 添加
  91. /// </summary>
  92. /// <param name="request"></param>
  93. /// <returns></returns>
  94. public async Task<AddressBookGroup> InsertAsync(AddressBookGroupInRequest request)
  95. {
  96. var addressBookGroup = new AddressBookGroup
  97. {
  98. GroupName = request.AddressBookGroupName,
  99. UserId = request.UserId,
  100. ParentId = request.ParentId
  101. };
  102. var id = await _repository.InsertAsync(addressBookGroup);
  103. addressBookGroup = await _repository.GetAsync(id);
  104. addressBookGroup.Sort = id;
  105. await _repository.UpdateAsync(addressBookGroup);
  106. return addressBookGroup;
  107. }
  108. /// <summary>
  109. /// 获取通讯录用户组
  110. /// </summary>
  111. /// <param name="request"></param>
  112. /// <returns></returns>
  113. public async Task<AddressBookListResult> GetAddressBookListAsync(AddressBookListRequest request)
  114. {
  115. var result = new AddressBookListResult();
  116. //获取系统分配组
  117. var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), 0));
  118. result.Departments = departments.ToList();
  119. var addressBookGroups =
  120. await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).OrderByDesc(nameof(AddressBookGroup.Sort)));
  121. var list = addressBookGroups.ToList();
  122. result.AddressBookGroups = list;
  123. return result;
  124. }
  125. /// <summary>
  126. /// 添加通讯录用户
  127. /// </summary>
  128. /// <param name="addressBookGroupId"></param>
  129. /// <param name="addressBookGroupUser"></param>
  130. /// <returns></returns>
  131. public async Task<bool> IsInsertAddressBookUserAsync(int addressBookGroupId,
  132. AddressBookGroupUser addressBookGroupUser)
  133. {
  134. if (await _addressBookGroupUserRepository.CountAsync(Q
  135. .Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId)
  136. .Where(nameof(AddressBookGroupUser.UserId), addressBookGroupUser.UserId)) == 0)
  137. {
  138. await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  139. }
  140. return true;
  141. }
  142. /// <summary>
  143. /// 添加联系人
  144. /// </summary>
  145. /// <param name="request"></param>
  146. /// <returns></returns>
  147. public async Task<bool> InsertAddressBookUserAsync(AddressBookInUserRequest request)
  148. {
  149. if (request.AddressBookGroupId <= 0)
  150. {
  151. throw new BusinessException("文件ID为空");
  152. }
  153. try
  154. {
  155. using (TransactionScope transactionScope = new TransactionScope())
  156. {
  157. //添加用户
  158. foreach (var userId in request.UserIds)
  159. {
  160. var addressBookGroupUser = new AddressBookGroupUser
  161. {
  162. AddressBookGroupId = request.AddressBookGroupId,
  163. UserId = userId
  164. };
  165. //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  166. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  167. }
  168. //添加部门人员
  169. foreach (var departmentId in request.DepartmentIds)
  170. {
  171. List<User> users = new List<User>();
  172. users = await GetUserByDepartmentIdAsync(users, departmentId);
  173. foreach (var user in users)
  174. {
  175. var addressBookGroupUser = new AddressBookGroupUser
  176. {
  177. AddressBookGroupId = request.AddressBookGroupId,
  178. UserId = user.Id
  179. };
  180. // await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  181. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  182. }
  183. }
  184. //添加组人员
  185. foreach (var addressBookGroupId in request.AddressBookGroupIds)
  186. {
  187. var addressBookGroupUserList = new List<AddressBookGroupUser>();
  188. if (addressBookGroupId == 0)
  189. {
  190. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync();
  191. addressBookGroupUserList.AddRange(addressBookGroupUsers);
  192. }
  193. else
  194. {
  195. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(
  196. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId));
  197. addressBookGroupUserList.AddRange(addressBookGroupUsers);
  198. }
  199. foreach (var item in addressBookGroupUserList)
  200. {
  201. var addressBookGroupUser = new AddressBookGroupUser
  202. {
  203. AddressBookGroupId = request.AddressBookGroupId,
  204. UserId = item.UserId
  205. };
  206. //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  207. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  208. }
  209. }
  210. transactionScope.Complete();
  211. }
  212. }
  213. catch (Exception e)
  214. {
  215. throw new BusinessException(e.Message);
  216. }
  217. return true;
  218. }
  219. /// <summary>
  220. /// 递归查询部门下级用户
  221. /// </summary>
  222. /// <param name="users"></param>
  223. /// <param name="departmentId"></param>
  224. /// <returns></returns>
  225. public async Task<List<User>> GetUserByDepartmentIdAsync(List<User> users, int departmentId)
  226. {
  227. var userList = await _userRepository.GetAllAsync(Q.Where(nameof(User.DepartmentId), departmentId));
  228. users.AddRange(userList);
  229. var departments =
  230. await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), departmentId));
  231. if (departments.Any())
  232. {
  233. foreach (var department in departments)
  234. {
  235. await GetUserByDepartmentIdAsync(users, department.Id);
  236. }
  237. }
  238. return users;
  239. }
  240. /// <summary>
  241. /// 根据通讯录PID获取用户
  242. /// </summary>
  243. /// <param name="users"></param>
  244. /// <param name="pid"></param>
  245. /// <param name="userId"></param>
  246. /// <returns></returns>
  247. public async Task<List<User>> GetUserByAddressBookGroupPidAsync(List<User> users, int pid, int userId)
  248. {
  249. var addressBookGroups = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.ParentId), pid).Where(nameof(AddressBookGroup.UserId), userId));
  250. if (!addressBookGroups.Any())
  251. {
  252. return users;
  253. }
  254. foreach (var addressBookGroup in addressBookGroups)
  255. {
  256. var addressBookGroupUsers =
  257. await _addressBookGroupUserRepository.GetAllAsync(
  258. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroup.Id));
  259. if (!addressBookGroupUsers.Any())
  260. continue;
  261. foreach (var item in addressBookGroupUsers)
  262. {
  263. var user = await _userRepository.GetAsync(item.UserId);
  264. users.Add(user);
  265. }
  266. await GetUserByAddressBookGroupPidAsync(users, addressBookGroup.Id, userId);
  267. }
  268. return users;
  269. }
  270. /// <summary>
  271. /// 根据组ID获取用户
  272. /// </summary>
  273. /// <param name="addressBookGroupId"></param>
  274. /// <returns></returns>
  275. public async Task<List<User>> GetUserByAddressBookGroupIdAsync(int addressBookGroupId)
  276. {
  277. var users = new List<User>();
  278. var addressBookGroupUserList =
  279. await _addressBookGroupUserRepository.GetAllAsync(
  280. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId));
  281. foreach (var item in addressBookGroupUserList)
  282. {
  283. var user = await _userRepository.GetAsync(item.UserId);
  284. users.Add(user);
  285. }
  286. return users;
  287. }
  288. public async Task<List<AddressBookGroup>> GetAllAsync(SqlKata.Query query)
  289. {
  290. return await _repository.GetAllAsync(query);
  291. }
  292. /// <summary>
  293. /// 排序
  294. /// </summary>
  295. /// <param name="request"></param>
  296. /// <returns></returns>
  297. public async Task<bool> SortAsync(AddressBookGroupSortRequest request)
  298. {
  299. var frist = await _repository.GetAsync(request.FristId);
  300. var last = await _repository.GetAsync(request.LastId);
  301. if (last != null && frist != null)
  302. {
  303. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  304. var database = new Database(databaseType, _connectionString);
  305. var connection = database.GetConnection();
  306. if (frist.Sort < last.Sort)
  307. {
  308. //修改last
  309. var lessThanSql = $"update tede_address_book_group set Sort=Sort-1 where UserId={request.UserId} and Sort<={last.Sort} and Id>0";
  310. if (await connection.ExecuteAsync(lessThanSql) > 0)
  311. {
  312. //修改sort
  313. frist.Sort = last.Sort;
  314. await _repository.UpdateAsync(frist);
  315. return true;
  316. }
  317. }
  318. else
  319. {
  320. //修改last
  321. var lessThanSql = $"update tede_address_book_group set Sort=Sort+1 where UserId={request.UserId} and Sort>={last.Sort} and Id>0";
  322. if (await connection.ExecuteAsync(lessThanSql) > 0)
  323. {
  324. //修改sort
  325. frist.Sort = last.Sort;
  326. await _repository.UpdateAsync(frist);
  327. return true;
  328. }
  329. }
  330. }
  331. return false;
  332. }
  333. }
  334. }