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 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}%')
  430. or a.FlowId In(SELECT
  431. FlowId
  432. FROM
  433. tede_flow_todo
  434. WHERE
  435. UserId IN (SELECT
  436. Id
  437. FROM
  438. tede_user
  439. WHERE
  440. Name LIKE '%{request.Keyword}%'))
  441. or a.FlowId In(SELECT
  442. FlowId
  443. FROM
  444. tede_flow_message
  445. WHERE
  446. Message LIKE '%{request.Keyword}%'))";
  447. }
  448. if (request.ProcessIds.Count > 0)
  449. {
  450. var processId = "";
  451. foreach (var item in request.ProcessIds)
  452. {
  453. if (item == 0)
  454. continue;
  455. processId += $"{item},";
  456. }
  457. if (!string.IsNullOrWhiteSpace(processId))
  458. {
  459. processId = processId.Remove(processId.Length - 1, 1);
  460. sql += $" AND b.Id IN ({processId})";
  461. }
  462. }
  463. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  464. var database = new Database(databaseType, _connectionString);
  465. var connection = database.GetConnection();
  466. var result =
  467. await connection
  468. .ExecuteScalarAsync<int>(sql);
  469. return result;
  470. }
  471. /// <summary>
  472. /// 已审批数量
  473. /// </summary>
  474. /// <param name="userId"></param>
  475. /// <param name="type"></param>
  476. /// <param name="isDone"></param>
  477. /// <param name="request"></param>
  478. /// <returns></returns>
  479. public async Task<int> GetMyCheckedCountAsync(int userId, string type, bool isDone, FlowListRequest request)
  480. {
  481. string sql = $@"
  482. SELECT
  483. count(1)
  484. FROM
  485. tede_flow
  486. WHERE
  487. Id IN (
  488. SELECT
  489. a.FlowId
  490. FROM
  491. tede_flow_todo a
  492. INNER JOIN
  493. tede_process b ON a.ProcessId = b.id
  494. INNER JOIN
  495. tede_flow c ON c.Id = a.FlowId
  496. INNER JOIN
  497. tede_user d on d.Id=a.UserId
  498. INNER JOIN
  499. tede_user e on e.Id=c.UserId
  500. WHERE
  501. a.UserId = {userId}
  502. AND a.Type = '{type}'";
  503. if (isDone)
  504. sql += $@" AND a.IsDone = 1";
  505. else sql += $@" AND a.IsDone = 0 AND a.IsOperate = 1";
  506. if (!string.IsNullOrWhiteSpace(request.Keyword))
  507. {
  508. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  509. OR a.FlowId IN (SELECT
  510. FlowId
  511. FROM
  512. tede_flow_field_value
  513. WHERE
  514. StringValues LIKE '%{request.Keyword}%')
  515. OR c.UserId IN (SELECT
  516. Id
  517. FROM
  518. tede_user
  519. WHERE
  520. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  521. FlowId
  522. FROM
  523. tede_flow_todo
  524. WHERE
  525. UserId IN (SELECT
  526. Id
  527. FROM
  528. tede_user
  529. WHERE
  530. Name LIKE '%{request.Keyword}%'))
  531. or a.FlowId In(SELECT
  532. FlowId
  533. FROM
  534. tede_flow_message
  535. WHERE
  536. Message LIKE '%{request.Keyword}%'))";
  537. }
  538. if (request.ProcessIds.Count > 0)
  539. {
  540. var processId = "";
  541. foreach (var item in request.ProcessIds)
  542. {
  543. if (item == 0)
  544. continue;
  545. processId += $"{item},";
  546. }
  547. if (!string.IsNullOrWhiteSpace(processId))
  548. {
  549. processId = processId.Remove(processId.Length - 1, 1);
  550. sql += $" AND b.Id IN ({processId})";
  551. }
  552. }
  553. sql += ")";
  554. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  555. var database = new Database(databaseType, _connectionString);
  556. var connection = database.GetConnection();
  557. var result =
  558. await connection
  559. .ExecuteScalarAsync<int>(sql);
  560. return result;
  561. }
  562. /// <summary>
  563. /// 未读数量
  564. /// </summary>
  565. /// <param name="userId"></param>
  566. /// <param name="type"></param>
  567. /// <param name="isDone"></param>
  568. /// <param name="request"></param>
  569. /// <returns></returns>
  570. #region
  571. public async Task<int> GetUReadCountAsync(int userId, string type, bool isDone, FlowListRequest request)
  572. {
  573. var query = Q
  574. .Select(nameof(FlowTodo.FlowId))
  575. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  576. .Where(nameof(FlowTodo.UserId), userId)
  577. .Where(nameof(FlowTodo.Type), type)
  578. .Where(nameof(FlowTodo.IsDone), isDone)
  579. .OrderByDesc(nameof(FlowTodo.Id));
  580. if (!isDone && type == nameof(TodoTypeConst.ApproverCheck))
  581. {
  582. query.Where(nameof(FlowTodo.IsOperate), true);
  583. }
  584. if (request.ProcessIds != null && request.ProcessIds.Any())
  585. {
  586. query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  587. }
  588. #region 新增
  589. if (!string.IsNullOrWhiteSpace(request.Keyword))
  590. {
  591. //获取用户
  592. var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  593. //获取
  594. var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  595. query.WhereIn(nameof(FlowTodo.FlowId), flow.Select(n => n.Id));
  596. }
  597. #endregion
  598. return await _repository.CountAsync(query);
  599. }
  600. #endregion
  601. public async Task<IEnumerable<(int TodoId, int FlowId)>> GetTodoIdFlowIdListAsync(int userId, string type, FlowListRequest request, bool isRead)
  602. {
  603. string sql = $@"SELECT
  604. a.Id,a.FlowId
  605. FROM
  606. tede_flow_todo a
  607. INNER JOIN
  608. tede_process b ON a.ProcessId = b.id
  609. INNER JOIN
  610. tede_flow c ON c.Id = a.FlowId
  611. WHERE
  612. a.UserId = {userId}
  613. AND a.Type = '{type}'";
  614. if (isRead)
  615. sql += $@"AND a.IsRead = 1";
  616. else sql += $@"AND a.IsRead = 0";
  617. if (!string.IsNullOrWhiteSpace(request.Keyword))
  618. {
  619. sql += $@" AND (b.Name LIKE '%{request.Keyword}%'
  620. OR a.FlowId IN (SELECT
  621. FlowId
  622. FROM
  623. tede_flow_field_value
  624. WHERE
  625. StringValues LIKE '%{request.Keyword}%')
  626. OR c.UserId IN (SELECT
  627. Id
  628. FROM
  629. tede_user
  630. WHERE
  631. Name LIKE '%{request.Keyword}%') or a.FlowId In(SELECT
  632. FlowId
  633. FROM
  634. tede_flow_todo
  635. WHERE
  636. UserId IN (SELECT
  637. Id
  638. FROM
  639. tede_user
  640. WHERE
  641. Name LIKE '%{request.Keyword}%'))
  642. or a.FlowId In(SELECT
  643. FlowId
  644. FROM
  645. tede_flow_message
  646. WHERE
  647. Message LIKE '%{request.Keyword}%'))";
  648. }
  649. if (request.ProcessIds.Count > 0)
  650. {
  651. var processId = "";
  652. foreach (var item in request.ProcessIds)
  653. {
  654. if (item == 0)
  655. continue;
  656. processId += $"{item},";
  657. }
  658. if (!string.IsNullOrWhiteSpace(processId))
  659. {
  660. processId = processId.Remove(processId.Length - 1, 1);
  661. sql += $" AND b.Id IN ({processId})";
  662. }
  663. }
  664. sql += $" order by a.CreatedDate desc limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  665. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  666. var database = new Database(databaseType, _connectionString);
  667. var connection = database.GetConnection();
  668. var result =
  669. await connection
  670. .QueryAsync<(int TodoId, int FlowId)>(sql);
  671. return result;
  672. // var query = Q
  673. // .WhereNot(nameof(FlowTodo.ProcessId), 0)
  674. // .Where(nameof(FlowTodo.UserId), userId)
  675. // .Where(nameof(FlowTodo.Type), type)
  676. // .ForPage(request.Page, request.PerPage)
  677. // .OrderByDesc(nameof(FlowTodo.Id));
  678. // if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  679. // {
  680. // query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  681. // }
  682. // #region 新增
  683. // if (!string.IsNullOrWhiteSpace(request.Keyword))
  684. // {
  685. // //获取用户
  686. // var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  687. // if (users.Count > 0)
  688. // {
  689. // //获取
  690. // var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  691. // var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  692. // var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  693. // var flowIds = new List<int>();
  694. // if (flow.Count > 0)
  695. // flowIds.AddRange(flow.Select(n => n.Id));
  696. // //审批人
  697. // if (flowTodosApproverCheck.Count > 0)
  698. // flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  699. // //抄送人
  700. // if (flowTodosCarbonCopy.Count > 0)
  701. // flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  702. // query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  703. // }
  704. // else
  705. // {
  706. // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  707. // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  708. // // if (flowIdList == null || !flowIdList.Any())
  709. // // {
  710. // // return new List<(int TodoId, int FlowId)>();
  711. // // }
  712. // if (flowIdList.Count() == 0)
  713. // {
  714. // var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  715. // query.WhereIn(nameof(FlowTodo.ProcessId), processList.Select(n => n.Id));
  716. // var result = await _repository.GetAllAsync(query);
  717. // return result.Select(n => (n.Id, n.FlowId));
  718. // }
  719. // else
  720. // {
  721. // var result = await _repository.GetAllAsync(query.WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  722. // return result.Select(n => (n.Id, n.FlowId));
  723. // }
  724. // //return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id),nameof(FlowTodo.FlowId)).WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  725. // }
  726. // }
  727. // #endregion
  728. // // if (!string.IsNullOrEmpty(request.Keyword))
  729. // // {
  730. // // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  731. // // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  732. // // if (flowIdList == null || !flowIdList.Any())
  733. // // {
  734. // // return new List<(int TodoId, int FlowId)>();
  735. // // }
  736. // // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id)).WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  737. // // }
  738. // return await _repository.GetAllAsync<(int TodoId, int FlowId)>(query.Select(nameof(FlowTodo.Id), nameof(FlowTodo.FlowId)));
  739. }
  740. public async Task<int> GetCountAsync(int userId, string type, FlowListRequest request)
  741. {
  742. var query = Q
  743. .Select(nameof(FlowTodo.FlowId))
  744. .WhereNot(nameof(FlowTodo.ProcessId), 0)
  745. .Where(nameof(FlowTodo.UserId), userId)
  746. .Where(nameof(FlowTodo.Type), type)
  747. .OrderByDesc(nameof(FlowTodo.Id));
  748. if (request.ProcessIds != null && request.ProcessIds.Any() && !request.ProcessIds.Contains(0))
  749. {
  750. query.WhereIn(nameof(FlowTodo.ProcessId), request.ProcessIds);
  751. }
  752. #region 新增
  753. if (!string.IsNullOrWhiteSpace(request.Keyword))
  754. {
  755. //获取用户
  756. var users = await _userRepository.GetAllAsync(Q.WhereLike(nameof(Entity.User.Name), $"%{request.Keyword}%"));
  757. if (users.Count > 0)
  758. {
  759. //获取
  760. var flow = await _flowRepository.GetAllAsync(Q.WhereIn(nameof(Flow.UserId), users.Select(n => n.Id)));
  761. var flowTodosApproverCheck = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "ApproverCheck"));
  762. var flowTodosCarbonCopy = await _repository.GetAllAsync(Q.WhereIn(nameof(FlowTodo.UserId), users.Select(n => n.Id)).Where(nameof(FlowTodo.Type), "CarbonCopy"));
  763. var flowIds = new List<int>();
  764. if (flow.Count > 0)
  765. flowIds.AddRange(flow.Select(n => n.Id));
  766. //审批人
  767. if (flowTodosApproverCheck.Count > 0)
  768. flowIds.AddRange(flowTodosApproverCheck.Select(n => n.FlowId));
  769. //抄送人
  770. if (flowTodosCarbonCopy.Count > 0)
  771. flowIds.AddRange(flowTodosCarbonCopy.Select(n => n.FlowId));
  772. query.WhereIn(nameof(FlowTodo.FlowId), flowIds);
  773. }
  774. else
  775. {
  776. var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  777. var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  778. // if (flowIdList == null || !flowIdList.Any())
  779. // {
  780. // return 0;
  781. // }
  782. if (flowIdList.Count() == 0)
  783. {
  784. var processList = await _processRepository.GetAllAsync(Q.WhereLike(nameof(Entity.WorkProcess.Process.Name), $"%{request.Keyword}%"));
  785. query.WhereIn(nameof(FlowTodo.ProcessId), processList.Select(n => n.Id));
  786. }
  787. else
  788. {
  789. return 0;
  790. }
  791. }
  792. }
  793. #endregion
  794. // if (!string.IsNullOrEmpty(request.Keyword))
  795. // {
  796. // var allFlowIdList = await _repository.GetAllAsync<int>(query.Select(nameof(FlowTodo.FlowId)));
  797. // var flowIdList = await _flowFieldValueRepository.GetFlowIdListAsync(allFlowIdList, request.Keyword);
  798. // if (flowIdList == null || !flowIdList.Any())
  799. // {
  800. // return 0;
  801. // }
  802. // return await _repository.CountAsync(query.WhereIn(nameof(FlowTodo.FlowId), flowIdList));
  803. // }
  804. return await _repository.CountAsync(query);
  805. }
  806. public async Task<FlowTodo> GetAsync(int todoId)
  807. {
  808. return await _repository.GetAsync(todoId);
  809. }
  810. public async Task<FlowTodo> GetAsync(SqlKata.Query query)
  811. {
  812. return await _repository.GetAsync(query);
  813. }
  814. /// <summary>
  815. /// 获取未处理的
  816. /// </summary>
  817. /// <param name="flowId"></param>
  818. /// <returns></returns>
  819. public async Task<FlowTodo> GetUCheckedAsync(int flowId)
  820. {
  821. string sql = $@" SELECT
  822. a.*
  823. FROM
  824. tede_flow_todo a
  825. INNER JOIN
  826. tede_user b ON a.UserId = b.Id
  827. WHERE
  828. a.FlowId ={flowId} AND a.IsDone = 0
  829. AND a.Type = 'ApproverCheck'
  830. AND IsChecked = 0 order by a.Sort limit 1";
  831. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  832. var database = new Database(databaseType, _connectionString);
  833. var connection = database.GetConnection();
  834. var result = await connection.QueryFirstOrDefaultAsync<FlowTodo>(sql);
  835. return result;
  836. }
  837. public async Task<bool> UpdateAsync(FlowTodo todo)
  838. {
  839. return await _repository.UpdateAsync(todo);
  840. }
  841. public async Task<bool> UpdateSortAsync(FlowTodo todo)
  842. {
  843. string sql = $"UPDATE `ccpph`.`tede_flow_todo` SET `Sort` = {todo.Sort} WHERE `Id` = {todo.Id}";
  844. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  845. var database = new Database(databaseType, _connectionString);
  846. var connection = database.GetConnection();
  847. var result = await connection.ExecuteAsync(sql);
  848. return result > 0;
  849. }
  850. public async Task<bool> UpdateAsync(SqlKata.Query query)
  851. {
  852. return await _repository.UpdateAsync(query) > 0;
  853. }
  854. public async Task<bool> UpdateReadAsync(int userId, int flowId)
  855. {
  856. string sql = $"UPDATE `ccpph`.`tede_flow_todo` SET `IsRead` = 1 WHERE `FlowId` = {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<bool> UpdateFlowReadAsync(int userId, int flowId)
  864. {
  865. string sql = $"UPDATE `ccpph`.`tede_flow` SET `IsRead` = 1 WHERE `Id` = {flowId} and `UserId`={userId}";
  866. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  867. var database = new Database(databaseType, _connectionString);
  868. var connection = database.GetConnection();
  869. var result = await connection.ExecuteAsync(sql);
  870. return result > 0;
  871. }
  872. public async Task UpdateCCIsDoneAsync(int userId, int todoId)
  873. {
  874. var flowId = 0;
  875. var query = Q.NewQuery();
  876. query.Where(nameof(Entity.WorkFlow.FlowTodo.Id), todoId);
  877. query.Where(nameof(Entity.WorkFlow.FlowTodo.UserId), userId);
  878. var flowTodo = await _repository.GetAsync(query);
  879. if (flowTodo == null)
  880. flowId = todoId;
  881. else
  882. flowId = flowTodo.FlowId;
  883. await UpdateReadAsync(userId, flowId);
  884. await UpdateFlowReadAsync(userId, flowId);
  885. }
  886. public async Task DeleteCheckingByFlowIdAsync(int flowId)
  887. {
  888. await _repository.DeleteAsync(Q
  889. .Where(nameof(FlowTodo.FlowId), flowId)
  890. .Where(nameof(FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck))
  891. .Where(nameof(FlowTodo.IsDone), false)
  892. );
  893. }
  894. public async Task<IEnumerable<int>> GetAllCheckedUserIdListAsync(int flowId)
  895. {
  896. return await _repository.GetAllAsync<int>(Q
  897. .Select(nameof(FlowTodo.UserId))
  898. .Where(nameof(FlowTodo.FlowId), flowId)
  899. .Where(nameof(FlowTodo.Type), nameof(TodoTypeConst.ApproverCheck))
  900. .Where(nameof(FlowTodo.IsChecked), true)
  901. .Where(nameof(FlowTodo.IsDone), true)
  902. );
  903. }
  904. /// <summary>
  905. /// 获取操作成功数量
  906. /// </summary>
  907. /// <param name="flowId"></param>
  908. /// <returns></returns>
  909. public async Task<int> CheckedCountAsync(int flowId)
  910. {
  911. string sql = $@"SELECT
  912. count(1)
  913. FROM
  914. tede_flow_todo a
  915. INNER JOIN
  916. tede_user b ON a.UserId = b.Id
  917. WHERE
  918. a.FlowId ={flowId} AND a.IsDone = 1
  919. AND a.Type = 'ApproverCheck'
  920. AND IsChecked = 1";
  921. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  922. var database = new Database(databaseType, _connectionString);
  923. var connection = database.GetConnection();
  924. var result = await connection.ExecuteScalarAsync<int>(sql);
  925. return result;
  926. }
  927. /// <summary>
  928. /// 获取所有数量
  929. /// </summary>
  930. /// <param name="flowId"></param>
  931. /// <returns></returns>
  932. public async Task<int> AllCountAsync(int flowId)
  933. {
  934. string sql = $@"SELECT
  935. count(1)
  936. FROM
  937. tede_flow_todo a
  938. INNER JOIN
  939. tede_user b ON a.UserId = b.Id
  940. WHERE
  941. a.FlowId ={flowId}
  942. AND a.Type = 'ApproverCheck'";
  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<int> CountAsync(SqlKata.Query query)
  950. {
  951. return await _repository.CountAsync(query);
  952. }
  953. /// <summary>
  954. /// 抄送未读数量
  955. /// </summary>
  956. /// <param name="userId"></param>
  957. /// <returns></returns>
  958. public async Task<int> FlowCcUReadCountAsync(int userId)
  959. {
  960. 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)";
  961. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  962. var database = new Database(databaseType, _connectionString);
  963. var connection = database.GetConnection();
  964. var result = await connection.ExecuteScalarAsync<int>(sql);
  965. return result;
  966. }
  967. public async Task<IEnumerable<FlowTodo>> GetAllAsync(Query query)
  968. {
  969. return await _repository.GetAllAsync(query);
  970. }
  971. }
  972. }