FlowTodoRepository.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using GxPress.Common.AppOptions;
  5. using GxPress.Common.Tools;
  6. using GxPress.Entity.WorkFlow;
  7. using GxPress.EnumConst;
  8. using GxPress.Repository.Interface.WorkFlow;
  9. using GxPress.Request.App.Flow;
  10. using Microsoft.Extensions.Options;
  11. using Datory;
  12. using SqlKata;
  13. using Dapper;
  14. namespace GxPress.Repository.Implement.WorkFlow
  15. {
  16. public class FlowTodoRepository : IFlowTodoRepository
  17. {
  18. private readonly Repository<FlowTodo> _repository;
  19. private readonly Repository<Entity.User> _userRepository;
  20. private readonly IFlowFieldValueRepository _flowFieldValueRepository;
  21. private readonly Repository<Flow> _flowRepository;
  22. private readonly Repository<Entity.WorkProcess.Process> _processRepository;
  23. private readonly string _connectionString;
  24. private readonly string _databaseTypeStr;
  25. public FlowTodoRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IFlowFieldValueRepository flowFieldValueRepository)
  26. {
  27. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  28. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  29. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  31. _repository = new Repository<FlowTodo>(database);
  32. _flowFieldValueRepository = flowFieldValueRepository;
  33. _userRepository = new Repository<Entity.User>(database);
  34. _flowRepository = new Repository<Flow>(database);
  35. _processRepository = new Repository<Entity.WorkProcess.Process>(database);
  36. }
  37. public IDatabase Database => _repository.Database;
  38. public string TableName => _repository.TableName;
  39. public List<TableColumn> TableColumns => _repository.TableColumns;
  40. public async Task<int> InsertAsync(FlowTodo item)
  41. {
  42. var id = await _repository.InsertAsync(item);
  43. item = await _repository.GetAsync(id);
  44. if (item.Sort == 0)
  45. {
  46. item.Sort = item.Id;
  47. await _repository.UpdateAsync(item);
  48. }
  49. return id;
  50. }
  51. public async Task<int> GetCountByUserIdAsync(int userId)
  52. {
  53. return await _repository.CountAsync(Q
  54. .Where(nameof(FlowTodo.UserId), userId)
  55. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  56. .WhereNot(nameof(FlowTodo.FlowId), 0).Where(nameof(FlowTodo.IsDone), false).Where(nameof(FlowTodo.Type), nameof(EnumConst.TodoTypeConst.ApproverCheck))
  57. );
  58. }
  59. public async Task<IEnumerable<FlowTodo>> GetListByUserIdAsync(int userId, int processId, int page, int perPage)
  60. {
  61. var query = Q
  62. .Where(nameof(FlowTodo.UserId), userId)
  63. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  64. .WhereNot(nameof(FlowTodo.FlowId), 0).Where(nameof(FlowTodo.IsDone), false).Where(nameof(FlowTodo.IsOperate), true)
  65. .ForPage(page, perPage)
  66. .OrderByDesc(nameof(FlowTodo.Id));
  67. if (processId > 0)
  68. {
  69. query.Where(nameof(FlowTodo.ProcessId), processId);
  70. }
  71. return await _repository.GetAllAsync(query);
  72. }
  73. public async Task<IEnumerable<FlowTodo>> GetListByFlowIdAsync(int flowId)
  74. {
  75. var orderByKey = new string[] { nameof(FlowTodo.Sort) };
  76. return await _repository.GetAllAsync(Q
  77. .Where(nameof(FlowTodo.FlowId), flowId)
  78. .OrderBy(orderByKey)
  79. );
  80. }
  81. public async Task<bool> DeleteAsync(int todoId)
  82. {
  83. return await _repository.DeleteAsync(todoId);
  84. }
  85. public async Task<bool> DeleteAllAsync(int flowId)
  86. {
  87. return await _repository.DeleteAsync(Q
  88. .Where(nameof(FlowTodo.FlowId), flowId)
  89. ) > 0;
  90. }
  91. public async Task<IEnumerable<(int TodoId, int FlowId)>> GetTodoIdFlowIdListAsync(int userId, string type, bool isDone, FlowListRequest request)
  92. {
  93. string sql = $@"SELECT
  94. a.Id,a.FlowId
  95. FROM
  96. tede_flow_todo a
  97. INNER JOIN
  98. tede_process b ON a.ProcessId = b.id
  99. INNER JOIN
  100. tede_flow c ON c.Id = a.FlowId
  101. INNER JOIN
  102. tede_user d on d.UserId=a.UserId
  103. WHERE
  104. a.UserId = {userId}
  105. AND a.Type = '{type}'";
  106. if (isDone)
  107. sql += $@"AND a.IsDone = 1";
  108. else sql += $@"AND a.IsDone = 0 AND a.IsOperate = 1";
  109. if (!string.IsNullOrWhiteSpace(request.Keyword))
  110. {
  111. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  112. OR a.FlowId IN (SELECT
  113. FlowId
  114. FROM
  115. tede_flow_field_value
  116. WHERE
  117. StringValues LIKE '%{request.Keyword}%')
  118. OR c.UserId IN (SELECT
  119. Id
  120. FROM
  121. tede_user
  122. WHERE
  123. Name LIKE '%{request.Keyword}%')
  124. or a.FlowId In(SELECT
  125. FlowId
  126. FROM
  127. tede_flow_todo
  128. WHERE
  129. UserId IN (SELECT
  130. Id
  131. FROM
  132. tede_user
  133. WHERE
  134. Name LIKE '%{request.Keyword}%'))
  135. or a.FlowId In(SELECT
  136. FlowId
  137. FROM
  138. tede_flow_message
  139. WHERE
  140. Message LIKE '%{request.Keyword}%'))";
  141. }
  142. if (request.ProcessIds.Count > 0)
  143. {
  144. var processId = "";
  145. foreach (var item in request.ProcessIds)
  146. {
  147. if (item == 0)
  148. continue;
  149. processId += $"{item},";
  150. }
  151. if (!string.IsNullOrWhiteSpace(processId))
  152. {
  153. processId = processId.Remove(processId.Length - 1, 1);
  154. sql += $" AND b.Id IN ({processId})";
  155. }
  156. }
  157. sql += $" ORDER BY a.CreatedDate DESC limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  158. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  159. var database = new Database(databaseType, _connectionString);
  160. var connection = database.GetConnection();
  161. var result =
  162. await connection
  163. .QueryAsync<(int TodoId, int FlowId)>(sql);
  164. return result;
  165. // var query = Q
  166. // .Select(nameof(FlowTodo.Id), nameof(FlowTodo.FlowId))
  167. // .WhereNot(nameof(FlowTodo.ProcessId), 0)
  168. // .Where(nameof(FlowTodo.UserId), userId)
  169. // .Where(nameof(FlowTodo.Type), type)
  170. // .Where(nameof(FlowTodo.IsDone), isDone)
  171. // .ForPage(request.Page, request.PerPage)
  172. // .OrderByDesc(nameof(FlowTodo.Id));
  173. // if (type == nameof(TodoTypeConst.ApproverCheck) && !isDone)
  174. // {
  175. // query.Where(nameof(FlowTodo.IsOperate), true);
  176. // }
  177. // if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  178. // {
  179. // query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  180. // }
  181. // #region 新增
  182. // if (!string.IsNullOrWhiteSpace(request.Keyword))
  183. // {
  184. // //获取用户
  185. // var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  186. // if (users.Count > 0)
  187. // {
  188. // //获取
  189. // var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  190. // var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  191. // var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  192. // var flowIds = new List<int>();
  193. // if (flow.Count > 0)
  194. // flowIds.AddRange(flow.Select(n => n.Id));
  195. // //审批人
  196. // if (flowTodosApproverCheck.Count > 0)
  197. // flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  198. // //抄送人
  199. // if (flowTodosCarbonCopy.Count > 0)
  200. // flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  201. // query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  202. // }
  203. // else
  204. // {
  205. // var flowFieldValues = await _flowFieldValueRepository.GetAllAsync(Q.WhereLike(nameof(FlowFieldValue.StringValue), $"%{request.Keyword}%"));
  206. // query.WhereIn(nameof(Flow.Id), flowFieldValues.Select(n => n.FlowId));
  207. // if (flowFieldValues.Count() == 0)
  208. // {
  209. // var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  210. // query.WhereIn(nameof(Flow.ProcessId), processList.Select(n => n.Id));
  211. // }
  212. // else
  213. // query.WhereIn(nameof(Flow.Id), flowFieldValues.Select(n => n.FlowId));
  214. // }
  215. // }
  216. // #endregion
  217. // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query);
  218. }
  219. public async Task<int> GetReadCountAsync(int userId, string type, FlowListRequest request, bool isRead)
  220. {
  221. string sql = $@"SELECT
  222. count(1)
  223. FROM
  224. tede_flow_todo a
  225. INNER JOIN
  226. tede_process b ON a.ProcessId = b.id
  227. INNER JOIN
  228. tede_flow c ON c.Id = a.FlowId
  229. WHERE
  230. a.UserId = {userId}
  231. AND a.Type = '{type}'";
  232. if (isRead)
  233. sql += $@"AND a.IsRead = 1";
  234. else sql += $@"AND a.IsRead = 0";
  235. if (!string.IsNullOrWhiteSpace(request.Keyword))
  236. {
  237. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  238. OR a.FlowId IN (SELECT
  239. FlowId
  240. FROM
  241. tede_flow_field_value
  242. WHERE
  243. StringValues LIKE '%{request.Keyword}%')
  244. OR c.UserId IN (SELECT
  245. Id
  246. FROM
  247. tede_user
  248. WHERE
  249. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  250. FlowId
  251. FROM
  252. tede_flow_todo
  253. WHERE
  254. UserId IN (SELECT
  255. Id
  256. FROM
  257. tede_user
  258. WHERE
  259. Name LIKE '%{request.Keyword}%')) or a.FlowId In(SELECT
  260. FlowId
  261. FROM
  262. tede_flow_message
  263. WHERE
  264. Message LIKE '%{request.Keyword}%'))";
  265. }
  266. if (request.ProcessIds.Count > 0)
  267. {
  268. var processId = "";
  269. foreach (var item in request.ProcessIds)
  270. {
  271. if (item == 0)
  272. continue;
  273. processId += $"{item},";
  274. }
  275. if (!string.IsNullOrWhiteSpace(processId))
  276. {
  277. processId = processId.Remove(processId.Length - 1, 1);
  278. sql += $" AND b.Id IN ({processId})";
  279. }
  280. }
  281. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  282. var database = new Database(databaseType, _connectionString);
  283. var connection = database.GetConnection();
  284. var result =
  285. await connection
  286. .ExecuteScalarAsync<int>(sql);
  287. return result;
  288. }
  289. public async Task<int> GetCountAsync(int userId, string type, bool isDone, FlowListRequest request)
  290. {
  291. string sql = $@"SELECT
  292. count(1)
  293. FROM
  294. tede_flow_todo a
  295. INNER JOIN
  296. tede_process b ON a.ProcessId = b.id
  297. INNER JOIN
  298. tede_flow c ON c.Id = a.FlowId
  299. WHERE
  300. a.UserId = {userId}
  301. AND a.Type = '{type}'";
  302. if (isDone)
  303. sql += $@" AND a.IsDone = 1";
  304. else sql += $@" AND a.IsDone = 0 AND a.IsOperate = 1";
  305. if (!string.IsNullOrWhiteSpace(request.Keyword))
  306. {
  307. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  308. OR a.FlowId IN (SELECT
  309. FlowId
  310. FROM
  311. tede_flow_field_value
  312. WHERE
  313. StringValues LIKE '%{request.Keyword}%')
  314. OR c.UserId IN (SELECT
  315. Id
  316. FROM
  317. tede_user
  318. WHERE
  319. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  320. FlowId
  321. FROM
  322. tede_flow_todo
  323. WHERE
  324. UserId IN (SELECT
  325. Id
  326. FROM
  327. tede_user
  328. WHERE
  329. Name LIKE '%{request.Keyword}%'))
  330. or a.FlowId In(SELECT
  331. FlowId
  332. FROM
  333. tede_flow_message
  334. WHERE
  335. Message LIKE '%{request.Keyword}%'))";
  336. }
  337. if (request.ProcessIds.Count > 0)
  338. {
  339. var processId = "";
  340. foreach (var item in request.ProcessIds)
  341. {
  342. if (item == 0)
  343. continue;
  344. processId += $"{item},";
  345. }
  346. if (!string.IsNullOrWhiteSpace(processId))
  347. {
  348. processId = processId.Remove(processId.Length - 1, 1);
  349. sql += $" AND b.Id IN ({processId})";
  350. }
  351. }
  352. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  353. var database = new Database(databaseType, _connectionString);
  354. var connection = database.GetConnection();
  355. var result =
  356. await connection
  357. .ExecuteScalarAsync<int>(sql);
  358. return result;
  359. // var query = Q
  360. // .Select(nameof(FlowTodo.FlowId))
  361. // .WhereNot(nameof(FlowTodo.ProcessId), 0)
  362. // .Where(nameof(FlowTodo.UserId), userId)
  363. // .Where(nameof(FlowTodo.Type), type)
  364. // .Where(nameof(FlowTodo.IsDone), isDone)
  365. // .OrderByDesc(nameof(FlowTodo.Id));
  366. // if (type == nameof(TodoTypeConst.ApproverCheck) && !isDone)
  367. // {
  368. // query.Where(nameof(FlowTodo.IsOperate), true);
  369. // }
  370. // if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  371. // {
  372. // query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  373. // }
  374. // #region 新增
  375. // if (!string.IsNullOrWhiteSpace(request.Keyword))
  376. // {
  377. // //获取用户
  378. // var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  379. // if (users.Count > 0)
  380. // {
  381. // //获取
  382. // var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  383. // var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  384. // var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  385. // var flowIds = new List<int>();
  386. // if (flow.Count > 0)
  387. // flowIds.AddRange(flow.Select(n => n.Id));
  388. // //审批人
  389. // if (flowTodosApproverCheck.Count > 0)
  390. // flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  391. // //抄送人
  392. // if (flowTodosCarbonCopy.Count > 0)
  393. // flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  394. // query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  395. // }
  396. // else
  397. // {
  398. // var flowFieldValues = await _flowFieldValueRepository.GetAllAsync(Q.WhereLike(nameof(FlowFieldValue.StringValue), $"%{request.Keyword}%"));
  399. // query.WhereIn(nameof(Flow.Id), flowFieldValues.Select(n => n.FlowId));
  400. // if (flowFieldValues.Count() == 0)
  401. // {
  402. // var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  403. // query.WhereIn(nameof(Flow.ProcessId), processList.Select(n => n.Id));
  404. // }
  405. // else
  406. // query.WhereIn(nameof(Flow.Id), flowFieldValues.Select(n => n.FlowId));
  407. // }
  408. // }
  409. // #endregion
  410. // return await _repository.CountAsync(query);
  411. }
  412. /// <summary>
  413. /// 未读数量
  414. /// </summary>
  415. /// <param name="userId"></param>
  416. /// <param name="type"></param>
  417. /// <param name="isDone"></param>
  418. /// <param name="request"></param>
  419. /// <returns></returns>
  420. #region
  421. public async Task<int> GetUReadCountAsync(int userId, string type, bool isDone, FlowListRequest request)
  422. {
  423. var query = Q
  424. .Select(nameof(FlowTodo.FlowId))
  425. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  426. .Where(nameof(FlowTodo.UserId), userId)
  427. .Where(nameof(FlowTodo.Type), type)
  428. .Where(nameof(FlowTodo.IsDone), isDone)
  429. .OrderByDesc(nameof(FlowTodo.Id));
  430. if (!isDone && type == nameof(TodoTypeConst.ApproverCheck))
  431. {
  432. query.Where(nameof(FlowTodo.IsOperate), true);
  433. }
  434. if (request.ProcessIds != null && request.ProcessIds.Any())
  435. {
  436. query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  437. }
  438. #region 新增
  439. if (!string.IsNullOrWhiteSpace(request.Keyword))
  440. {
  441. //获取用户
  442. var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  443. //获取
  444. var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  445. query.WhereIn(nameof(FlowTodo.FlowId), flow.Select(n => n.Id));
  446. }
  447. #endregion
  448. return await _repository.CountAsync(query);
  449. }
  450. #endregion
  451. public async Task<IEnumerable<(int TodoId, int FlowId)>> GetTodoIdFlowIdListAsync(int userId, string type, FlowListRequest request, bool isRead)
  452. {
  453. string sql = $@"SELECT
  454. a.Id,a.FlowId
  455. FROM
  456. tede_flow_todo a
  457. INNER JOIN
  458. tede_process b ON a.ProcessId = b.id
  459. INNER JOIN
  460. tede_flow c ON c.Id = a.FlowId
  461. WHERE
  462. a.UserId = {userId}
  463. AND a.Type = '{type}'";
  464. if (isRead)
  465. sql += $@"AND a.IsRead = 1";
  466. else sql += $@"AND a.IsRead = 0";
  467. if (!string.IsNullOrWhiteSpace(request.Keyword))
  468. {
  469. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  470. OR a.FlowId IN (SELECT
  471. FlowId
  472. FROM
  473. tede_flow_field_value
  474. WHERE
  475. StringValues LIKE '%{request.Keyword}%')
  476. OR c.UserId IN (SELECT
  477. Id
  478. FROM
  479. tede_user
  480. WHERE
  481. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  482. FlowId
  483. FROM
  484. tede_flow_todo
  485. WHERE
  486. UserId IN (SELECT
  487. Id
  488. FROM
  489. tede_user
  490. WHERE
  491. Name LIKE '%{request.Keyword}%'))
  492. or a.FlowId In(SELECT
  493. FlowId
  494. FROM
  495. tede_flow_message
  496. WHERE
  497. Message LIKE '%{request.Keyword}%'))";
  498. }
  499. if (request.ProcessIds.Count > 0)
  500. {
  501. var processId = "";
  502. foreach (var item in request.ProcessIds)
  503. {
  504. if (item == 0)
  505. continue;
  506. processId += $"{item},";
  507. }
  508. if (!string.IsNullOrWhiteSpace(processId))
  509. {
  510. processId = processId.Remove(processId.Length - 1, 1);
  511. sql += $" AND b.Id IN ({processId})";
  512. }
  513. }
  514. sql += $" order by a.CreatedDate desc limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  515. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  516. var database = new Database(databaseType, _connectionString);
  517. var connection = database.GetConnection();
  518. var result =
  519. await connection
  520. .QueryAsync<(int TodoId, int FlowId)>(sql);
  521. return result;
  522. // var query = Q
  523. // .WhereNot(nameof(FlowTodo.ProcessId), 0)
  524. // .Where(nameof(FlowTodo.UserId), userId)
  525. // .Where(nameof(FlowTodo.Type), type)
  526. // .ForPage(request.Page, request.PerPage)
  527. // .OrderByDesc(nameof(FlowTodo.Id));
  528. // if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  529. // {
  530. // query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  531. // }
  532. // #region 新增
  533. // if (!string.IsNullOrWhiteSpace(request.Keyword))
  534. // {
  535. // //获取用户
  536. // var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  537. // if (users.Count > 0)
  538. // {
  539. // //获取
  540. // var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  541. // var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  542. // var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  543. // var flowIds = new List<int>();
  544. // if (flow.Count > 0)
  545. // flowIds.AddRange(flow.Select(n => n.Id));
  546. // //审批人
  547. // if (flowTodosApproverCheck.Count > 0)
  548. // flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  549. // //抄送人
  550. // if (flowTodosCarbonCopy.Count > 0)
  551. // flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  552. // query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  553. // }
  554. // else
  555. // {
  556. // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  557. // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  558. // // if (flowIdList == null || !flowIdList.Any())
  559. // // {
  560. // // return new List<(int TodoId, int FlowId)>();
  561. // // }
  562. // if (flowIdList.Count() == 0)
  563. // {
  564. // var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  565. // query.WhereIn(nameof(FlowTodo.ProcessId), processList.Select(n => n.Id));
  566. // var result = await _repository.GetAllAsync(query);
  567. // return result.Select(n => (n.Id, n.FlowId));
  568. // }
  569. // else
  570. // {
  571. // var result = await _repository.GetAllAsync(query.WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  572. // return result.Select(n => (n.Id, n.FlowId));
  573. // }
  574. // //return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id),nameof(FlowTodo.FlowId)).WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  575. // }
  576. // }
  577. // #endregion
  578. // // if (!string.IsNullOrEmpty(request.Keyword))
  579. // // {
  580. // // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  581. // // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  582. // // if (flowIdList == null || !flowIdList.Any())
  583. // // {
  584. // // return new List<(int TodoId, int FlowId)>();
  585. // // }
  586. // // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id)).WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  587. // // }
  588. // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id), nameof(FlowTodo.FlowId)));
  589. }
  590. public async Task<int> GetCountAsync(int userId, string type, FlowListRequest request)
  591. {
  592. var query = Q
  593. .Select(nameof(FlowTodo.FlowId))
  594. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  595. .Where(nameof(FlowTodo.UserId), userId)
  596. .Where(nameof(FlowTodo.Type), type)
  597. .OrderByDesc(nameof(FlowTodo.Id));
  598. if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  599. {
  600. query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  601. }
  602. #region 新增
  603. if (!string.IsNullOrWhiteSpace(request.Keyword))
  604. {
  605. //获取用户
  606. var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  607. if (users.Count > 0)
  608. {
  609. //获取
  610. var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  611. var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  612. var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  613. var flowIds = new List<int>();
  614. if (flow.Count > 0)
  615. flowIds.AddRange(flow.Select(n => n.Id));
  616. //审批人
  617. if (flowTodosApproverCheck.Count > 0)
  618. flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  619. //抄送人
  620. if (flowTodosCarbonCopy.Count > 0)
  621. flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  622. query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  623. }
  624. else
  625. {
  626. var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  627. var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  628. // if (flowIdList == null || !flowIdList.Any())
  629. // {
  630. // return 0;
  631. // }
  632. if (flowIdList.Count() == 0)
  633. {
  634. var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  635. query.WhereIn(nameof(FlowTodo.ProcessId), processList.Select(n => n.Id));
  636. }
  637. else
  638. {
  639. return 0;
  640. }
  641. }
  642. }
  643. #endregion
  644. // if (!string.IsNullOrEmpty(request.Keyword))
  645. // {
  646. // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  647. // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  648. // if (flowIdList == null || !flowIdList.Any())
  649. // {
  650. // return 0;
  651. // }
  652. // return await _repository.CountAsync(query.WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  653. // }
  654. return await _repository.CountAsync(query);
  655. }
  656. public async Task<FlowTodo> GetAsync(int todoId)
  657. {
  658. return await _repository.GetAsync(todoId);
  659. }
  660. public async Task<FlowTodo> GetAsync(SqlKata.Query query)
  661. {
  662. return await _repository.GetAsync(query);
  663. }
  664. public async Task<bool> UpdateAsync(FlowTodo todo)
  665. {
  666. return await _repository.UpdateAsync(todo);
  667. }
  668. public async Task<bool> UpdateSortAsync(FlowTodo todo)
  669. {
  670. string sql = $"UPDATE `ccpph`.`tede_flow_todo` SET `Sort` = {todo.Sort} WHERE `Id` = {todo.Id}";
  671. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  672. var database = new Database(databaseType, _connectionString);
  673. var connection = database.GetConnection();
  674. var result = await connection.ExecuteAsync(sql);
  675. return result > 0;
  676. }
  677. public async Task<bool> UpdateAsync(SqlKata.Query query)
  678. {
  679. return await _repository.UpdateAsync(query) > 0;
  680. }
  681. public async Task<bool> UpdateReadAsync(int userId, int flowId)
  682. {
  683. string sql = $"UPDATE `ccpph`.`tede_flow_todo` SET `IsRead` = 1 WHERE `FlowId` = {flowId} and `UserId`={userId} ";
  684. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  685. var database = new Database(databaseType, _connectionString);
  686. var connection = database.GetConnection();
  687. var result = await connection.ExecuteAsync(sql);
  688. return result > 0;
  689. }
  690. public async Task<bool> UpdateFlowReadAsync(int userId, int flowId)
  691. {
  692. string sql = $"UPDATE `ccpph`.`tede_flow` SET `IsRead` = 1 WHERE `Id` = {flowId} and `UserId`={userId}";
  693. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  694. var database = new Database(databaseType, _connectionString);
  695. var connection = database.GetConnection();
  696. var result = await connection.ExecuteAsync(sql);
  697. return result > 0;
  698. }
  699. public async Task UpdateCCIsDoneAsync(int userId, int todoId)
  700. {
  701. var flowId = 0;
  702. var query = Q.NewQuery();
  703. query.Where(nameof(Entity.WorkFlow.FlowTodo.Id), todoId);
  704. query.Where(nameof(Entity.WorkFlow.FlowTodo.UserId), userId);
  705. var flowTodo = await _repository.GetAsync(query);
  706. if (flowTodo == null)
  707. flowId = todoId;
  708. else
  709. flowId = flowTodo.FlowId;
  710. await UpdateReadAsync(userId, flowId);
  711. await UpdateFlowReadAsync(userId, flowId);
  712. }
  713. public async Task DeleteCheckingByFlowIdAsync(int flowId)
  714. {
  715. await _repository.DeleteAsync(Q
  716. .Where(nameof(FlowTodo.FlowId), flowId)
  717. .Where(nameof(FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck))
  718. .Where(nameof(FlowTodo.IsDone), false)
  719. );
  720. }
  721. public async Task<IEnumerable<int>> GetAllCheckedUserIdListAsync(int flowId)
  722. {
  723. return await _repository.GetAllAsync<int>(Q
  724. .Select(nameof(FlowTodo.UserId))
  725. .Where(nameof(FlowTodo.FlowId), flowId)
  726. .Where(nameof(FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck))
  727. .Where(nameof(FlowTodo.IsChecked), true)
  728. .Where(nameof(FlowTodo.IsDone), true)
  729. );
  730. }
  731. public async Task<int> CountAsync(SqlKata.Query query)
  732. {
  733. return await _repository.CountAsync(query);
  734. }
  735. /// <summary>
  736. /// 抄送未读数量
  737. /// </summary>
  738. /// <param name="userId"></param>
  739. /// <returns></returns>
  740. public async Task<int> FlowCcUReadCountAsync(int userId)
  741. {
  742. string sql = $"SELECT count(1) FROM tede_flow_todo a where a.UserId={userId} and a.Type='CarbonCopy' and a.IsRead=0 and a.IsDone=0 and a.ProcessId=(select Id from tede_process where Id=a.ProcessId)";
  743. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  744. var database = new Database(databaseType, _connectionString);
  745. var connection = database.GetConnection();
  746. var result = await connection.ExecuteScalarAsync<int>(sql);
  747. return result;
  748. }
  749. public async Task<IEnumerable<FlowTodo>> GetAllAsync(Query query)
  750. {
  751. return await _repository.GetAllAsync(query);
  752. }
  753. }
  754. }