FlowTodoRepository.cs 41 KB

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