NoticeRepository.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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(SqlKata.Query query, SqlKata.Query countQuery)
  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(query, countQuery);
  559. }
  560. /// <summary>
  561. /// 获取通知列表
  562. /// </summary>
  563. /// <param name="request"></param>
  564. /// <returns></returns>
  565. public async Task<PagedList<NoticeListPageResult>> GetNewNoticeListPageAsync(SqlKata.Query query, SqlKata.Query countQuery)
  566. {
  567. var result = new PagedList<NoticeListPageResult>();
  568. result.Items = await _middleRepository.GetAllAsync<NoticeListPageResult>(query);
  569. result.Total = await _middleRepository.CountAsync(countQuery);
  570. foreach (var item in result.Items)
  571. {
  572. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  573. item.AttributeValue = item.AttributeValue == 0 ? 1 : item.AttributeValue;
  574. }
  575. return result;
  576. }
  577. public async Task<int> CountAsync(string sql)
  578. {
  579. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  580. var database = new Database(databaseType, _connectionString);
  581. var connection = database.GetConnection();
  582. return await connection.ExecuteScalarAsync<int>(sql);
  583. }
  584. public async Task<IEnumerable<Notice>> GetAllAsync()
  585. {
  586. return await _repository.GetAllAsync();
  587. }
  588. public async Task<IEnumerable<Notice>> GetAllAsync(Query query)
  589. {
  590. return await _repository.GetAllAsync(query);
  591. }
  592. public async Task<bool> DeleteAsync(Query query)
  593. {
  594. return await _repository.DeleteAsync(query) > 0;
  595. }
  596. /// <summary>
  597. ///
  598. /// </summary>
  599. /// <returns></returns>
  600. public async Task<List<JobNoticeResult>> ElasticSearchNotice()
  601. {
  602. 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
  603. inner join tede_notice b on a.MiddleId=b.Id
  604. inner join tede_user c on c.Id=b.UserId
  605. where a.FolderType=1 and a.IsUpload=0 order by a.CreatedDate desc limit 0,100";
  606. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  607. var database = new Database(databaseType, _connectionString);
  608. var connection = database.GetConnection();
  609. var items = await connection
  610. .QueryAsync<JobNoticeResult, Notice, User, JobNoticeResult>(sql,
  611. (jobNoticeResult, notice, user) =>
  612. {
  613. jobNoticeResult.MiddleId = jobNoticeResult.Id;
  614. jobNoticeResult.Id = notice.Id;
  615. jobNoticeResult.Title = notice != null ? notice.Title : "";
  616. jobNoticeResult.Content = notice != null ? notice.Content : "";
  617. jobNoticeResult.AvatarUrl = user != null ? user.AvatarUrl : "";
  618. jobNoticeResult.Name = user != null ? user.Name : "";
  619. return jobNoticeResult;
  620. },
  621. splitOn: "Id,Id,Name");
  622. return items.ToList();
  623. }
  624. }
  625. }