FlowTodoRepository.cs 51 KB

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