NoticeRepository.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Transactions;
  5. using AutoMapper;
  6. using Dapper;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Exceptions;
  9. using GxPress.Common.Page;
  10. using GxPress.Common.Tools;
  11. using GxPress.Entity;
  12. using GxPress.Entity.Middle;
  13. using GxPress.EnumConst;
  14. using GxPress.Repository.Interface;
  15. using GxPress.Request.Notice;
  16. using GxPress.Result.Notice;
  17. using Microsoft.Extensions.Options;
  18. using Datory;
  19. using SqlKata;
  20. using GxPress.Result.Job;
  21. using System.Linq;
  22. namespace GxPress.Repository.Implement
  23. {
  24. public class NoticeRepository : INoticeRepository
  25. {
  26. private readonly Repository<Notice> _repository;
  27. private readonly Repository<Addressee> _noticeAddresseeRepository;
  28. private readonly Repository<Cc> _noticeCcRepository;
  29. private readonly Repository<User> _userRepository;
  30. private readonly Repository<Middle> _middleRepository;
  31. private readonly Repository<Entity.AdminVerify> _adminVerifyRepository;
  32. private readonly IMapper _mapper;
  33. private readonly string _connectionString;
  34. private readonly string _databaseTypeStr;
  35. public NoticeRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  36. {
  37. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  38. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  39. var databaseType =
  40. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  41. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  42. _repository = new Repository<Notice>(database);
  43. _noticeAddresseeRepository = new Repository<Addressee>(database);
  44. _noticeCcRepository = new Repository<Cc>(database);
  45. _userRepository = new Repository<User>(database);
  46. _middleRepository = new Repository<Middle>(database);
  47. _adminVerifyRepository = new Repository<Entity.AdminVerify>(database);
  48. _mapper = mapper;
  49. }
  50. public IDatabase Database => _repository.Database;
  51. public string TableName => _repository.TableName;
  52. public List<TableColumn> TableColumns => _repository.TableColumns;
  53. public async Task<Notice> GetAsync(int id)
  54. {
  55. return await _repository.GetAsync(id);
  56. }
  57. public async Task<Notice> GetAsync(SqlKata.Query query)
  58. {
  59. return await _repository.GetAsync(query);
  60. }
  61. /// <summary>
  62. /// 添加通知
  63. /// </summary>
  64. /// <param name="request"></param>
  65. /// <returns></returns>
  66. public async Task<int> InsertAsync(NoticeInRequest request)
  67. {
  68. var noticeId = 0;
  69. try
  70. {
  71. using (TransactionScope transactionScope = new TransactionScope())
  72. {
  73. var userEntity = await _userRepository.GetAsync(request.UserId);
  74. if (userEntity == null)
  75. throw new BusinessException("用户不存在");
  76. //通知
  77. var notice = new Notice
  78. {
  79. NoticeType = request.NoticeType.GetHashCode() == 0 ? NoticeTypeConst.Send : request.NoticeType,
  80. UserId = request.UserId,
  81. TypeValue = request.TypeValue,
  82. Content = request.Content,
  83. Title = request.Title,
  84. ParentId = request.ParentId,
  85. UserName = userEntity.Name,
  86. HtmlContent = request.HtmlContent,
  87. SourceId = request.SoureId,
  88. IsDelete = false,
  89. IsTop = false,
  90. SonId = request.SonId
  91. };
  92. noticeId = await _repository.InsertAsync(notice);
  93. transactionScope.Complete();
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. throw new BusinessException(e.Message);
  99. }
  100. return noticeId;
  101. }
  102. /// <summary>
  103. /// 根据用户ID查询
  104. /// </summary>
  105. /// <param name="request"></param>
  106. /// <returns></returns>
  107. public async Task<PagedList<Notice>> GetNoticeByUserIdAsync(NoticePageSearchRequest request)
  108. {
  109. var query = Q.NewQuery();
  110. if (request.UserId > 0)
  111. {
  112. query.Where(nameof(Addressee.UserId), request.UserId);
  113. }
  114. if (request.SearchType == 0)
  115. {
  116. query.Where(nameof(Addressee.UserId), request.UserId);
  117. }
  118. if (request.SearchType == 1)
  119. {
  120. query.Where(nameof(Addressee.IsAdmin), true);
  121. }
  122. if (request.SearchType == 2)
  123. {
  124. query.Where(nameof(Addressee.NoticeAddresseeType), NoticeTypeConst.Draft);
  125. }
  126. if (request.SearchType == 3)
  127. {
  128. query.Where(nameof(Addressee.IsDelete), true);
  129. }
  130. if (!string.IsNullOrEmpty(request.Keyword))
  131. {
  132. var like = $"%{request.Keyword}%";
  133. query.WhereLike(nameof(Notice.Title), like);
  134. }
  135. var pagedList = new PagedList<Notice>
  136. {
  137. Total = await _repository.CountAsync(query)
  138. };
  139. var list = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  140. pagedList.Items = list;
  141. return pagedList;
  142. }
  143. /// <summary>
  144. /// admin根据用户ID查询
  145. /// </summary>
  146. /// <param name="request"></param>
  147. /// <returns></returns>
  148. public async Task<PagedList<NoticeListPageResult>> GetAdminNoticeByUserIdAsync(NoticePageSearchRequest request)
  149. {
  150. //用户收件人
  151. var noticeQuery = Q.NewQuery();
  152. if (request.SearchType == 2)
  153. {
  154. noticeQuery.Where(nameof(Notice.NoticeType), NoticeTypeConst.Draft);
  155. }
  156. noticeQuery.Where(nameof(Notice.IsDelete), request.SearchType == 3);
  157. if (!string.IsNullOrEmpty(request.Keyword))
  158. {
  159. noticeQuery.WhereLike(nameof(Notice.Title), $"%{request.Keyword}%");
  160. }
  161. var noticeList = await _repository.GetAllAsync(noticeQuery);
  162. var result = new PagedList<NoticeListPageResult>();
  163. result.Total = await _repository.CountAsync(noticeQuery);
  164. var notices = await _repository.GetAllAsync(noticeQuery.OrderByDesc(nameof(Notice.CreatedDate)).ForPage(request.Page, request.PerPage));
  165. var noticeListPageResults = new List<NoticeListPageResult>();
  166. foreach (var notice in notices)
  167. {
  168. var noticeListPageResultEntity = _mapper.Map<NoticeListPageResult>(notice);
  169. var user = await _userRepository.GetAsync(notice.UserId);
  170. if (user != null)
  171. {
  172. noticeListPageResultEntity.AvatarUrl = StringUtils.AddDomain(user.AvatarUrl);
  173. }
  174. //阅读数量
  175. noticeListPageResultEntity.ReadCount = await _noticeAddresseeRepository.CountAsync(
  176. Q.Where(nameof(Addressee.SourceId), notice.Id)
  177. .Where(nameof(Addressee.IsRead), true).Where(nameof(Addressee.IsAdmin), false));
  178. //收件人数量
  179. noticeListPageResultEntity.AllCount = await _noticeAddresseeRepository.CountAsync(
  180. Q.Where(nameof(Addressee.SourceId), notice.Id)
  181. .Where(nameof(Addressee.IsAdmin), false));
  182. noticeListPageResults.Add(noticeListPageResultEntity);
  183. }
  184. result.Items = noticeListPageResults;
  185. return result;
  186. }
  187. /// <summary>
  188. /// 管理员删除
  189. /// </summary>
  190. /// <param name="id"></param>
  191. /// <returns></returns>
  192. public async Task<bool> AdminDeleteAsync(int id)
  193. {
  194. try
  195. {
  196. var notice = await _repository.GetAsync(id);
  197. if (notice == null)
  198. throw new BusinessException("通知不存在");
  199. using (TransactionScope transactionScope = new TransactionScope())
  200. {
  201. //通知
  202. await _repository.DeleteAsync(id);
  203. //抄送
  204. await _noticeCcRepository.DeleteAsync(Q.Where(nameof(Cc.SourceId), id));
  205. //收件人
  206. await _noticeAddresseeRepository.DeleteAsync(Q.Where(nameof(Addressee.SourceId), id));
  207. transactionScope.Complete();
  208. }
  209. }
  210. catch (Exception e)
  211. {
  212. throw new BusinessException(e.Message);
  213. }
  214. return true;
  215. }
  216. /// <summary>
  217. /// 自己删除
  218. /// </summary>
  219. /// <param name="request"></param>
  220. /// <returns></returns>
  221. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  222. {
  223. try
  224. {
  225. using (TransactionScope transactionScope = new TransactionScope())
  226. {
  227. //var notice = await _repository.GetAsync(request.NoticeId);
  228. //if (notice == null)
  229. // throw new BusinessException("通知不存在");
  230. //notice.IsDelete = true;
  231. //await _repository.UpdateAsync(notice);
  232. //
  233. await _noticeAddresseeRepository.UpdateAsync(Q.Set(nameof(Addressee.IsDelete), true)
  234. .WhereIn(nameof(Addressee.Id), request.MiddleIds));
  235. transactionScope.Complete();
  236. }
  237. }
  238. catch (Exception e)
  239. {
  240. throw new BusinessException(e.Message);
  241. }
  242. return true;
  243. }
  244. /// <summary>
  245. /// 恢复删除通知
  246. /// </summary>
  247. /// <param name="request"></param>
  248. /// <returns></returns>
  249. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  250. {
  251. try
  252. {
  253. using (TransactionScope transactionScope = new TransactionScope())
  254. {
  255. //
  256. await _noticeAddresseeRepository.UpdateAsync(Q.Set(nameof(Addressee.IsDelete), false)
  257. .Where(nameof(Addressee.Id), request.MiddleIds));
  258. transactionScope.Complete();
  259. }
  260. }
  261. catch (Exception e)
  262. {
  263. throw new BusinessException(e.Message);
  264. }
  265. return true;
  266. }
  267. /// <summary>
  268. /// 通知修改
  269. /// </summary>
  270. /// <param name="id"></param>
  271. /// <param name="request"></param>
  272. /// <returns></returns>
  273. public async Task<bool> UpdateNoticeAsync(Notice notice)
  274. {
  275. return await _repository.UpdateAsync(notice);
  276. }
  277. /// <summary>
  278. /// 通知修改
  279. /// </summary>
  280. /// <param name="id"></param>
  281. /// <param name="request"></param>
  282. /// <returns></returns>
  283. public async Task<bool> UpdateAsync(SqlKata.Query query)
  284. {
  285. return await _repository.UpdateAsync(query) > 0;
  286. }
  287. /// <summary>
  288. /// 获取通知列表
  289. /// </summary>
  290. /// <param name="request"></param>
  291. /// <returns></returns>
  292. public async Task<PagedList<NoticeListPageResult>> GetNoticeListPageAsync(NoticePageSearchRequest request)
  293. {
  294. // //用户收件人
  295. // string sb = "";
  296. // if (request.SearchType == 1)
  297. // sb += " AND a.IsAdmin = 1 and a.NoticeAddresseeType=1 AND a.IsDelete = 0";
  298. // else if (request.SearchType == 2)
  299. // sb += " and a.NoticeAddresseeType=2 AND a.AttributeValue=1 AND a.IsDelete = 0";
  300. // else if (request.SearchType == 3)
  301. // sb += " AND a.IsDelete = 1 AND a.AttributeValue=1";
  302. // else if (request.SearchType > 3)
  303. // sb += $"and a.NoticeAddresseeType<>2 AND b.TypeValue={request.SearchType} AND a.IsDelete = 0";
  304. // else
  305. // sb += " AND a.IsDelete = 0 AND a.NoticeAddresseeType != 2";
  306. // if (!string.IsNullOrEmpty(request.Keyword))
  307. // {
  308. // if (request.NoticeFolderId > 0)
  309. // sb += $@" AND a.ParentId ={request.NoticeFolderId} AND a.AttributeValue != 2 and (b.Title LIKE '%{request.Keyword}%'
  310. // OR b.Content LIKE '%{request.Keyword}%'
  311. // OR c.Name LIKE '%{request.Keyword}%'
  312. // OR d.UserName LIKE '%{request.Keyword}%'
  313. // OR a.CreatedDate LIKE '%{request.Keyword}%' or a.MiddleId in (SELECT
  314. // g.SourceId
  315. // FROM
  316. // tede_addressee g
  317. // INNER JOIN
  318. // tede_user h ON g.UserId = h.Id
  319. // WHERE
  320. // g.SourceId =a.MiddleId AND h.Name LIKE '%{request.Keyword}%'))";
  321. // else
  322. // sb += $@" AND a.AttributeValue != 2 AND (b.Title LIKE '%{request.Keyword}%'
  323. // OR b.Content LIKE '%{request.Keyword}%'
  324. // OR c.Name LIKE '%{request.Keyword}%'
  325. // OR d.UserName LIKE '%{request.Keyword}%'
  326. // OR a.CreatedDate LIKE '%{request.Keyword}%' or a.MiddleId in (SELECT
  327. // g.SourceId
  328. // FROM
  329. // tede_addressee g
  330. // INNER JOIN
  331. // tede_user h ON g.UserId = h.Id
  332. // WHERE
  333. // g.SourceId =a.MiddleId AND h.Name LIKE '%{request.Keyword}%'))";
  334. // }
  335. // else
  336. // {
  337. // var searchList = new List<int> { 1, 2, 3 };
  338. // if (!searchList.Contains(request.SearchType))
  339. // sb += " AND a.ParentId = " + request.NoticeFolderId;
  340. // }
  341. // var noticeTypeValue = GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode();
  342. // var sql = $@"SELECT a.*,
  343. // (SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=10 and DataSourceId=a.MiddleId) as EnjoyUser,
  344. // (SELECT
  345. // GROUP_CONCAT(Name
  346. // SEPARATOR '、')
  347. // FROM
  348. // tede_user
  349. // WHERE
  350. // Id IN (SELECT
  351. // UserId
  352. // FROM
  353. // tede_flow_todo
  354. // WHERE
  355. // FlowId = b.SourceId
  356. // AND type = 'ApproverCheck')) AS FlowUserName,
  357. // (SELECT
  358. // COUNT(1)
  359. // FROM
  360. // tede_flow_todo
  361. // WHERE
  362. // FlowId = b.SourceId
  363. // AND type = 'ApproverCheck') AS UCheckedCount,
  364. // (SELECT
  365. // COUNT(1)
  366. // FROM
  367. // tede_flow_todo
  368. // WHERE
  369. // FlowId = b.SourceId
  370. // AND type = 'ApproverCheck'
  371. // AND IsRead = 1) AS CheckedCount,
  372. // (SELECT
  373. // IsRead
  374. // FROM
  375. // tede_flow_todo
  376. // WHERE
  377. // UserId = a.UserId
  378. // AND FlowId = b.SourceId
  379. // AND Type = 'CarbonCopy'
  380. // LIMIT 1) AS IsFlowRead,
  381. // (SELECT
  382. // IsRead
  383. // FROM
  384. // tede_flow_todo
  385. // WHERE
  386. // UserId = b.UserId
  387. // AND FlowId = b.SourceId
  388. // AND Type = 'ApproverCheck' and IsRead=1
  389. // LIMIT 1) AS IsFlowChecked,
  390. // (SELECT
  391. // IsRead
  392. // FROM
  393. // tede_flow
  394. // WHERE
  395. // id = b.SourceId AND UserId = b.UserId) AS IsMyFlowRead,
  396. // (select IsRecall from tede_notice where id=a.MiddleId) as IsRecall,
  397. // (SELECT
  398. // IsDone
  399. // FROM
  400. // tede_flow_todo
  401. // WHERE
  402. // type = 'ApproverCheck'
  403. // AND UserId = a.UserId
  404. // LIMIT 1) AS IsDone,
  405. // (SELECT
  406. // IsChecked
  407. // FROM
  408. // tede_flow_todo
  409. // WHERE
  410. // type = 'ApproverCheck'
  411. // AND UserId = a.UserId
  412. // LIMIT 1) AS IsChecked,
  413. // (SELECT
  414. // DoneType
  415. // FROM
  416. // tede_flow_todo
  417. // WHERE
  418. // type = 'ApproverCheck'
  419. // AND UserId = a.UserId
  420. // LIMIT 1) AS DoneType,
  421. // (SELECT
  422. // COUNT(1)
  423. // FROM
  424. // tede_analyze
  425. // WHERE
  426. // TypeValue = {noticeTypeValue}
  427. // AND SourceId = a.MiddleId
  428. // AND AnalyzeType = 1) AS PraiseCount,
  429. // (SELECT
  430. // COUNT(1)
  431. // FROM
  432. // tede_analyze
  433. // WHERE
  434. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  435. // AND SourceId = a.MiddleId
  436. // AND AnalyzeType = 1
  437. // LIMIT 0 , 1) AS IsLaud,
  438. // (SELECT
  439. // COUNT(1)
  440. // FROM
  441. // tede_comment
  442. // WHERE
  443. // ArticleId = a.MiddleId and pid=0
  444. // AND TypeValue = {noticeTypeValue}) AS CommentCount,
  445. // (SELECT
  446. // COUNT(1)
  447. // FROM
  448. // tede_analyze
  449. // WHERE
  450. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  451. // AND SourceId = a.MiddleId
  452. // AND AnalyzeType = 4) AS RetransmissionCount,
  453. // (SELECT
  454. // COUNT(1)
  455. // FROM
  456. // tede_analyze
  457. // WHERE
  458. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  459. // AND SourceId = a.MiddleId
  460. // AND AnalyzeType = 4
  461. // LIMIT 0 , 1) AS IsRetransmission,
  462. // (SELECT
  463. // COUNT(1)
  464. // FROM
  465. // tede_analyze
  466. // WHERE
  467. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  468. // AND SourceId = a.MiddleId
  469. // AND AnalyzeType = 4
  470. // LIMIT 0 , 1) AS IsCollect,
  471. // (SELECT
  472. // COUNT(1)
  473. // FROM
  474. // tede_analyze
  475. // WHERE
  476. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  477. // AND SourceId = a.MiddleId
  478. // AND AnalyzeType = 4) AS CollectCount,
  479. // (SELECT
  480. // COUNT(1)
  481. // FROM
  482. // tede_middle
  483. // WHERE
  484. // ParentId = a.Id and IsDelete=0 and NoticeAddresseeType<>2) as FileCount,(SELECT
  485. // COUNT(1)
  486. // FROM
  487. // tede_addressee
  488. // WHERE
  489. // SourceId = a.MiddleId AND IsRead = 1) as ReadCount,(SELECT
  490. // COUNT(1)
  491. // FROM
  492. // tede_addressee
  493. // WHERE
  494. // SourceId = a.MiddleId ) as AllCount,(SELECT
  495. // COUNT(1)
  496. // FROM
  497. // tede_addressee
  498. // WHERE
  499. // SourceId = a.MiddleId AND IsRead = 1 and UserId={request.UserId} limit 0,1) as IsRead, b.*,a.Id, c.AvatarUrl, d.Id as NoticeAddresseeId,d.SourceId as NoticeId,(SELECT
  500. // Name
  501. // FROM
  502. // tede_user
  503. // WHERE
  504. // id = (SELECT
  505. // userId
  506. // FROM
  507. // tede_flow
  508. // WHERE
  509. // id = b.SourceId)) as UserName
  510. // ,(select count(1) from tede_flow where id=b.SourceId and UserId={request.UserId}) as IsFlowAdmin,b.UserId,a.IsTop
  511. // FROM
  512. // tede_middle a
  513. // left JOIN
  514. // tede_notice b ON a.MiddleId = b.Id
  515. // left JOIN
  516. // tede_user c ON b.UserId = c.Id
  517. // left JOIN
  518. // tede_addressee d ON d.Id = a.MiddleSonId
  519. // WHERE a.IsRecall=0 and
  520. // a.UserId = {request.UserId} and a.FolderType={noticeTypeValue}
  521. // {sb}
  522. // ORDER BY a.AttributeValue desc,a.IsTop desc,a.LastModifiedDate desc
  523. // LIMIT {(request.Page - 1) * request.PerPage},{request.PerPage}";
  524. // var countSql = string.Format(@"SELECT
  525. // count(1)
  526. // FROM
  527. // tede_middle a
  528. // left JOIN
  529. // tede_notice b ON a.MiddleId = b.Id
  530. // left JOIN
  531. // tede_user c ON a.UserId = c.Id
  532. // left JOIN
  533. // tede_addressee d ON d.Id = a.MiddleSonId
  534. // WHERE a.IsRecall=0 and
  535. // a.UserId = {1} and a.FolderType={2}
  536. // {0}", sb, request.UserId, noticeTypeValue);
  537. // var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  538. // var database = new Database(databaseType, _connectionString);
  539. // var connection = database.GetConnection();
  540. // var items = await connection.QueryAsync<NoticeListPageResult>(sql);
  541. // foreach (var item in items)
  542. // {
  543. // item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  544. // if (item.AttributeValue == 3)
  545. // {
  546. // var query = Q.NewQuery();
  547. // query.Where(nameof(Entity.AdminVerify.AdminId), request.UserId);
  548. // query.Where(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.UDispose.GetHashCode());
  549. // item.UReadCount = await _adminVerifyRepository.CountAsync(query);
  550. // }
  551. // }
  552. // PagedList<NoticeListPageResult> result = new PagedList<NoticeListPageResult>
  553. // {
  554. // Total = await CountAsync(countSql),
  555. // Items = items
  556. // };
  557. // return result;
  558. return await GetNewNoticeListPageAsync(request);
  559. }
  560. /// <summary>
  561. /// 获取通知列表
  562. /// </summary>
  563. /// <param name="request"></param>
  564. /// <returns></returns>
  565. public async Task<PagedList<NoticeListPageResult>> GetNewNoticeListPageAsync(NoticePageSearchRequest request)
  566. {
  567. var result = new PagedList<NoticeListPageResult>();
  568. var noticeTypeValue = GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode();
  569. var query = new SqlKata.Query("tede_middle");
  570. query.LeftJoin("tede_notice", "tede_notice.Id", "tede_middle.MiddleId");
  571. query.LeftJoin("tede_user", "tede_user.Id", "tede_notice.UserId");
  572. query.LeftJoin("tede_addressee", "tede_addressee.Id", "tede_middle.MiddleSonId");
  573. if (request.SearchType == 1)
  574. {
  575. query.Where("tede_middle.IsAdmin", true);
  576. query.Where("tede_middle.NoticeAddresseeType", 1);
  577. query.Where("tede_middle.IsDelete", false);
  578. }
  579. else if (request.SearchType == 2)
  580. {
  581. query.Where("tede_middle.AttributeValue", 1);
  582. query.Where("tede_middle.NoticeAddresseeType", 2);
  583. query.Where("tede_middle.IsDelete", false);
  584. }
  585. else if (request.SearchType == 3)
  586. {
  587. query.Where("tede_middle.AttributeValue", 1);
  588. query.Where("tede_middle.IsDelete", true);
  589. }
  590. else if (request.SearchType > 3)
  591. {
  592. query.Where("tede_notice.TypeValue", request.SearchType);
  593. query.WhereNot("tede_middle.NoticeAddresseeType", 2);
  594. query.Where("tede_middle.IsDelete", false);
  595. }
  596. else
  597. {
  598. query.WhereNot("tede_middle.NoticeAddresseeType", 2);
  599. query.Where("tede_middle.IsDelete", false);
  600. }
  601. if (!string.IsNullOrEmpty(request.Keyword))
  602. {
  603. if (request.NoticeFolderId > 0)
  604. query.Where("tede_middle.ParentId", request.NoticeFolderId);
  605. var sourceIdQuery = new SqlKata.Query("tede_addressee");
  606. sourceIdQuery.Join("tede_user", "tede_user.Id", "tede_addressee.UserId");
  607. sourceIdQuery.WhereColumns("tede_addressee.SourceId", "=", "tede_middle.MiddleId");
  608. sourceIdQuery.WhereLike("tede_user.Name", $"%{request.Keyword}%");
  609. sourceIdQuery.Select("tede_addressee.SourceId");
  610. query.WhereNot("tede_middle.AttributeValue", 2);
  611. query.Where(n => n.OrWhereLike("tede_notice.Title", $"%{request.Keyword}%").OrWhereLike("tede_notice.Content", $"%{request.Keyword}%")
  612. .OrWhere(n => n.WhereIn("tede_middle.MiddleId", sourceIdQuery)));
  613. }
  614. else
  615. {
  616. var searchList = new List<int> { 1, 2, 3 };
  617. if (!searchList.Contains(request.SearchType))
  618. query.Where("tede_middle.ParentId", request.NoticeFolderId);
  619. }
  620. query.Where("tede_middle.IsRecall", false);
  621. query.Where("tede_middle.UserId", request.UserId);
  622. query.Where("tede_middle.FolderType", noticeTypeValue);
  623. //条数
  624. result.Total = await _middleRepository.CountAsync(query);
  625. var enjoyUser = new SqlKata.Query("tede_user_middle");
  626. enjoyUser.Where("MiddleType", 10);
  627. enjoyUser.WhereColumns("DataSourceId", "=", "tede_middle.MiddleId");
  628. enjoyUser.SelectRaw("GROUP_CONCAT(SourceName Separator'、')");
  629. query.Select(enjoyUser, "EnjoyUser");
  630. var flowUserName = new SqlKata.Query("tede_user");
  631. flowUserName.SelectRaw("GROUP_CONCAT(Name Separator'、')");
  632. var flowTodo = new SqlKata.Query("tede_flow_todo");
  633. flowTodo.WhereColumns("tede_flow_todo.FlowId", "=", "tede_notice.SourceId");
  634. flowTodo.Where("tede_flow_todo.type", "ApproverCheck");
  635. flowTodo.Select("tede_flow_todo.UserId");
  636. flowUserName.WhereIn("tede_user.Id", flowTodo);
  637. query.Select(flowUserName, "FlowUserName");
  638. var uCheckedCount = new SqlKata.Query("tede_flow_todo");
  639. uCheckedCount.WhereColumns("tede_flow_todo.FlowId", "=", "tede_notice.SourceId");
  640. uCheckedCount.Where("tede_flow_todo.type", "ApproverCheck");
  641. uCheckedCount.AsCount();
  642. query.Select(uCheckedCount, "UCheckedCount");
  643. var checkedCount = new SqlKata.Query("tede_flow_todo");
  644. checkedCount.WhereColumns("tede_flow_todo.FlowId", "=", "tede_notice.SourceId");
  645. checkedCount.Where("tede_flow_todo.type", "ApproverCheck");
  646. checkedCount.Where("tede_flow_todo.IsRead", true);
  647. checkedCount.AsCount();
  648. query.Select(checkedCount, "CheckedCount");
  649. var isFlowRead = new SqlKata.Query("tede_flow_todo");
  650. isFlowRead.WhereColumns("tede_flow_todo.FlowId", "=", "tede_notice.SourceId");
  651. isFlowRead.WhereColumns("tede_flow_todo.UserId", "=", "tede_middle.UserId");
  652. isFlowRead.Where("tede_flow_todo.type", "CarbonCopy");
  653. isFlowRead.Limit(1);
  654. isFlowRead.Select("IsRead");
  655. query.Select(isFlowRead, "IsFlowRead");
  656. var isFlowChecked = new SqlKata.Query("tede_flow_todo");
  657. isFlowChecked.WhereColumns("tede_flow_todo.FlowId", "=", "tede_notice.SourceId");
  658. isFlowChecked.Where("tede_flow_todo.type", "ApproverCheck");
  659. isFlowChecked.Where("tede_flow_todo.IsRead", true);
  660. isFlowChecked.Limit(1);
  661. isFlowChecked.Select("IsRead");
  662. query.Select(isFlowChecked, "IsFlowChecked");
  663. var IsMyFlowRead = new SqlKata.Query("tede_flow");
  664. IsMyFlowRead.WhereColumns("tede_flow.Id", "=", "tede_notice.SourceId");
  665. IsMyFlowRead.WhereColumns("tede_flow.UserId", "=", "tede_notice.UserId");
  666. IsMyFlowRead.Select("IsRead");
  667. query.Select(IsMyFlowRead, "IsMyFlowRead");
  668. var IsDone = new SqlKata.Query("tede_flow_todo");
  669. IsDone.WhereColumns("tede_flow_todo.UserId", "=", "tede_middle.UserId");
  670. IsDone.Where("tede_flow_todo.type", "ApproverCheck");
  671. IsDone.Limit(1);
  672. IsDone.Select("IsDone");
  673. query.Select(IsDone, "IsDone");
  674. var IsChecked = new SqlKata.Query("tede_flow_todo");
  675. IsChecked.WhereColumns("tede_flow_todo.UserId", "=", "tede_middle.UserId");
  676. IsChecked.Where("tede_flow_todo.type", "ApproverCheck");
  677. IsChecked.Limit(1);
  678. IsChecked.Select("IsChecked");
  679. query.Select(IsChecked, "IsChecked");
  680. var DoneType = new SqlKata.Query("tede_flow_todo");
  681. DoneType.WhereColumns("tede_flow_todo.UserId", "=", "tede_middle.UserId");
  682. DoneType.Where("tede_flow_todo.type", "ApproverCheck");
  683. DoneType.Limit(1);
  684. DoneType.Select("DoneType");
  685. query.Select(DoneType, "DoneType");
  686. var PraiseCount = new SqlKata.Query("tede_analyze");
  687. PraiseCount.WhereColumns("tede_analyze.SourceId", "=", "tede_middle.MiddleId");
  688. PraiseCount.Where("tede_analyze.TypeValue", noticeTypeValue);
  689. PraiseCount.Where("tede_analyze.AnalyzeType", 1);
  690. PraiseCount.AsCount();
  691. query.Select(PraiseCount, "PraiseCount");
  692. var IsLaud = new SqlKata.Query("tede_analyze");
  693. IsLaud.WhereColumns("tede_analyze.SourceId", "=", "tede_middle.MiddleId");
  694. IsLaud.Where("tede_analyze.TypeValue", noticeTypeValue);
  695. IsLaud.Where("tede_analyze.UserId", request.UserId);
  696. IsLaud.Where("tede_analyze.AnalyzeType", 1);
  697. IsLaud.Limit(1);
  698. IsLaud.AsCount();
  699. query.Select(IsLaud, "IsLaud");
  700. var CommentCount = new SqlKata.Query("tede_comment");
  701. CommentCount.WhereColumns("tede_comment.ArticleId", "=", "tede_middle.MiddleId");
  702. CommentCount.Where("tede_comment.pid", 0);
  703. CommentCount.Where("tede_comment.TypeValue", noticeTypeValue);
  704. CommentCount.AsCount();
  705. query.Select(CommentCount, "CommentCount");
  706. var RetransmissionCount = new SqlKata.Query("tede_analyze");
  707. RetransmissionCount.WhereColumns("tede_analyze.SourceId", "=", "tede_middle.MiddleId");
  708. RetransmissionCount.Where("tede_analyze.TypeValue", noticeTypeValue);
  709. RetransmissionCount.Where("tede_analyze.UserId", request.UserId);
  710. RetransmissionCount.Where("tede_analyze.AnalyzeType", 4);
  711. RetransmissionCount.AsCount();
  712. query.Select(RetransmissionCount, "RetransmissionCount");
  713. var IsRetransmission = new SqlKata.Query("tede_analyze");
  714. IsRetransmission.WhereColumns("tede_analyze.SourceId", "=", "tede_middle.MiddleId");
  715. IsRetransmission.Where("tede_analyze.TypeValue", noticeTypeValue);
  716. IsRetransmission.Where("tede_analyze.UserId", request.UserId);
  717. IsRetransmission.Where("tede_analyze.AnalyzeType", 4);
  718. IsRetransmission.Limit(1);
  719. IsRetransmission.AsCount();
  720. query.Select(IsRetransmission, "IsRetransmission");
  721. var IsCollect = new SqlKata.Query("tede_analyze");
  722. IsCollect.WhereColumns("tede_analyze.SourceId", "=", "tede_middle.MiddleId");
  723. IsCollect.Where("tede_analyze.TypeValue", noticeTypeValue);
  724. IsCollect.Where("tede_analyze.UserId", request.UserId);
  725. IsCollect.Where("tede_analyze.AnalyzeType", 3);
  726. IsCollect.Limit(1);
  727. IsCollect.AsCount();
  728. query.Select(IsCollect, "IsCollect");
  729. var CollectCount = new SqlKata.Query("tede_analyze");
  730. CollectCount.WhereColumns("tede_analyze.SourceId", "=", "tede_middle.MiddleId");
  731. CollectCount.Where("tede_analyze.TypeValue", noticeTypeValue);
  732. CollectCount.Where("tede_analyze.UserId", request.UserId);
  733. CollectCount.Where("tede_analyze.AnalyzeType", 3);
  734. CollectCount.AsCount();
  735. query.Select(CollectCount, "CollectCount");
  736. var FileCount = new SqlKata.Query("tede_middle");
  737. FileCount.WhereColumns("tede_middle.ParentId", "=", "tede_middle.Id");
  738. FileCount.Where("tede_middle.IsDelete", false);
  739. FileCount.WhereNot("tede_middle.NoticeAddresseeType", 2); ;
  740. FileCount.AsCount();
  741. query.Select(FileCount, "FileCount");
  742. var ReadCount = new SqlKata.Query("tede_addressee");
  743. ReadCount.WhereColumns("tede_addressee.SourceId", "=", "tede_middle.Id");
  744. ReadCount.Where("tede_addressee.IsRead", true);
  745. ReadCount.AsCount();
  746. query.Select(ReadCount, "ReadCount");
  747. var AllCount = new SqlKata.Query("tede_addressee");
  748. AllCount.WhereColumns("tede_addressee.SourceId", "=", "tede_middle.Id");
  749. AllCount.AsCount();
  750. query.Select(AllCount, "AllCount");
  751. var IsRead = new SqlKata.Query("tede_addressee");
  752. IsRead.WhereColumns("tede_addressee.SourceId", "=", "tede_middle.Id");
  753. IsRead.Where("tede_addressee.IsRead", true);
  754. IsRead.Where("tede_addressee.UserId", request.UserId);
  755. IsRead.Limit(1);
  756. IsRead.AsCount();
  757. query.Select(IsRead, "IsRead");
  758. var userName = new SqlKata.Query("tede_user");
  759. var tedeFlow = new SqlKata.Query("tede_flow");
  760. tedeFlow.WhereColumns("tede_flow.Id", "=", "tede_notice.SourceId");
  761. tedeFlow.Select("tede_flow.UserId");
  762. userName.WhereIn("tede_user.Id", tedeFlow);
  763. userName.Limit(1);
  764. userName.Select("Name");
  765. query.Select(userName, "Name");
  766. var IsFlowAdmin = new SqlKata.Query("tede_flow");
  767. IsFlowAdmin.WhereColumns("tede_flow.Id", "=", "tede_notice.SourceId");
  768. IsFlowAdmin.Where("tede_flow.UserId", request.UserId);
  769. IsFlowAdmin.AsCount();
  770. query.Select(IsFlowAdmin, "IsFlowAdmin");
  771. query.OrderByDesc("tede_middle.AttributeValue");
  772. query.OrderByDesc("tede_middle.IsTop");
  773. query.OrderByDesc("tede_middle.LastModifiedDate");
  774. query.OrderByDesc("tede_middle.Sort");
  775. query.ForPage(request.Page, request.PerPage);
  776. query.Select(
  777. "tede_middle.Id", "tede_middle.MiddleId", "tede_middle.IsTop"
  778. , "tede_notice.{UserId,UserName,CreatedDate,Title,Content,TypeValue,IsRecall}"
  779. , "tede_user.{AvatarUrl}"
  780. , "tede_addressee.{Id as NoticeAddresseeId, SourceId as NoticeId}");
  781. result.Items = await _middleRepository.GetAllAsync<NoticeListPageResult>(query);
  782. foreach (var item in result.Items)
  783. {
  784. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  785. item.AttributeValue = item.AttributeValue == 0 ? 1 : item.AttributeValue;
  786. }
  787. return result;
  788. }
  789. public async Task<int> CountAsync(string sql)
  790. {
  791. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  792. var database = new Database(databaseType, _connectionString);
  793. var connection = database.GetConnection();
  794. return await connection.ExecuteScalarAsync<int>(sql);
  795. }
  796. public async Task<IEnumerable<Notice>> GetAllAsync()
  797. {
  798. return await _repository.GetAllAsync();
  799. }
  800. public async Task<IEnumerable<Notice>> GetAllAsync(Query query)
  801. {
  802. return await _repository.GetAllAsync(query);
  803. }
  804. public async Task<bool> DeleteAsync(Query query)
  805. {
  806. return await _repository.DeleteAsync(query) > 0;
  807. }
  808. /// <summary>
  809. ///
  810. /// </summary>
  811. /// <returns></returns>
  812. public async Task<List<JobNoticeResult>> ElasticSearchNotice()
  813. {
  814. var sql = @"select a.Id,a.UserId,a.GuId,a.CreatedDate,b.Id,b.Title,b.Content,b.CreatedDate,c.Name,c.AvatarUrl from tede_middle a
  815. inner join tede_notice b on a.MiddleId=b.Id
  816. inner join tede_user c on c.Id=b.UserId
  817. where a.FolderType=1 and a.IsUpload=0 order by a.CreatedDate desc limit 0,100";
  818. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  819. var database = new Database(databaseType, _connectionString);
  820. var connection = database.GetConnection();
  821. var items = await connection
  822. .QueryAsync<JobNoticeResult, Notice, User, JobNoticeResult>(sql,
  823. (jobNoticeResult, notice, user) =>
  824. {
  825. jobNoticeResult.MiddleId = jobNoticeResult.Id;
  826. jobNoticeResult.Id = notice.Id;
  827. jobNoticeResult.Title = notice != null ? notice.Title : "";
  828. jobNoticeResult.Content = notice != null ? notice.Content : "";
  829. jobNoticeResult.AvatarUrl = user != null ? user.AvatarUrl : "";
  830. jobNoticeResult.Name = user != null ? user.Name : "";
  831. return jobNoticeResult;
  832. },
  833. splitOn: "Id,Id,Name");
  834. return items.ToList();
  835. }
  836. }
  837. }