FlowTodoRepository.cs 40 KB

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