NoticeRepository.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. SimplenessTtile = request.SimplenessTitle
  92. };
  93. noticeId = await _repository.InsertAsync(notice);
  94. transactionScope.Complete();
  95. }
  96. }
  97. catch (Exception e)
  98. {
  99. throw new BusinessException(e.Message);
  100. }
  101. return noticeId;
  102. }
  103. /// <summary>
  104. /// 根据用户ID查询
  105. /// </summary>
  106. /// <param name="request"></param>
  107. /// <returns></returns>
  108. public async Task<PagedList<Notice>> GetNoticeByUserIdAsync(NoticePageSearchRequest request)
  109. {
  110. var query = Q.NewQuery();
  111. if (request.UserId > 0)
  112. {
  113. query.Where(nameof(Addressee.UserId), request.UserId);
  114. }
  115. if (request.SearchType == 0)
  116. {
  117. query.Where(nameof(Addressee.UserId), request.UserId);
  118. }
  119. if (request.SearchType == 1)
  120. {
  121. query.Where(nameof(Addressee.IsAdmin), true);
  122. }
  123. if (request.SearchType == 2)
  124. {
  125. query.Where(nameof(Addressee.NoticeAddresseeType), NoticeTypeConst.Draft);
  126. }
  127. if (request.SearchType == 3)
  128. {
  129. query.Where(nameof(Addressee.IsDelete), true);
  130. }
  131. if (!string.IsNullOrEmpty(request.Keyword))
  132. {
  133. var like = $"%{request.Keyword}%";
  134. query.WhereLike(nameof(Notice.Title), like);
  135. }
  136. var pagedList = new PagedList<Notice>
  137. {
  138. Total = await _repository.CountAsync(query)
  139. };
  140. var list = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  141. pagedList.Items = list;
  142. return pagedList;
  143. }
  144. /// <summary>
  145. /// admin根据用户ID查询
  146. /// </summary>
  147. /// <param name="request"></param>
  148. /// <returns></returns>
  149. public async Task<PagedList<NoticeListPageResult>> GetAdminNoticeByUserIdAsync(NoticePageSearchRequest request)
  150. {
  151. //用户收件人
  152. var noticeQuery = Q.NewQuery();
  153. if (request.SearchType == 2)
  154. {
  155. noticeQuery.Where(nameof(Notice.NoticeType), NoticeTypeConst.Draft);
  156. }
  157. noticeQuery.Where(nameof(Notice.IsDelete), request.SearchType == 3);
  158. if (!string.IsNullOrEmpty(request.Keyword))
  159. {
  160. noticeQuery.WhereLike(nameof(Notice.Title), $"%{request.Keyword}%");
  161. }
  162. var noticeList = await _repository.GetAllAsync(noticeQuery);
  163. var result = new PagedList<NoticeListPageResult>();
  164. result.Total = await _repository.CountAsync(noticeQuery);
  165. var notices = await _repository.GetAllAsync(noticeQuery.OrderByDesc(nameof(Notice.CreatedDate)).ForPage(request.Page, request.PerPage));
  166. var noticeListPageResults = new List<NoticeListPageResult>();
  167. foreach (var notice in notices)
  168. {
  169. var noticeListPageResultEntity = _mapper.Map<NoticeListPageResult>(notice);
  170. var user = await _userRepository.GetAsync(notice.UserId);
  171. if (user != null)
  172. {
  173. noticeListPageResultEntity.AvatarUrl = StringUtils.AddDomain(user.AvatarUrl);
  174. }
  175. //阅读数量
  176. noticeListPageResultEntity.ReadCount = await _noticeAddresseeRepository.CountAsync(
  177. Q.Where(nameof(Addressee.SourceId), notice.Id)
  178. .Where(nameof(Addressee.IsRead), true).Where(nameof(Addressee.IsAdmin), false));
  179. //收件人数量
  180. noticeListPageResultEntity.AllCount = await _noticeAddresseeRepository.CountAsync(
  181. Q.Where(nameof(Addressee.SourceId), notice.Id)
  182. .Where(nameof(Addressee.IsAdmin), false));
  183. noticeListPageResults.Add(noticeListPageResultEntity);
  184. }
  185. result.Items = noticeListPageResults;
  186. return result;
  187. }
  188. /// <summary>
  189. /// 管理员删除
  190. /// </summary>
  191. /// <param name="id"></param>
  192. /// <returns></returns>
  193. public async Task<bool> AdminDeleteAsync(int id)
  194. {
  195. try
  196. {
  197. var notice = await _repository.GetAsync(id);
  198. if (notice == null)
  199. throw new BusinessException("通知不存在");
  200. using (TransactionScope transactionScope = new TransactionScope())
  201. {
  202. //通知
  203. await _repository.DeleteAsync(id);
  204. //抄送
  205. await _noticeCcRepository.DeleteAsync(Q.Where(nameof(Cc.SourceId), id));
  206. //收件人
  207. await _noticeAddresseeRepository.DeleteAsync(Q.Where(nameof(Addressee.SourceId), id));
  208. transactionScope.Complete();
  209. }
  210. }
  211. catch (Exception e)
  212. {
  213. throw new BusinessException(e.Message);
  214. }
  215. return true;
  216. }
  217. /// <summary>
  218. /// 自己删除
  219. /// </summary>
  220. /// <param name="request"></param>
  221. /// <returns></returns>
  222. public async Task<bool> DeleteAsync(NoticeDeRequest request)
  223. {
  224. try
  225. {
  226. using (TransactionScope transactionScope = new TransactionScope())
  227. {
  228. //var notice = await _repository.GetAsync(request.NoticeId);
  229. //if (notice == null)
  230. // throw new BusinessException("通知不存在");
  231. //notice.IsDelete = true;
  232. //await _repository.UpdateAsync(notice);
  233. //
  234. await _noticeAddresseeRepository.UpdateAsync(Q.Set(nameof(Addressee.IsDelete), true)
  235. .WhereIn(nameof(Addressee.Id), request.MiddleIds));
  236. transactionScope.Complete();
  237. }
  238. }
  239. catch (Exception e)
  240. {
  241. throw new BusinessException(e.Message);
  242. }
  243. return true;
  244. }
  245. /// <summary>
  246. /// 恢复删除通知
  247. /// </summary>
  248. /// <param name="request"></param>
  249. /// <returns></returns>
  250. public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
  251. {
  252. try
  253. {
  254. using (TransactionScope transactionScope = new TransactionScope())
  255. {
  256. //
  257. await _noticeAddresseeRepository.UpdateAsync(Q.Set(nameof(Addressee.IsDelete), false)
  258. .Where(nameof(Addressee.Id), request.MiddleIds));
  259. transactionScope.Complete();
  260. }
  261. }
  262. catch (Exception e)
  263. {
  264. throw new BusinessException(e.Message);
  265. }
  266. return true;
  267. }
  268. /// <summary>
  269. /// 通知修改
  270. /// </summary>
  271. /// <param name="id"></param>
  272. /// <param name="request"></param>
  273. /// <returns></returns>
  274. public async Task<bool> UpdateNoticeAsync(Notice notice)
  275. {
  276. return await _repository.UpdateAsync(notice);
  277. }
  278. /// <summary>
  279. /// 通知修改
  280. /// </summary>
  281. /// <param name="id"></param>
  282. /// <param name="request"></param>
  283. /// <returns></returns>
  284. public async Task<bool> UpdateAsync(SqlKata.Query query)
  285. {
  286. return await _repository.UpdateAsync(query) > 0;
  287. }
  288. /// <summary>
  289. /// 通知修改
  290. /// </summary>
  291. /// <param name="id"></param>
  292. /// <param name="request"></param>
  293. /// <returns></returns>
  294. public async Task<bool> UpdateAsync(Entity.Notice notice)
  295. {
  296. return await _repository.UpdateAsync(notice);
  297. }
  298. /// <summary>
  299. /// 获取通知列表
  300. /// </summary>
  301. /// <param name="request"></param>
  302. /// <returns></returns>
  303. public async Task<PagedList<NoticeListPageResult>> GetNoticeListPageAsync(SqlKata.Query query, SqlKata.Query countQuery)
  304. {
  305. // //用户收件人
  306. // string sb = "";
  307. // if (request.SearchType == 1)
  308. // sb += " AND a.IsAdmin = 1 and a.NoticeAddresseeType=1 AND a.IsDelete = 0";
  309. // else if (request.SearchType == 2)
  310. // sb += " and a.NoticeAddresseeType=2 AND a.AttributeValue=1 AND a.IsDelete = 0";
  311. // else if (request.SearchType == 3)
  312. // sb += " AND a.IsDelete = 1 AND a.AttributeValue=1";
  313. // else if (request.SearchType > 3)
  314. // sb += $"and a.NoticeAddresseeType<>2 AND b.TypeValue={request.SearchType} AND a.IsDelete = 0";
  315. // else
  316. // sb += " AND a.IsDelete = 0 AND a.NoticeAddresseeType != 2";
  317. // if (!string.IsNullOrEmpty(request.Keyword))
  318. // {
  319. // if (request.NoticeFolderId > 0)
  320. // sb += $@" AND a.ParentId ={request.NoticeFolderId} AND a.AttributeValue != 2 and (b.Title LIKE '%{request.Keyword}%'
  321. // OR b.Content LIKE '%{request.Keyword}%'
  322. // OR c.Name LIKE '%{request.Keyword}%'
  323. // OR d.UserName LIKE '%{request.Keyword}%'
  324. // OR a.CreatedDate LIKE '%{request.Keyword}%' or a.MiddleId in (SELECT
  325. // g.SourceId
  326. // FROM
  327. // tede_addressee g
  328. // INNER JOIN
  329. // tede_user h ON g.UserId = h.Id
  330. // WHERE
  331. // g.SourceId =a.MiddleId AND h.Name LIKE '%{request.Keyword}%'))";
  332. // else
  333. // sb += $@" AND a.AttributeValue != 2 AND (b.Title LIKE '%{request.Keyword}%'
  334. // OR b.Content LIKE '%{request.Keyword}%'
  335. // OR c.Name LIKE '%{request.Keyword}%'
  336. // OR d.UserName LIKE '%{request.Keyword}%'
  337. // OR a.CreatedDate LIKE '%{request.Keyword}%' or a.MiddleId in (SELECT
  338. // g.SourceId
  339. // FROM
  340. // tede_addressee g
  341. // INNER JOIN
  342. // tede_user h ON g.UserId = h.Id
  343. // WHERE
  344. // g.SourceId =a.MiddleId AND h.Name LIKE '%{request.Keyword}%'))";
  345. // }
  346. // else
  347. // {
  348. // var searchList = new List<int> { 1, 2, 3 };
  349. // if (!searchList.Contains(request.SearchType))
  350. // sb += " AND a.ParentId = " + request.NoticeFolderId;
  351. // }
  352. // var noticeTypeValue = GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode();
  353. // var sql = $@"SELECT a.*,
  354. // (SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=10 and DataSourceId=a.MiddleId) as EnjoyUser,
  355. // (SELECT
  356. // GROUP_CONCAT(Name
  357. // SEPARATOR '、')
  358. // FROM
  359. // tede_user
  360. // WHERE
  361. // Id IN (SELECT
  362. // UserId
  363. // FROM
  364. // tede_flow_todo
  365. // WHERE
  366. // FlowId = b.SourceId
  367. // AND type = 'ApproverCheck')) AS FlowUserName,
  368. // (SELECT
  369. // COUNT(1)
  370. // FROM
  371. // tede_flow_todo
  372. // WHERE
  373. // FlowId = b.SourceId
  374. // AND type = 'ApproverCheck') AS UCheckedCount,
  375. // (SELECT
  376. // COUNT(1)
  377. // FROM
  378. // tede_flow_todo
  379. // WHERE
  380. // FlowId = b.SourceId
  381. // AND type = 'ApproverCheck'
  382. // AND IsRead = 1) AS CheckedCount,
  383. // (SELECT
  384. // IsRead
  385. // FROM
  386. // tede_flow_todo
  387. // WHERE
  388. // UserId = a.UserId
  389. // AND FlowId = b.SourceId
  390. // AND Type = 'CarbonCopy'
  391. // LIMIT 1) AS IsFlowRead,
  392. // (SELECT
  393. // IsRead
  394. // FROM
  395. // tede_flow_todo
  396. // WHERE
  397. // UserId = b.UserId
  398. // AND FlowId = b.SourceId
  399. // AND Type = 'ApproverCheck' and IsRead=1
  400. // LIMIT 1) AS IsFlowChecked,
  401. // (SELECT
  402. // IsRead
  403. // FROM
  404. // tede_flow
  405. // WHERE
  406. // id = b.SourceId AND UserId = b.UserId) AS IsMyFlowRead,
  407. // (select IsRecall from tede_notice where id=a.MiddleId) as IsRecall,
  408. // (SELECT
  409. // IsDone
  410. // FROM
  411. // tede_flow_todo
  412. // WHERE
  413. // type = 'ApproverCheck'
  414. // AND UserId = a.UserId
  415. // LIMIT 1) AS IsDone,
  416. // (SELECT
  417. // IsChecked
  418. // FROM
  419. // tede_flow_todo
  420. // WHERE
  421. // type = 'ApproverCheck'
  422. // AND UserId = a.UserId
  423. // LIMIT 1) AS IsChecked,
  424. // (SELECT
  425. // DoneType
  426. // FROM
  427. // tede_flow_todo
  428. // WHERE
  429. // type = 'ApproverCheck'
  430. // AND UserId = a.UserId
  431. // LIMIT 1) AS DoneType,
  432. // (SELECT
  433. // COUNT(1)
  434. // FROM
  435. // tede_analyze
  436. // WHERE
  437. // TypeValue = {noticeTypeValue}
  438. // AND SourceId = a.MiddleId
  439. // AND AnalyzeType = 1) AS PraiseCount,
  440. // (SELECT
  441. // COUNT(1)
  442. // FROM
  443. // tede_analyze
  444. // WHERE
  445. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  446. // AND SourceId = a.MiddleId
  447. // AND AnalyzeType = 1
  448. // LIMIT 0 , 1) AS IsLaud,
  449. // (SELECT
  450. // COUNT(1)
  451. // FROM
  452. // tede_comment
  453. // WHERE
  454. // ArticleId = a.MiddleId and pid=0
  455. // AND TypeValue = {noticeTypeValue}) AS CommentCount,
  456. // (SELECT
  457. // COUNT(1)
  458. // FROM
  459. // tede_analyze
  460. // WHERE
  461. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  462. // AND SourceId = a.MiddleId
  463. // AND AnalyzeType = 4) AS RetransmissionCount,
  464. // (SELECT
  465. // COUNT(1)
  466. // FROM
  467. // tede_analyze
  468. // WHERE
  469. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  470. // AND SourceId = a.MiddleId
  471. // AND AnalyzeType = 4
  472. // LIMIT 0 , 1) AS IsRetransmission,
  473. // (SELECT
  474. // COUNT(1)
  475. // FROM
  476. // tede_analyze
  477. // WHERE
  478. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  479. // AND SourceId = a.MiddleId
  480. // AND AnalyzeType = 4
  481. // LIMIT 0 , 1) AS IsCollect,
  482. // (SELECT
  483. // COUNT(1)
  484. // FROM
  485. // tede_analyze
  486. // WHERE
  487. // UserId = {request.UserId} AND TypeValue = {noticeTypeValue}
  488. // AND SourceId = a.MiddleId
  489. // AND AnalyzeType = 4) AS CollectCount,
  490. // (SELECT
  491. // COUNT(1)
  492. // FROM
  493. // tede_middle
  494. // WHERE
  495. // ParentId = a.Id and IsDelete=0 and NoticeAddresseeType<>2) as FileCount,(SELECT
  496. // COUNT(1)
  497. // FROM
  498. // tede_addressee
  499. // WHERE
  500. // SourceId = a.MiddleId AND IsRead = 1) as ReadCount,(SELECT
  501. // COUNT(1)
  502. // FROM
  503. // tede_addressee
  504. // WHERE
  505. // SourceId = a.MiddleId ) as AllCount,(SELECT
  506. // COUNT(1)
  507. // FROM
  508. // tede_addressee
  509. // WHERE
  510. // 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
  511. // Name
  512. // FROM
  513. // tede_user
  514. // WHERE
  515. // id = (SELECT
  516. // userId
  517. // FROM
  518. // tede_flow
  519. // WHERE
  520. // id = b.SourceId)) as UserName
  521. // ,(select count(1) from tede_flow where id=b.SourceId and UserId={request.UserId}) as IsFlowAdmin,b.UserId,a.IsTop
  522. // FROM
  523. // tede_middle a
  524. // left JOIN
  525. // tede_notice b ON a.MiddleId = b.Id
  526. // left JOIN
  527. // tede_user c ON b.UserId = c.Id
  528. // left JOIN
  529. // tede_addressee d ON d.Id = a.MiddleSonId
  530. // WHERE a.IsRecall=0 and
  531. // a.UserId = {request.UserId} and a.FolderType={noticeTypeValue}
  532. // {sb}
  533. // ORDER BY a.AttributeValue desc,a.IsTop desc,a.LastModifiedDate desc
  534. // LIMIT {(request.Page - 1) * request.PerPage},{request.PerPage}";
  535. // var countSql = string.Format(@"SELECT
  536. // count(1)
  537. // FROM
  538. // tede_middle a
  539. // left JOIN
  540. // tede_notice b ON a.MiddleId = b.Id
  541. // left JOIN
  542. // tede_user c ON a.UserId = c.Id
  543. // left JOIN
  544. // tede_addressee d ON d.Id = a.MiddleSonId
  545. // WHERE a.IsRecall=0 and
  546. // a.UserId = {1} and a.FolderType={2}
  547. // {0}", sb, request.UserId, noticeTypeValue);
  548. // var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  549. // var database = new Database(databaseType, _connectionString);
  550. // var connection = database.GetConnection();
  551. // var items = await connection.QueryAsync<NoticeListPageResult>(sql);
  552. // foreach (var item in items)
  553. // {
  554. // item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  555. // if (item.AttributeValue == 3)
  556. // {
  557. // var query = Q.NewQuery();
  558. // query.Where(nameof(Entity.AdminVerify.AdminId), request.UserId);
  559. // query.Where(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.UDispose.GetHashCode());
  560. // item.UReadCount = await _adminVerifyRepository.CountAsync(query);
  561. // }
  562. // }
  563. // PagedList<NoticeListPageResult> result = new PagedList<NoticeListPageResult>
  564. // {
  565. // Total = await CountAsync(countSql),
  566. // Items = items
  567. // };
  568. // return result;
  569. return await GetNewNoticeListPageAsync(query, countQuery);
  570. }
  571. /// <summary>
  572. /// 获取通知列表
  573. /// </summary>
  574. /// <param name="request"></param>
  575. /// <returns></returns>
  576. public async Task<PagedList<NoticeListPageResult>> GetNewNoticeListPageAsync(SqlKata.Query query, SqlKata.Query countQuery)
  577. {
  578. var result = new PagedList<NoticeListPageResult>();
  579. result.Items = await _middleRepository.GetAllAsync<NoticeListPageResult>(query);
  580. result.Total = await _middleRepository.CountAsync(countQuery);
  581. foreach (var item in result.Items)
  582. {
  583. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  584. item.AttributeValue = item.AttributeValue == 0 ? 1 : item.AttributeValue;
  585. }
  586. return result;
  587. }
  588. public async Task<int> CountAsync(string sql)
  589. {
  590. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  591. var database = new Database(databaseType, _connectionString);
  592. var connection = database.GetConnection();
  593. return await connection.ExecuteScalarAsync<int>(sql);
  594. }
  595. public async Task<IEnumerable<Notice>> GetAllAsync()
  596. {
  597. return await _repository.GetAllAsync();
  598. }
  599. public async Task<IEnumerable<Notice>> GetAllAsync(Query query)
  600. {
  601. return await _repository.GetAllAsync(query);
  602. }
  603. public async Task<bool> DeleteAsync(Query query)
  604. {
  605. return await _repository.DeleteAsync(query) > 0;
  606. }
  607. /// <summary>
  608. ///
  609. /// </summary>
  610. /// <returns></returns>
  611. public async Task<List<JobNoticeResult>> ElasticSearchNotice()
  612. {
  613. 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
  614. inner join tede_notice b on a.MiddleId=b.Id
  615. inner join tede_user c on c.Id=b.UserId
  616. where a.FolderType=1 and a.IsUpload=0 order by a.CreatedDate desc limit 0,100";
  617. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  618. var database = new Database(databaseType, _connectionString);
  619. var connection = database.GetConnection();
  620. var items = await connection
  621. .QueryAsync<JobNoticeResult, Notice, User, JobNoticeResult>(sql,
  622. (jobNoticeResult, notice, user) =>
  623. {
  624. jobNoticeResult.MiddleId = jobNoticeResult.Id;
  625. jobNoticeResult.Id = notice.Id;
  626. jobNoticeResult.Title = notice != null ? notice.Title : "";
  627. jobNoticeResult.Content = notice != null ? notice.Content : "";
  628. jobNoticeResult.AvatarUrl = user != null ? user.AvatarUrl : "";
  629. jobNoticeResult.Name = user != null ? user.Name : "";
  630. return jobNoticeResult;
  631. },
  632. splitOn: "Id,Id,Name");
  633. return items.ToList();
  634. }
  635. }
  636. }