using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Text;
namespace Learun.Application.IM
{
///
/// 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架
/// Copyright (c) 2013-2018 上海力软信息技术有限公司
/// 创建人:力软-框架开发组
/// 日 期:2018.05.31
/// 描 述:最近联系人列表
///
public class IMContactsService: RepositoryFactory
{
#region 构造函数和属性
private string fieldSql;
public IMContactsService()
{
fieldSql = @"
t.F_Id,
t.F_MyUserId,
t.F_OtherUserId,
t.F_Content,
t.F_Time,
t.F_IsRead
";
}
#endregion
#region 获取数据
///
/// 获取列表数据
///
/// 用户Id
///
public IEnumerable GetList(string userId)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_IM_Contacts t where t.F_MyUserId = @userId Order By t.F_Time Desc ");
return this.BaseRepository().FindList(strSql.ToString(), new { userId = userId });
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取列表数据
///
/// 用户Id
/// 时间
///
public IEnumerable GetList(string userId, DateTime time)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(fieldSql);
strSql.Append(" FROM LR_IM_Contacts t where t.F_MyUserId = @userId AND t.F_Time >= @time Order By t.F_Time Asc ");
return this.BaseRepository().FindList(strSql.ToString(), new { userId, time });
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取实体
///
/// 发送人
/// 接收人
///
public IMContactsEntity GetEntity(string userId, string otherUserId)
{
try
{
return this.BaseRepository().FindEntity(t=>t.F_MyUserId.Equals(userId) && t.F_OtherUserId.Equals(otherUserId));
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(IMContactsEntity entity)
{
try
{
IMContactsEntity entity2 = GetEntity(entity.F_MyUserId, entity.F_OtherUserId);
entity.F_IsRead = 2;
if (entity2 == null)
{
entity.Create();
this.BaseRepository().Insert(entity);
}
else {
entity.Modify(entity2.F_Id);
this.BaseRepository().Update(entity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 更新记录读取状态
///
/// 自己本身用户ID
/// 对方用户ID
public void UpdateState(string myUserId, string otherUserId)
{
try
{
IMContactsEntity entity = GetEntity(myUserId, otherUserId);
if (entity != null) {
entity.F_IsRead = 2;
this.BaseRepository().Update(entity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 删除最近联系人
///
/// 发起者id
public void DeleteEntity(string myUserId, string otherUserId)
{
try
{
this.BaseRepository().Delete(t=>t.F_MyUserId.Equals(myUserId) && t.F_OtherUserId.Equals(otherUserId));
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}