using Dapper; using Learun.DataBase.Repository; using Learun.Util; using System; using System.Collections.Generic; using System.Data; using System.Text; using Learun.Application.Organization; namespace Learun.Application.TwoDevelopment.EducationalAdministration { /// /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创 建:超级管理员 /// 日 期:2019-03-06 17:15 /// 描 述:日志发送 /// public class JournalSendService : RepositoryFactory { #region 获取数据 /// /// 获取页面显示列表数据 /// /// 查询参数 /// public IEnumerable GetPageList(Pagination pagination, string queryJson) { try { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(@" JournalSendId, JTitle, JTypeId, JSenderId, JSender, JReceiveId, JReceive, JIsSend, JSendTime "); strSql.Append(" FROM JournalSend t "); strSql.Append(" WHERE 1=1 "); var queryParam = queryJson.ToJObject(); // 虚拟参数 var dp = new DynamicParameters(new { }); if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty()) { dp.Add("startTime", queryParam["StartTime"].ToDate(), DbType.DateTime); dp.Add("endTime", queryParam["EndTime"].ToDate(), DbType.DateTime); strSql.Append(" AND ( JSendTime >= @startTime AND JSendTime <= @endTime ) "); } if (!queryParam["keyword"].IsEmpty()) { dp.Add("keyword", "%" + queryParam["keyword"].ToString() + "%", DbType.String); strSql.Append(" AND JTitle Like @keyword "); } if (!queryParam["userId"].IsEmpty()) { dp.Add("userId", "" + queryParam["userId"].ToString() + "", DbType.String); strSql.Append(" AND JSenderId=@userId "); } return this.BaseRepository().FindList(strSql.ToString(), dp, pagination); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取页面显示列表数据 /// /// 查询参数 /// public IEnumerable GetList(string queryJson) { try { var strSql = new StringBuilder(); strSql.Append("SELECT "); strSql.Append(@" t.JournalSendId, t.JTitle, t.JTypeId, t.JReceiveId, t.JContent "); strSql.Append(" FROM JournalSend t "); strSql.Append(" WHERE 1=1 "); var queryParam = queryJson.ToJObject(); // 虚拟参数 var dp = new DynamicParameters(new { }); if (!queryParam["JTitle"].IsEmpty()) { dp.Add("JTitle", "%" + queryParam["JTitle"].ToString() + "%", DbType.String); strSql.Append(" AND t.JTitle Like @JTitle "); } if (!queryParam["JTypeId"].IsEmpty()) { dp.Add("JTypeId", queryParam["JTypeId"].ToString(), DbType.String); strSql.Append(" AND t.JTypeId = @JTypeId "); } if (!queryParam["JReceiveId"].IsEmpty()) { dp.Add("JReceiveId", queryParam["JReceiveId"].ToString(), DbType.String); strSql.Append(" AND t.JReceiveId = @JReceiveId "); } return this.BaseRepository().FindList(strSql.ToString(), dp); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取JournalSend表实体数据 /// 主键 /// /// public JournalSendEntity GetJournalSendEntity(string keyValue) { try { return this.BaseRepository().FindEntity(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 获取JournalSend表实体数据 /// 主键 /// /// public JournalSendEntity GetJournalSendEntityNoHtml(string keyValue) { try { var result = this.BaseRepository().FindEntity(keyValue); if (result != null) { result.JContent = Str.ReplaceHtml(result.JContent); } return result; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } #endregion #region 提交数据 /// /// 删除实体数据 /// 主键 /// /// public void DeleteEntity(string keyValue) { try { this.BaseRepository().Delete(t => t.JournalSendId == keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 保存实体数据(新增、修改) /// 主键 /// /// public void SaveEntity(string keyValue, JournalSendEntity entity) { try { if (!string.IsNullOrEmpty(keyValue)) { entity.Modify(keyValue); this.BaseRepository().Update(entity); } else { entity.Create(); this.BaseRepository().Insert(entity); } } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } /// /// 保存实体数据(新增、修改) /// 主键 /// /// public void SaveEntity(UserInfo userInfo, string keyValue, JournalSendEntity entity) { try { if (!string.IsNullOrEmpty(keyValue)) { entity.Modify(keyValue, userInfo); this.BaseRepository().Update(entity); } else { entity.Create(userInfo); this.BaseRepository().Insert(entity); } } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } #endregion /// /// 发送 /// /// 日志发送主键 public void Send(string journalSendId) { var db = this.BaseRepository().BeginTrans(); try { var messageentity = GetJournalSendEntity(journalSendId); if (messageentity != null) { messageentity.JIsSend = true; messageentity.JSendTime = DateTime.Now; db.Update(messageentity); if (!string.IsNullOrEmpty(messageentity.JReceiveId)) { List userInfos = new List(); if (messageentity.JReceiveId.IndexOf(',') == -1) { userInfos.Add(new UserEntity { F_UserId = messageentity.JReceiveId, F_RealName = db.FindEntity(x => x.F_UserId == messageentity.JReceiveId)?.F_RealName }); } else { foreach (var rid in messageentity.JReceiveId.Split(',')) { userInfos.Add(new UserEntity { F_UserId = rid, F_RealName = db.FindEntity(m => m.F_UserId == rid)?.F_RealName }); } } foreach (var uitem in userInfos) { JournalReceiveEntity receiveMessageEntity = new JournalReceiveEntity(); receiveMessageEntity.Create(); receiveMessageEntity.JSenderId = messageentity.JSenderId; receiveMessageEntity.JSender = messageentity.JSender; receiveMessageEntity.JReceiveId = uitem.F_UserId; receiveMessageEntity.JReceive = uitem.F_RealName; receiveMessageEntity.JTitle = messageentity.JTitle; receiveMessageEntity.JTypeId = messageentity.JTypeId; receiveMessageEntity.JContent = messageentity.JContent; receiveMessageEntity.JIsRead = false; receiveMessageEntity.JSendTime = DateTime.Now; db.Insert(receiveMessageEntity); } } } db.Commit(); } catch (Exception ex) { db.Rollback(); if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowServiceException(ex); } } } } }