|
- using Dapper;
- using Learun.DataBase.Repository;
- using Learun.Util;
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace Learun.Application.OA
- {
- /// <summary>
- /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
- /// Copyright (c) 2013-2018 北京泉江科技有限公司
- /// 创建人:陈彬彬
- /// 日 期:2017.04.17
- /// 描 述:公告管理
- /// </summary>
- public class NoticeService : RepositoryFactory
- {
- #region 获取数据
- /// <summary>
- /// 公告列表
- /// </summary>
- /// <param name="pagination">分页参数</param>
- /// <param name="keyword">关键词</param>
- /// <returns></returns>
- public IEnumerable<NewsEntity> GetPageList(Pagination pagination, string keyword)
- {
- try
- {
- var user = LoginUserInfo.Get();
- var strSql = new StringBuilder();
- strSql.Append("SELECT * FROM LR_OA_News t WHERE t.F_TypeId = 2 and F_DeleteMark=0 ");
- if (!string.IsNullOrEmpty(keyword))
- {
- strSql.Append(" AND F_FullHead like @keyword");
- }
- if (user.Description != "超级管理员")
- {
- strSql.Append(" AND F_CreateUserName ='" + user.realName + "'");
- }
- return this.BaseRepository().FindList<NewsEntity>(strSql.ToString(), new { keyword = "%" + keyword + "%" }, pagination);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
-
- /// <summary>
- /// 通知公告阅读统计
- /// </summary>
- /// <param name="pagination"></param>
- /// <param name="queryJson"></param>
- /// <returns></returns>
- public IEnumerable<NewsEntity> GetPageListForStatistics(Pagination pagination, string queryJson)
- {
- try
- {
- var queryParam = queryJson.ToJObject();
- var sql = new StringBuilder(@"select t.*,u.F_Account,u.f_realname,f_departmentid from ");
- sql.Append("(");
- sql.Append("select t1.ruserid,isnull(srnum,0) as srnum,isnull(rnum,0) as rnum from (");
- sql.Append(
- @"select ruserid,COUNT(1) as srnum from LR_OA_News t left join LR_OA_NewsShouldRead s on s.newsid=t.f_newsid
- left join lr_base_user u on s.RUserId=u.F_UserId
- WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1
- --and u.F_Account like '20%'
- --and t.F_ReleaseTime>CONVERT(datetime,SUBSTRING(u.F_Account,1,4)+'-08-31',120)
- ");
- if (!queryParam["Year"].IsEmpty() && !queryParam["Month"].IsEmpty())
- {
- sql.Append($" and year(t.F_ReleaseTime)='{queryParam["Year"].ToString()}' and month(t.F_ReleaseTime)='{queryParam["Month"].ToString()}'");
- }
-
- sql.Append(" group by ruserid ) t1 left join (");
- sql.Append(
- @" select ruserid,COUNT(1) as rnum from LR_OA_News t left join LR_OA_NewsRead r on r.newsid=t.f_newsid
- left join lr_base_user u on r.RUserId=u.F_UserId
- WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1
- --and u.F_Account like '20%'
- --and t.F_ReleaseTime>CONVERT(datetime,SUBSTRING(u.F_Account,1,4)+'-08-31',120)
- and DATEDIFF(HOUR,t.F_ReleaseTime,r.RTime)<=24*7 ");
- if (!queryParam["Year"].IsEmpty() && !queryParam["Month"].IsEmpty())
- {
- sql.Append($" and year(t.F_ReleaseTime)='{queryParam["Year"].ToString()}' and month(t.F_ReleaseTime)='{queryParam["Month"].ToString()}'");
- }
-
- sql.Append(" group by ruserid ) t2 on t1.ruserid=t2.ruserid ");
- sql.Append(") t");
- sql.Append(" left join lr_base_user u on t.ruserid=u.f_userid where 1=1 ");
-
- // 虚拟参数
- var dp = new DynamicParameters(new { });
- if (queryParam.HasValues)
- {
- if (!queryParam["F_Encode"].IsEmpty())
- {
- sql.Append($" AND u.f_encode like '%{queryParam["F_Encode"].ToString()}%' ");
- }
- if (!queryParam["F_RealName"].IsEmpty())
- {
- sql.Append($" AND u.f_realname like '%{queryParam["F_RealName"].ToString()}%' ");
- }
- if (!queryParam["F_Departmentid"].IsEmpty())
- {
- sql.Append($" AND f_departmentid = '{queryParam["F_Departmentid"].ToString()}' ");
- }
- }
- var list = this.BaseRepository().FindList<NewsEntity>(sql.ToString(), pagination);
-
- return list;
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
-
- /// <summary>
- /// 保存通知公告应读人员
- /// </summary>
- public void SaveNewsShouldRead()
- {
- var db = this.BaseRepository();
- try
- {
- db.BeginTrans();
-
- var sql =
- @"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,
- F_SendDeptId,F_SendPostId,* FROM LR_OA_News t WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1
- and t.f_Newsid not in (SELECT distinct Newsid from [dbo].[LR_OA_NewsShouldRead])";
- var list = db.FindList<NewsEntity>(sql);
- foreach (var news in list)
- {
- if (string.IsNullOrEmpty(news.F_SendDeptId) && string.IsNullOrEmpty(news.F_SendPostId))
- {
- //接收人是全部教师
- db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
- SELECT newid(),'{news.F_NewsId}',f_userid from lr_base_user where f_description='教师' and
- f_userid not in (
- SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
- )
- ");
- }
- else
- {
- //接收部门
- if (!string.IsNullOrEmpty(news.F_SendDeptId))
- {
- var deptids = string.Join("','", news.F_SendDeptId.Split(','));
- db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
- SELECT newid(),'{news.F_NewsId}',f_userid from lr_base_user where f_description='教师' and F_DepartmentId in ('{deptids}')
- and f_userid not in (
- SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
- )");
- //岗位
- if (!string.IsNullOrEmpty(news.F_SendPostId))
- {
- var postids = string.Join("','", news.F_SendPostId.Split(','));
- db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
- SELECT newid(),'{news.F_NewsId}',t.f_userid from lr_base_user t
- join LR_Base_UserRelation r on r.f_category='2' and t.F_UserId=r.f_userid
- where t.f_description='教师' and r.f_objectid in ('{postids}')
- and t.F_DepartmentId not in ('{deptids}')
- and t.f_userid not in (
- SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
- )");
- }
- }
- //接收部门空 , 接收岗位不为空
- if (string.IsNullOrEmpty(news.F_SendDeptId) && !string.IsNullOrEmpty(news.F_SendPostId))
- {
- var postids = string.Join("','", news.F_SendPostId.Split(','));
- db.ExecuteBySql($@"insert into LR_OA_NewsShouldRead
- SELECT newid(),'{news.F_NewsId}',t.f_userid from lr_base_user t
- join LR_Base_UserRelation r on r.f_category='2' and t.F_UserId=r.f_userid
- where t.f_description='教师' and r.f_objectid in ('{postids}')
- and t.f_userid not in (
- SELECT ruserid from [dbo].[LR_OA_NewsShouldRead] where newsid='{news.F_NewsId}'
- )");
- }
- }
-
- }
- db.Commit();
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
-
- /// <summary>
- /// 新闻公告实体
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <returns></returns>
- public NewsEntity GetEntity(string keyValue)
- {
- try
- {
- return this.BaseRepository().FindEntity<NewsEntity>(keyValue);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
- #endregion
-
- #region 提交数据
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="keyValue">主键</param>
- public void DeleteEntity(string keyValue)
- {
- var db = this.BaseRepository().BeginTrans();
- try
- {
- var list = keyValue.Split(',');
- foreach (var item in list)
- {
- var entity = db.FindEntity<NewsEntity>(x => x.F_NewsId == item);
- if (entity != null)
- {
- entity.F_DeleteMark = 1;
- db.Update(entity);
- //db.Delete(entity);
- }
-
- string sql = $"delete MessageRemind where InstanceId='{entity.F_NewsId}'";
- db.ExecuteBySql(sql);
- }
-
- db.Commit();
- }
- catch (Exception ex)
- {
- db.Rollback();
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
- /// <summary>
- /// 保存(新增、修改)
- /// </summary>
- /// <param name="keyValue">主键值</param>
- /// <param name="newsEntity">新闻公告实体</param>
- /// <returns></returns>
- public void SaveEntity(string keyValue, NewsEntity newsEntity)
- {
- try
- {
- newsEntity.F_TypeId = 2;
- if (!string.IsNullOrEmpty(keyValue))
- {
- newsEntity.Modify(keyValue);
- this.BaseRepository().Update(newsEntity);
- }
- else
- {
- newsEntity.Create();
- this.BaseRepository().Insert(newsEntity);
- }
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
-
- }
-
- #endregion
- #region 扩展数据
-
- /// <summary>
- /// 公告列表
- /// </summary>
- /// <param name="keyword">关键词</param>
- /// <returns></returns>
- public IEnumerable<NewsEntity> GetList(string keyword, string categoryId = null)
- {
- try
- {
- var userinfo = LoginUserInfo.Get();
- var userId = userinfo.userId;
- var deptId = userinfo.departmentId;
- var postIds = userinfo.postIds;
- var strSql = new StringBuilder();
- strSql.Append("SELECT t.*,r.RNewsId,r.RTime FROM LR_OA_News t ");
- strSql.Append(" left join LR_OA_NewsRead r on t.F_NewsId = r.NewsId and r.RUserId=@userId ");
- strSql.Append(" WHERE t.F_TypeId = 2 and t.F_DeleteMark=0 and t.F_EnabledMark=1 ");
- strSql.Append($@" and (
- ((t.F_SendDeptId is null or len(t.F_SendDeptId)=0) and (t.F_SendPostId is null or len(t.F_SendPostId)=0))
- ");
- if (!string.IsNullOrEmpty(deptId))
- {
- strSql.Append($" or (t.F_SendDeptId is not null and t.F_SendDeptId like '%{deptId}%')");
- }
- if (!string.IsNullOrEmpty(postIds))
- {
- strSql.Append(" or (t.F_SendPostId is not null and ");
- if (postIds.Contains(","))
- {
- string postidSql = " (";
- foreach (var postId in postIds)
- {
- postidSql += $" t.F_SendPostId like '%{postId}%' or";
- }
-
- postidSql = postidSql.Substring(0, postidSql.LastIndexOf("or")) + ")";
- strSql.Append(postidSql);
- }
- else
- {
- strSql.Append($" t.F_SendPostId like '%{postIds}%'");
- }
-
- strSql.Append(")");
- }
-
- strSql.Append(") ");
- if (!string.IsNullOrEmpty(categoryId))
- {
- strSql.Append($" AND t.F_CategoryId = '{categoryId}'");
- }
- if (!string.IsNullOrEmpty(keyword))
- {
- strSql.Append(" AND t.F_FullHead like @keyword");
- }
- strSql.Append(" ORDER BY t.F_ReleaseTime DESC ");
- return this.BaseRepository().FindList<NewsEntity>(strSql.ToString(), new { keyword = "%" + keyword + "%", userId = userId });
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
-
- #endregion
-
- public void ChangeStatusById(string keyValue, int status, string processId)
- {
- try
- {
- BaseRepository().ExecuteBySql($"UPDATE dbo.LR_OA_News SET F_Status='{status}',F_ProgressId='{processId}' WHERE F_NewsId='{keyValue}'", null);
- }
- catch (Exception ex)
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
-
- public NewsEntity GetEntityByProcessId(string processId)
- {
- try
- {
- return this.BaseRepository().FindEntity<NewsEntity>(t => t.F_ProgressId == processId);
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
-
- public void SaveFormAndSubmit(string keyValue, NewsEntity entity)
- {
- try
- {
- entity.F_TypeId = 2;
- if (!string.IsNullOrEmpty(keyValue))
- {
- entity.Modify(keyValue);
- this.BaseRepository().Update(entity);
- }
- else
- {
- entity.Create();
- entity.F_EnabledMark = 1;
- this.BaseRepository().Insert(entity);
- }
- }
- catch (Exception ex)
- {
- if (ex is ExceptionEx)
- {
- throw;
- }
- else
- {
- throw ExceptionEx.ThrowServiceException(ex);
- }
- }
- }
- }
- }
|