public HttpResponseMessage GetBLCList(int page = 1, int pageSize = 10)
 {
     var sech = new BrokerLeadClientSearchCondition
     {
         OrderBy = EnumBrokerLeadClientSearchOrderBy.OrderById,
         Status = EnumBLeadType.预约中,
        
     };
     var list = _brokerleadclientService.GetBrokerLeadClientsByCondition(sech).Select(p => new
     {
         p.Id,
         p.Brokername,
         p.ClientName,
         p.ClientInfo.Phone,
         p.ProjectId,
         p.Appointmenttime
     }).ToList()==null? null : _brokerleadclientService.GetBrokerLeadClientsByCondition(sech).Select(p => new
     {
         p.Id,
         p.Brokername,
         p.ClientName,
         p.ClientInfo.Phone,
         p.ProjectId,
         p.Appointmenttime
     }).ToList().Select(a => new
     {
         a.Id,
         a.Brokername,
         a.ClientName,
         a.Phone,
         ProjectName =a.ProjectId==0?"":  _productService.GetProductById(a.ProjectId).Productname,
         Appointmenttime = a.Appointmenttime.ToString("yyy-MM-dd")
     });
     var count = _brokerleadclientService.GetBrokerLeadClientCount(sech);
     return PageHelper.toJson(new { List = list, Condition = sech, totalCount = count });
 }
        public HttpResponseMessage GetStatusByUserId(int page)
        {
          //============================================chenda start===========================================
            var user = (UserBase)_workContext.CurrentUser;
            var broker = _brokerService.GetBrokerByUserId(user.Id);
            
            if (broker != null) 
            {
                var condition = new BrokerLeadClientSearchCondition
                {
                    OrderBy = EnumBrokerLeadClientSearchOrderBy.OrderByTime,
                    Page = page,
                    PageCount = 10,
                    Brokers = broker
                };
                var conditon2 = new BrokerRECClientSearchCondition
                {
                    OrderBy =EnumBrokerRECClientSearchOrderBy.OrderByTime,
                    Page = page,
                    PageCount = 10,
                    Brokers = broker
                };
                var model = _brokerLeadClientService.GetBrokerLeadClientsByCondition(condition).ToList();
                if (model == null)
                {
                    return PageHelper.toJson(PageHelper.ReturnValue(false, "当前经纪人没有带过客户"));
                }
                var model2 = _brokerRecClientService.GetBrokerRECClientsByCondition(conditon2).ToList();
                if (model2 == null)
                {
                    return PageHelper.toJson(PageHelper.ReturnValue(false, "当前经纪人没有推荐过客户"));
                }

                List<ReturnCustomModel> listModel = new List<ReturnCustomModel>();

                // 带客
               var  listdk= model.Select(p => new
                {
                    StrType = "带客",
                    Clientname = p.ClientInfo.Clientname,
                    Housetype = p.ClientInfo.Housetype,
                    Houses = p.ClientInfo.Houses,
                    Phone = p.Broker.Phone,
                    Status = p.Status,
                    Id = p.Id,
                }).ToList().ToList();

                foreach(var p in listdk)
                {
                    listModel.Add(new ReturnCustomModel{ Clientname=p.Clientname, Houses=p.Houses, Housetype=p.Housetype, Id=p.Id.ToString(), Phone=p.Phone, Status=p.Status.ToString(), StrType=p.StrType});
                }

                //推荐
                var  listtj= model2.Select(c => new
                {
                        StrType = "推荐",
                        Clientname = c.ClientInfo.Clientname,
                        Housetype = c.ClientInfo.Housetype,
                        Houses = c.ClientInfo.Houses,
                        Phone = c.Broker.Phone,
                        Status = c.Status,
                        Id = c.Id,
                        Uptime = c.Uptime.ToString(CultureInfo.InvariantCulture)
                }).ToList();

                foreach (var p in listtj)
                {
                    listModel.Add(new ReturnCustomModel { Clientname = p.Clientname, Houses = p.Houses, Housetype = p.Housetype, Id = p.Id.ToString(), Phone = p.Phone, Status = p.Status.ToString(), StrType = p.StrType});
                }

                int totalCount = _brokerLeadClientService.GetBrokerLeadClientCount(condition) + _brokerRecClientService.GetBrokerRECClientCount(conditon2);
                return PageHelper.toJson(new { List = listModel, totalCount = totalCount });       
            
            }
            return PageHelper.toJson(PageHelper.ReturnValue(false, "获取用户失败,请检查是否登陆"));
            //================================================================chenda end============================================================================
        }
        public HttpResponseMessage Add([FromBody] BrokerLeadClientModel brokerleadclient)
        {
            if (brokerleadclient.Adduser == 0) return PageHelper.toJson(PageHelper.ReturnValue(false, "经济人ID不能为空!"));
            if (brokerleadclient.Broker == 0) return PageHelper.toJson(PageHelper.ReturnValue(false, "经纪人ID不能为空!"));
            if (string.IsNullOrEmpty(brokerleadclient.Clientname)) return PageHelper.toJson(PageHelper.ReturnValue(false, "客户名不能为空"));
            if (string.IsNullOrEmpty(brokerleadclient.Phone)) return PageHelper.toJson(PageHelper.ReturnValue(false, "客户电话不能为空!"));

            //查询客户信息
            var sech = new BrokerLeadClientSearchCondition
            {
                ClientName = brokerleadclient.Clientname,
                Phone = brokerleadclient.Phone,
                Projectids = new[] { brokerleadclient.Projectid },
                DelFlag = EnumDelFlag.默认
            };

            var cmodel = _brokerleadclientService.GetBrokerLeadClientsByCondition(sech).FirstOrDefault();
            //检测客户是否存在于数据库
            if (cmodel == null)
            {
                //客户信息
                var client = new ClientInfoEntity
                {
                    Clientname = brokerleadclient.Clientname,
                    Phone = brokerleadclient.Phone.ToString(CultureInfo.InvariantCulture),
                    Housetype = brokerleadclient.HouseType,
                    Houses = brokerleadclient.Houses,
                    Note = brokerleadclient.Note,
                    Adduser = brokerleadclient.Broker,
                    Addtime = DateTime.Now,
                    Upuser = brokerleadclient.Broker,
                    Uptime = DateTime.Now
                };

                _clientInfoService.Create(client);
            }
            else
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "该客户正在被带客!"));
            }

            //查询客户信息
            var sech2 = new ClientInfoSearchCondition
            {
                Clientname = brokerleadclient.Clientname, 
                Phone = brokerleadclient.Phone.ToString(CultureInfo.InvariantCulture),
            };
            var cmodel2 = _clientInfoService.GetClientInfosByCondition(sech2).FirstOrDefault();
            if (cmodel2 == null)
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "带客出错!"));
            }
            //查询经纪人信息
            var broker = _brokerService.GetBrokerByUserId(brokerleadclient.Adduser);
            //创建代客流程
            var model = new BrokerLeadClientEntity();
            model.Broker = _brokerService.GetBrokerById(brokerleadclient.Adduser);
            model.ClientInfo = cmodel2;
            model.ClientName = brokerleadclient.Clientname;
            model.Appointmenttime = Convert.ToDateTime(brokerleadclient.Appointmenttime);
            //model.Qq = Convert.ToInt32(brokerrecclient.Qq);
            model.Phone = brokerleadclient.Phone;       //客户电话
            model.Brokername = broker.Brokername;
            //model.BrokerLevel = broker.Level.Name;
            model.Broker = broker;
            model.Adduser = brokerleadclient.Adduser;
            model.Addtime = DateTime.Now;
            model.Upuser = brokerleadclient.Adduser;
            model.Uptime = DateTime.Now;
            //model.ProjectId = brokerleadclient.Id;
            model.ProjectId = brokerleadclient.Projectid;
            model.Projectname = brokerleadclient.Projectname;
            model.Status = EnumBLeadType.预约中;
            model.DelFlag = EnumDelFlag.默认;
            model.ComOrder = (int)EnumOrderType.带客订单;
            model.Details = brokerleadclient.Note;

            _brokerleadclientService.Create(model);

            return PageHelper.toJson(PageHelper.ReturnValue(true, "提交成功"));
        }
        public HttpResponseMessage GetLeadClientInfoByBrokerName(EnumBLeadType status, string brokername, EnumBrokerLeadClientSearchOrderBy orderByAll = EnumBrokerLeadClientSearchOrderBy .OrderByTime, bool isDes = true, int page = 1, int pageSize = 10)
        {

            var condition = new BrokerLeadClientSearchCondition
            {
                OrderBy = orderByAll,
                Page = page,
                PageCount = pageSize,
                Status = status,
                ClientName = brokername,
                isDescending =isDes 

            };

            var list = _brokerleadclientService.GetBrokerLeadClientsByCondition(condition).Select(a => new
            {
                a.Id,
                a.Appointmenttime,
                a.Brokername,
                a.ClientInfo.Phone,
                a.Projectname,
                a.Addtime,
                a.ClientInfo.Clientname,

                SecretaryName = a.SecretaryId.Brokername,
                a.SecretaryPhone,
                Waiter = a.WriterId.Brokername,
                a.WriterPhone,
                a.Uptime

            }).ToList().Select(b => new
            {
                b.Id,
                b.Brokername,
                b.Clientname,

                b.Phone,
                b.Projectname,
                Appointmenttime = b.Appointmenttime.ToString("yyy-MM-dd"),
                Addtime = b.Addtime.ToString("yyy-MM-dd"),


                SecretaryName = b.Brokername,
                b.SecretaryPhone,
                Waiter = b.Brokername,
                b.WriterPhone,
                Uptime = b.Uptime.ToString("yyy-MM-dd")
            });

            var totalCont = _brokerleadclientService.GetBrokerLeadClientCount(condition);

            return PageHelper.toJson(new { list1 = list, condition1 = condition, totalCont1 = totalCont });
        }
        public HttpResponseMessage SearchBrokerLeadClient(string userid)
        {
            var sech = new BrokerLeadClientSearchCondition
            {
                Brokers = _brokerService.GetBrokerById(Convert.ToInt32(userid))
            };
            var list = _brokerleadclientService.GetBrokerLeadClientsByCondition(sech).Select(p => new
            {
                p.Id,
                p.Brokername,
                p.ClientName,
                p.ClientInfo.Phone,
                p.ProjectId,
                p.Appointmenttime
            }).ToList().Select(a => new 
            {
                a.Id,
                a.Brokername,
                a.ClientName,
                a.Phone,
                ProjectName = a.ProjectId == 0 ? "" : _productService.GetProductById(a.ProjectId).Productname,
                Appointmenttime = a.Appointmenttime.ToString("yyy-MM-dd")
            });
            return PageHelper.toJson(new { List = list});

        }
		public IQueryable<BrokerLeadClientEntity> GetBrokerLeadClientsByCondition(BrokerLeadClientSearchCondition condition)
		{
			var query = _brokerleadclientRepository.Table;
			try
			{
				if (condition.AppointmenttimeBegin.HasValue)
                {
                    query = query.Where(q => q.Appointmenttime>= condition.AppointmenttimeBegin.Value);
                }
                if (condition.AppointmenttimeEnd.HasValue)
                {
                    query = query.Where(q => q.Appointmenttime < condition.AppointmenttimeEnd.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.Appointmentstatus))
                {
                    query = query.Where(q => q.Appointmentstatus.Contains(condition.Appointmentstatus));
                }
				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.DelFlag != null)
                {
                    query = query.Where(q => q.DelFlag== condition.DelFlag);
                }
                if (condition.Projectids != null && condition.Projectids.Any())
                {
                    query = query.Where(q => condition.Projectids.Contains(q.ProjectId));
                }
				if (condition.ClientInfos != null && condition.ClientInfos.Any())
                {
                    query = query.Where(q => condition.ClientInfos.Contains(q.ClientInfo));
                }
                if (condition.ClientName != null && condition.ClientName.Any())
                {
                    query = query.Where(q => condition.ClientName.Contains(q.ClientName));
                }
                if (condition.Phone != null && condition.Phone.Any())
                {
                    query = query.Where(q => condition.Phone.Contains(q.Phone));
                }
				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));
                }
				if(condition.OrderBy.HasValue)
				{
					switch (condition.OrderBy.Value)
                    {
						case EnumBrokerLeadClientSearchOrderBy.OrderById:
							query = condition.isDescending?query.OrderBy(q=>q.Id):query.OrderByDescending(q=>q.Id);
							break;
                        case EnumBrokerLeadClientSearchOrderBy.OrderByTime:
                            query = condition.isDescending ? query.OrderBy(q => q.Uptime) : query.OrderByDescending(q => q.Uptime);
                            break;
                        case EnumBrokerLeadClientSearchOrderBy.OrderByBrokername:
                            query = condition.isDescending ? query.OrderBy(q => q.Brokername) : query.OrderByDescending(q => q.Brokername);
                            break;
                        case EnumBrokerLeadClientSearchOrderBy.OrderByClientName:
                            query = condition.isDescending ? query.OrderBy(q => q.ClientName) : query.OrderByDescending(q => q.ClientName);
                            break;
                        case EnumBrokerLeadClientSearchOrderBy.OrderByPhone:
                            query = condition.isDescending ? query.OrderBy(q => q.Phone) : query.OrderByDescending(q => q.Phone);
                            break;
                        case EnumBrokerLeadClientSearchOrderBy.OrderByProjectname:
                            query = condition.isDescending ? query.OrderBy(q => q.Projectname) : query.OrderByDescending(q => q.Projectname);
                            break;
                        case EnumBrokerLeadClientSearchOrderBy.OrderByAppointmenttime:
                            query = condition.isDescending ? query.OrderBy(q => q.Appointmenttime) : query.OrderByDescending(q => q.Appointmenttime);
                            break;
                        case EnumBrokerLeadClientSearchOrderBy.OrderByClientname:
                            query = condition.isDescending ? query.OrderBy(q => q.ClientName) : query.OrderByDescending(q => q.ClientName);
                            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 GetBrokerLeadClientCount (BrokerLeadClientSearchCondition condition)
		{
			var query = _brokerleadclientRepository.Table;
			try
			{
				if (condition.AppointmenttimeBegin.HasValue)
                {
                    query = query.Where(q => q.Appointmenttime>= condition.AppointmenttimeBegin.Value);
                }
                if (condition.AppointmenttimeEnd.HasValue)
                {
                    query = query.Where(q => q.Appointmenttime < condition.AppointmenttimeEnd.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.Appointmentstatus))
                {
                    query = query.Where(q => q.Appointmentstatus.Contains(condition.Appointmentstatus));
                }
				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.ClientInfos != null && condition.ClientInfos.Any())
                {
                    query = query.Where(q => condition.ClientInfos.Contains(q.ClientInfo));
                }
				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));
                }
                if (condition.Status.HasValue)
                {
                    query = query.Where(c => c.Status == condition.Status);
                }

				return query.Count();
			}
			catch(Exception e)
			{
				_log.Error(e,"数据库操作出错");
                return -1;
			}
		}