public LastReadedMessage GetLastReadedMessageForEmployee(IUnitOfWork uow, Chat chat, Employee employee) { LastReadedMessage lastReadedAlias = null; return(uow.Session.QueryOver <LastReadedMessage> (() => lastReadedAlias) .Where(() => lastReadedAlias.Chat.Id == chat.Id) .Where(() => lastReadedAlias.Employee.Id == employee.Id) .SingleOrDefault()); }
public static IList <UnreadedChatDTO> GetUnreadedChatMessages(IUnitOfWork uow, Employee forEmployee, bool accessLogisticChat) { ChatMessage chatMessageAlias = null; Chat chatAlias = null; LastReadedMessage lastReadedAlias = null; Employee driverAlias = null; UnreadedChatDTO resultAlias = null; var chatQuery = uow.Session.QueryOver <Chat>(() => chatAlias); if (!accessLogisticChat) { chatQuery.Where(x => x.ChatType != ChatType.DriverAndLogists); } var resultList = chatQuery.JoinAlias(() => chatAlias.LastReaded, () => lastReadedAlias, NHibernate.SqlCommand.JoinType.LeftOuterJoin, Restrictions.Where(() => lastReadedAlias.Employee.Id == forEmployee.Id)) .JoinAlias(() => chatAlias.Messages, () => chatMessageAlias) .JoinAlias(() => chatAlias.Driver, () => driverAlias, NHibernate.SqlCommand.JoinType.LeftOuterJoin) .Where(() => (lastReadedAlias.LastDateTime == null && chatMessageAlias.DateTime > forEmployee.CreationDate) || (lastReadedAlias.LastDateTime != null && chatMessageAlias.DateTime > lastReadedAlias.LastDateTime)) .SelectList(list => list .SelectGroup(() => chatAlias.Id).WithAlias(() => resultAlias.ChatId) .Select(() => chatAlias.ChatType).WithAlias(() => resultAlias.ChatType) .Select(() => driverAlias.Id).WithAlias(() => resultAlias.EmployeeId) .Select(() => driverAlias.LastName).WithAlias(() => resultAlias.EmployeeLastName) .Select(() => driverAlias.Name).WithAlias(() => resultAlias.EmployeeName) .Select(() => driverAlias.Patronymic).WithAlias(() => resultAlias.EmployeePatronymic) .SelectCount(() => chatMessageAlias.Id).WithAlias(() => resultAlias.UnreadedMessagesTotal) .Select(Projections.SqlFunction( //Использована проекция, так как при вызове встроенной функции тип получатеся bool new SQLFunctionTemplate(NHibernateUtil.Int32, "SUM( ?1 )"), NHibernateUtil.Int32, Projections.Property(() => chatMessageAlias.IsAutoCeated))) .WithAlias(() => resultAlias.UnreadedMessagesAuto) ).TransformUsing(Transformers.AliasToBean <UnreadedChatDTO> ()) .List <UnreadedChatDTO>(); return(resultList); }