/// <summary>
        /// 根据提现ID查询提现明细信息  
        /// chen 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[Description("根据经纪人查询提现明细")]
        //[HttpGet]
        public HttpResponseMessage GetBrokerWithdrawDetailByBrokerWithdrawId(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据错误!"));
            }
            var seach = new BrokerWithdrawDetailSearchCondition
            {
                OrderBy = EnumBrokerWithdrawDetailSearchOrderBy.OrderById,
                BrokerWithdraw = _brokerwithdrawService.GetBrokerWithdrawById(Convert.ToInt32(id)),
            };
            var list = _brokerwithdrawdetailService.GetBrokerWithdrawDetailsByCondition(seach).Select(b => new
            {
                b.Id,
                b.Withdrawnum,
                b.BrokeAccount_Id,
                b.Withdrawtime,
                b.Type,
                b.BrokerWithdraw.WithdrawDesc,

            }).ToList().Select(a => new
            {
                a.Id,
                a.Withdrawnum,
                a.BrokeAccount_Id,
                a.Type,
                WithdrawDesc = a.WithdrawDesc,
                Withdrawtime = a.Withdrawtime.ToString("yyy-MM-dd"),
            });
            //取出所有提现明细的ID
            StringBuilder SB = new StringBuilder();
            foreach (var p in list)
            {
                SB.Append(p.Id.ToString() + ",");
            }
            ////////////取出账户明细ID
            StringBuilder stb = new StringBuilder();
            foreach (var b in list)
            {
                stb.Append(b.BrokeAccount_Id.Id.ToString() + ",");
            }
            return PageHelper.toJson(new { List = list, Ids = SB.ToString(), BrokeAccountId = stb.ToString() });
        }
        public HttpResponseMessage SetPay([FromBody]PayModel payModel)
        {
            var user = (UserBase) _workContext.CurrentUser;
            var broker = new BrokerEntity { };
            var BrokeAccount = new BrokeAccountEntity { };
            var BrokerWithdraw = new BrokerWithdrawEntity { };
            var BrokerWithDrawDetail = new BrokerWithdrawDetailEntity { };
            //var condition = new BrokerWithdrawDetailSearchCondition
            //{
            //    BrokerWithdraw = payModel.Id
            //};
            if (user == null)
            {
                //broker = _brokerService.GetBrokerByUserId(user.Id); //获取当前经纪人
                //if (broker == null)
                //{
                    return PageHelper.toJson(PageHelper.ReturnValue(false, "获取用户失败,请检查是否登陆"));
                //}
            }

            if (string.IsNullOrEmpty(payModel.Id))
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据不能为空"));
            }
            //根据经纪人提现ID查询经纪人体现信息
            BrokerWithdraw = _brokerwithdrawService.GetBrokerWithdrawById(Convert.ToInt32(payModel.Id));
            // 判断提现状态,如果状态为一,则表示已经提现           
            if (BrokerWithdraw.State == 1) 
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "财务已经打款"));
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////
            //if (string.IsNullOrEmpty(payModel.Ids)) 
            //{
            //    return PageHelper.toJson(PageHelper.ReturnValue(false, "数据不能为空"));
            //}
            //if (string.IsNullOrEmpty(payModel.BrokeAccountId))
            //{
            //    return PageHelper.toJson(PageHelper.ReturnValue(false,"数据不能为空"));
            //}
            //构建查询实体
            var seach = new BrokerWithdrawDetailSearchCondition
            {
                OrderBy = EnumBrokerWithdrawDetailSearchOrderBy.OrderByTime,
                BrokerWithdraw = _brokerwithdrawService.GetBrokerWithdrawById(Convert.ToInt32(payModel.Id)),
            };
            var list = _brokerwithdrawDetailService.GetBrokerWithdrawDetailsByCondition(seach).Select(b => new
            {
                b.Id,
                b.Withdrawnum,
                b.BrokeAccount_Id,
                b.Withdrawtime,
                b.Type,
                b.BrokerWithdraw.WithdrawDesc,
                b.BrokerWithdraw.BankCard.Num,

            }).ToList().Select(a => new
            {
                a.Id,
                a.Withdrawnum,
                a.BrokeAccount_Id,
                a.Type,
                a.Num,
                WithdrawDesc = a.WithdrawDesc,
                Withdrawtime = a.Withdrawtime.ToString("yyy-MM-dd"),
            });
            foreach (var p in list) 
            {
                if (p == null) 
                {
                    return PageHelper.toJson(PageHelper.ReturnValue(false, "数据错误"));
                }
                if (Convert.ToInt32(p.Type) == 0) 
                {
                    var blModel = new BLPayEntity
                    {
                        Name = payModel.Name,
                        Describe = payModel.Describe,
                        BankCard = p.Num,
                        Accountantid = user.Id,
                        Amount = p.Withdrawnum,
                        Adduser = user.Id,
                        Upuser = user.Id,
                        Addtime = DateTime.Now,
                        Uptime = DateTime.Now,
                    };
                    _blPayService.Create(blModel);

                }
                if (Convert.ToInt32(p.Type) == 1)
                {
                    var breModel = new BRECPayEntity
                    {
                        Name = payModel.Name,
                        Describe = payModel.Describe,
                        BankCard = p.Num,
                        Accountantid = user.Id,
                        Amount = p.Withdrawnum,
                        Adduser = user.Id,
                        Upuser = user.Id,
                        Addtime = DateTime.Now,
                        Uptime = DateTime.Now,
                    };
                    _brecPayService.Create(breModel);
                }
                BrokeAccount = _brokerAcountService.GetBrokeAccountById(p.BrokeAccount_Id.Id);
                BrokeAccount.State = 1;
                BrokeAccount.Uptime = DateTime.Now;
                BrokeAccount.Upuser = user.Id;
                _brokerAcountService.Update(BrokeAccount);
            }
            //string[] strBrokeAccountId = payModel.BrokeAccountId.Split(',');
            //foreach (var BrokeAccountId in strBrokeAccountId)
            //{
            //    if (string.IsNullOrEmpty(BrokeAccountId))
            //    {
            //        return PageHelper.toJson(PageHelper.ReturnValue(false, "数据错误"));
            //    }
            //    BrokeAccount = _brokerAcountService.GetBrokeAccountById(Convert.ToInt32(BrokeAccountId));
            //    if (BrokeAccount.State == 1) 
            //    {
            //        break;
            //    }
            //}
            //string[] strIds = payModel.Ids.Split(',');
            //foreach (var id in strIds)
            //{
            //    if(string.IsNullOrEmpty(id))
            //    {
            //        break;
            //    }
            //    var model = _brokerwithdrawDetailService.GetBrokerWithdrawDetailById(Convert.ToInt32(id));
            //    if (Convert.ToInt32(model.Type) == 0) 
            //    {
            //        var blModel = new BLPayEntity
            //        {
            //            Name = payModel.Name,
            //            Describe = payModel.Describe,
            //            BankCard = Convert.ToInt32(model.BankCard.Num),
            //            Accountantid = broker.Id,
            //            Amount = model.Withdrawnum,
            //            Adduser = broker.Id,
            //            Upuser = broker.Id,
            //            Addtime = DateTime.Now,
            //            Uptime = DateTime.Now,
            //        };
            //        _blPayService.Create(blModel);
            //    }
            //    if (Convert.ToInt32(model.Type) == 1) 
            //    {
            //        var breModel = new BRECPayEntity
            //        {
            //            Name = payModel.Name,
            //            Describe = payModel.Describe,
            //            BankCard = Convert.ToInt32(model.BankCard.Num),
            //            Accountantid = broker.Id,
            //            Amount = model.Withdrawnum,
            //            Adduser = broker.Id,
            //            Upuser = broker.Id,
            //            Addtime = DateTime.Now,
            //            Uptime = DateTime.Now, 
            //        };
            //        _brecPayService.Create(breModel);
            //    }
               
            //}
            BrokerWithdraw.State = 1;
            //BrokerWithdraw.AccAccountantId.UserId = user.Id;
            BrokerWithdraw.Uptime = DateTime.Now;
            BrokerWithdraw.Upuser = user.Id;
            BrokerWithdraw.WithdrawDesc = payModel.Describe;
            BrokerWithdraw.BankSn = payModel.BankSn;
            _brokerwithdrawService.Update(BrokerWithdraw);
           
            return PageHelper.toJson(PageHelper.ReturnValue(true, "打款成功"));
        }
		public IQueryable<BrokerWithdrawDetailEntity> GetBrokerWithdrawDetailsByCondition(BrokerWithdrawDetailSearchCondition condition)
		{
			var query = _brokerwithdrawdetailRepository.Table;
			try
			{
				if (condition.WithdrawtimeBegin.HasValue)
                {
                    query = query.Where(q => q.Withdrawtime>= condition.WithdrawtimeBegin.Value);
                }
                if (condition.WithdrawtimeEnd.HasValue)
                {
                    query = query.Where(q => q.Withdrawtime < condition.WithdrawtimeEnd.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 (condition.Withdrawnum.HasValue)
                {
                    query = query.Where(q => q.Withdrawnum == condition.Withdrawnum.Value);
                }
				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.BankCards != null && condition.BankCards.Any())
                {
                    query = query.Where(q => condition.BankCards.Contains(q.BankCard));
                }
				if (condition.Addusers != null && condition.Addusers.Any())
                {
                    query = query.Where(q => condition.Addusers.Contains(q.Adduser));
                }

                if (!string.IsNullOrEmpty(condition.Type))
                {
                    query = query.Where(q => q.Type == condition.Type);
                }

				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 EnumBrokerWithdrawDetailSearchOrderBy.OrderById:
							query = condition.isDescending?query.OrderByDescending(q=>q.Id):query.OrderBy(q=>q.Id);
							break;

                        case EnumBrokerWithdrawDetailSearchOrderBy.OrderByTime:
                            query = condition.isDescending ? query.OrderBy(q => q.Uptime) : query.OrderByDescending(q => q.Uptime);
                            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 int GetBrokerWithdrawDetailCount (BrokerWithdrawDetailSearchCondition condition)
		{
			var query = _brokerwithdrawdetailRepository.Table;
			try
			{
				if (condition.WithdrawtimeBegin.HasValue)
                {
                    query = query.Where(q => q.Withdrawtime>= condition.WithdrawtimeBegin.Value);
                }
                if (condition.WithdrawtimeEnd.HasValue)
                {
                    query = query.Where(q => q.Withdrawtime < condition.WithdrawtimeEnd.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 (!string.IsNullOrEmpty(condition.Type))
                {
                    query = query.Where(q => q.Type == condition.Type);
                }
                if (condition.UptimeEnd.HasValue)
                {
                    query = query.Where(q => q.Uptime < condition.UptimeEnd.Value);
                }
				if (condition.Withdrawnum.HasValue)
                {
                    query = query.Where(q => q.Withdrawnum == condition.Withdrawnum.Value);
                }
				if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
				if (condition.Brokers != null )
                {
                    query = query.Where(q => condition.Brokers.Id ==(q.Broker.Id));
                }
				if (condition.BankCards != null && condition.BankCards.Any())
                {
                    query = query.Where(q => condition.BankCards.Contains(q.BankCard));
                }
				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));
                }
				return query.Count();
			}
			catch(Exception e)
			{
				_log.Error(e,"数据库操作出错");
                return -1;
			}
		}
        public HttpResponseMessage GetBrokerWithdrawDetailListByUserId(string userId = null, int page = 1, int pageSize = 10)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据错误!"));
            }

            var brokerwithdrawdetailcon = new BrokerWithdrawDetailSearchCondition
            {
                Brokers = _brokerService.GetBrokerById(Convert.ToInt32(userId)),
                Page = Convert.ToInt32(page),
                PageCount = pageSize
            };
            var PointDetailList = _brokerwithdrawdetailService.GetBrokerWithdrawDetailsByCondition(brokerwithdrawdetailcon).Select(p => new
            {
                Id = p.Id,
                bankname = p.BankCard.Bank.Codeid,
                banknumber = p.BankCard.Num,
                Withdrawnum = p.Withdrawnum,
                Withdrawtime = p.Withdrawtime
            }).ToList().Select(p => new
            {
                Id = p.Id,
                bankname = p.bankname,
                banknumber = p.banknumber,
                Withdrawnum=p.Withdrawnum,
                Withdrawtime = p.Withdrawtime.ToString("yyyy-MM-dd")
            });
            var PointDetailListCount = _brokerwithdrawdetailService.GetBrokerWithdrawDetailCount(brokerwithdrawdetailcon);
            return PageHelper.toJson(new { List = PointDetailList, Condition = brokerwithdrawdetailcon, totalCount = PointDetailListCount });

        }