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.
 
 
 
 
 
 

429 lines
15 KiB

  1. using Dapper;
  2. using Learun.DataBase.Repository;
  3. using Learun.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Text;
  8. namespace Learun.Application.OA
  9. {
  10. /// <summary>
  11. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  12. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  13. /// 创建人:陈彬彬
  14. /// 日 期:2017.04.17
  15. /// 描 述:公告管理
  16. /// </summary>
  17. public class NoticeService : RepositoryFactory
  18. {
  19. #region 获取数据
  20. /// <summary>
  21. /// 公告列表
  22. /// </summary>
  23. /// <param name="pagination">分页参数</param>
  24. /// <param name="keyword">关键词</param>
  25. /// <returns></returns>
  26. public IEnumerable<NewsEntity> GetPageList(Pagination pagination, string keyword)
  27. {
  28. try
  29. {
  30. var strSql = new StringBuilder();
  31. strSql.Append("SELECT * FROM LR_OA_News t WHERE t.F_TypeId = 2 and F_DeleteMark=0 ");
  32. if (!string.IsNullOrEmpty(keyword))
  33. {
  34. strSql.Append(" AND F_FullHead like @keyword");
  35. }
  36. return this.BaseRepository().FindList<NewsEntity>(strSql.ToString(), new { keyword = "%" + keyword + "%" }, pagination);
  37. }
  38. catch (Exception ex)
  39. {
  40. if (ex is ExceptionEx)
  41. {
  42. throw;
  43. }
  44. else
  45. {
  46. throw ExceptionEx.ThrowServiceException(ex);
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 通知公告阅读统计
  52. /// </summary>
  53. /// <param name="pagination"></param>
  54. /// <param name="queryJson"></param>
  55. /// <returns></returns>
  56. public IEnumerable<NewsEntity> GetPageListForStatistics(Pagination pagination, string queryJson)
  57. {
  58. try
  59. {
  60. var sql = new StringBuilder(@"select t.*,u.f_encode,u.f_realname,f_departmentid from (
  61. select t1.ruserid,isnull(srnum,0) as srnum,isnull(rnum,0) as rnum from (
  62. select ruserid,COUNT(1) as srnum from LR_OA_News t left join LR_OA_NewsShouldRead s on s.newsid=t.f_newsid
  63. WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1 group by ruserid
  64. ) t1
  65. left join (
  66. select ruserid,COUNT(1) as rnum from LR_OA_News t left join LR_OA_NewsRead r on r.newsid=t.f_newsid
  67. WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1 and DATEDIFF(HOUR,t.F_ReleaseTime,r.RTime)<=24*7 group by ruserid
  68. ) t2 on t1.ruserid=t2.ruserid
  69. ) t
  70. left join lr_base_user u on t.ruserid=u.f_userid where 1=1
  71. ");
  72. var queryParam = queryJson.ToJObject();
  73. // 虚拟参数
  74. var dp = new DynamicParameters(new { });
  75. if (queryParam.HasValues)
  76. {
  77. if (!queryParam["F_Encode"].IsEmpty())
  78. {
  79. sql.Append($" AND u.f_encode like '%{queryParam["F_Encode"].ToString()}%' ");
  80. }
  81. if (!queryParam["F_RealName"].IsEmpty())
  82. {
  83. sql.Append($" AND u.f_realname like '%{queryParam["F_RealName"].ToString()}%' ");
  84. }
  85. if (!queryParam["F_Departmentid"].IsEmpty())
  86. {
  87. sql.Append($" AND f_departmentid = '{queryParam["F_Departmentid"].ToString()}' ");
  88. }
  89. }
  90. var list = this.BaseRepository().FindList<NewsEntity>(sql.ToString(), pagination);
  91. return list;
  92. }
  93. catch (Exception ex)
  94. {
  95. if (ex is ExceptionEx)
  96. {
  97. throw;
  98. }
  99. else
  100. {
  101. throw ExceptionEx.ThrowServiceException(ex);
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// 保存通知公告应读人员
  107. /// </summary>
  108. public void SaveNewsShouldRead()
  109. {
  110. var db = this.BaseRepository();
  111. try
  112. {
  113. db.BeginTrans();
  114. var sql =
  115. @"SELECT case when (F_SendDeptId is null or len(F_SendDeptId)=0) and (F_SendPostId is null or len(F_SendDeptId)=0) then '1' else '' end as F_Send,
  116. F_SendDeptId,F_SendPostId,* FROM LR_OA_News t WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1
  117. and t.f_Newsid not in (SELECT distinct Newsid from [dbo].[LR_OA_NewsShouldRead])";
  118. var list = db.FindList<NewsEntity>(sql);
  119. foreach (var news in list)
  120. {
  121. if (!string.IsNullOrEmpty(news.F_Send) && news.F_Send == "1")
  122. {
  123. //接收人是全部教师
  124. db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
  125. SELECT newid(),'{news.F_NewsId}',f_userid from lr_base_user where f_description='教师' and
  126. f_userid not in (
  127. SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
  128. )
  129. ");
  130. }
  131. else
  132. {
  133. //接收部门
  134. if (!string.IsNullOrEmpty(news.F_SendDeptId))
  135. {
  136. var deptids = string.Join("','", news.F_SendDeptId.Split(','));
  137. db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
  138. SELECT newid(),'{news.F_NewsId}',f_userid from lr_base_user where f_description='教师' and F_DepartmentId in ('{deptids}')
  139. and f_userid not in (
  140. SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
  141. )");
  142. //岗位
  143. if (!string.IsNullOrEmpty(news.F_SendPostId))
  144. {
  145. var postids = string.Join("','", news.F_SendPostId.Split(','));
  146. db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
  147. SELECT newid(),'{news.F_NewsId}',t.f_userid from lr_base_user t
  148. join LR_Base_UserRelation r on r.f_category='2' and t.F_UserId=r.f_userid
  149. where t.f_description='教师' and r.f_objectid in ('{postids}')
  150. and t.F_DepartmentId not in ('{deptids}')
  151. and t.f_userid not in (
  152. SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
  153. )");
  154. }
  155. }
  156. //接收部门空 , 接收岗位不为空
  157. if (string.IsNullOrEmpty(news.F_SendDeptId) && !string.IsNullOrEmpty(news.F_SendPostId))
  158. {
  159. var postids = string.Join("','", news.F_SendPostId.Split(','));
  160. db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
  161. SELECT newid(),'{news.F_NewsId}',t.f_userid from lr_base_user t
  162. join LR_Base_UserRelation r on r.f_category='2' and t.F_UserId=r.f_userid
  163. where t.f_description='教师' and r.f_objectid in ('{postids}')
  164. and t.f_userid not in (
  165. SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
  166. )");
  167. }
  168. }
  169. }
  170. db.Commit();
  171. }
  172. catch (Exception ex)
  173. {
  174. if (ex is ExceptionEx)
  175. {
  176. throw;
  177. }
  178. else
  179. {
  180. throw ExceptionEx.ThrowServiceException(ex);
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 新闻公告实体
  186. /// </summary>
  187. /// <param name="keyValue">主键值</param>
  188. /// <returns></returns>
  189. public NewsEntity GetEntity(string keyValue)
  190. {
  191. try
  192. {
  193. return this.BaseRepository().FindEntity<NewsEntity>(keyValue);
  194. }
  195. catch (Exception ex)
  196. {
  197. if (ex is ExceptionEx)
  198. {
  199. throw;
  200. }
  201. else
  202. {
  203. throw ExceptionEx.ThrowServiceException(ex);
  204. }
  205. }
  206. }
  207. #endregion
  208. #region 提交数据
  209. /// <summary>
  210. /// 删除
  211. /// </summary>
  212. /// <param name="keyValue">主键</param>
  213. public void DeleteEntity(string keyValue)
  214. {
  215. var db = this.BaseRepository().BeginTrans();
  216. try
  217. {
  218. var list = keyValue.Split(',');
  219. foreach (var item in list)
  220. {
  221. var entity = db.FindEntity<NewsEntity>(x => x.F_NewsId == item);
  222. if (entity != null)
  223. {
  224. entity.F_DeleteMark = 1;
  225. db.Update(entity);
  226. //db.Delete(entity);
  227. }
  228. string sql = $"delete MessageRemind where InstanceId='{entity.F_NewsId}'";
  229. db.ExecuteBySql(sql);
  230. }
  231. db.Commit();
  232. }
  233. catch (Exception ex)
  234. {
  235. db.Rollback();
  236. if (ex is ExceptionEx)
  237. {
  238. throw;
  239. }
  240. else
  241. {
  242. throw ExceptionEx.ThrowServiceException(ex);
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// 保存(新增、修改)
  248. /// </summary>
  249. /// <param name="keyValue">主键值</param>
  250. /// <param name="newsEntity">新闻公告实体</param>
  251. /// <returns></returns>
  252. public void SaveEntity(string keyValue, NewsEntity newsEntity)
  253. {
  254. try
  255. {
  256. newsEntity.F_TypeId = 2;
  257. if (!string.IsNullOrEmpty(keyValue))
  258. {
  259. newsEntity.Modify(keyValue);
  260. this.BaseRepository().Update(newsEntity);
  261. }
  262. else
  263. {
  264. newsEntity.Create();
  265. this.BaseRepository().Insert(newsEntity);
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. if (ex is ExceptionEx)
  271. {
  272. throw;
  273. }
  274. else
  275. {
  276. throw ExceptionEx.ThrowServiceException(ex);
  277. }
  278. }
  279. }
  280. #endregion
  281. #region 扩展数据
  282. /// <summary>
  283. /// 公告列表
  284. /// </summary>
  285. /// <param name="keyword">关键词</param>
  286. /// <returns></returns>
  287. public IEnumerable<NewsEntity> GetList(string keyword, string categoryId = null)
  288. {
  289. try
  290. {
  291. var userinfo = LoginUserInfo.Get();
  292. var userId = userinfo.userId;
  293. var deptId = userinfo.departmentId;
  294. var postIds = userinfo.postIds;
  295. var strSql = new StringBuilder();
  296. strSql.Append("SELECT t.*,r.RNewsId,r.RTime FROM LR_OA_News t ");
  297. strSql.Append(" left join LR_OA_NewsRead r on t.F_NewsId = r.NewsId and r.RUserId=@userId ");
  298. strSql.Append(" WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1 ");
  299. strSql.Append($@" and (
  300. ((t.F_SendDeptId is null or len(t.F_SendDeptId)=0) and (t.F_SendPostId is null or len(t.F_SendPostId)=0))
  301. ");
  302. if (!string.IsNullOrEmpty(deptId))
  303. {
  304. strSql.Append($" or (t.F_SendDeptId is not null and t.F_SendDeptId like '%{deptId}%')");
  305. }
  306. if (!string.IsNullOrEmpty(postIds))
  307. {
  308. strSql.Append(" or (t.F_SendPostId is not null and ");
  309. if (postIds.Contains(","))
  310. {
  311. string postidSql = " (";
  312. foreach (var postId in postIds)
  313. {
  314. postidSql += $" t.F_SendPostId like '%{postId}%' or";
  315. }
  316. postidSql = postidSql.Substring(0, postidSql.LastIndexOf("or")) + ")";
  317. strSql.Append(postidSql);
  318. }
  319. else
  320. {
  321. strSql.Append($" t.F_SendPostId like '%{postIds}%'");
  322. }
  323. strSql.Append(")");
  324. }
  325. strSql.Append(") ");
  326. if (!string.IsNullOrEmpty(categoryId))
  327. {
  328. strSql.Append($" AND t.F_CategoryId = '{categoryId}'");
  329. }
  330. if (!string.IsNullOrEmpty(keyword))
  331. {
  332. strSql.Append(" AND t.F_FullHead like @keyword");
  333. }
  334. strSql.Append(" ORDER BY t.F_ReleaseTime DESC ");
  335. return this.BaseRepository().FindList<NewsEntity>(strSql.ToString(), new { keyword = "%" + keyword + "%", userId = userId });
  336. }
  337. catch (Exception ex)
  338. {
  339. if (ex is ExceptionEx)
  340. {
  341. throw;
  342. }
  343. else
  344. {
  345. throw ExceptionEx.ThrowServiceException(ex);
  346. }
  347. }
  348. }
  349. #endregion
  350. public void ChangeStatusById(string keyValue, int status, string processId)
  351. {
  352. try
  353. {
  354. BaseRepository().ExecuteBySql($"UPDATE dbo.LR_OA_News SET F_Status='{status}',F_ProgressId='{processId}' WHERE F_NewsId='{keyValue}'", null);
  355. }
  356. catch (Exception ex)
  357. {
  358. throw ExceptionEx.ThrowServiceException(ex);
  359. }
  360. }
  361. public NewsEntity GetEntityByProcessId(string processId)
  362. {
  363. try
  364. {
  365. return this.BaseRepository().FindEntity<NewsEntity>(t => t.F_ProgressId == processId);
  366. }
  367. catch (Exception ex)
  368. {
  369. if (ex is ExceptionEx)
  370. {
  371. throw;
  372. }
  373. else
  374. {
  375. throw ExceptionEx.ThrowServiceException(ex);
  376. }
  377. }
  378. }
  379. public void SaveFormAndSubmit(string keyValue, NewsEntity entity)
  380. {
  381. try
  382. {
  383. entity.F_TypeId = 2;
  384. if (!string.IsNullOrEmpty(keyValue))
  385. {
  386. entity.Modify(keyValue);
  387. this.BaseRepository().Update(entity);
  388. }
  389. else
  390. {
  391. entity.Create();
  392. entity.F_EnabledMark = 1;
  393. this.BaseRepository().Insert(entity);
  394. }
  395. }
  396. catch (Exception ex)
  397. {
  398. if (ex is ExceptionEx)
  399. {
  400. throw;
  401. }
  402. else
  403. {
  404. throw ExceptionEx.ThrowServiceException(ex);
  405. }
  406. }
  407. }
  408. }
  409. }