public HttpResponseMessage LoginEntry([FromBody]UserModel model)
 {
     MemberSearchCondition serch = new MemberSearchCondition
     {
         Phone = model.UserName
     };
     MemberEntity member = _memService.GetMembersByCondition(serch).FirstOrDefault();
     if (member == null)
     {
         return PageHelper.toJson(PageHelper.ReturnValue(false, "手机号或密码错误"));
     }
     var user = _userService.FindUser(member.UserId);
     if (user == null)
         return PageHelper.toJson(PageHelper.ReturnValue(false, "用户名或密码错误"));
     if (!PasswordHelper.ValidatePasswordHashed(user, model.Password))
         return PageHelper.toJson(PageHelper.ReturnValue(false, "用户名或密码错误"));
     _authenticationService.SignIn(user, model.Remember);
     return PageHelper.toJson(PageHelper.ReturnValue(true, "登陆成功", new
     {
         user.Id,
         Roles = user.UserRoles.Select(r => new { r.Role.RoleName }).ToArray(),
         user.UserName
     }));
 }
		public IQueryable<MemberEntity> GetMembersByCondition(MemberSearchCondition condition)
		{
			var query = _memberRepository.Table;
			try
			{
				if (!string.IsNullOrEmpty(condition.RealName))
                {
                    query = query.Where(q => q.RealName == condition.RealName);
                }
				if (!string.IsNullOrEmpty(condition.IdentityNo))
                {
                    query = query.Where(q => q.IdentityNo == condition.IdentityNo);
                }
				if (condition.Gender.HasValue)
                {
                    query = query.Where(q => q.Gender == condition.Gender.Value);
                }
				if (!string.IsNullOrEmpty(condition.Phone))
                {
                    query = query.Where(q => q.Phone == condition.Phone);
                }
				if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
				if(condition.OrderBy.HasValue)
				{
					switch (condition.OrderBy.Value)
                    {
						case EnumMemberSearchOrderBy.OrderById:
							query = condition.IsDescending?query.OrderByDescending(q=>q.Id):query.OrderBy(q=>q.Id);
							break;
						case EnumMemberSearchOrderBy.OrderByRealName:
							query = condition.IsDescending?query.OrderByDescending(q=>q.RealName):query.OrderBy(q=>q.RealName);
							break;
						case EnumMemberSearchOrderBy.OrderByGender:
							query = condition.IsDescending?query.OrderByDescending(q=>q.Gender):query.OrderBy(q=>q.Gender);
							break;
						case EnumMemberSearchOrderBy.OrderByPhone:
							query = condition.IsDescending?query.OrderByDescending(q=>q.Phone):query.OrderBy(q=>q.Phone);
							break;
                    }
					
				}
				else
				{
					query = query.OrderBy(q=>q.Id);
				}

				if (condition.Page.HasValue && condition.PageCount.HasValue)
                {
                    query = query.Skip((condition.Page.Value - 1)*condition.PageCount.Value).Take(condition.PageCount.Value);
                }
				return query;
			}
			catch(Exception e)
			{
				_log.Error(e,"数据库操作出错");
                return null;
			}
		}
        public HttpResponseMessage AddMember([FromBody]MemberModel memberModel)
        {
            var validMsg = "";
            if (!memberModel.ValidateModel(out validMsg))
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误,请重新输入"));
            }

            if (memberModel.Password != memberModel.SecondPassword)
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "两次密码输入不一致"));
            }

            var user = _userService.GetUserByName(memberModel.UserName);
            if (user != null)
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "用户名已经存在"));
            }
            var condition = new MemberSearchCondition
            {
                OrderBy = EnumMemberSearchOrderBy.OrderById,
                Phone = memberModel.Phone
            };

            //判断user表和member表中是否存在用户名
            int user2 = _memberService.GetMemberCount(condition);
            if (user2 != 0) return PageHelper.toJson(PageHelper.ReturnValue(false, "手机号已经存在"));

            var memRole = _roleService.GetRoleByName("user");

            //User权限缺少时自动添加
            if (memRole == null)
            {
                memRole = new Role
                {
                    RoleName = "user",
                    RolePermissions = null,
                    Status = RoleStatus.Normal,
                    Description = "刚注册的用户默认归为普通用户user"
                };
            }

            var newUser = new UserBase
            {
                UserName = memberModel.UserName,
                Password = memberModel.Password,
                RegTime = DateTime.Now,
                NormalizedName = memberModel.UserName.ToLower(),
                //注册用户添加权限
                UserRoles = new List<UserRole>(){new UserRole()
                {
                    Role = memRole
                }},
                Status = 0
            };
            
            PasswordHelper.SetPasswordHashed(newUser, memberModel.Password);

            var model = new MemberEntity();
            model.UserId = _userService.InsertUser(newUser).Id;
            model.RealName = memberModel.UserName;
            model.UserName = memberModel.UserName;
            model.Phone = memberModel.Phone;
            model.Points=0;
            model.IdentityNo="";
            model.Icq="";
            model.PostNo="";
            model.AccountNumber=0;
            model.AddTime=DateTime.Now;
            model.Gender=EnumGender.Male;
            model.UpdTime =DateTime.Now;
            model.UpdUser=0;
            var newMember = _memberService.Create(model);
            return PageHelper.toJson(PageHelper.ReturnValue(true, "注册成功"));
        }
		public int GetMemberCount (MemberSearchCondition condition)
		{
			var query = _memberRepository.Table;
			try
			{
				if (!string.IsNullOrEmpty(condition.RealName))
                {
                    query = query.Where(q => q.RealName == condition.RealName);
                }
				if (!string.IsNullOrEmpty(condition.IdentityNo))
                {
                    query = query.Where(q => q.IdentityNo == condition.IdentityNo);
                }
				if (condition.Gender.HasValue)
                {
                    query = query.Where(q => q.Gender == condition.Gender.Value);
                }
				if (!string.IsNullOrEmpty(condition.Phone))
                {
                    query = query.Where(q => q.Phone == condition.Phone);
                }
				if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
				return query.Count();
			}
			catch(Exception e)
			{
				_log.Error(e,"数据库操作出错");
                return -1;
			}
		}