using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using QMFrameWork.Data; using QMAPP.Entity.Sys; namespace QMAPP.DAL.Sys { /// /// 通知管理 /// 创建者:王丹丹 /// 创建日期:2015.03.09 /// public class NoticeManageDAL : BaseDAL { #region 通知信息操作 #region 获取通知信息 /// /// 获取通知信息 /// /// 条件 /// 通知信息 public NoticeInfo Get(NoticeInfo notice) { try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //获取通知信息 notice = session.Get(notice); } return notice; } catch (Exception ex) { throw; } } #endregion #region 获取通知信息列表 /// /// 获取通知信息列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetList(NoticeInfo condition, DataPage page) { string sql = ""; List parameters = new List(); try { //分页关键字段及排序 page.KeyName = "NOTICEID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "CREATEDATE DESC"; } sql = GetListSQL(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); page = session.GetDataPage(sql, parameters.ToArray(), page); } return page; } catch (Exception ex) { throw; } } #endregion #region 获取通知信息列表SQL /// /// 获取通知信息列表 /// /// 查询条件 /// /// sql语句 public string GetListSQL(NoticeInfo condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.Append(" SELECT N.NOTICEID,N.NOTICETITLE,CASE N.NOTICETYPE WHEN '1' THEN '即时通知' else '普通通知' end as NOTICETYPE, "); sqlBuilder.Append(" N.NOTICECONTEXT,N.USETIME,N.OUTTIME, "); sqlBuilder.Append(" N.USETIME AS USETIMETXT, "); sqlBuilder.Append(" N.OUTTIME AS OUTTIMETXT, "); sqlBuilder.Append(" N.ATTACHFILE,CASE N.CANREPLY WHEN '1' THEN '是' else '否' end as CANREPLY,N.SENDFLG, "); sqlBuilder.Append(" N.SENDAIM,N.SENDTIME,N.CREATEDATE ,N.UPDATEDATE,T_QM_USER.USERNAME AS CREATEUSER, "); sqlBuilder.Append(" U.USERNAME AS UPDATEUSER,C.REPLYCOUNT "); sqlBuilder.Append(" FROM T_QM_NOTICE N LEFT JOIN T_QM_USER ON N.CREATEUSER = T_QM_USER.USERID "); sqlBuilder.Append(" LEFT JOIN T_QM_USER U ON N.UPDATEUSER = U.USERID "); sqlBuilder.Append(" LEFT JOIN ( SELECT COUNT(*) AS REPLYCOUNT,NOTICEID FROM T_QM_NOTICEBROWSE "); sqlBuilder.Append(" WHERE REPLYCONTENT IS NOT NULL GROUP BY NOTICEID ) C "); sqlBuilder.Append(" ON C.NOTICEID=N.NOTICEID "); //查询条件 if (condition.UserName.ToLower() != "admin") { sqlBuilder.Append(" LEFT JOIN T_QM_NOTICEBROWSE NB ON NB.NOTICEID=N.NOTICEID "); //浏览用户ID if (!string.IsNullOrEmpty(condition.UserID)) { whereBuilder.Append(" AND NB.USERID=@USERID"); parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = condition.UserID }); } } //通知标题 if (string.IsNullOrEmpty(condition.NOTICETITLE) == false) { whereBuilder.Append(" AND NOTICETITLE LIKE @NOTICETITLE"); parameters.Add(new DataParameter { ParameterName = "NOTICETITLE", DataType = DbType.String, Value = "%"+condition.NOTICETITLE+"%" }); } //通知开始时间 if (!string.IsNullOrEmpty(condition.USETIMESTART)) { whereBuilder.Append(" AND N.USETIME >= @USETIMESTART"); parameters.Add(new DataParameter { ParameterName = "USETIMESTART", DataType = DbType.DateTime, Value = DateTime.Parse(condition.USETIMESTART) }); } //通知结束时间 if (!string.IsNullOrEmpty(condition.USETIMEEND)) { whereBuilder.Append(" AND N.USETIME < @USETIMEEND"); parameters.Add(new DataParameter { ParameterName = "USETIMEEND", DataType = DbType.DateTime, Value = Convert.ToDateTime(condition.USETIMEEND).AddDays(1) }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw; } } #endregion #region 判断通知标题是否存在 /// /// 判断通知标题是否存在 /// /// 信息 /// true@已存在;fasel@不存在。 public bool ExistsNotice(NoticeInfo notice) { string noticeID = ""; int count = 0; string sql = null; try { if (string.IsNullOrEmpty(notice.NOTICEID) == false) { noticeID = notice.NOTICEID; } sql = "SELECT COUNT(*) FROM T_QM_NOTICE WHERE NOTICEID <> @NOTICEID AND NOTICETITLE=@NOTICETITLE"; using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); count = Convert.ToInt32(session.ExecuteSqlScalar(sql, new DataParameter("NOTICEID", noticeID), new DataParameter { ParameterName = "NOTICETITLE", Value = notice.NOTICETITLE })); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { throw; } } #endregion #region 插入通知 /// /// 插入通知 /// /// 通知信息 /// 插入数 public int Insert(NoticeInfo user) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(user); } return count; } catch (Exception ex) { throw; } } #endregion #region 删除通知 /// /// 删除通知信息 /// /// 通知信息 /// 删除个数 public int Delete(NoticeInfo notice) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //删除基本信息 count = session.Delete(notice); } return count; } catch (Exception ex) { throw; } } #endregion #region 更新通知 /// /// 更新通知 /// /// 通知信息 /// 更新个数 public int Update(NoticeInfo notice) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //更新基本信息 count = session.Update(notice); } return count; } catch (Exception ex) { throw; } } #endregion #endregion #region 获取未读有通知信息 /// /// 获取未读通知信息 /// /// public List GetNotReadNotice(NoticeInfo model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); List resultList = new List(); string sql = ""; DataPage page = new DataPage(); try { //构成查询语句 sqlBuilder.Append(" SELECT N.NOTICEID,N.NOTICETITLE,N.USETIME,N.CREATEDATE,N.OUTTIME,NB.ISREAD "); sqlBuilder.Append(" FROM T_QM_NOTICE N "); sqlBuilder.Append(" LEFT JOIN T_QM_NOTICEBROWSE NB ON NB.NOTICEID=N.NOTICEID "); sqlBuilder.Append(" WHERE N.USETIME <= @NOWDATE "); sqlBuilder.Append(" AND N.OUTTIME >= @NOWDATE "); sqlBuilder.Append(" AND NB.USERID=@USERID "); sqlBuilder.Append(" AND NB.ISREAD='0' "); parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = model.UserID }); parameters.Add(new DataParameter { ParameterName = "NOWDATE", DataType = DbType.DateTime, Value = DateTime.Now }); page.KeyName = "NOTICEID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "CREATEDATE DESC"; } page.PageIndex=1; page.PageSize=5; using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session); resultList = session.GetDataPage(sql, parameters.ToArray(), page).Result as List; } return resultList; } catch (Exception ex) { throw; } } #endregion #region 获取未读即时通知信息 /// /// 获取未读有通知信息 /// /// public List GetNotReadInstantNotice(NoticeInfo model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); List resultList = new List(); string sql = ""; DataPage page = new DataPage(); try { //构成查询语句 sqlBuilder.Append(" SELECT N.NOTICEID,N.NOTICETITLE,N.USETIME,N.CREATEDATE,N.OUTTIME,NB.ISREAD "); sqlBuilder.Append(" FROM T_QM_NOTICE N "); sqlBuilder.Append(" LEFT JOIN T_QM_NOTICEBROWSE NB ON NB.NOTICEID=N.NOTICEID "); sqlBuilder.Append(" WHERE N.USETIME <= @NOWDATE"); sqlBuilder.Append(" AND N.OUTTIME >=@NOWDATE "); sqlBuilder.Append(" AND NB.USERID=@USERID "); sqlBuilder.Append(" AND NB.ISREAD='0' "); sqlBuilder.Append(" AND N.NOTICETYPE='1' "); parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = model.UserID }); parameters.Add(new DataParameter { ParameterName = "NOWDATE", DataType = DbType.DateTime, Value = DateTime.Now }); page.KeyName = "NOTICEID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "CREATEDATE DESC"; } page.PageIndex = 1; page.PageSize = 5; using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session); resultList = session.GetDataPage(sql, parameters.ToArray(), page).Result as List; } return resultList; } catch (Exception ex) { throw; } } #endregion #region 通知浏览记录操作 #region 根据通知ID,从通知浏览记录表中,获取发送目标 /// /// 根据通知ID,从通知浏览记录表中,获取发送目标 /// /// 条件 /// 数据页 /// 数据页 public IList GetSendaimIdList(string noticeID) { List parameters = new List(); IList listModel = new List(); NoticeBrowse condition = new NoticeBrowse(); try { condition.NOTICEID = noticeID; string sql = GetBrowseSql(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); listModel = session.GetList(sql, parameters.ToArray()); } return listModel; } catch (Exception ex) { throw ex; } } #endregion #region 获取通知浏览记录列表 #region 获取通知浏览记录列表 /// /// 获取通知浏览记录列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetBrowseList(NoticeBrowse condition,DataPage page) { IList list = new List(); string sql = null; List parameters = new List(); try { //分页关键字段及排序 page.KeyName = "PID"; //获取执行语句 sql = GetBrowseListSql(condition, ref parameters).ToString(); using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); page = session.GetDataPage(sql, parameters.ToArray(), page); } return page; } catch (Exception ex) { throw; } } #endregion #region 构造通知公告浏览记录查询语句(用于管理员查看通知浏览记录) /// /// 构造通知公告浏览记录查询语句 /// /// 查询条件 /// sql语句 public string GetBrowseListSql(NoticeBrowse condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.Append("SELECT PID,NOTICEID,ISREAD,READTIME,ISAFFIRM,AFFIRMTIME,REPLYCONTENT,REPLYTIME,N.USERID,T_QM_USER.USERNAME AS UserName "); sqlBuilder.Append("FROM T_QM_NOTICEBROWSE N INNER JOIN T_QM_USER ON N.USERID = T_QM_USER.USERID "); //通知主键 if (!string.IsNullOrEmpty(condition.NOTICEID)) { whereBuilder.Append(" AND N.NOTICEID=@NOTICEID "); parameters.Add(new DataParameter { ParameterName = "NOTICEID", DataType = DbType.String, Value = condition.NOTICEID }); } //查询条件 if (condition.UserName != null) { if (condition.UserName.ToLower() != "admin") { //浏览用户主键 if (!string.IsNullOrEmpty(condition.USERID)) { whereBuilder.Append(" AND N.USERID=@USERID "); parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = condition.USERID }); } } } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw; } } #endregion #endregion #region 获取通知浏览记录 #region 获取通知浏览记录 /// /// 获取通知浏览记录 /// /// 查询条件 /// 通知浏览记录信息 public NoticeBrowse GetBrowse(NoticeBrowse condition) { string sql = null; List parameters = new List(); try { //获取执行语句 sql = GetBrowseListSql(condition, ref parameters).ToString(); using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); condition = session.Get(sql, parameters.ToArray()); } return condition; } catch (Exception ex) { throw; } } #endregion #region 获取通知浏览记录(普通员工查看浏览记录) /// /// 获取通知浏览记录 /// /// 条件 /// 数据页 /// 数据页 public NoticeBrowse GetBrowseModel(NoticeBrowse condition) { string sql = null; List parameters = new List(); try { //获取执行语句 sql = GetBrowseSql(condition, ref parameters).ToString(); using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); condition = session.Get(sql, parameters.ToArray()); } return condition; } catch (Exception ex) { throw; } } #endregion #region 构造通知公告浏览记录查询语句(用于普通员工查看通知浏览记录) /// /// 构造通知公告浏览记录查询语句 /// /// 查询条件 /// sql语句 public string GetBrowseSql(NoticeBrowse condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.Append("SELECT PID,NOTICEID,ISREAD,READTIME,ISAFFIRM,AFFIRMTIME,REPLYCONTENT,REPLYTIME,N.USERID,T_QM_USER.USERNAME AS UserName "); sqlBuilder.Append("FROM T_QM_NOTICEBROWSE N INNER JOIN T_QM_USER ON N.USERID = T_QM_USER.USERID "); //通知主键 if (!string.IsNullOrEmpty(condition.NOTICEID)) { whereBuilder.Append(" AND N.NOTICEID=@NOTICEID "); parameters.Add(new DataParameter { ParameterName = "NOTICEID", DataType = DbType.String, Value = condition.NOTICEID }); } //浏览用户主键 if (!string.IsNullOrEmpty(condition.USERID)) { whereBuilder.Append(" AND N.USERID=@USERID "); parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = condition.USERID }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw; } } #endregion #endregion #region 插入通知浏览记录 /// /// 插入通知浏览记录 /// /// 通知浏览记录信息 /// 插入数 public int Insert(NoticeBrowse model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } return count; } catch (Exception ex) { throw; } } #endregion #region 更新通知浏览记录 /// /// 更新通知浏览记录 /// /// 通知浏览记录信息 /// 更新数 public int Update(NoticeBrowse model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //更新基本信息 count = session.Update(model); } return count; } catch (Exception ex) { throw; } } #endregion #region 删除通知浏览记录 /// /// 删除通知浏览记录 /// /// 通知浏览记录信息 /// 删除个数 public int Delete(NoticeBrowse notice) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { session.OpenTs(); //删除基本信息 sqlBuilder.Append(" DELETE FROM T_QM_NOTICEBROWSE WHERE NOTICEID='" + notice.NOTICEID + "'"); if (string.IsNullOrEmpty(notice.USERID)) { sqlBuilder.Append(" AND USERID='" + notice.USERID + "'"); } count = session.ExecuteSql(sqlBuilder.ToString()); session.CommitTs(); } return count; } catch (Exception ex) { throw; } } #endregion #region 定时删除超过六个月的通知浏览记录 /// /// 定时删除超过六个月的通知浏览记录 /// /// 删除个数 public int DeleteNoticeBrowse() { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); int count = 0; DateTime deleteDate = DateTime.Now.AddMonths(-6); try { using (IDataSession session = AppDataFactory.CreateMainSession()) { session.OpenTs(); //删除信息 sqlBuilder.Append(" DELETE FROM T_QM_NOTICEBROWSE "); sqlBuilder.Append(" WHERE NOTICEID IN( "); sqlBuilder.Append(" SELECT NOTICEID FROM T_QM_NOTICE WHERE "); sqlBuilder.Append(" USETIME <= to_date('" + deleteDate + "','YYYY-mm-dd hh24:mi:ss')) "); count = session.ExecuteSql(sqlBuilder.ToString()); session.CommitTs(); } return count; } catch (Exception ex) { throw; } } #endregion #endregion #region 获取用户信息 /// /// 获取用户信息信息 /// /// 条件 /// 用户信息信息 public User Get(User user) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //获取用户信息 sqlBuilder.Append(" SELECT *"); sqlBuilder.Append(" FROM T_QM_USER A WHERE USERID = '" + user.UserID + "'"); user = session.Get(sqlBuilder.ToString()); } return user; } catch (Exception ex) { throw ex; } } #endregion #region 获取导出通知信息 /// /// 获取导出通知信息 /// /// 查询条件 /// 数据 public DataTable GetExportData(NoticeInfo model) { DataTable dt = null; string sql = null; List parameters = new List(); try { //构成查询语句 sql = GetListSQL(model, ref parameters); sql += " ORDER BY CREATEDATE DESC"; using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); dt = session.GetTable(sql, parameters.ToArray()); } dt.TableName = "NoticeInfo"; return dt; } catch (Exception ex) { throw; } } #endregion #region 获取组织机构下人员列表 /// /// 获取组织机构下人员列表 /// /// 条件 /// 同级别菜单列表 public DataPage GetNoticeUserList(string orgaID, DataPage page) { List parameters = new List(); string sql = ""; StringBuilder sqlBuilder = new StringBuilder(); try { //分页关键字段及排序 page.KeyName = "USERID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "UPDATEDATE DESC"; } using (IDataSession session = AppDataFactory.CreateMainSession()) { sqlBuilder.Append(" SELECT A.USERID, "); sqlBuilder.Append(" A.LOGINUSERID,A.EMPLOYEENO,A.USERNAME,A.PASSWORD,A.TEL,B.ORGADESC "); sqlBuilder.Append(" ,A.MOBILETEL,A.EMAIL,A.CREATEUSER,A.CREATEDATE,A.UPDATEUSER,A.UPDATEDATE,A.USERTYPE "); sqlBuilder.Append(" ,A.FLGDEL,(CASE WHEN A.ACTIVATEFLG ='0' THEN '否' else '是' end) as ACTIVATEFLG "); sqlBuilder.Append(" FROM T_QM_USER A LEFT JOIN T_QM_ORGANIZATION B ON B.ORGAID=A.ORGANID WHERE A.FLGDEL ='0' "); sqlBuilder.Append(" AND A.ACTIVATEFLG = '1' "); //组织机构 if (!string.IsNullOrEmpty(orgaID)) { sqlBuilder.Append(" AND A.ORGANID = @ORGANID"); parameters.Add(new DataParameter { ParameterName = "ORGANID", DataType = DbType.String, Value = orgaID }); } sql =this.ChangeSqlByDB(sqlBuilder.ToString(),session); page = session.GetDataPage(sql, parameters.ToArray(), page); } return page; } catch (Exception ex) { throw ex; } } #endregion #region 获取组织机构树菜单列表 /// /// 获取所有组织机构树菜单列表 /// /// 条件 /// 同级别菜单列表 public List GetAllList() { List parameters = new List(); List list = null; string sql = null; try { sql = "SELECT * FROM T_QM_ORGANIZATION WHERE FLGDEL='0'"; using (IDataSession session = AppDataFactory.CreateMainSession()) { list = session.GetList(sql, parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { throw; } } #endregion #region 判断当前通知是否存已存在 /// /// 判断当前通知是否存已存在 /// /// 信息 public bool OverTimeRemindNoticeExists(NoticeInfo notice) { List parameters = new List(); int count = 0; string sql = null; try { sql = "SELECT COUNT(*) FROM T_QM_NOTICE WHERE NOTICETITLE=@NOTICETITLE AND USETIME = TO_DATE('" + notice.USETIME.ToString("yyyy-MM-dd") + "','YYYY-MM-DD')"; parameters.Add(new DataParameter { ParameterName = "NOTICETITLE", DataType = DbType.String, Value = notice.NOTICETITLE }); using (IDataSession session = AppDataFactory.CreateMainSession()) { sql = this.ChangeSqlByDB(sql, session); count = Convert.ToInt32(session.ExecuteSqlScalar(sql, parameters.ToArray())); } return count > 0; } catch (Exception ex) { throw ex; } } #endregion } }