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.
 
 
 
 
 
 

205 lines
6.1 KiB

  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. /// 日 期:2018.05.31
  13. /// 描 述:最近联系人列表
  14. /// </summary>
  15. public class IMContactsService: RepositoryFactory
  16. {
  17. #region 构造函数和属性
  18. private string fieldSql;
  19. public IMContactsService()
  20. {
  21. fieldSql = @"
  22. t.F_Id,
  23. t.F_MyUserId,
  24. t.F_OtherUserId,
  25. t.F_Content,
  26. t.F_Time,
  27. t.F_IsRead
  28. ";
  29. }
  30. #endregion
  31. #region 获取数据
  32. /// <summary>
  33. /// 获取列表数据
  34. /// </summary>
  35. /// <param name="userId">用户Id</param>
  36. /// <returns></returns>
  37. public IEnumerable<IMContactsEntity> GetList(string userId)
  38. {
  39. try
  40. {
  41. var strSql = new StringBuilder();
  42. strSql.Append("SELECT ");
  43. strSql.Append(fieldSql);
  44. strSql.Append(" FROM LR_IM_Contacts t where t.F_MyUserId = @userId Order By t.F_Time Desc ");
  45. return this.BaseRepository().FindList<IMContactsEntity>(strSql.ToString(), new { userId = userId });
  46. }
  47. catch (Exception ex)
  48. {
  49. if (ex is ExceptionEx)
  50. {
  51. throw;
  52. }
  53. else
  54. {
  55. throw ExceptionEx.ThrowServiceException(ex);
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// 获取列表数据
  61. /// </summary>
  62. /// <param name="userId">用户Id</param>
  63. /// <param name="time">时间</param>
  64. /// <returns></returns>
  65. public IEnumerable<IMContactsEntity> GetList(string userId, DateTime time)
  66. {
  67. try
  68. {
  69. var strSql = new StringBuilder();
  70. strSql.Append("SELECT ");
  71. strSql.Append(fieldSql);
  72. strSql.Append(" FROM LR_IM_Contacts t where t.F_MyUserId = @userId AND t.F_Time >= @time Order By t.F_Time Asc ");
  73. return this.BaseRepository().FindList<IMContactsEntity>(strSql.ToString(), new { userId, time });
  74. }
  75. catch (Exception ex)
  76. {
  77. if (ex is ExceptionEx)
  78. {
  79. throw;
  80. }
  81. else
  82. {
  83. throw ExceptionEx.ThrowServiceException(ex);
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// 获取实体
  89. /// </summary>
  90. /// <param name="userId">发送人</param>
  91. /// <param name="otherUserId">接收人</param>
  92. /// <returns></returns>
  93. public IMContactsEntity GetEntity(string userId, string otherUserId)
  94. {
  95. try
  96. {
  97. return this.BaseRepository().FindEntity<IMContactsEntity>(t=>t.F_MyUserId.Equals(userId) && t.F_OtherUserId.Equals(otherUserId));
  98. }
  99. catch (Exception ex)
  100. {
  101. if (ex is ExceptionEx)
  102. {
  103. throw;
  104. }
  105. else
  106. {
  107. throw ExceptionEx.ThrowServiceException(ex);
  108. }
  109. }
  110. }
  111. #endregion
  112. #region 提交数据
  113. /// <summary>
  114. /// 保存实体数据(新增、修改)
  115. /// <param name="keyValue">主键</param>
  116. /// <summary>
  117. /// <returns></returns>
  118. public void SaveEntity(IMContactsEntity entity)
  119. {
  120. try
  121. {
  122. IMContactsEntity entity2 = GetEntity(entity.F_MyUserId, entity.F_OtherUserId);
  123. entity.F_IsRead = 2;
  124. if (entity2 == null)
  125. {
  126. entity.Create();
  127. this.BaseRepository().Insert(entity);
  128. }
  129. else {
  130. entity.Modify(entity2.F_Id);
  131. this.BaseRepository().Update(entity);
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. if (ex is ExceptionEx)
  137. {
  138. throw;
  139. }
  140. else
  141. {
  142. throw ExceptionEx.ThrowServiceException(ex);
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// 更新记录读取状态
  148. /// </summary>
  149. /// <param name="myUserId">自己本身用户ID</param>
  150. /// <param name="otherUserId">对方用户ID</param>
  151. public void UpdateState(string myUserId, string otherUserId)
  152. {
  153. try
  154. {
  155. IMContactsEntity entity = GetEntity(myUserId, otherUserId);
  156. if (entity != null) {
  157. entity.F_IsRead = 2;
  158. this.BaseRepository().Update(entity);
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. if (ex is ExceptionEx)
  164. {
  165. throw;
  166. }
  167. else
  168. {
  169. throw ExceptionEx.ThrowServiceException(ex);
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 删除最近联系人
  175. /// </summary>
  176. /// <param name="myUserId">发起者id</param>
  177. public void DeleteEntity(string myUserId, string otherUserId)
  178. {
  179. try
  180. {
  181. this.BaseRepository().Delete<IMContactsEntity>(t=>t.F_MyUserId.Equals(myUserId) && t.F_OtherUserId.Equals(otherUserId));
  182. }
  183. catch (Exception ex)
  184. {
  185. if (ex is ExceptionEx)
  186. {
  187. throw;
  188. }
  189. else
  190. {
  191. throw ExceptionEx.ThrowServiceException(ex);
  192. }
  193. }
  194. }
  195. #endregion
  196. }
  197. }