public List <SYS_MSGUSER> QueryMsg(string userId, Guid?msgId, int pageIndex, int pageSize, out int total)
        {
            total = 0;
            List <SYS_MSGUSER> result = null;
            Expression <Func <SYS_MSGUSER, bool> > userIdFunc = f => true;
            Expression <Func <SYS_MSGUSER, bool> > msgIdFunc  = f => true;

            if (!string.IsNullOrWhiteSpace(userId))
            {
                var g = new Guid(userId);
                userIdFunc = f => f.USER_ID == g;
            }
            if (msgId.HasValue)
            {
                msgIdFunc = f => f.MSG_ID == msgId;
            }

            var query = from mu in _msguserRepository.GetAll().Where(x => x.DELETE_FLAG == 0).Where(userIdFunc).Where(msgIdFunc)
                        join msg in _msgRepository.GetAll() on mu.MSG_ID equals msg.Key
                        join su in _userRepository.GetAll() on mu.SENDUSER_ID equals su.Key into mu_su
                        from musu in mu_su.DefaultIfEmpty()
                        join ru in _userRepository.GetAll() on mu.USER_ID equals ru.Key into mu_ru
                        from muru in mu_ru.DefaultIfEmpty()
                        orderby mu.CREATE_DATETIME descending
                        select new
            {
                mu.CREATE_ACCOUNT, mu.CREATE_DATETIME, mu.DELETE_FLAG, mu.LASTUPDATE_ACCOUNT, mu.LASTUPDATE_DATETIME, mu.Key, mu.MSG_ID, mu.SENDUSER_ID, mu.USERDEL_FLAG, mu.USERREAD_FLAG, mu.USER_ID,
                msg.MSGCONT, MsgId = msg.Key, msg.MsgLevel, msg.MSGTITLE, msg.MsgType,
                SendUserId         = musu.Key, SendUserName = musu.USERNAME,
                RecieveUserId      = muru.Key, RecieveUserName = muru.USERNAME
            };

            total = query.Count();

            if (total > 0)
            {
                result = new List <SYS_MSGUSER>();
                foreach (var i in query.Skip((pageIndex - 1) * pageSize).Take(pageSize))
                {
                    var item = new SYS_MSGUSER
                    {
                        CREATE_ACCOUNT     = i.CREATE_ACCOUNT, Key = i.Key, CREATE_DATETIME = i.CREATE_DATETIME, DELETE_FLAG = i.DELETE_FLAG,
                        LASTUPDATE_ACCOUNT = i.LASTUPDATE_ACCOUNT, LASTUPDATE_DATETIME = i.LASTUPDATE_DATETIME, MSG_ID = i.MSG_ID, SENDUSER_ID = i.SENDUSER_ID, USERDEL_FLAG = i.USERDEL_FLAG,
                        USERREAD_FLAG      = i.USERREAD_FLAG, USER_ID = i.USER_ID
                    };
                    item.SYS_MSG = new SYS_MSG {
                        MsgType = i.MsgType, MSGTITLE = i.MSGTITLE, MsgLevel = i.MsgLevel, Key = i.MsgId, MSGCONT = i.MSGCONT
                    };
                    item.SYS_USER = new SYS_USER {
                        Key = i.SendUserId, USERNAME = i.SendUserName
                    };
                    item.SYS_USER1 = new SYS_USER {
                        Key = i.RecieveUserId, USERNAME = i.RecieveUserName
                    };
                    result.Add(item);
                }
            }

            return(result);
        }
 public static SysMsgDto ToSysMsgDto(this SYS_MSGUSER mu)
 {
     return(new SysMsgDto
     {
         CreateDatetime = mu.CREATE_DATETIME.HasValue ? mu.CREATE_DATETIME.Value.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty,
         Id = mu.Key, Msgcont = mu.SYS_MSG.MSGCONT, MsgId = mu.SYS_MSG.Key.ToString(), Msgtitle = mu.SYS_MSG.MSGTITLE, MsgType = mu.SYS_MSG.MsgType ?? 0, ReadFlag = mu.USERREAD_FLAG, Sender = mu.SYS_USER.USERNAME
     });
 }
        public List <SYS_MSGUSER> GetUnreadMsgs(string userid, int pageIndex, int pageSize, out int total)
        {
            if (string.IsNullOrWhiteSpace(userid))
            {
                throw new ArgumentNullException("userid");
            }

            total = 0;
            List <SYS_MSGUSER> result = null;

            Guid uid = new Guid(userid);

            var query = from mu in _msguserRepository.GetAll()
                        join msg in _msgRepository.GetAll() on mu.MSG_ID equals msg.Key into mu_msg
                        from mumsg in mu_msg.DefaultIfEmpty()
                        join ru in _userRepository.GetAll() on mu.USER_ID equals ru.Key into mu_ru
                        from muru in mu_ru.DefaultIfEmpty()
                        join su in _userRepository.GetAll() on mu.SENDUSER_ID equals su.Key into mu_su
                        from musu in mu_su.DefaultIfEmpty()
                        where mu.DELETE_FLAG == 0 && mu.USERREAD_FLAG != 1 && muru.Key == uid
                        orderby mu.CREATE_DATETIME descending
                        select new
            {
                mu.CREATE_ACCOUNT, mu.CREATE_DATETIME, mu.DELETE_FLAG, mu.LASTUPDATE_ACCOUNT, mu.LASTUPDATE_DATETIME, mu.Key, mu.MSG_ID, mu.SENDUSER_ID, mu.USERDEL_FLAG, mu.USERREAD_FLAG, mu.USER_ID,
                mumsg.MSGCONT, MsgId = mumsg.Key, mumsg.MsgLevel, mumsg.MSGTITLE, mumsg.MsgType,
                SendUserId           = musu.Key, SendUserName = musu.USERNAME,
                RecieveUserId        = muru.Key, RecieveUserName = muru.USERNAME
            };

            total = query.Count();

            if (total > 0)
            {
                result = new List <SYS_MSGUSER>();
                foreach (var i in query.Skip((pageIndex - 1) * pageSize).Take(pageSize))
                {
                    var item = new SYS_MSGUSER
                    {
                        CREATE_ACCOUNT     = i.CREATE_ACCOUNT, Key = i.Key, CREATE_DATETIME = i.CREATE_DATETIME, DELETE_FLAG = i.DELETE_FLAG,
                        LASTUPDATE_ACCOUNT = i.LASTUPDATE_ACCOUNT, LASTUPDATE_DATETIME = i.LASTUPDATE_DATETIME, MSG_ID = i.MSG_ID, SENDUSER_ID = i.SENDUSER_ID, USERDEL_FLAG = i.USERDEL_FLAG,
                        USERREAD_FLAG      = i.USERREAD_FLAG, USER_ID = i.USER_ID
                    };
                    item.SYS_MSG = new SYS_MSG {
                        MsgType = i.MsgType, MSGTITLE = i.MSGTITLE, MsgLevel = i.MsgLevel, Key = i.MsgId, MSGCONT = i.MSGCONT
                    };
                    item.SYS_USER = new SYS_USER {
                        Key = i.SendUserId, USERNAME = i.SendUserName
                    };
                    item.SYS_USER1 = new SYS_USER {
                        Key = i.RecieveUserId, USERNAME = i.RecieveUserName
                    };
                    result.Add(item);
                }
            }

            return(result);
        }