You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewsService.cs 11 KiB

4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using Learun.DataBase.Repository;
  2. using Learun.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Text;
  7. using Nancy.Json;
  8. using Spire.Pdf.General.Render.Decode.Jpeg2000.Icc;
  9. using static Learun.Application.OA.NewsEntity;
  10. using Nancy.Helpers;
  11. using System.Configuration;
  12. using System.Threading.Tasks;
  13. using Newtonsoft.Json;
  14. using System.Linq;
  15. namespace Learun.Application.OA
  16. {
  17. /// <summary>
  18. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  19. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  20. /// 创建人:陈彬彬
  21. /// 日 期:2017.04.17
  22. /// 描 述:新闻管理
  23. /// </summary>
  24. public class NewsService : RepositoryFactory
  25. {
  26. #region 获取数据
  27. /// <summary>
  28. /// 新闻列表
  29. /// </summary>
  30. /// <param name="pagination">分页参数</param>
  31. /// <param name="categoryId">类型</param>
  32. /// <param name="keyword">关键词</param>
  33. /// <returns></returns>
  34. public IEnumerable<NewsEntity> GetPageList(Pagination pagination, string keyword)
  35. {
  36. try
  37. {
  38. var strSql = new StringBuilder();
  39. strSql.Append("SELECT * FROM LR_OA_News t WHERE t.F_TypeId = 1 ");
  40. if (!string.IsNullOrEmpty(keyword))
  41. {
  42. strSql.Append(" AND F_FullHead like @keyword");
  43. }
  44. return this.BaseRepository().FindList<NewsEntity>(strSql.ToString(), new { keyword = "%" + keyword + "%" }, pagination);
  45. }
  46. catch (Exception ex)
  47. {
  48. if (ex is ExceptionEx)
  49. {
  50. throw;
  51. }
  52. else
  53. {
  54. throw ExceptionEx.ThrowServiceException(ex);
  55. }
  56. }
  57. }
  58. public IEnumerable<NewsEntity> GetAboutSchool()
  59. {
  60. try
  61. {
  62. return this.BaseRepository().FindList<NewsEntity>(a => a.F_Category == "关于学院");
  63. }
  64. catch (Exception ex)
  65. {
  66. if (ex is ExceptionEx)
  67. {
  68. throw;
  69. }
  70. else
  71. {
  72. throw ExceptionEx.ThrowServiceException(ex);
  73. }
  74. }
  75. }
  76. public IEnumerable<NewsEntity> GetNews()
  77. {
  78. try
  79. {
  80. return this.BaseRepository().FindList<NewsEntity>(a => a.F_Category == "新闻公告");
  81. }
  82. catch (Exception ex)
  83. {
  84. if (ex is ExceptionEx)
  85. {
  86. throw;
  87. }
  88. else
  89. {
  90. throw ExceptionEx.ThrowServiceException(ex);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 新闻公告实体
  96. /// </summary>
  97. /// <param name="keyValue">主键值</param>
  98. /// <returns></returns>
  99. public NewsEntity GetEntity(string keyValue)
  100. {
  101. try
  102. {
  103. return this.BaseRepository().FindEntity<NewsEntity>(keyValue);
  104. }
  105. catch (Exception ex)
  106. {
  107. if (ex is ExceptionEx)
  108. {
  109. throw;
  110. }
  111. else
  112. {
  113. throw ExceptionEx.ThrowServiceException(ex);
  114. }
  115. }
  116. }
  117. #endregion
  118. #region 提交数据
  119. /// <summary>
  120. /// 删除
  121. /// </summary>
  122. /// <param name="keyValue">主键</param>
  123. public void DeleteEntity(string keyValue)
  124. {
  125. try
  126. {
  127. NewsEntity entity = new NewsEntity()
  128. {
  129. F_NewsId = keyValue,
  130. };
  131. this.BaseRepository().Delete(entity);
  132. }
  133. catch (Exception ex)
  134. {
  135. if (ex is ExceptionEx)
  136. {
  137. throw;
  138. }
  139. else
  140. {
  141. throw ExceptionEx.ThrowServiceException(ex);
  142. }
  143. }
  144. }
  145. /// <summary>
  146. /// 保存(新增、修改)
  147. /// </summary>
  148. /// <param name="keyValue">主键值</param>
  149. /// <param name="newsEntity">新闻公告实体</param>
  150. /// <returns></returns>
  151. public void SaveEntity(string keyValue, NewsEntity newsEntity)
  152. {
  153. try
  154. {
  155. newsEntity.F_TypeId = 1;
  156. if (!string.IsNullOrEmpty(keyValue))
  157. {
  158. newsEntity.Modify(keyValue);
  159. this.BaseRepository().Update(newsEntity);
  160. }
  161. else
  162. {
  163. newsEntity.Create();
  164. this.BaseRepository().Insert(newsEntity);
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. if (ex is ExceptionEx)
  170. {
  171. throw;
  172. }
  173. else
  174. {
  175. throw ExceptionEx.ThrowServiceException(ex);
  176. }
  177. }
  178. }
  179. #endregion
  180. public void ChangeStatusByProcessId(string parameterProcessId, int status)
  181. {
  182. try
  183. {
  184. var newEntity = this.BaseRepository().FindEntity<NewsEntity>(a => a.F_ProgressId == parameterProcessId);
  185. if (null != newEntity)
  186. {
  187. if (status == 2)
  188. {
  189. newEntity.F_Status = "2";
  190. newEntity.F_EnabledMark = 1;
  191. }
  192. else
  193. {
  194. newEntity.F_Status = "0";
  195. }
  196. }
  197. this.BaseRepository().Update(newEntity);
  198. if (newEntity.IsSend == "1" && status == 2)
  199. {
  200. BaseRepository()
  201. .ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'准备开始run task') ");
  202. Task.Run(async () => { sendNew(newEntity); });
  203. }
  204. }
  205. catch (Exception ex)
  206. {
  207. BaseRepository()
  208. .ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'run task 报错"+ex.Message+"') ");
  209. if (ex is ExceptionEx)
  210. {
  211. throw;
  212. }
  213. else
  214. {
  215. throw ExceptionEx.ThrowServiceException(ex);
  216. }
  217. }
  218. }
  219. public void sendNew(NewsEntity newEntity)
  220. {
  221. try
  222. {
  223. if (newEntity.IsSend == "1") //判断是否下发
  224. {
  225. #region 获取配置文件
  226. string siteId = ConfigurationManager.AppSettings["SiteId"];
  227. string channelId = ConfigurationManager.AppSettings["ChannelId"];
  228. string ApiKey = ConfigurationManager.AppSettings["ApiKey"];
  229. string ports = ConfigurationManager.AppSettings["Ports"];
  230. #endregion
  231. BaseRepository()
  232. .ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'准备开始组装实体') ");
  233. SemdNewList SendNew = new SemdNewList
  234. {
  235. Title = newEntity.F_FullHead,
  236. SubTitle = newEntity.F_BriefHead,
  237. Content = HttpUtility.HtmlDecode(newEntity.F_NewsContent),
  238. Author = newEntity.F_AuthorName,
  239. Source = newEntity.F_SourceName,
  240. AddDate = DateTime.Now,
  241. Tags = newEntity.F_TagWord,
  242. AddUserName = newEntity.F_CreateUserName,
  243. };
  244. WebHeaderCollection ApiId = new WebHeaderCollection
  245. {
  246. { "X-SS-API-KEY", ApiKey }
  247. };
  248. BaseRepository()
  249. .ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'准备开始post cms') ");
  250. string responses = HttpMethods.HttpPosts("http://" + ports + "/api/v1/contents/" + siteId + "/" + channelId, SendNew.ToJson(), ApiId);
  251. BaseRepository()
  252. .ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'"+HttpUtility.UrlEncode(responses)+"') ");
  253. //#region 修改审核状态
  254. //string Nid = JsonConvert.DeserializeObject<Root>(responses).value.id;
  255. //AuditList AList = new AuditList();
  256. //if (Nid != null)
  257. //{
  258. // AList.siteId = Convert.ToInt32(siteId);
  259. // List<contents> listCon = new List<contents>();
  260. // //这里应该循环,如果多个
  261. // contents con = new contents();
  262. // con.channelId = Convert.ToInt32(channelId);
  263. // con.id = Convert.ToInt32(Nid);
  264. // listCon.Add(con);
  265. // AList.contents = listCon;
  266. // //AList.reasons = "终审通过";
  267. // HttpMethods.HttpPosts("http://" + ports + "/api/v1/contents/check", AList.ToJson(), ApiId);
  268. //}
  269. //#endregion
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. BaseRepository()
  275. .ExecuteBySql("insert INTO LR_Base_Log(F_LogId,F_CategoryId,F_SourceContentJson) VALUES (newid(),2222,'"+ex.Message + "') ");
  276. }
  277. }
  278. public int GetNewSum()
  279. {
  280. return this.BaseRepository().FindList<NewsEntity>().Count();
  281. }
  282. public Dictionary<int,int> GetNewMonthSum()
  283. {
  284. var dt = new DateTime(DateTime.Now.Year, 1, 1);
  285. var ls = this.BaseRepository().FindList<NewsEntity>(x=>x.F_CreateDate>=dt).Select(x => new { Year = x.F_CreateDate.Value.Year, Month = x.F_CreateDate.Value.Month }).GroupBy(x => new { x.Year, x.Month }).ToDictionary(x => x.Key.Month, a => a.Count());
  286. return ls;
  287. }
  288. }
  289. //public class Root
  290. //{
  291. // public RootValue value { get; set; }
  292. //}
  293. //public class RootValue
  294. //{
  295. // public string id { get; set; }
  296. //}
  297. }