ProcessRepository.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Exceptions;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity.WorkProcess;
  10. using GxPress.EnumConst;
  11. using GxPress.Repository.Interface;
  12. using GxPress.Repository.Interface.WorkProcess;
  13. using GxPress.Result.Process;
  14. using Microsoft.Extensions.Caching.Distributed;
  15. using Microsoft.Extensions.Options;
  16. using ProcessNode = GxPress.Entity.WorkProcess.ProcessNode;
  17. namespace GxPress.Repository.Implement.WorkProcess
  18. {
  19. public partial class ProcessRepository : IProcessRepository
  20. {
  21. private readonly Repository<Process> _repository;
  22. private readonly IMapper _mapper;
  23. private readonly IProcessFieldRepository _processFieldRepository;
  24. private readonly IProcessGroupRepository _processGroupRepository;
  25. private readonly IProcessNodeRepository _processNodeRepository;
  26. private readonly IProcessRequestLimitRepository _processRequestLimitRepository;
  27. private readonly IRuleConditionRepository _conditionRuleRepository;
  28. private readonly IRuleCarbonCopyRepository _ruleCarbonCopyRepository;
  29. private readonly IRuleApproverCheckRepository _ruleApproverCheckRepository;
  30. private readonly IDepartmentRepository _departmentRepository;
  31. private readonly IRoleRepository _roleRepository;
  32. private readonly IUserRepository _userRepository;
  33. public ProcessRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
  34. IDistributedCache cache,
  35. IProcessFieldRepository processFieldRepository,
  36. IProcessGroupRepository processGroupRepository,
  37. IProcessRequestLimitRepository processRequestLimitRepository,
  38. IProcessNodeRepository processNodeRepository,
  39. IRuleConditionRepository conditionRuleRepository,
  40. IRuleCarbonCopyRepository ruleCarbonCopyRepository,
  41. IRuleApproverCheckRepository ruleApproverCheckRepository,
  42. IDepartmentRepository departmentRepository,
  43. IRoleRepository roleRepository,
  44. IUserRepository userRepository)
  45. {
  46. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  47. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  48. _repository = new Repository<Process>(database, cache);
  49. _mapper = mapper;
  50. _processFieldRepository = processFieldRepository;
  51. _processGroupRepository = processGroupRepository;
  52. _processRequestLimitRepository = processRequestLimitRepository;
  53. _processNodeRepository = processNodeRepository;
  54. _conditionRuleRepository = conditionRuleRepository;
  55. _ruleCarbonCopyRepository = ruleCarbonCopyRepository;
  56. _ruleApproverCheckRepository = ruleApproverCheckRepository;
  57. _departmentRepository = departmentRepository;
  58. _roleRepository = roleRepository;
  59. _userRepository = userRepository;
  60. }
  61. public IDatabase Database => _repository.Database;
  62. public string TableName => _repository.TableName;
  63. public List<TableColumn> TableColumns => _repository.TableColumns;
  64. public string CacheKey(int id) => $"{nameof(ProcessRepository)}:{id}";
  65. public async Task<int> InsertAsync(Process process)
  66. {
  67. process.Id = 0;
  68. return await _repository.InsertAsync(process);
  69. }
  70. public async Task UpdateAsync(Process process)
  71. {
  72. await _repository.UpdateAsync(process, Q.CachingSet(CacheKey(process.Id)));
  73. }
  74. public async Task<Process> GetAsync(int processId)
  75. {
  76. return await _repository.GetAsync(Q
  77. .Where(nameof(Process.Id), processId)
  78. // .CachingGet(CacheKey(processId))
  79. );
  80. }
  81. public async Task<bool> ExistsAsync(int id)
  82. {
  83. return await _repository.ExistsAsync(id);
  84. }
  85. public async Task<IEnumerable<Process>> GetListAsync(string keyword)
  86. {
  87. var result = await _repository.GetAllAsync(Q
  88. .OrderByDesc(nameof(Process.Sort))
  89. .OrderBy(nameof(Process.Id)));
  90. foreach (var item in result)
  91. item.IconUrl = StringUtils.AddDomain(item.IconUrl);
  92. return result;
  93. }
  94. public async Task<IEnumerable<Process>> GetAllAsync(SqlKata.Query query)
  95. {
  96. return await _repository.GetAllAsync(query);
  97. }
  98. public async Task<IEnumerable<Process>> GetListByGroupIdAsync(int groupId)
  99. {
  100. var result = await _repository.GetAllAsync(Q
  101. .Where(nameof(Process.GroupId), groupId)
  102. .OrderByDesc(nameof(Process.Sort))
  103. .OrderBy(nameof(Process.Id))
  104. );
  105. foreach (var item in result)
  106. item.IconUrl = StringUtils.AddDomain(item.IconUrl);
  107. return result;
  108. }
  109. public async Task<IEnumerable<Process>> GetListByDepartmentIdAsync(int departmentId)
  110. {
  111. var departmentIds = await GetDepartmentIdsAsync(departmentId, new List<int>());
  112. var processIdList = await _processRequestLimitRepository.GetProcessIdListByDepartmentIdAsync(departmentIds);
  113. var result = await _repository.GetAllAsync(Q
  114. .WhereIn(nameof(Process.Id), processIdList)
  115. .OrWhere(nameof(Process.LimitType), nameof(LimitTypeConst.NoLimit))
  116. .OrderByDesc(nameof(Process.Sort))
  117. .OrderBy(nameof(Process.Id))
  118. );
  119. foreach (var item in result)
  120. item.IconUrl = StringUtils.AddDomain(item.IconUrl);
  121. return result;
  122. }
  123. public async Task<List<int>> GetDepartmentIdsAsync(int departmentId, List<int> departmentIds)
  124. {
  125. var department = await _departmentRepository.GetAsync(departmentId);
  126. departmentIds.Add(department.Id);
  127. if (department != null && department.ParentId > 0)
  128. {
  129. department = await _departmentRepository.GetAsync(department.ParentId);
  130. departmentIds.Add(department.Id);
  131. if (department.ParentId > 0)
  132. await GetDepartmentIdsAsync(department.ParentId, departmentIds);
  133. }
  134. return departmentIds;
  135. }
  136. public async Task<ProcessBaseInfoResult> GetBaseInfoAsync(int id)
  137. {
  138. var process = await _repository.GetAsync(id);
  139. if (process == null) throw new BusinessException("该流程不存在");
  140. var result = _mapper.Map<ProcessBaseInfoResult>(process);
  141. return result;
  142. }
  143. public async Task<List<ProcessFormSettingResult>> GetFormSettingAsync(int id)
  144. {
  145. var fields = await _processFieldRepository.GetListAsync(id);
  146. var result = _mapper.Map<List<ProcessFormSettingResult>>(fields);
  147. return result;
  148. }
  149. public async Task<ProcessNodeTreeResult> GetNodeTreeAsync(int id)
  150. {
  151. var nodeList = await _processNodeRepository.GetListAsync(id, 0);
  152. return await ListToTreeAsync(nodeList.FirstOrDefault(x => x.ParentId == 0), nodeList);
  153. }
  154. private async Task<ProcessNodeTreeResult> ListToTreeAsync(ProcessNode node, IEnumerable<ProcessNode> all)
  155. {
  156. var model = new ProcessNodeTreeResult
  157. {
  158. Id = node.Id,
  159. Name = node.Name,
  160. Type = node.Type
  161. };
  162. var children = all.Where(m => m.ParentId == model.Id);
  163. foreach (var child in children)
  164. {
  165. model.Children.Add(await ListToTreeAsync(child, all));
  166. }
  167. return model;
  168. }
  169. public async Task<bool> DeleteAsync(int id)
  170. {
  171. return await _repository.DeleteAsync(Q
  172. .Where(nameof(Process.Id), id)
  173. .CachingRemove(CacheKey(id))
  174. ) == 1;
  175. }
  176. }
  177. }