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.
 
 
 
 
 
 

454 lines
16 KiB

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