AddressBookGroupRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. }
  86. return addressBookGroupResults;
  87. }
  88. /// <summary>
  89. /// 添加
  90. /// </summary>
  91. /// <param name="request"></param>
  92. /// <returns></returns>
  93. public async Task<AddressBookGroup> InsertAsync(AddressBookGroupInRequest request)
  94. {
  95. var addressBookGroup = new AddressBookGroup
  96. {
  97. GroupName = request.AddressBookGroupName,
  98. UserId = request.UserId,
  99. ParentId = request.ParentId
  100. };
  101. var id = await _repository.InsertAsync(addressBookGroup);
  102. addressBookGroup = await _repository.GetAsync(id);
  103. addressBookGroup.Sort = id;
  104. await _repository.UpdateAsync(addressBookGroup);
  105. return addressBookGroup;
  106. }
  107. /// <summary>
  108. /// 获取通讯录用户组
  109. /// </summary>
  110. /// <param name="request"></param>
  111. /// <returns></returns>
  112. public async Task<AddressBookListResult> GetAddressBookListAsync(AddressBookListRequest request)
  113. {
  114. var result = new AddressBookListResult();
  115. //获取系统分配组
  116. var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), 0));
  117. result.Departments = departments.ToList();
  118. var addressBookGroups =
  119. await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.UserId), request.UserId).OrderByDesc(nameof(AddressBookGroup.Sort)));
  120. var list = addressBookGroups.ToList();
  121. result.AddressBookGroups = list;
  122. return result;
  123. }
  124. /// <summary>
  125. /// 添加通讯录用户
  126. /// </summary>
  127. /// <param name="addressBookGroupId"></param>
  128. /// <param name="addressBookGroupUser"></param>
  129. /// <returns></returns>
  130. public async Task<bool> IsInsertAddressBookUserAsync(int addressBookGroupId,
  131. AddressBookGroupUser addressBookGroupUser)
  132. {
  133. if (await _addressBookGroupUserRepository.CountAsync(Q
  134. .Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId)
  135. .Where(nameof(AddressBookGroupUser.UserId), addressBookGroupUser.UserId)) == 0)
  136. {
  137. await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  138. }
  139. return true;
  140. }
  141. /// <summary>
  142. /// 添加联系人
  143. /// </summary>
  144. /// <param name="request"></param>
  145. /// <returns></returns>
  146. public async Task<bool> InsertAddressBookUserAsync(AddressBookInUserRequest request)
  147. {
  148. if (request.AddressBookGroupId <= 0)
  149. {
  150. throw new BusinessException("文件ID为空");
  151. }
  152. try
  153. {
  154. using (TransactionScope transactionScope = new TransactionScope())
  155. {
  156. //添加用户
  157. foreach (var userId in request.UserIds)
  158. {
  159. var addressBookGroupUser = new AddressBookGroupUser
  160. {
  161. AddressBookGroupId = request.AddressBookGroupId,
  162. UserId = userId
  163. };
  164. //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  165. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  166. }
  167. //添加部门人员
  168. foreach (var departmentId in request.DepartmentIds)
  169. {
  170. List<User> users = new List<User>();
  171. users = await GetUserByDepartmentIdAsync(users, departmentId);
  172. foreach (var user in users)
  173. {
  174. var addressBookGroupUser = new AddressBookGroupUser
  175. {
  176. AddressBookGroupId = request.AddressBookGroupId,
  177. UserId = user.Id
  178. };
  179. // await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  180. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  181. }
  182. }
  183. //添加组人员
  184. foreach (var addressBookGroupId in request.AddressBookGroupIds)
  185. {
  186. var addressBookGroupUserList = new List<AddressBookGroupUser>();
  187. if (addressBookGroupId == 0)
  188. {
  189. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync();
  190. addressBookGroupUserList.AddRange(addressBookGroupUsers);
  191. }
  192. else
  193. {
  194. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(
  195. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId));
  196. addressBookGroupUserList.AddRange(addressBookGroupUsers);
  197. }
  198. foreach (var item in addressBookGroupUserList)
  199. {
  200. var addressBookGroupUser = new AddressBookGroupUser
  201. {
  202. AddressBookGroupId = request.AddressBookGroupId,
  203. UserId = item.UserId
  204. };
  205. //await _addressBookGroupUserRepository.InsertAsync(addressBookGroupUser);
  206. await IsInsertAddressBookUserAsync(request.AddressBookGroupId, addressBookGroupUser);
  207. }
  208. }
  209. transactionScope.Complete();
  210. }
  211. }
  212. catch (Exception e)
  213. {
  214. throw new BusinessException(e.Message);
  215. }
  216. return true;
  217. }
  218. /// <summary>
  219. /// 递归查询部门下级用户
  220. /// </summary>
  221. /// <param name="users"></param>
  222. /// <param name="departmentId"></param>
  223. /// <returns></returns>
  224. public async Task<List<User>> GetUserByDepartmentIdAsync(List<User> users, int departmentId)
  225. {
  226. var userList = await _userRepository.GetAllAsync(Q.Where(nameof(User.DepartmentId), departmentId));
  227. users.AddRange(userList);
  228. var departments =
  229. await _departmentRepository.GetAllAsync(Q.Where(nameof(Department.ParentId), departmentId));
  230. if (departments.Any())
  231. {
  232. foreach (var department in departments)
  233. {
  234. await GetUserByDepartmentIdAsync(users, department.Id);
  235. }
  236. }
  237. return users;
  238. }
  239. /// <summary>
  240. /// 根据通讯录PID获取用户
  241. /// </summary>
  242. /// <param name="users"></param>
  243. /// <param name="pid"></param>
  244. /// <param name="userId"></param>
  245. /// <returns></returns>
  246. public async Task<List<User>> GetUserByAddressBookGroupPidAsync(List<User> users, int pid, int userId)
  247. {
  248. var addressBookGroups = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroup.ParentId), pid).Where(nameof(AddressBookGroup.UserId), userId));
  249. if (!addressBookGroups.Any())
  250. {
  251. return users;
  252. }
  253. foreach (var addressBookGroup in addressBookGroups)
  254. {
  255. var addressBookGroupUsers =
  256. await _addressBookGroupUserRepository.GetAllAsync(
  257. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroup.Id));
  258. if (!addressBookGroupUsers.Any())
  259. continue;
  260. foreach (var item in addressBookGroupUsers)
  261. {
  262. var user = await _userRepository.GetAsync(item.UserId);
  263. users.Add(user);
  264. }
  265. await GetUserByAddressBookGroupPidAsync(users, addressBookGroup.Id, userId);
  266. }
  267. return users;
  268. }
  269. /// <summary>
  270. /// 根据组ID获取用户
  271. /// </summary>
  272. /// <param name="addressBookGroupId"></param>
  273. /// <returns></returns>
  274. public async Task<List<User>> GetUserByAddressBookGroupIdAsync(int addressBookGroupId)
  275. {
  276. var users = new List<User>();
  277. var addressBookGroupUserList =
  278. await _addressBookGroupUserRepository.GetAllAsync(
  279. Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), addressBookGroupId));
  280. foreach (var item in addressBookGroupUserList)
  281. {
  282. var user = await _userRepository.GetAsync(item.UserId);
  283. users.Add(user);
  284. }
  285. return users;
  286. }
  287. public async Task<List<AddressBookGroup>> GetAllAsync(SqlKata.Query query)
  288. {
  289. return await _repository.GetAllAsync(query);
  290. }
  291. /// <summary>
  292. /// 排序
  293. /// </summary>
  294. /// <param name="request"></param>
  295. /// <returns></returns>
  296. public async Task<bool> SortAsync(AddressBookGroupSortRequest request)
  297. {
  298. var frist = await _repository.GetAsync(request.FristId);
  299. var last = await _repository.GetAsync(request.LastId);
  300. if (last != null && frist != null)
  301. {
  302. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  303. var database = new Database(databaseType, _connectionString);
  304. var connection = database.GetConnection();
  305. if (frist.Sort < last.Sort)
  306. {
  307. //修改last
  308. var lessThanSql = $"update tede_address_book_group set Sort=Sort-1 where UserId={request.UserId} and Sort<={last.Sort} and Id>0";
  309. if (await connection.ExecuteAsync(lessThanSql) > 0)
  310. {
  311. //修改sort
  312. frist.Sort = last.Sort;
  313. await _repository.UpdateAsync(frist);
  314. return true;
  315. }
  316. }
  317. else
  318. {
  319. //修改last
  320. var lessThanSql = $"update tede_address_book_group set Sort=Sort+1 where UserId={request.UserId} and Sort>={last.Sort} and Id>0";
  321. if (await connection.ExecuteAsync(lessThanSql) > 0)
  322. {
  323. //修改sort
  324. frist.Sort = last.Sort;
  325. await _repository.UpdateAsync(frist);
  326. return true;
  327. }
  328. }
  329. }
  330. return false;
  331. }
  332. }
  333. }