郑州市第一人民医院

Sys_NewsRepository.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using MadRunFabric.Common;
  2. using MadRunFabric.Common.Options;
  3. using MadRunFabric.Model;
  4. using MessageApi.IRepositories;
  5. using MessageApi.Model.Dto;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using MongoDB.Driver;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. namespace MessageApi.Repositories
  13. {
  14. public class Sys_NewsRepository : BaseRepository<Sys_News, string>, ISys_NewsRepository
  15. {
  16. protected readonly ILogger<BaseRepository<Sys_News, string>> _logger;
  17. protected readonly IMongoCollection<Pro_Project_Info> _collection_pro_project_info;
  18. protected readonly IMongoCollection<Sys_DictionaryValue> _collection_sys_dictionaryvalue;
  19. protected readonly IMongoCollection<Sys_Provinces> _collection_sys_provinces;
  20. protected readonly IMongoCollection<Sys_City> _collection_sys_city;
  21. public Sys_NewsRepository(IOptions<MongodbOptions> settings, ILogger<BaseRepository<Sys_News, string>> logger) : base(settings, logger)
  22. {
  23. _logger = logger;
  24. _collection_pro_project_info= _context.GetCollection<Pro_Project_Info>();
  25. _collection_sys_dictionaryvalue = _context.GetCollection<Sys_DictionaryValue>();
  26. _collection_sys_provinces = _context.GetCollection<Sys_Provinces>();
  27. _collection_sys_city = _context.GetCollection<Sys_City>();
  28. }
  29. /// <summary>
  30. /// 列表 -- 关联查询
  31. /// </summary>
  32. /// <param name="keyword"></param>
  33. /// <param name="projectid"></param>
  34. /// <param name="typeid"></param>
  35. /// <param name="pageIndex"></param>
  36. /// <param name="pageSize"></param>
  37. /// <param name="recordCount"></param>
  38. /// <returns></returns>
  39. public IEnumerable<SysNewsDto> GetListsByPage(string keyword, string projectid, string typeid, int state, int level, UserAccountInfoModel userinfo,string rolecode, int pageIndex, int pageSize, out int recordCount)
  40. {
  41. DateTime dtnow = DateTime.Now;
  42. var query = from p in _collection.AsQueryable()
  43. join pro in _collection_pro_project_info.AsQueryable() on p.projectid equals pro.id into proinfo
  44. join dic in _collection_sys_dictionaryvalue.AsQueryable() on p.typeid equals dic.id into dicinfo
  45. join city in _collection_sys_city.AsQueryable() on p.city equals city.citycode into cityDefa
  46. join provinces in _collection_sys_provinces.AsQueryable() on p.province equals provinces.provincecode into provincesDefa
  47. where p.isdelete == 0 //&& (p.endtime == null || p.endtime <= dtnow) //有效期时间 //&& p.state == 0
  48. select new SysNewsDto
  49. {
  50. id = p.id,
  51. province = p.province,
  52. city = p.city,
  53. cityname = cityDefa.Count() > 0 ? cityDefa.First().cityname : "所有市",
  54. provincename = provincesDefa.Count() > 0 ? provincesDefa.First().provincename : "所有省",
  55. projectid = p.projectid,
  56. projectname = proinfo.Count() > 0 ? proinfo.First().project_name : "所有项目",
  57. typeid = p.typeid,
  58. typename = dicinfo.Count() > 0 ? dicinfo.First().name : "",
  59. title = p.title,
  60. content = p.content,
  61. headimg = p.headimg,
  62. headsmallimg = p.headsmallimg,
  63. level = p.level,
  64. sort = p.sort,
  65. state = p.state,
  66. publishtype = p.publishtype,
  67. timingtime = p.timingtime,
  68. endtime = p.endtime,
  69. rolecode = p.rolecode,
  70. createtime = p.createtime,
  71. createuser = p.createuser,
  72. createusername = p.createusername
  73. };
  74. #region 条件
  75. if (!string.IsNullOrEmpty(keyword))
  76. query = query.Where(it => it.title.Contains(keyword) || it.content.Contains(keyword));
  77. if (!string.IsNullOrEmpty(projectid))
  78. query = query.Where(it => it.projectid == projectid || it.projectid == null);
  79. if (!string.IsNullOrEmpty(typeid))
  80. query = query.Where(it => it.typeid == typeid);
  81. if (level != 0)
  82. query = query.Where(it => it.level == level);
  83. if (state >= 0)
  84. {
  85. //有效期时间/显示不显示
  86. query = query.Where(it => it.state == state);
  87. if (state == 2)
  88. query = query.Where(it => it.endtime < dtnow); //已到期
  89. else
  90. query = query.Where(it => (it.endtime == null || it.endtime >= dtnow)); //未到期 和 所有
  91. }
  92. if (userinfo.isallproject == 0)
  93. {
  94. query = query.Where(it => userinfo.projectlist.Contains(it.projectid));
  95. //文章 角色权限(空值所有人都可以看)
  96. if (!string.IsNullOrEmpty(rolecode))
  97. query = query.Where(it => it.rolecode == "" || it.rolecode == null || it.rolecode.Contains(rolecode));
  98. }
  99. #endregion
  100. recordCount = query.Count();
  101. var list = query.OrderByDescending(it => it.createtime).Skip((pageIndex - 1) * pageSize).Take(pageSize);
  102. return list;
  103. }
  104. /// <summary>
  105. /// Linq 关联查询详情
  106. /// </summary>
  107. /// <param name="id"></param>
  108. /// <returns></returns>
  109. public object GetDetails(string id)
  110. {
  111. var query = from p in _collection.AsQueryable()
  112. join pro in _collection_pro_project_info.AsQueryable() on p.projectid equals pro.id into proinfo
  113. join dic in _collection_sys_dictionaryvalue.AsQueryable() on p.typeid equals dic.id into dicinfo
  114. join city in _collection_sys_city.AsQueryable() on p.city equals city.citycode into cityDefa
  115. join provinces in _collection_sys_provinces.AsQueryable() on p.province equals provinces.provincecode into provincesDefa
  116. where p.isdelete == 0 && p.id == id
  117. select new
  118. {
  119. p.id,
  120. p.province,
  121. p.city,
  122. cityname = cityDefa.Count() > 0 ? cityDefa.First().cityname : "所有市",
  123. provincename = provincesDefa.Count() > 0 ? provincesDefa.First().provincename : "所有省",
  124. p.projectid,
  125. projectname = proinfo.Count() > 0 ? proinfo.First().project_name : "所有项目",
  126. p.typeid,
  127. typename = dicinfo.Count() > 0 ? dicinfo.First().name : "",
  128. p.title,
  129. p.content,
  130. p.headimg,
  131. p.headsmallimg,
  132. p.files,
  133. p.level,
  134. p.sort,
  135. p.state,
  136. p.publishtype,
  137. p.timingtime,
  138. p.endtime,
  139. p.rolecode,
  140. p.createtime,
  141. createuser = p.createuser,
  142. createusername = p.createusername
  143. };
  144. var model = query.FirstOrDefault();
  145. return model;
  146. }
  147. }
  148. }