FlowTodoRepository.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. string sql = $@"
  76. SELECT
  77. a.*
  78. FROM
  79. tede_flow_todo a
  80. INNER JOIN
  81. tede_user b ON a.UserId = b.Id
  82. WHERE
  83. a.FlowId = {flowId} order by a.Sort
  84. ";
  85. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  86. var database = new Database(databaseType, _connectionString);
  87. var connection = database.GetConnection();
  88. var result =
  89. await connection
  90. .QueryAsync<FlowTodo>(sql);
  91. return result;
  92. }
  93. public async Task<bool> DeleteAsync(int todoId)
  94. {
  95. return await _repository.DeleteAsync(todoId);
  96. }
  97. public async Task<bool> DeleteAllAsync(int flowId)
  98. {
  99. return await _repository.DeleteAsync(Q
  100. .Where(nameof(FlowTodo.FlowId), flowId)
  101. ) > 0;
  102. }
  103. /// <summary>
  104. /// 已审批
  105. /// </summary>
  106. /// <param name="userId"></param>
  107. /// <param name="type"></param>
  108. /// <param name="isDone"></param>
  109. /// <param name="request"></param>
  110. /// <returns></returns>
  111. public async Task<IEnumerable<(int FlowId, int TodoId)>> GetMyCheckedTodoIdFlowIdListAsync(int userId, string type, bool isDone, FlowListRequest request)
  112. {
  113. string sql = $@"
  114. SELECT
  115. a.FlowId,a.Id
  116. FROM
  117. tede_flow_todo a
  118. INNER JOIN
  119. tede_process b ON a.ProcessId = b.id
  120. INNER JOIN
  121. tede_flow c ON c.Id = a.FlowId
  122. INNER JOIN
  123. tede_user d on d.Id=a.UserId
  124. INNER JOIN
  125. tede_user e on e.Id=c.UserId
  126. WHERE
  127. a.UserId = {userId}
  128. AND a.Type = '{type}'";
  129. if (isDone)
  130. sql += $@"AND a.IsDone = 1";
  131. else sql += $@"AND a.IsDone = 0 AND a.IsOperate = 1";
  132. if (!string.IsNullOrWhiteSpace(request.Keyword))
  133. {
  134. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  135. OR a.FlowId IN (SELECT
  136. FlowId
  137. FROM
  138. tede_flow_field_value
  139. WHERE
  140. StringValues LIKE '%{request.Keyword}%')
  141. OR c.UserId IN (SELECT
  142. Id
  143. FROM
  144. tede_user
  145. WHERE
  146. Name LIKE '%{request.Keyword}%')
  147. or a.FlowId In(SELECT
  148. FlowId
  149. FROM
  150. tede_flow_todo
  151. WHERE
  152. UserId IN (SELECT
  153. Id
  154. FROM
  155. tede_user
  156. WHERE
  157. Name LIKE '%{request.Keyword}%'))
  158. or a.FlowId In(SELECT
  159. FlowId
  160. FROM
  161. tede_flow_message
  162. WHERE
  163. Message LIKE '%{request.Keyword}%'))";
  164. }
  165. if (request.ProcessIds.Count > 0)
  166. {
  167. var processId = "";
  168. foreach (var item in request.ProcessIds)
  169. {
  170. if (item == 0)
  171. continue;
  172. processId += $"{item},";
  173. }
  174. if (!string.IsNullOrWhiteSpace(processId))
  175. {
  176. processId = processId.Remove(processId.Length - 1, 1);
  177. sql += $" AND b.Id IN ({processId})";
  178. }
  179. }
  180. sql += $" ORDER BY a.CreatedDate DESC limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  181. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  182. var database = new Database(databaseType, _connectionString);
  183. var connection = database.GetConnection();
  184. var result =
  185. await connection
  186. .QueryAsync<(int FlowId, int TodoId)>(sql);
  187. return result;
  188. }
  189. public async Task<IEnumerable<(int TodoId, int FlowId)>> GetTodoIdFlowIdListAsync(int userId, string type, bool isDone, FlowListRequest request)
  190. {
  191. string sql = $@"SELECT
  192. a.Id,a.FlowId
  193. FROM
  194. tede_flow_todo a
  195. INNER JOIN
  196. tede_process b ON a.ProcessId = b.id
  197. INNER JOIN
  198. tede_flow c ON c.Id = a.FlowId
  199. INNER JOIN
  200. tede_user d on d.Id=a.UserId
  201. INNER JOIN
  202. tede_user e on e.Id=c.UserId
  203. WHERE
  204. a.UserId = {userId}
  205. AND a.Type = '{type}'";
  206. if (isDone)
  207. sql += $@"AND a.IsDone = 1";
  208. else sql += $@"AND a.IsDone = 0 AND a.IsOperate = 1";
  209. if (!string.IsNullOrWhiteSpace(request.Keyword))
  210. {
  211. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  212. OR a.FlowId IN (SELECT
  213. FlowId
  214. FROM
  215. tede_flow_field_value
  216. WHERE
  217. StringValues LIKE '%{request.Keyword}%')
  218. OR c.UserId IN (SELECT
  219. Id
  220. FROM
  221. tede_user
  222. WHERE
  223. Name LIKE '%{request.Keyword}%')
  224. or a.FlowId In(SELECT
  225. FlowId
  226. FROM
  227. tede_flow_todo
  228. WHERE
  229. UserId IN (SELECT
  230. Id
  231. FROM
  232. tede_user
  233. WHERE
  234. Name LIKE '%{request.Keyword}%'))
  235. or a.FlowId In(SELECT
  236. FlowId
  237. FROM
  238. tede_flow_message
  239. WHERE
  240. Message LIKE '%{request.Keyword}%'))";
  241. }
  242. if (request.ProcessIds.Count > 0)
  243. {
  244. var processId = "";
  245. foreach (var item in request.ProcessIds)
  246. {
  247. if (item == 0)
  248. continue;
  249. processId += $"{item},";
  250. }
  251. if (!string.IsNullOrWhiteSpace(processId))
  252. {
  253. processId = processId.Remove(processId.Length - 1, 1);
  254. sql += $" AND b.Id IN ({processId})";
  255. }
  256. }
  257. sql += $" ORDER BY a.CreatedDate DESC limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  258. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  259. var database = new Database(databaseType, _connectionString);
  260. var connection = database.GetConnection();
  261. var result =
  262. await connection
  263. .QueryAsync<(int TodoId, int FlowId)>(sql);
  264. return result;
  265. // var query = Q
  266. // .Select(nameof(FlowTodo.Id), nameof(FlowTodo.FlowId))
  267. // .WhereNot(nameof(FlowTodo.ProcessId), 0)
  268. // .Where(nameof(FlowTodo.UserId), userId)
  269. // .Where(nameof(FlowTodo.Type), type)
  270. // .Where(nameof(FlowTodo.IsDone), isDone)
  271. // .ForPage(request.Page, request.PerPage)
  272. // .OrderByDesc(nameof(FlowTodo.Id));
  273. // if (type == nameof(TodoTypeConst.ApproverCheck) && !isDone)
  274. // {
  275. // query.Where(nameof(FlowTodo.IsOperate), true);
  276. // }
  277. // if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  278. // {
  279. // query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  280. // }
  281. // #region 新增
  282. // if (!string.IsNullOrWhiteSpace(request.Keyword))
  283. // {
  284. // //获取用户
  285. // var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  286. // if (users.Count > 0)
  287. // {
  288. // //获取
  289. // var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  290. // var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  291. // var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  292. // var flowIds = new List<int>();
  293. // if (flow.Count > 0)
  294. // flowIds.AddRange(flow.Select(n => n.Id));
  295. // //审批人
  296. // if (flowTodosApproverCheck.Count > 0)
  297. // flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  298. // //抄送人
  299. // if (flowTodosCarbonCopy.Count > 0)
  300. // flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  301. // query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  302. // }
  303. // else
  304. // {
  305. // var flowFieldValues = await _flowFieldValueRepository.GetAllAsync(Q.WhereLike(nameof(FlowFieldValue.StringValue), $"%{request.Keyword}%"));
  306. // query.WhereIn(nameof(Flow.Id), flowFieldValues.Select(n => n.FlowId));
  307. // if (flowFieldValues.Count() == 0)
  308. // {
  309. // var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  310. // query.WhereIn(nameof(Flow.ProcessId), processList.Select(n => n.Id));
  311. // }
  312. // else
  313. // query.WhereIn(nameof(Flow.Id), flowFieldValues.Select(n => n.FlowId));
  314. // }
  315. // }
  316. // #endregion
  317. // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query);
  318. }
  319. public async Task<int> GetReadCountAsync(int userId, string type, FlowListRequest request, bool isRead)
  320. {
  321. string sql = $@"SELECT
  322. count(1)
  323. FROM
  324. tede_flow_todo a
  325. INNER JOIN
  326. tede_process b ON a.ProcessId = b.id
  327. INNER JOIN
  328. tede_flow c ON c.Id = a.FlowId
  329. INNER JOIN
  330. tede_user d ON d.Id = c.UserId
  331. WHERE
  332. a.UserId = {userId}
  333. AND a.Type = '{type}'";
  334. if (isRead)
  335. sql += $@"AND a.IsRead = 1";
  336. else sql += $@"AND a.IsRead = 0";
  337. if (!string.IsNullOrWhiteSpace(request.Keyword))
  338. {
  339. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  340. OR a.FlowId IN (SELECT
  341. FlowId
  342. FROM
  343. tede_flow_field_value
  344. WHERE
  345. StringValues LIKE '%{request.Keyword}%')
  346. OR c.UserId IN (SELECT
  347. Id
  348. FROM
  349. tede_user
  350. WHERE
  351. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  352. FlowId
  353. FROM
  354. tede_flow_todo
  355. WHERE
  356. UserId IN (SELECT
  357. Id
  358. FROM
  359. tede_user
  360. WHERE
  361. Name LIKE '%{request.Keyword}%')) or a.FlowId In(SELECT
  362. FlowId
  363. FROM
  364. tede_flow_message
  365. WHERE
  366. Message LIKE '%{request.Keyword}%'))";
  367. }
  368. if (request.ProcessIds.Count > 0)
  369. {
  370. var processId = "";
  371. foreach (var item in request.ProcessIds)
  372. {
  373. if (item == 0)
  374. continue;
  375. processId += $"{item},";
  376. }
  377. if (!string.IsNullOrWhiteSpace(processId))
  378. {
  379. processId = processId.Remove(processId.Length - 1, 1);
  380. sql += $" AND b.Id IN ({processId})";
  381. }
  382. }
  383. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  384. var database = new Database(databaseType, _connectionString);
  385. var connection = database.GetConnection();
  386. var result =
  387. await connection
  388. .ExecuteScalarAsync<int>(sql);
  389. return result;
  390. }
  391. public async Task<int> GetCountAsync(int userId, string type, bool isDone, FlowListRequest request)
  392. {
  393. string sql = $@"SELECT
  394. count(1)
  395. FROM
  396. tede_flow_todo a
  397. INNER JOIN
  398. tede_process b ON a.ProcessId = b.id
  399. INNER JOIN
  400. tede_flow c ON c.Id = a.FlowId
  401. INNER JOIN
  402. tede_user d on d.Id=a.UserId
  403. INNER JOIN
  404. tede_user e on e.Id=c.UserId
  405. WHERE
  406. a.UserId = {userId}
  407. AND a.Type = '{type}'";
  408. if (isDone)
  409. sql += $@"AND a.IsDone = 1";
  410. else sql += $@"AND a.IsDone = 0 AND a.IsOperate = 1";
  411. if (!string.IsNullOrWhiteSpace(request.Keyword))
  412. {
  413. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  414. OR a.FlowId IN (SELECT
  415. FlowId
  416. FROM
  417. tede_flow_field_value
  418. WHERE
  419. StringValues LIKE '%{request.Keyword}%')
  420. OR c.UserId IN (SELECT
  421. Id
  422. FROM
  423. tede_user
  424. WHERE
  425. Name LIKE '%{request.Keyword}%')
  426. or a.FlowId In(SELECT
  427. FlowId
  428. FROM
  429. tede_flow_todo
  430. WHERE
  431. UserId IN (SELECT
  432. Id
  433. FROM
  434. tede_user
  435. WHERE
  436. Name LIKE '%{request.Keyword}%'))
  437. or a.FlowId In(SELECT
  438. FlowId
  439. FROM
  440. tede_flow_message
  441. WHERE
  442. Message LIKE '%{request.Keyword}%'))";
  443. }
  444. if (request.ProcessIds.Count > 0)
  445. {
  446. var processId = "";
  447. foreach (var item in request.ProcessIds)
  448. {
  449. if (item == 0)
  450. continue;
  451. processId += $"{item},";
  452. }
  453. if (!string.IsNullOrWhiteSpace(processId))
  454. {
  455. processId = processId.Remove(processId.Length - 1, 1);
  456. sql += $" AND b.Id IN ({processId})";
  457. }
  458. }
  459. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  460. var database = new Database(databaseType, _connectionString);
  461. var connection = database.GetConnection();
  462. var result =
  463. await connection
  464. .ExecuteScalarAsync<int>(sql);
  465. return result;
  466. }
  467. /// <summary>
  468. /// 已审批数量
  469. /// </summary>
  470. /// <param name="userId"></param>
  471. /// <param name="type"></param>
  472. /// <param name="isDone"></param>
  473. /// <param name="request"></param>
  474. /// <returns></returns>
  475. public async Task<int> GetMyCheckedCountAsync(int userId, string type, bool isDone, FlowListRequest request)
  476. {
  477. string sql = $@"
  478. SELECT
  479. count(1)
  480. FROM
  481. tede_flow_todo a
  482. INNER JOIN
  483. tede_process b ON a.ProcessId = b.id
  484. INNER JOIN
  485. tede_flow c ON c.Id = a.FlowId
  486. INNER JOIN
  487. tede_user d on d.Id=a.UserId
  488. INNER JOIN
  489. tede_user e on e.Id=c.UserId
  490. WHERE
  491. a.UserId = {userId}
  492. AND a.Type = '{type}'";
  493. if (isDone)
  494. sql += $@" AND a.IsDone = 1";
  495. else sql += $@" AND a.IsDone = 0 AND a.IsOperate = 1";
  496. if (!string.IsNullOrWhiteSpace(request.Keyword))
  497. {
  498. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  499. OR a.FlowId IN (SELECT
  500. FlowId
  501. FROM
  502. tede_flow_field_value
  503. WHERE
  504. StringValues LIKE '%{request.Keyword}%')
  505. OR c.UserId IN (SELECT
  506. Id
  507. FROM
  508. tede_user
  509. WHERE
  510. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  511. FlowId
  512. FROM
  513. tede_flow_todo
  514. WHERE
  515. UserId IN (SELECT
  516. Id
  517. FROM
  518. tede_user
  519. WHERE
  520. Name LIKE '%{request.Keyword}%'))
  521. or a.FlowId In(SELECT
  522. FlowId
  523. FROM
  524. tede_flow_message
  525. WHERE
  526. Message LIKE '%{request.Keyword}%'))";
  527. }
  528. if (request.ProcessIds.Count > 0)
  529. {
  530. var processId = "";
  531. foreach (var item in request.ProcessIds)
  532. {
  533. if (item == 0)
  534. continue;
  535. processId += $"{item},";
  536. }
  537. if (!string.IsNullOrWhiteSpace(processId))
  538. {
  539. processId = processId.Remove(processId.Length - 1, 1);
  540. sql += $" AND b.Id IN ({processId})";
  541. }
  542. }
  543. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  544. var database = new Database(databaseType, _connectionString);
  545. var connection = database.GetConnection();
  546. var result =
  547. await connection
  548. .ExecuteScalarAsync<int>(sql);
  549. return result;
  550. }
  551. /// <summary>
  552. /// 未读数量
  553. /// </summary>
  554. /// <param name="userId"></param>
  555. /// <param name="type"></param>
  556. /// <param name="isDone"></param>
  557. /// <param name="request"></param>
  558. /// <returns></returns>
  559. #region
  560. public async Task<int> GetUReadCountAsync(int userId, string type, bool isDone, FlowListRequest request)
  561. {
  562. var query = Q
  563. .Select(nameof(FlowTodo.FlowId))
  564. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  565. .Where(nameof(FlowTodo.UserId), userId)
  566. .Where(nameof(FlowTodo.Type), type)
  567. .Where(nameof(FlowTodo.IsDone), isDone)
  568. .OrderByDesc(nameof(FlowTodo.Id));
  569. if (!isDone && type == nameof(TodoTypeConst.ApproverCheck))
  570. {
  571. query.Where(nameof(FlowTodo.IsOperate), true);
  572. }
  573. if (request.ProcessIds != null && request.ProcessIds.Any())
  574. {
  575. query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  576. }
  577. #region 新增
  578. if (!string.IsNullOrWhiteSpace(request.Keyword))
  579. {
  580. //获取用户
  581. var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  582. //获取
  583. var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  584. query.WhereIn(nameof(FlowTodo.FlowId), flow.Select(n => n.Id));
  585. }
  586. #endregion
  587. return await _repository.CountAsync(query);
  588. }
  589. #endregion
  590. public async Task<IEnumerable<(int TodoId, int FlowId)>> GetTodoIdFlowIdListAsync(int userId, string type, FlowListRequest request, bool isRead)
  591. {
  592. string sql = $@"SELECT
  593. a.Id,a.FlowId
  594. FROM
  595. tede_flow_todo a
  596. INNER JOIN
  597. tede_process b ON a.ProcessId = b.id
  598. INNER JOIN
  599. tede_flow c ON c.Id = a.FlowId
  600. INNER JOIN
  601. tede_user d ON d.Id = c.UserId
  602. WHERE
  603. a.UserId = {userId}
  604. AND a.Type = '{type}'";
  605. if (isRead)
  606. sql += $@"AND a.IsRead = 1";
  607. else sql += $@"AND a.IsRead = 0";
  608. if (!string.IsNullOrWhiteSpace(request.Keyword))
  609. {
  610. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  611. OR a.FlowId IN (SELECT
  612. FlowId
  613. FROM
  614. tede_flow_field_value
  615. WHERE
  616. StringValues LIKE '%{request.Keyword}%')
  617. OR c.UserId IN (SELECT
  618. Id
  619. FROM
  620. tede_user
  621. WHERE
  622. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  623. FlowId
  624. FROM
  625. tede_flow_todo
  626. WHERE
  627. UserId IN (SELECT
  628. Id
  629. FROM
  630. tede_user
  631. WHERE
  632. Name LIKE '%{request.Keyword}%'))
  633. or a.FlowId In(SELECT
  634. FlowId
  635. FROM
  636. tede_flow_message
  637. WHERE
  638. Message LIKE '%{request.Keyword}%'))";
  639. }
  640. if (request.ProcessIds.Count > 0)
  641. {
  642. var processId = "";
  643. foreach (var item in request.ProcessIds)
  644. {
  645. if (item == 0)
  646. continue;
  647. processId += $"{item},";
  648. }
  649. if (!string.IsNullOrWhiteSpace(processId))
  650. {
  651. processId = processId.Remove(processId.Length - 1, 1);
  652. sql += $" AND b.Id IN ({processId})";
  653. }
  654. }
  655. sql += $" order by a.CreatedDate desc limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  656. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  657. var database = new Database(databaseType, _connectionString);
  658. var connection = database.GetConnection();
  659. var result =
  660. await connection
  661. .QueryAsync<(int TodoId, int FlowId)>(sql);
  662. return result;
  663. // var query = Q
  664. // .WhereNot(nameof(FlowTodo.ProcessId), 0)
  665. // .Where(nameof(FlowTodo.UserId), userId)
  666. // .Where(nameof(FlowTodo.Type), type)
  667. // .ForPage(request.Page, request.PerPage)
  668. // .OrderByDesc(nameof(FlowTodo.Id));
  669. // if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  670. // {
  671. // query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  672. // }
  673. // #region 新增
  674. // if (!string.IsNullOrWhiteSpace(request.Keyword))
  675. // {
  676. // //获取用户
  677. // var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  678. // if (users.Count > 0)
  679. // {
  680. // //获取
  681. // var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  682. // var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  683. // var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  684. // var flowIds = new List<int>();
  685. // if (flow.Count > 0)
  686. // flowIds.AddRange(flow.Select(n => n.Id));
  687. // //审批人
  688. // if (flowTodosApproverCheck.Count > 0)
  689. // flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  690. // //抄送人
  691. // if (flowTodosCarbonCopy.Count > 0)
  692. // flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  693. // query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  694. // }
  695. // else
  696. // {
  697. // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  698. // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  699. // // if (flowIdList == null || !flowIdList.Any())
  700. // // {
  701. // // return new List<(int TodoId, int FlowId)>();
  702. // // }
  703. // if (flowIdList.Count() == 0)
  704. // {
  705. // var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  706. // query.WhereIn(nameof(FlowTodo.ProcessId), processList.Select(n => n.Id));
  707. // var result = await _repository.GetAllAsync(query);
  708. // return result.Select(n => (n.Id, n.FlowId));
  709. // }
  710. // else
  711. // {
  712. // var result = await _repository.GetAllAsync(query.WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  713. // return result.Select(n => (n.Id, n.FlowId));
  714. // }
  715. // //return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id),nameof(FlowTodo.FlowId)).WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  716. // }
  717. // }
  718. // #endregion
  719. // // if (!string.IsNullOrEmpty(request.Keyword))
  720. // // {
  721. // // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  722. // // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  723. // // if (flowIdList == null || !flowIdList.Any())
  724. // // {
  725. // // return new List<(int TodoId, int FlowId)>();
  726. // // }
  727. // // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id)).WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  728. // // }
  729. // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id), nameof(FlowTodo.FlowId)));
  730. }
  731. public async Task<int> GetCountAsync(int userId, string type, FlowListRequest request)
  732. {
  733. var query = Q
  734. .Select(nameof(FlowTodo.FlowId))
  735. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  736. .Where(nameof(FlowTodo.UserId), userId)
  737. .Where(nameof(FlowTodo.Type), type)
  738. .OrderByDesc(nameof(FlowTodo.Id));
  739. if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  740. {
  741. query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  742. }
  743. #region 新增
  744. if (!string.IsNullOrWhiteSpace(request.Keyword))
  745. {
  746. //获取用户
  747. var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  748. if (users.Count > 0)
  749. {
  750. //获取
  751. var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  752. var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  753. var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  754. var flowIds = new List<int>();
  755. if (flow.Count > 0)
  756. flowIds.AddRange(flow.Select(n => n.Id));
  757. //审批人
  758. if (flowTodosApproverCheck.Count > 0)
  759. flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  760. //抄送人
  761. if (flowTodosCarbonCopy.Count > 0)
  762. flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  763. query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  764. }
  765. else
  766. {
  767. var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  768. var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  769. // if (flowIdList == null || !flowIdList.Any())
  770. // {
  771. // return 0;
  772. // }
  773. if (flowIdList.Count() == 0)
  774. {
  775. var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  776. query.WhereIn(nameof(FlowTodo.ProcessId), processList.Select(n => n.Id));
  777. }
  778. else
  779. {
  780. return 0;
  781. }
  782. }
  783. }
  784. #endregion
  785. // if (!string.IsNullOrEmpty(request.Keyword))
  786. // {
  787. // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  788. // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  789. // if (flowIdList == null || !flowIdList.Any())
  790. // {
  791. // return 0;
  792. // }
  793. // return await _repository.CountAsync(query.WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  794. // }
  795. return await _repository.CountAsync(query);
  796. }
  797. public async Task<FlowTodo> GetAsync(int todoId)
  798. {
  799. return await _repository.GetAsync(todoId);
  800. }
  801. public async Task<FlowTodo> GetAsync(SqlKata.Query query)
  802. {
  803. return await _repository.GetAsync(query);
  804. }
  805. /// <summary>
  806. /// 获取未处理的
  807. /// </summary>
  808. /// <param name="flowId"></param>
  809. /// <returns></returns>
  810. public async Task<FlowTodo> GetUCheckedAsync(int flowId)
  811. {
  812. string sql = $@" SELECT
  813. a.*
  814. FROM
  815. tede_flow_todo a
  816. INNER JOIN
  817. tede_user b ON a.UserId = b.Id
  818. WHERE
  819. a.FlowId ={flowId} AND a.IsDone = 0
  820. AND a.Type = 'ApproverCheck'
  821. AND IsChecked = 0 order by a.Sort limit 1";
  822. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  823. var database = new Database(databaseType, _connectionString);
  824. var connection = database.GetConnection();
  825. var result = await connection.QueryFirstOrDefaultAsync<FlowTodo>(sql);
  826. return result;
  827. }
  828. public async Task<bool> UpdateAsync(FlowTodo todo)
  829. {
  830. return await _repository.UpdateAsync(todo);
  831. }
  832. public async Task<bool> UpdateSortAsync(FlowTodo todo)
  833. {
  834. string sql = $"UPDATE `ccpph`.`tede_flow_todo` SET `Sort` = {todo.Sort} WHERE `Id` = {todo.Id}";
  835. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  836. var database = new Database(databaseType, _connectionString);
  837. var connection = database.GetConnection();
  838. var result = await connection.ExecuteAsync(sql);
  839. return result > 0;
  840. }
  841. public async Task<bool> UpdateAsync(SqlKata.Query query)
  842. {
  843. return await _repository.UpdateAsync(query) > 0;
  844. }
  845. public async Task<bool> UpdateReadAsync(int userId, int flowId)
  846. {
  847. string sql = $"UPDATE `ccpph`.`tede_flow_todo` SET `IsRead` = 1 WHERE `FlowId` = {flowId} and `UserId`={userId} ";
  848. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  849. var database = new Database(databaseType, _connectionString);
  850. var connection = database.GetConnection();
  851. var result = await connection.ExecuteAsync(sql);
  852. return result > 0;
  853. }
  854. public async Task<bool> UpdateFlowReadAsync(int userId, int flowId)
  855. {
  856. string sql = $"UPDATE `ccpph`.`tede_flow` SET `IsRead` = 1 WHERE `Id` = {flowId} and `UserId`={userId}";
  857. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  858. var database = new Database(databaseType, _connectionString);
  859. var connection = database.GetConnection();
  860. var result = await connection.ExecuteAsync(sql);
  861. return result > 0;
  862. }
  863. public async Task UpdateCCIsDoneAsync(int userId, int flowId)
  864. {
  865. await UpdateReadAsync(userId, flowId);
  866. await UpdateFlowReadAsync(userId, flowId);
  867. }
  868. public async Task DeleteCheckingByFlowIdAsync(int flowId)
  869. {
  870. await _repository.DeleteAsync(Q
  871. .Where(nameof(FlowTodo.FlowId), flowId)
  872. .Where(nameof(FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck))
  873. .Where(nameof(FlowTodo.IsDone), false)
  874. );
  875. }
  876. public async Task<IEnumerable<int>> GetAllCheckedUserIdListAsync(int flowId)
  877. {
  878. return await _repository.GetAllAsync<int>(Q
  879. .Select(nameof(FlowTodo.UserId))
  880. .Where(nameof(FlowTodo.FlowId), flowId)
  881. .Where(nameof(FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck))
  882. .Where(nameof(FlowTodo.IsChecked), true)
  883. .Where(nameof(FlowTodo.IsDone), true)
  884. );
  885. }
  886. /// <summary>
  887. /// 获取操作成功数量
  888. /// </summary>
  889. /// <param name="flowId"></param>
  890. /// <returns></returns>
  891. public async Task<int> CheckedCountAsync(int flowId)
  892. {
  893. string sql = $@"SELECT
  894. count(1)
  895. FROM
  896. tede_flow_todo a
  897. INNER JOIN
  898. tede_user b ON a.UserId = b.Id
  899. WHERE
  900. a.FlowId ={flowId} AND a.IsDone = 1
  901. AND a.Type = 'ApproverCheck'
  902. AND IsChecked = 1";
  903. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  904. var database = new Database(databaseType, _connectionString);
  905. var connection = database.GetConnection();
  906. var result = await connection.ExecuteScalarAsync<int>(sql);
  907. return result;
  908. }
  909. /// <summary>
  910. /// 获取所有数量
  911. /// </summary>
  912. /// <param name="flowId"></param>
  913. /// <returns></returns>
  914. public async Task<int> AllCountAsync(int flowId)
  915. {
  916. string sql = $@"SELECT
  917. count(1)
  918. FROM
  919. tede_flow_todo a
  920. INNER JOIN
  921. tede_user b ON a.UserId = b.Id
  922. WHERE
  923. a.FlowId ={flowId}
  924. AND a.Type = 'ApproverCheck'";
  925. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  926. var database = new Database(databaseType, _connectionString);
  927. var connection = database.GetConnection();
  928. var result = await connection.ExecuteScalarAsync<int>(sql);
  929. return result;
  930. }
  931. public async Task<int> CountAsync(SqlKata.Query query)
  932. {
  933. return await _repository.CountAsync(query);
  934. }
  935. /// <summary>
  936. /// 抄送未读数量
  937. /// </summary>
  938. /// <param name="userId"></param>
  939. /// <returns></returns>
  940. public async Task<int> FlowCcUReadCountAsync(int userId)
  941. {
  942. 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)";
  943. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  944. var database = new Database(databaseType, _connectionString);
  945. var connection = database.GetConnection();
  946. var result = await connection.ExecuteScalarAsync<int>(sql);
  947. return result;
  948. }
  949. public async Task<IEnumerable<FlowTodo>> GetAllAsync(Query query)
  950. {
  951. return await _repository.GetAllAsync(query);
  952. }
  953. }
  954. }