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.

IMMsgService.cs 9.5 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using Learun.DataBase.Repository;
  2. using Learun.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. namespace Learun.Application.IM
  7. {
  8. /// <summary>
  9. /// 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架
  10. /// Copyright (c) 2013-2018 上海力软信息技术有限公司
  11. /// 创建人:力软-框架开发组
  12. /// 日 期:2017.04.17
  13. /// 描 述:即时通讯消息内容
  14. /// </summary>
  15. public class IMMsgService : RepositoryFactory
  16. {
  17. #region 构造函数和属性
  18. private string fieldSql;
  19. public IMMsgService()
  20. {
  21. fieldSql = @"
  22. t.F_MsgId,
  23. t.F_IsSystem,
  24. t.F_SendUserId,
  25. t.F_RecvUserId,
  26. t.F_Content,
  27. t.F_CreateDate
  28. ";
  29. }
  30. #endregion
  31. #region 获取数据
  32. /// <summary>
  33. /// 获取列表数据(最近的10条聊天记录)
  34. /// <summary>
  35. /// <returns></returns>
  36. public IEnumerable<IMMsgEntity> GetList(string sendUserId, string recvUserId)
  37. {
  38. try
  39. {
  40. var strSql = new StringBuilder();
  41. strSql.Append("SELECT ");
  42. strSql.Append(fieldSql);
  43. strSql.Append(" FROM LR_IM_Msg t where (t.F_SendUserId = @sendUserId and t.F_RecvUserId = @recvUserId ) or (t.F_SendUserId = @recvUserId and t.F_RecvUserId = @sendUserId ) ");
  44. Pagination pagination = new Pagination();
  45. pagination.page = 1;
  46. pagination.rows = 10;
  47. pagination.sidx = "F_CreateDate";
  48. pagination.sord = "DESC";
  49. this.BaseRepository().ExecuteBySql(" Update LR_IM_Contacts Set F_ISREAD = 2 where F_MyUserId = @sendUserId AND F_OtherUserId = @recvUserId ",new { sendUserId , recvUserId });
  50. return this.BaseRepository().FindList<IMMsgEntity>(strSql.ToString(),new { sendUserId, recvUserId }, pagination);
  51. }
  52. catch (Exception ex)
  53. {
  54. if (ex is ExceptionEx)
  55. {
  56. throw;
  57. }
  58. else
  59. {
  60. throw ExceptionEx.ThrowServiceException(ex);
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// 获取列表数据(小于某个时间点的5条记录)
  66. /// </summary>
  67. /// <param name="myUserId">我的ID</param>
  68. /// <param name="otherUserId">对方的ID</param>
  69. /// <param name="time">时间</param>
  70. /// <returns></returns>
  71. public IEnumerable<IMMsgEntity> GetListByTime(string myUserId, string otherUserId, DateTime time)
  72. {
  73. try
  74. {
  75. var strSql = new StringBuilder();
  76. strSql.Append("SELECT ");
  77. strSql.Append(fieldSql);
  78. strSql.Append(" FROM LR_IM_Msg t where ((t.F_SendUserId = @myUserId and t.F_RecvUserId = @otherUserId ) or (t.F_SendUserId = @otherUserId and t.F_RecvUserId = @myUserId )) AND t.F_CreateDate <= @time ");
  79. Pagination pagination = new Pagination();
  80. pagination.page = 1;
  81. pagination.rows = 5;
  82. pagination.sidx = "F_CreateDate";
  83. pagination.sord = "DESC";
  84. return this.BaseRepository().FindList<IMMsgEntity>(strSql.ToString(), new { myUserId, otherUserId, time }, pagination);
  85. }
  86. catch (Exception ex)
  87. {
  88. if (ex is ExceptionEx)
  89. {
  90. throw;
  91. }
  92. else
  93. {
  94. throw ExceptionEx.ThrowServiceException(ex);
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// 获取列表数据(大于某个时间的所有数据)
  100. /// </summary>
  101. /// <param name="myUserId">我的ID</param>
  102. /// <param name="otherUserId">对方的ID</param>
  103. /// <param name="time">时间</param>
  104. /// <returns></returns>
  105. public IEnumerable<IMMsgEntity> GetListByTime2(string myUserId, string otherUserId, DateTime time)
  106. {
  107. try
  108. {
  109. time = time.AddSeconds(1);
  110. var strSql = new StringBuilder();
  111. strSql.Append("SELECT ");
  112. strSql.Append(fieldSql);
  113. strSql.Append(" FROM LR_IM_Msg t where ((t.F_SendUserId = @myUserId and t.F_RecvUserId = @otherUserId ) or (t.F_SendUserId = @otherUserId and t.F_RecvUserId = @myUserId )) AND t.F_CreateDate >= @time Order By F_CreateDate ASC ");
  114. return this.BaseRepository().FindList<IMMsgEntity>(strSql.ToString(), new { myUserId, otherUserId, time });
  115. }
  116. catch (Exception ex)
  117. {
  118. if (ex is ExceptionEx)
  119. {
  120. throw;
  121. }
  122. else
  123. {
  124. throw ExceptionEx.ThrowServiceException(ex);
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// 获取列表分页数据
  130. /// <summary>
  131. /// <param name="pagination">分页参数</param>
  132. /// <param name="sendUserId"></param>
  133. /// <param name="recvUserId"></param>
  134. /// <param name="keyword"></param>
  135. /// <returns></returns>
  136. public IEnumerable<IMMsgEntity> GetPageList(Pagination pagination, string sendUserId, string recvUserId,string keyword)
  137. {
  138. try
  139. {
  140. var strSql = new StringBuilder();
  141. strSql.Append("SELECT ");
  142. strSql.Append(fieldSql);
  143. strSql.Append(" FROM LR_IM_Msg t where ((t.F_SendUserId = @sendUserId and t.F_RecvUserId = @recvUserId ) or (t.F_SendUserId = @recvUserId and t.F_RecvUserId = @sendUserId )) ");
  144. if (!string.IsNullOrEmpty(keyword)) {
  145. keyword = "%" + keyword + "%";
  146. strSql.Append(" AND F_Content like @keyword ");
  147. }
  148. return this.BaseRepository().FindList<IMMsgEntity>(strSql.ToString(), new { sendUserId, recvUserId, keyword }, pagination);
  149. }
  150. catch (Exception ex)
  151. {
  152. if (ex is ExceptionEx)
  153. {
  154. throw;
  155. }
  156. else
  157. {
  158. throw ExceptionEx.ThrowServiceException(ex);
  159. }
  160. }
  161. }
  162. #endregion
  163. #region 提交数据
  164. /// <summary>
  165. /// 删除实体数据
  166. /// <param name="keyValue">主键</param>
  167. /// <summary>
  168. /// <returns></returns>
  169. public void DeleteEntity(string keyValue)
  170. {
  171. try
  172. {
  173. this.BaseRepository().Delete<IMMsgEntity>(t=>t.F_MsgId == keyValue);
  174. }
  175. catch (Exception ex)
  176. {
  177. if (ex is ExceptionEx)
  178. {
  179. throw;
  180. }
  181. else
  182. {
  183. throw ExceptionEx.ThrowServiceException(ex);
  184. }
  185. }
  186. }
  187. /// <summary>
  188. /// 保存实体数据(新增)
  189. /// <param name="entity">实体数据</param>
  190. /// <summary>
  191. /// <returns></returns>
  192. public void SaveEntity(IMMsgEntity entity)
  193. {
  194. IMContactsEntity myContacts = new IMContactsEntity();
  195. IMContactsEntity otherContacts = new IMContactsEntity();
  196. myContacts.F_MyUserId = entity.F_SendUserId;
  197. myContacts.F_OtherUserId = entity.F_RecvUserId;
  198. myContacts.F_Content = entity.F_Content;
  199. otherContacts.F_MyUserId = entity.F_RecvUserId;
  200. otherContacts.F_OtherUserId = entity.F_SendUserId;
  201. otherContacts.F_Content = entity.F_Content;
  202. IMContactsEntity myContactsTmp = this.BaseRepository().FindEntity<IMContactsEntity>(t => t.F_MyUserId.Equals(myContacts.F_MyUserId) && t.F_OtherUserId.Equals(myContacts.F_OtherUserId));
  203. IMContactsEntity otherContactsTmp = this.BaseRepository().FindEntity<IMContactsEntity>(t => t.F_MyUserId.Equals(otherContacts.F_MyUserId) && t.F_OtherUserId.Equals(otherContacts.F_OtherUserId));
  204. var db = this.BaseRepository().BeginTrans();
  205. try
  206. {
  207. myContacts.F_IsRead = 2;
  208. if (myContactsTmp == null)
  209. {
  210. myContacts.Create();
  211. db.Insert(myContacts);
  212. }
  213. else
  214. {
  215. myContacts.Modify(myContactsTmp.F_Id);
  216. db.Update(myContacts);
  217. }
  218. otherContacts.F_IsRead = 1;
  219. if (otherContactsTmp == null)
  220. {
  221. otherContacts.Create();
  222. db.Insert(otherContacts);
  223. }
  224. else
  225. {
  226. otherContacts.Modify(otherContactsTmp.F_Id);
  227. db.Update(otherContacts);
  228. }
  229. entity.Create();
  230. db.Insert(entity);
  231. db.Commit();
  232. }
  233. catch (Exception ex)
  234. {
  235. db.Rollback();
  236. if (ex is ExceptionEx)
  237. {
  238. throw;
  239. }
  240. else
  241. {
  242. throw ExceptionEx.ThrowServiceException(ex);
  243. }
  244. }
  245. }
  246. #endregion
  247. }
  248. }