IMContentService.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using LeaRun.Application.Entity.MessageManage;
  2. using LeaRun.Application.IService.MessageManage;
  3. using LeaRun.Data;
  4. using LeaRun.Data.Repository;
  5. using LeaRun.Util;
  6. using LeaRun.Util.Extension;
  7. using LeaRun.Util.WebControl;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Text;
  13. namespace LeaRun.Application.Service.MessageManage
  14. {
  15. /// <summary>
  16. /// 版 本 V6.1
  17. /// Copyright (c) 2013-2016 上海力软信息技术有限公司
  18. /// 创建人:陈彬彬
  19. /// 日 期:2015.11.26 19:39
  20. /// 描 述:即时通信群组管理
  21. /// </summary>
  22. public class IMContentService : RepositoryFactory, IMsgContentService
  23. {
  24. /// <summary>
  25. /// 获取消息列表(单对单)
  26. /// </summary>
  27. /// <param name="pagination"></param>
  28. /// <param name="queryJson"></param>
  29. /// <returns></returns>
  30. public IEnumerable<IMReadModel> GetList(Pagination pagination, string userId, string sendId,string flag = "1")
  31. {
  32. var strSql = new StringBuilder();
  33. strSql.Append(@"SELECT * from(
  34. SELECT t1.ReadId ,
  35. t1.ContentId ,
  36. t1.CreateDate ,
  37. t1.UserId ,
  38. t2.SendId ,
  39. t2.MsgContent as Content
  40. FROM IM_Read t1
  41. INNER JOIN IM_Content t2 ON t1.ContentId = t2.ContentId and t2.IsGroup = 0)T
  42. WHERE 1=1 ");
  43. var parameter = new List<DbParameter>();
  44. //信息接收者
  45. if (!userId.IsEmpty())
  46. {
  47. if (flag == "1" && !sendId.IsEmpty())//获取一个聊天窗口的消息记录
  48. {
  49. strSql.Append(" AND ( T.UserId = @UserId or T.UserId = @UserId2 )");
  50. parameter.Add(DbParameters.CreateDbParameter("@UserId", userId.ToString()));
  51. parameter.Add(DbParameters.CreateDbParameter("@UserId2", sendId.ToString()));
  52. }
  53. else
  54. {
  55. strSql.Append(" AND T.UserId = @UserId");
  56. parameter.Add(DbParameters.CreateDbParameter("@UserId", userId.ToString()));
  57. }
  58. }
  59. //信息发送者
  60. if (!sendId.IsEmpty())
  61. {
  62. if (flag == "1" && !userId.IsEmpty())//获取一个聊天窗口的消息记录
  63. {
  64. strSql.Append(" AND ( T.SendId = @SendId or T.SendId = @SendId2 )");
  65. parameter.Add(DbParameters.CreateDbParameter("@SendId", sendId.ToString()));
  66. parameter.Add(DbParameters.CreateDbParameter("@SendId2", userId.ToString()));
  67. }
  68. else
  69. {
  70. strSql.Append(" AND T.SendId = @SendId");
  71. parameter.Add(DbParameters.CreateDbParameter("@SendId", sendId.ToString()));
  72. }
  73. }
  74. return this.BaseRepository().FindList<IMReadModel>(strSql.ToString(), parameter.ToArray(), pagination);
  75. }
  76. /// <summary>
  77. /// 获取消息列表(群组)
  78. /// </summary>
  79. /// <param name="pagination"></param>
  80. /// <param name="queryJson"></param>
  81. /// <returns></returns>
  82. public IEnumerable<IMReadModel> GetListByGroupId(Pagination pagination, string queryJson)
  83. {
  84. var strSql = new StringBuilder();
  85. strSql.Append(@"SELECT t.ContentId ,
  86. t.SendId ,
  87. t.ToId ,
  88. t.MsgContent ,
  89. t.CreateDate
  90. FROM IM_Content t
  91. WHERE t.IsGroup = 1");
  92. var parameter = new List<DbParameter>();
  93. var queryParam = queryJson.ToJObject();
  94. //群组Id
  95. if (!queryParam["groupId"].IsEmpty())
  96. {
  97. strSql.Append(" AND t.ToId = @ToId");
  98. parameter.Add(DbParameters.CreateDbParameter("@ToId", queryParam["groupId"].ToString()));
  99. }
  100. return this.BaseRepository().FindList<IMReadModel>(strSql.ToString(), parameter.ToArray(), pagination);
  101. }
  102. /// <summary>
  103. /// 获取消息数量列表
  104. /// </summary>
  105. /// <param name="queryJson">readStatus,userId</param>
  106. /// <returns></returns>
  107. public IEnumerable<IMReadNumModel> GetReadList(string userId)
  108. {
  109. var strSql = new StringBuilder();
  110. strSql.Append(@"SELECT
  111. SUM( CASE WHEN t1.ReadStatus = 0 THEN 1 ELSE 0 END )AS unReadNum,
  112. t1.SendId,
  113. t1.UserId
  114. FROM
  115. IM_Read t1
  116. WHERE
  117. 1 = 1
  118. ");
  119. var parameter = new List<DbParameter>();
  120. //接收者Id
  121. if (!userId.IsEmpty())
  122. {
  123. strSql.Append(" AND ( t1.UserId = @UserId OR t1.SendId = @UserId )");
  124. parameter.Add(DbParameters.CreateDbParameter("@UserId", userId.ToString()));
  125. }
  126. strSql.Append(@" GROUP BY t1.SendId, t1.UserId
  127. ORDER BY MAX (t1.CreateDate) DESC");
  128. return this.BaseRepository().FindList<IMReadNumModel>(strSql.ToString(), parameter.ToArray());
  129. }
  130. /// <summary>
  131. /// 获取某用户某种消息的总数
  132. /// </summary>
  133. /// <param name="userId"></param>
  134. /// <param name="status"></param>
  135. /// <returns></returns>
  136. public DataTable GetReadAllNum(string userId, string status)
  137. {
  138. var strSql = new StringBuilder();
  139. strSql.Append(@"SELECT COUNT(ReadId) AS unReadNum
  140. FROM IM_Read
  141. WHERE 1 = 1 ");
  142. var parameter = new List<DbParameter>();
  143. //消息状态
  144. if (!status.IsEmpty())
  145. {
  146. strSql.Append(" AND ReadStatus = @ReadStatus");
  147. parameter.Add(DbParameters.CreateDbParameter("@ReadStatus", status));
  148. }
  149. //接收者Id
  150. if (!userId.IsEmpty())
  151. {
  152. strSql.Append(" AND UserId = @UserId");
  153. parameter.Add(DbParameters.CreateDbParameter("@UserId", userId.ToString()));
  154. }
  155. return this.BaseRepository().FindTable(strSql.ToString(), parameter.ToArray());
  156. }
  157. #region 提交数据
  158. /// <summary>
  159. /// 增加一条消息内容
  160. /// </summary>
  161. /// <param name="entity"></param>
  162. public void Add(IMContentEntity entity, DataTable dtGroupUserId)
  163. {
  164. IDatabase db = DbFactory.Base().BeginTrans();
  165. try
  166. {
  167. //增加一条消息内容
  168. entity.Create();
  169. db.Insert<IMContentEntity>(entity);
  170. if (entity.IsGroup == 1)
  171. {
  172. foreach (DataRow item in dtGroupUserId.Rows)
  173. {
  174. IMReadEntity msgreadentity = new IMReadEntity();
  175. msgreadentity.Create();
  176. msgreadentity.ContentId = entity.ContentId;
  177. msgreadentity.UserId = item["userId"].ToString();
  178. msgreadentity.SendId = entity.ToId;//群组消息发送者为群组Id
  179. msgreadentity.CreateUserId = entity.CreateUserId;
  180. msgreadentity.CreateUserName = entity.CreateUserName;
  181. msgreadentity.ReadStatus = 0;
  182. db.Insert<IMReadEntity>(msgreadentity);
  183. }
  184. }
  185. else
  186. {
  187. IMReadEntity msgreadentity = new IMReadEntity();
  188. msgreadentity.Create();
  189. msgreadentity.ContentId = entity.ContentId;
  190. msgreadentity.UserId = entity.ToId;
  191. msgreadentity.SendId = entity.SendId;
  192. msgreadentity.CreateUserId = entity.CreateUserId;
  193. msgreadentity.CreateUserName = entity.CreateUserName;
  194. msgreadentity.ReadStatus = 0;
  195. db.Insert<IMReadEntity>(msgreadentity);
  196. }
  197. db.Commit();
  198. }
  199. catch (Exception)
  200. {
  201. db.Rollback();
  202. throw;
  203. }
  204. }
  205. /// <summary>
  206. /// 更新消息状态
  207. /// </summary>
  208. /// <param name="userId"></param>
  209. /// <param name="sendId"></param>
  210. /// <param name="status"></param>
  211. /// <returns></returns>
  212. public int Update(string userId, string sendId, string status)
  213. {
  214. var strSql = new StringBuilder();
  215. strSql.Append("UPDATE IM_Read");
  216. var parameter = new List<DbParameter>();
  217. //更新状态
  218. if (!status.IsEmpty())
  219. {
  220. strSql.Append(" SET ReadStatus = @ReadStatus");
  221. parameter.Add(DbParameters.CreateDbParameter("@ReadStatus", status));
  222. }
  223. strSql.Append(" WHERE 1 = 1 ");
  224. //发送者
  225. if (!sendId.IsEmpty())
  226. {
  227. strSql.Append(" AND SendId = @SendId");
  228. parameter.Add(DbParameters.CreateDbParameter("@SendId", sendId));
  229. }
  230. //接收者
  231. if (!userId.IsEmpty())
  232. {
  233. strSql.Append(" AND UserId = @UserId");
  234. parameter.Add(DbParameters.CreateDbParameter("@UserId", userId));
  235. }
  236. return this.BaseRepository().ExecuteBySql(strSql.ToString(), parameter.ToArray());
  237. }
  238. #endregion
  239. }
  240. }