public IQueryable<PartnerListEntity> GetPartnerListsByCondition(PartnerListSearchCondition condition)
		{
			var query = _partnerlistRepository.Table;
			try
			{
				if (condition.RegtimeBegin.HasValue)
                {
                    query = query.Where(q => q.Regtime>= condition.RegtimeBegin.Value);
                }
                if (condition.RegtimeEnd.HasValue)
                {
                    query = query.Where(q => q.Regtime < condition.RegtimeEnd.Value);
                }
				if (condition.AddtimeBegin.HasValue)
                {
                    query = query.Where(q => q.Addtime>= condition.AddtimeBegin.Value);
                }
                if (condition.AddtimeEnd.HasValue)
                {
                    query = query.Where(q => q.Addtime < condition.AddtimeEnd.Value);
                }
				if (condition.UptimeBegin.HasValue)
                {
                    query = query.Where(q => q.Uptime>= condition.UptimeBegin.Value);
                }
                if (condition.UptimeEnd.HasValue)
                {
                    query = query.Where(q => q.Uptime < condition.UptimeEnd.Value);
                }
                if (!string.IsNullOrEmpty(condition.Phone))
                {
                    query = query.Where(q => q.Phone.Contains(condition.Phone));
                }
				if (!string.IsNullOrEmpty(condition.Brokername))
                {
                    query = query.Where(q => q.Brokername.Contains(condition.Brokername));
                }
				if (!string.IsNullOrEmpty(condition.Agentlevel))
                {
                    query = query.Where(q => q.Agentlevel.Contains(condition.Agentlevel));
                }
				if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
				if (condition.Brokers != null )
                {
                    query = query.Where(q =>q.Broker.Id==condition.Brokers.Id);
                }
				if (condition.PartnerIds != null && condition.PartnerIds.Any())
                {
                    query = query.Where(q => condition.PartnerIds.Contains(q.PartnerId));
                }
				if (condition.Addusers != null && condition.Addusers.Any())
                {
                    query = query.Where(q => condition.Addusers.Contains(q.Adduser));
                }
				if (condition.Upusers != null && condition.Upusers.Any())
                {
                    query = query.Where(q => condition.Upusers.Contains(q.Upuser));
                }
                query = query.Where(q => q.Status == condition.Status);


				if(condition.OrderBy.HasValue)
				{
					switch (condition.OrderBy.Value)
                    {
						case EnumPartnerListSearchOrderBy.OrderById:
							query = condition.isDescending?query.OrderByDescending(q=>q.Id):query.OrderBy(q=>q.Id);
							break;
                    }
					
				}
				else
				{
					query = query.OrderBy(q=>q.Id);
				}
                if (condition.Status.HasValue)
                {
                    query = query.Where(c => c.Status == condition.Status);
                }
				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 int GetPartnerListCount (PartnerListSearchCondition condition)
		{
			var query = _partnerlistRepository.Table;
			try
			{
				if (condition.RegtimeBegin.HasValue)
                {
                    query = query.Where(q => q.Regtime>= condition.RegtimeBegin.Value);
                }
                if (condition.RegtimeEnd.HasValue)
                {
                    query = query.Where(q => q.Regtime < condition.RegtimeEnd.Value);
                }
				if (condition.AddtimeBegin.HasValue)
                {
                    query = query.Where(q => q.Addtime>= condition.AddtimeBegin.Value);
                }
                if (condition.AddtimeEnd.HasValue)
                {
                    query = query.Where(q => q.Addtime < condition.AddtimeEnd.Value);
                }
				if (condition.UptimeBegin.HasValue)
                {
                    query = query.Where(q => q.Uptime>= condition.UptimeBegin.Value);
                }
                if (condition.UptimeEnd.HasValue)
                {
                    query = query.Where(q => q.Uptime < condition.UptimeEnd.Value);
                }
                if (!string.IsNullOrEmpty(condition.Phone))
                {
                    query = query.Where(q => q.Phone.Contains(condition.Phone));
                }
				if (!string.IsNullOrEmpty(condition.Brokername))
                {
                    query = query.Where(q => q.Brokername.Contains(condition.Brokername));
                }
				if (!string.IsNullOrEmpty(condition.Agentlevel))
                {
                    query = query.Where(q => q.Agentlevel.Contains(condition.Agentlevel));
                }
				if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
				if (condition.Brokers != null )
                {
                    query = query.Where(q =>q.Broker.Id== condition.Brokers.Id);
                }
				if (condition.PartnerIds != null && condition.PartnerIds.Any())
                {
                    query = query.Where(q => condition.PartnerIds.Contains(q.PartnerId));
                }
				if (condition.Addusers != null && condition.Addusers.Any())
                {
                    query = query.Where(q => condition.Addusers.Contains(q.Adduser));
                }               

				if (condition.Upusers != null && condition.Upusers.Any())
                {
                    query = query.Where(q => condition.Upusers.Contains(q.Upuser));
                }
                query = query.Where(q => q.Status == condition.Status);

				return query.Count();
			}
			catch(Exception e)
			{
				_log.Error(e,"数据库操作出错");
                return -1;
			}
		}
        public HttpResponseMessage GetBrokerDetails()
        {
            var user = (UserBase)_workContext.CurrentUser;
            if (user != null)
            {
                var broker = _brokerService.GetBrokerByUserId(user.Id);//获取当前经纪人
                if (broker == null)
                {
                    return PageHelper.toJson(PageHelper.ReturnValue(false, "获取用户失败,请检查是否登陆"));
                }
                else
                {
                    //更新到经纪人表中 可用金额
                    broker.Amount = Convert.ToDecimal(GetBrokerAmount());
                    _brokerService.Update(broker);


                    var partnerCount = 0;//合伙人个数
                    var refereeCount = 0;//推荐人个数
                    var customerCount = 0;//客户个数
                    var levelStr = "";//等级
                    var orderStr = "0";//排名
                    var allMoneys = "0";//总佣金

                    var partnerlistsearchcon = new PartnerListSearchCondition
                    {
                        Brokers = broker,
                        Status = EnumPartnerType.同意
                    };
                    partnerCount = _partnerlistService.GetPartnerListCount(partnerlistsearchcon);

                    var recomagmentsearchcon = new RecommendAgentSearchCondition
                    {
                        BrokerId = broker.Id
                    };
                    refereeCount = _recommendagentService.GetRecommendAgentCount(recomagmentsearchcon);

                    var condition = new ClientInfoSearchCondition
                    {
                        Addusers = broker.Id
                    };
                    customerCount = _clientInfoService.GetClientInfoCount(condition);

                    levelStr = broker.Level.Name;

                    allMoneys = broker.Amount.ToString();

                    orderStr = GetOrdersByuserId(broker.Id.ToString());

                    return PageHelper.toJson(new { partnerCount = partnerCount, refereeCount = refereeCount, customerCount = customerCount, levelStr = levelStr, orderStr = orderStr, allMoneys = allMoneys, photo = broker.Headphoto, Name = broker.Brokername });
                }
            }
            return PageHelper.toJson(PageHelper.ReturnValue(false, "获取用户失败,请检查是否登陆"));
        }
 /// <summary>
 /// 获取合伙人
 /// </summary>
 /// <param name="brokerUserId">经济人UserId</param>
 /// <returns></returns>
 public BrokerModel GetPartner(int brokerUserId)
 {
     var partnerlistsearchcon = new PartnerListSearchCondition
     {
         Brokers = _brokerService.GetBrokerByUserId(brokerUserId),
         Status = EnumPartnerType.同意
     };
     var partner =
         _partnerlistService.GetPartnerListsByCondition(partnerlistsearchcon).Select(p => new BrokerModel
         {
             Id = p.Id,
             Brokername = p.Brokername,
             PartnersId = p.PartnerId
         }).FirstOrDefault();
     return partner;
 }
        public HttpResponseMessage AddPartnerList([FromBody] PartnerListEntity partnerList)
        {
            var sech = new BrokerSearchCondition
            {
                Phone = partnerList.Phone,
                State=1
            };
            var user = (UserBase)_workContext.CurrentUser;
            if (user != null)
            {
                var broker = _brokerService.GetBrokerByUserId(user.Id);//获取当前用户
                if (broker != null)
                {
                    var getbroker = _brokerService.GetBrokersByCondition(sech).FirstOrDefault();//通过手机号查询经纪人
                    if (getbroker != null)
                    {
                        //判断是否是本身
                        if (broker.Id == getbroker.Id)
                        {
                            return PageHelper.toJson(PageHelper.ReturnValue(false, "对不起,不能添加本身"));
                        }
                        //判断要添加的这个经纪人是否有上家
                        if (getbroker.PartnersId == null || getbroker.PartnersId == 0)//查询他的上家是否存在
                        {
                            //1 添加到partnerList表中  

                            var entity = new PartnerListEntity();
                            entity.PartnerId = getbroker.Id;     //添加的下家
                            entity.Brokername = getbroker.Brokername;
                            entity.Phone = getbroker.Phone;
                            entity.Agentlevel = getbroker.Level.Name;
                            entity.Regtime = getbroker.Regtime;

                            entity.Adduser = broker.Id;
                            entity.Addtime = DateTime.Now;
                            entity.Upuser = broker.Id;
                            entity.Uptime = DateTime.Now;
                            entity.Broker = broker;
                            entity.Status = EnumPartnerType.默认;
                            //判断当前用户的合伙人个数是否》=3

                            PartnerListSearchCondition plsearCon = new PartnerListSearchCondition
                            {
                                Brokers = broker,
                                Status = EnumPartnerType.同意
                            };
                            if (_partnerlistService.GetPartnerListCount(plsearCon) >= 3)
                            {
                                return PageHelper.toJson(PageHelper.ReturnValue(true, "对不起,您的合伙人数已满 不能添加"));
                            }

                            if (_partnerlistService.Create(entity) != null)
                            {

                                return PageHelper.toJson(PageHelper.ReturnValue(true, "邀请成功!等待对方同意"));

                            }
                            else
                            {
                                return PageHelper.toJson(PageHelper.ReturnValue(true, "数据更新失败!请与客服联系"));
                            }


                        }
                        else
                        {
                            return PageHelper.toJson(PageHelper.ReturnValue(false, "该用户已经是别人的合伙人了!"));

                        }


                    }
                    else
                    {
                        return PageHelper.toJson(PageHelper.ReturnValue(false, "该经纪人不存在"));
                    }
                }
            }
            return PageHelper.toJson(PageHelper.ReturnValue(false, "对不起,请登录"));






            //var list = _brokerService.GetBrokersByCondition(sech).FirstOrDefault();//通过手机号查询经纪人
            //if (list != null)
            //{
            //    if (list.PartnersId != 0)//查询他的上家是否存在
            //    {
            //        if (partnerList != null)
            //        {
            //            var entity = new PartnerListEntity();
            //            entity.PartnerId = list.Id;     //添加的下家
            //            entity.Phone = partnerList.Phone;

            //            if (list.Id == _brokerService.GetBrokerByUserId(_workContext.CurrentUser.Id).Id)
            //            {
            //                return PageHelper.toJson(PageHelper.ReturnValue(false, "不能添加自身!"));
            //            }

            //            //上家的属性
            //                 entity.Agentlevel = _brokerService.GetBrokerByUserId(_workContext.CurrentUser.Id).Level.Name;
            //                 entity.Brokername = _brokerService.GetBrokerByUserId(_workContext.CurrentUser.Id).Brokername;

            //                 entity.Regtime = DateTime.Now;
            //                 entity.Broker = _brokerService.GetBrokerByUserId(_workContext.CurrentUser.Id);

            //                 entity.Upuser = _brokerService.GetBrokerByUserId(_workContext.CurrentUser.Id).Id;
            //                 entity.Uptime = DateTime.Now;
            //                 entity.Adduser = _brokerService.GetBrokerByUserId(_workContext.CurrentUser.Id).Id;
            //                 entity.Addtime = DateTime.Now;
            //                 entity.Status = EnumPartnerType.默认;

            //            try
            //            {
            //                if (_partnerlistService.Create(entity) != null)
            //                {
            //                    return PageHelper.toJson(PageHelper.ReturnValue(true, "数据添加成功!等待对方同意"));
            //                }
            //            }
            //            catch
            //            {
            //                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据添加失败!"));
            //            }
            //        }
            //        return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误!"));
            //    }
            //    return PageHelper.toJson(PageHelper.ReturnValue(false, "该用户已经是别人的合伙人了!"));
            //}
            //return PageHelper.toJson(PageHelper.ReturnValue(false, "该用户不存在"));


        }
        public HttpResponseMessage PartnerListDetailed(int userId)
        {
            var partnerlistsearchcon = new PartnerListSearchCondition
            {
                Brokers = _brokerService.GetBrokerByUserId(userId),
                Status = EnumPartnerType.同意,
            };

            var partnerList = _partnerlistService.GetPartnerListsByCondition(partnerlistsearchcon).Where(p => p.Broker.UserId == userId).ToList().Select(c => new
                {
                    Name = c.Brokername,
                    AddTime = c.Addtime,
                    regtime = c.Regtime.ToString("yyyy-MM-dd"),
                    Phone = c.Phone,
                    Headphoto = _brokerService.GetBrokerById(Convert.ToInt32(c.PartnerId)).Headphoto,
                    Id = c.Id,
                    PartnerId = c.PartnerId,


                }).ToList().Select(c => new
                {
                    Name = c.Name,
                    AddTime = c.AddTime,
                    regtime = c.regtime,
                    Phone = c.Phone,
                    Headphoto = _brokerService.GetBrokerById(Convert.ToInt32(c.PartnerId)).Headphoto,
                    Id = c.Id,
                    PartnerId = c.PartnerId,
                    Agentlevel = _brokerService.GetBrokerById(c.PartnerId).Agentlevel,
                    Amount = _brokerService.GetBrokerById(c.PartnerId).Amount,
                    Sfz = _brokerService.GetBrokerById(c.PartnerId).Sfz,
                    Nickname = _brokerService.GetBrokerById(c.PartnerId).Nickname,
                });

            return PageHelper.toJson(new { partnerList });

        }