FlowRepository.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using AutoMapper;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity.WorkFlow;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Repository.Interface.WorkFlow;
  11. using GxPress.Repository.Interface.WorkProcess;
  12. using GxPress.Request.App.Flow;
  13. using Microsoft.Extensions.Options;
  14. using Datory;
  15. using GxPress.EnumConst;
  16. using Dapper;
  17. using GxPress.Common.Page;
  18. namespace GxPress.Repository.Implement.WorkFlow
  19. {
  20. public class FlowRepository : IFlowRepository
  21. {
  22. private readonly Repository<Flow> _repository;
  23. private readonly IMapper _mapper;
  24. private readonly IFlowFieldValueRepository _flowFieldValueRepository;
  25. private readonly IFlowTodoRepository _flowTodoRepository;
  26. private readonly IProcessRepository _processRepository;
  27. private readonly IProcessFieldRepository _processFieldRepository;
  28. private readonly IProcessGroupRepository _processGroupRepository;
  29. private readonly IProcessNodeRepository _processNodeRepository;
  30. private readonly IProcessRequestLimitRepository _processRequestLimitRepository;
  31. private readonly IRuleConditionRepository _conditionRuleRepository;
  32. private readonly IRuleCarbonCopyRepository _ruleCarbonCopyRepository;
  33. private readonly IRuleApproverCheckRepository _ruleApproverCheckRepository;
  34. private readonly IDepartmentRepository _departmentRepository;
  35. private readonly IRoleRepository _roleRepository;
  36. private readonly IUserRepository _userRepository;
  37. private readonly string _connectionString;
  38. private readonly string _databaseTypeStr;
  39. public FlowRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
  40. IFlowFieldValueRepository flowFieldValueRepository,
  41. IFlowTodoRepository flowTodoRepository,
  42. IProcessRepository processRepository,
  43. IProcessFieldRepository processFieldRepository,
  44. IProcessGroupRepository processGroupRepository,
  45. IProcessRequestLimitRepository processRequestLimitRepository,
  46. IProcessNodeRepository processNodeRepository,
  47. IRuleConditionRepository conditionRuleRepository,
  48. IRuleCarbonCopyRepository ruleCarbonCopyRepository,
  49. IRuleApproverCheckRepository ruleApproverCheckRepository,
  50. IDepartmentRepository departmentRepository,
  51. IRoleRepository roleRepository,
  52. IUserRepository userRepository)
  53. {
  54. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  55. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  56. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  57. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  58. _repository = new Repository<Flow>(database);
  59. _mapper = mapper;
  60. _flowFieldValueRepository = flowFieldValueRepository;
  61. _flowTodoRepository = flowTodoRepository;
  62. _processRepository = processRepository;
  63. _processFieldRepository = processFieldRepository;
  64. _processGroupRepository = processGroupRepository;
  65. _processRequestLimitRepository = processRequestLimitRepository;
  66. _processNodeRepository = processNodeRepository;
  67. _conditionRuleRepository = conditionRuleRepository;
  68. _ruleCarbonCopyRepository = ruleCarbonCopyRepository;
  69. _ruleApproverCheckRepository = ruleApproverCheckRepository;
  70. _departmentRepository = departmentRepository;
  71. _roleRepository = roleRepository;
  72. _userRepository = userRepository;
  73. }
  74. public IDatabase Database => _repository.Database;
  75. public string TableName => _repository.TableName;
  76. public List<TableColumn> TableColumns => _repository.TableColumns;
  77. public async Task<IEnumerable<Flow>> GetListAsync(int processId)
  78. {
  79. return await _repository.GetAllAsync(Q
  80. .Where(nameof(Flow.ProcessId), processId)
  81. );
  82. }
  83. public async Task<int> InsertAsync(Flow flow)
  84. {
  85. var id = await _repository.InsertAsync(flow);
  86. var no = StringUtils.GetFlowNo(DateTime.Now, id);
  87. await _repository.UpdateAsync(Q
  88. .Set(nameof(Flow.No), no)
  89. .Where(nameof(Flow.Id), id)
  90. );
  91. return id;
  92. }
  93. public async Task<Flow> GetAsync(int flowId)
  94. {
  95. return await _repository.GetAsync(flowId);
  96. }
  97. public async Task<int> GetCountByUserIdAsync(int userId, bool checking, FlowListRequest request)
  98. {
  99. string sql = $@"SELECT
  100. count(1)
  101. FROM
  102. ccpph.tede_flow a
  103. WHERE 1=1 ";
  104. if (!checking)
  105. sql += $@" and a.State IN ('Denied','Checked')";
  106. else
  107. sql += $@" and a.State Not IN ('Denied','Checked')";
  108. sql += $@" AND a.UserId = {userId}
  109. AND a.ProcessId IN (SELECT
  110. id
  111. FROM
  112. ccpph.tede_process)";
  113. if (!string.IsNullOrWhiteSpace(request.Keyword))
  114. {
  115. sql += $@" AND (a.ProcessId IN (SELECT
  116. Id
  117. FROM
  118. tede_process
  119. WHERE
  120. name LIKE '%{request.Keyword}%')
  121. OR a.Id IN (SELECT
  122. FlowId
  123. FROM
  124. tede_flow_field_value
  125. WHERE
  126. StringValues LIKE '%{request.Keyword}%')
  127. OR a.UserId IN (SELECT
  128. Id
  129. FROM
  130. ccpph.tede_user
  131. WHERE
  132. Name LIKE '%{request.Keyword}%') or a.Id In(SELECT
  133. FlowId
  134. FROM
  135. tede_flow_todo
  136. WHERE
  137. UserId IN (SELECT
  138. Id
  139. FROM
  140. tede_user
  141. WHERE
  142. Name LIKE '%{request.Keyword}%'))
  143. or a.Id In(SELECT
  144. FlowId
  145. FROM
  146. tede_flow_message
  147. WHERE
  148. Message LIKE '%{request.Keyword}%'))";
  149. }
  150. if (request.ProcessIds.Count > 0)
  151. {
  152. var processIdValue = "";
  153. foreach (var item in request.ProcessIds)
  154. {
  155. if (item == 0)
  156. continue;
  157. processIdValue += $"{item},";
  158. }
  159. if (!string.IsNullOrWhiteSpace(processIdValue))
  160. {
  161. processIdValue = processIdValue.Remove(processIdValue.Length - 1, 1);
  162. sql += $" and a.ProcessId in({processIdValue})";
  163. }
  164. }
  165. sql += "ORDER BY a.CreatedDate DESC";
  166. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  167. var database = new Database(databaseType, _connectionString);
  168. var connection = database.GetConnection();
  169. return await connection.ExecuteScalarAsync<int>(sql);
  170. }
  171. /// <summary>
  172. /// 新增 未读数量
  173. /// </summary>
  174. /// <param name="userId"></param>
  175. /// <param name="checking"></param>
  176. /// <param name="request"></param>
  177. /// <returns></returns>
  178. public async Task<int> GetUReadCountByUserIdAsync(int userId, bool checking, FlowListRequest request)
  179. {
  180. var query = Q
  181. .Where(nameof(Flow.UserId), userId)
  182. .WhereNot(nameof(Flow.ProcessId), 0);
  183. if (checking)
  184. {
  185. query.WhereNotIn(nameof(Flow.State), new List<string>
  186. {
  187. //FlowState.Meeting.GetValue(),
  188. FlowState.Checked.GetValue(),
  189. FlowState.Denied.GetValue()
  190. });
  191. }
  192. else
  193. {
  194. query.WhereIn(nameof(Flow.State), new List<string>
  195. {
  196. //FlowState.Meeting.GetValue(),
  197. FlowState.Checked.GetValue(),
  198. FlowState.Denied.GetValue()
  199. });
  200. }
  201. if (request.ProcessIds != null && request.ProcessIds.Any())
  202. {
  203. query.WhereIn(nameof(Flow.ProcessId), request.ProcessIds);
  204. }
  205. // if (!string.IsNullOrEmpty(request.Keyword))
  206. // {
  207. // var like = $"%{request.Keyword}%";
  208. // query.WhereLike(nameof(Flow.ProcessId), like);
  209. // }
  210. #region 新增
  211. if (!string.IsNullOrEmpty(request.Keyword))
  212. {
  213. if (int.TryParse(request.Keyword, out int dd))
  214. {
  215. var like = $"%{request.Keyword}%";
  216. query.WhereLike(nameof(Flow.ProcessId), like);
  217. }
  218. }
  219. if (!string.IsNullOrWhiteSpace(request.Keyword))
  220. {
  221. //获取用户
  222. var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  223. query.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id));
  224. }
  225. #endregion
  226. return await _repository.CountAsync(query);
  227. }
  228. public async Task<IEnumerable<Flow>> GetListByUserIdAsync(int userId, bool checking, FlowListRequest request)
  229. {
  230. string sql = $@"SELECT
  231. a.*
  232. FROM
  233. ccpph.tede_flow a
  234. WHERE 1=1 ";
  235. if (!checking)
  236. sql += $@" and a.State IN ('Denied','Checked')";
  237. else
  238. sql += $@" and a.State Not IN ('Denied','Checked')";
  239. sql += $@" AND a.UserId = {userId}
  240. AND a.ProcessId IN (SELECT
  241. id
  242. FROM
  243. ccpph.tede_process)";
  244. if (!string.IsNullOrWhiteSpace(request.Keyword))
  245. {
  246. sql += $@" AND (a.ProcessId IN (SELECT
  247. Id
  248. FROM
  249. tede_process
  250. WHERE
  251. name LIKE '%{request.Keyword}%')
  252. OR a.Id IN (SELECT
  253. FlowId
  254. FROM
  255. tede_flow_field_value
  256. WHERE
  257. StringValues LIKE '%{request.Keyword}%')
  258. OR a.UserId IN (SELECT
  259. Id
  260. FROM
  261. ccpph.tede_user
  262. WHERE
  263. Name LIKE '%{request.Keyword}%') or a.Id In(SELECT
  264. FlowId
  265. FROM
  266. tede_flow_todo
  267. WHERE
  268. UserId IN (SELECT
  269. Id
  270. FROM
  271. tede_user
  272. WHERE
  273. Name LIKE '%{request.Keyword}%'))
  274. or a.Id In(SELECT
  275. FlowId
  276. FROM
  277. tede_flow_message
  278. WHERE
  279. Message LIKE '%{request.Keyword}%'))";
  280. }
  281. if (request.ProcessIds.Count > 0)
  282. {
  283. var processIdValue = "";
  284. foreach (var item in request.ProcessIds)
  285. {
  286. if (item == 0)
  287. continue;
  288. processIdValue += $"{item},";
  289. }
  290. if (!string.IsNullOrWhiteSpace(processIdValue))
  291. {
  292. processIdValue = processIdValue.Remove(processIdValue.Length - 1, 1);
  293. sql += $" and a.ProcessId in({processIdValue})";
  294. }
  295. }
  296. sql += $"ORDER BY a.CreatedDate DESC limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  297. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  298. var database = new Database(databaseType, _connectionString);
  299. var connection = database.GetConnection();
  300. var items = await connection.QueryAsync<Flow>(sql);
  301. //result.Total = await connection.ExecuteScalarAsync<int>(AssembleSqlCount(request));
  302. return items;
  303. }
  304. public async Task<IEnumerable<Flow>> GetListByIdListAsync(IEnumerable<int> flowIdList, int page, int perPage)
  305. {
  306. return await _repository.GetAllAsync(Q
  307. .WhereIn(nameof(Flow.Id), flowIdList)
  308. .WhereNot(nameof(Flow.ProcessId), 0)
  309. .ForPage(page, perPage)
  310. .OrderByDesc(nameof(Flow.Id))
  311. );
  312. }
  313. public async Task<int> GetCountAsync(int processId, string state, string startDate, string endDate)
  314. {
  315. var query = Q
  316. .WhereNot(nameof(Flow.ProcessId), 0);
  317. if (processId > 0)
  318. {
  319. query.Where(nameof(Flow.ProcessId), processId);
  320. }
  321. if (!string.IsNullOrEmpty(state) && !StringUtils.EqualsIgnoreCase(state, "all"))
  322. {
  323. query.Where(nameof(Flow.State), state);
  324. }
  325. if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
  326. {
  327. query.WhereBetween(nameof(Flow.CreatedDate), StringUtils.ToDateTime(startDate), StringUtils.ToDateTime(endDate));
  328. }
  329. return await _repository.CountAsync(query);
  330. }
  331. public async Task<IEnumerable<Flow>> GetListAllAsync(int page, int perPage, int processId, string state, string startDate, string endDate)
  332. {
  333. var query = Q
  334. .WhereNot(nameof(Flow.ProcessId), 0)
  335. .ForPage(page, perPage)
  336. .OrderByDesc(nameof(Flow.Id));
  337. if (processId > 0)
  338. {
  339. query.Where(nameof(Flow.ProcessId), processId);
  340. }
  341. if (!string.IsNullOrEmpty(state) && !StringUtils.EqualsIgnoreCase(state, "all"))
  342. {
  343. query.Where(nameof(Flow.State), state);
  344. }
  345. if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
  346. {
  347. query.WhereBetween(nameof(Flow.CreatedDate), StringUtils.ToDateTime(startDate), StringUtils.ToDateTime(endDate));
  348. }
  349. return await _repository.GetAllAsync(query);
  350. }
  351. public async Task<PagedList<Flow>> GetFlowListAllAsync(int page, int perPage, int processId, string state, string startDate, string endDate)
  352. {
  353. var result = new PagedList<Flow>();
  354. var query = new SqlKata.Query("tede_flow");
  355. query.WhereNot(nameof(Flow.ProcessId), 0);
  356. if (processId > 0)
  357. query.Where(nameof(Flow.ProcessId), processId);
  358. if (!string.IsNullOrEmpty(state) && !StringUtils.EqualsIgnoreCase(state, "all"))
  359. {
  360. if (state.Equals("Meeting"))
  361. {
  362. var queryParameter = new SqlKata.Query("tede_flow_todo");
  363. queryParameter.WhereColumns("tede_flow_todo.FlowId", "=", "tede_flow.Id");
  364. queryParameter.Where("tede_flow_todo.DoneType", 1);
  365. queryParameter.Limit(1);
  366. queryParameter.Select("tede_flow_todo.FlowId");
  367. query.Where(nameof(Entity.WorkFlow.Flow.Id), "=", queryParameter);
  368. }
  369. else if (state.Equals("Checking"))
  370. query.Where(n => n.Where(nameof(Flow.State), 4).OrWhere(nameof(Entity.WorkFlow.Flow.State), state));
  371. else
  372. query.Where(nameof(Flow.State), state);
  373. }
  374. if (!string.IsNullOrEmpty(startDate) && !string.IsNullOrEmpty(endDate))
  375. query.WhereBetween(nameof(Flow.CreatedDate), StringUtils.ToDateTime(startDate), StringUtils.ToDateTime(endDate));
  376. result.Total = await _repository.CountAsync(query);
  377. query.ForPage(page, perPage).OrderByDesc(nameof(Flow.Id));
  378. result.Items = await _repository.GetAllAsync(query);
  379. return result;
  380. }
  381. public async Task<bool> DeleteAsync(int flowId)
  382. {
  383. return await _repository.DeleteAsync(flowId);
  384. }
  385. public async Task UpdateStateAsync(int flowId, string state)
  386. {
  387. await _repository.UpdateAsync(Q
  388. .Set(nameof(Flow.State), state)
  389. .Where(nameof(Flow.Id), flowId)
  390. );
  391. }
  392. public async Task UpdateProcessNodeIdAsync(int flowId, int processNodeId)
  393. {
  394. await _repository.UpdateAsync(Q
  395. .Set(nameof(Flow.ProcessNodeId), processNodeId)
  396. .Where(nameof(Flow.Id), flowId)
  397. );
  398. }
  399. public async Task<int> CountAsync(SqlKata.Query query)
  400. {
  401. return await _repository.CountAsync(query);
  402. }
  403. }
  404. }