飞星
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

186 rader
5.7 KiB

  1. using System;
  2. using System.Linq;
  3. using System.Net.Cache;
  4. using Microsoft.EntityFrameworkCore;
  5. using Permission.Entity.DbContext;
  6. using Permission.Entity.News;
  7. using Permission.Infrastructure.Repositories;
  8. using Permission.Infrastructure.WebControls;
  9. using Permission.Service.IServices;
  10. using Permission.Utils.Validate;
  11. namespace Permission.Service.Services
  12. {
  13. public class NewsInfoService : INewsInfoService
  14. {
  15. private readonly IUnitOfWork _unitOfWork;
  16. private readonly PermissionContext _dbContext;
  17. private readonly IBaseRepository<NewsInfo> _newsInfoRepository;
  18. public NewsInfoService(IUnitOfWork unitOfWork, PermissionContext dbContext, IBaseRepository<NewsInfo> newsInfoRepository)
  19. {
  20. this._unitOfWork = unitOfWork;
  21. this._dbContext = dbContext;
  22. this._newsInfoRepository = newsInfoRepository;
  23. }
  24. public bool AddModel(NewsInfo model)
  25. {
  26. try
  27. {
  28. model.GuidId = Guid.NewGuid().ToString();
  29. model.IsEnabled = model.IsEnabled != null;
  30. model.DeleteMark = false;
  31. model.CreateTime = DateTime.Now;
  32. var res = _newsInfoRepository.Save(model);
  33. return res;
  34. }
  35. catch (Exception ex)
  36. {
  37. throw ex;
  38. }
  39. }
  40. /// <summary>
  41. /// 编辑内容
  42. /// </summary>
  43. /// <param name="model"></param>
  44. /// <returns></returns>
  45. public bool ModifyModel(NewsInfo model)
  46. {
  47. try
  48. {
  49. var newinfo = _newsInfoRepository.Get(model.Id);
  50. newinfo.SortCode = model.SortCode;
  51. newinfo.InfoLevel = model.InfoLevel;
  52. newinfo.NewsTitle = model.NewsTitle;
  53. newinfo.SubTitle = model.SubTitle;
  54. newinfo.PushContent = model.PushContent;
  55. newinfo.ModifyTime = model.ModifyTime;
  56. var res = _newsInfoRepository.Update(newinfo);
  57. return res;
  58. }
  59. catch (Exception ex)
  60. {
  61. throw ex;
  62. }
  63. }
  64. /// <summary>
  65. /// Modifies the model.
  66. /// </summary>
  67. /// <param name="newId">The new identifier.</param>
  68. /// <param name="op">The op.</param>
  69. /// <returns></returns>
  70. public bool ModifyModel(int newId, int op)
  71. {
  72. try
  73. {
  74. var newinfo = _newsInfoRepository.Get(newId);
  75. newinfo.Status = op == 1 ? 1 : 0;
  76. newinfo.ModifyTime = DateTime.Now;
  77. var res = _newsInfoRepository.Update(newinfo);
  78. return res;
  79. }
  80. catch (Exception ex)
  81. {
  82. throw ex;
  83. }
  84. }
  85. public bool DeleteModel(int key)
  86. {
  87. bool flag = false;
  88. try
  89. {
  90. if (!key.IsEmpty() || key > 0)
  91. {
  92. var model = _newsInfoRepository.Get(key);
  93. _newsInfoRepository.Delete(model);
  94. flag = true;
  95. }
  96. else
  97. {
  98. flag = false;
  99. }
  100. return flag;
  101. }
  102. catch (Exception ex)
  103. {
  104. throw ex;
  105. }
  106. }
  107. public NewsInfo GetNewsInfoByKey(int key)
  108. {
  109. try
  110. {
  111. if (!key.IsEmpty() || key > 0)
  112. {
  113. var data = _newsInfoRepository.Get(key);
  114. return data;
  115. }
  116. return null;
  117. }
  118. catch (Exception e)
  119. {
  120. throw e;
  121. }
  122. }
  123. /// <summary>
  124. /// 条件分页查询
  125. /// </summary>
  126. /// <param name="typeid">新闻消息类型 -1表示查询所有</param>
  127. /// <param name="keyword"></param>
  128. /// <param name="pagesize"></param>
  129. /// <param name="pageindex"></param>
  130. /// <returns></returns>
  131. public Page<NewsInfo> PageList(int typeid, string keyword, int pagesize, int pageindex)
  132. {
  133. try
  134. {
  135. int total = 0;
  136. Page<NewsInfo> data;
  137. if (keyword.IsEmpty())
  138. {
  139. data = _newsInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(d => d.SortCode).ThenByDescending(ur => ur.CreateTime),
  140. u => u,
  141. out total,
  142. u => u.NewsTypeId == typeid && u.DeleteMark == false);
  143. return data;
  144. }
  145. data = _newsInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(d => d.SortCode).ThenByDescending(ur => ur.CreateTime),
  146. u => u,
  147. out total,
  148. u => u.NewsTitle.Contains(keyword) && u.NewsTypeId == typeid && u.DeleteMark == false);
  149. return data;
  150. }
  151. catch (Exception e)
  152. {
  153. throw e;
  154. }
  155. }
  156. public Page<NewsInfo> NewsListPageList(int pagesize, int pageindex)
  157. {
  158. try
  159. {
  160. int total = 0;
  161. Page<NewsInfo> data;
  162. data = _newsInfoRepository.PageList(pageindex, pagesize, us => us.OrderBy(d => d.SortCode).ThenByDescending(ur => ur.CreateTime),
  163. u => u,
  164. out total,
  165. u => u.Status == 1 && u.DeleteMark == false);
  166. return data;
  167. }
  168. catch (Exception e)
  169. {
  170. throw e;
  171. }
  172. }
  173. }
  174. }