public bool Delete(BrokerWithdrawDetailEntity entity)
		{
			try
            {
                _brokerwithdrawdetailRepository.Delete(entity);
                return true;
            }
            catch (Exception e)
            {
				_log.Error(e,"数据库操作出错");
                return false;
            }
		}
		public BrokerWithdrawDetailEntity Update (BrokerWithdrawDetailEntity entity)
		{
			try
            {
                _brokerwithdrawdetailRepository.Update(entity);
                return entity;
            }
            catch (Exception e)
            {
				_log.Error(e,"数据库操作出错");
                return null;
            }
		}
        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 HttpResponseMessage AddBrokerWithdrawDetail([FromBody] AddMoneyEntity MoneyEntity)
        {
            int bankId = 0;//银行Id
            decimal withdrawMoney = 0;//提现金额
            if (string.IsNullOrEmpty(MoneyEntity.Bank) || string.IsNullOrEmpty( MoneyEntity.Ids) || string.IsNullOrEmpty(MoneyEntity.Hidm) || string.IsNullOrEmpty(MoneyEntity.MobileYzm) || string.IsNullOrEmpty(MoneyEntity.Money))
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误"));
            }
            if (!Int32.TryParse(MoneyEntity.Bank, out bankId))
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误"));
            }

            //if (!Int32.TryParse(MoneyEntity.Money, out withdrawMoney))
            //{
            //    return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误"));
            //}
            //else
            //{
            //    if (withdrawMoney <= 0)
            //    {
            //        return PageHelper.toJson(PageHelper.ReturnValue(false, "提现金额必须大于零"));
            //    }
            //}


            #region 验证码判断 解密
            var strDes = EncrypHelper.Decrypt(MoneyEntity.Hidm, "Hos2xNLrgfaYFY2MKuFf3g==");//解密
            string[] str = strDes.Split('$');
            string source = str[0];//获取验证码
            DateTime date = Convert.ToDateTime(str[1]);//获取发送验证码的时间
            DateTime dateNow = Convert.ToDateTime(DateTime.Now.ToLongTimeString());//获取当前时间
            TimeSpan ts = dateNow.Subtract(date);
            double secMinu = ts.TotalMinutes;//得到发送时间与现在时间的时间间隔分钟数
            if (secMinu > 3) //发送时间与接受时间是否大于3分钟
            {
                return PageHelper.toJson(PageHelper.ReturnValue(false, "你已超过时间验证,请重新发送验证码!"));
            }
            else
            {
                if (MoneyEntity.MobileYzm != source)//判断验证码是否一致
                {
                    return PageHelper.toJson(PageHelper.ReturnValue(false, "验证码错误,请重新发送!"));
                }
            }

            #endregion


            //非空验证

            var user = (UserBase)_workContext.CurrentUser;
            if (user != null)
            {
                var broker = _brokerService.GetBrokerByUserId(user.Id);//获取当前经纪人
                if (broker != null)
                {
                    //根据对应的经纪人账户明细Ids 添加到提现主表 附表中去 
                    if (!string.IsNullOrEmpty(MoneyEntity.Ids))
                    {
                        var bankCard = _bankcardService.GetBankCardById(Convert.ToInt32(MoneyEntity.Bank));
                        if(bankCard.Broker.Id!=broker.Id)
                        {
                            return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误"));
                        }


                        List<BrokerWithdrawDetailEntity> listBrokerWithDetail = new List<BrokerWithdrawDetailEntity>(); //提现明细List
                        var brokerWithdraw = new BrokerWithdrawEntity //提现主表
                        {
                            Addtime = DateTime.Now,
                            Adduser = broker.Id,
                            BankCard = bankCard,
                            BankSn = "",
                            Broker = broker,
                            State = 0,
                            Uptime = DateTime.Now,
                            Upuser = broker.Id,
                            WithdrawDesc = "",
                            WithdrawTime = DateTime.Now, 
                        };

                        try
                        {
                            foreach (var p in MoneyEntity.Ids.Split(','))
                            {
                                if(string.IsNullOrEmpty(p))
                                {
                                    continue;
                                }
                                var broaccount = _brokeaccountService.GetBrokeAccountById(Convert.ToInt32(p));//获取该笔账户
                                if (broaccount.Broker.Id != broker.Id)//判断该笔账户金额是否是当前这个经纪人
                                {
                                    return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误"));
                                }
                                else
                                {
                                    withdrawMoney += broaccount.Balancenum;//提现总金额

                                    var bwithdrawDetail = new BrokerWithdrawDetailEntity
                                    {
                                        BankCard = bankCard,
                                        Withdrawnum = Convert.ToDecimal(broaccount.Balancenum),
                                        Withdrawtime = DateTime.Now,
                                        Broker = broker,
                                        Uptime = DateTime.Now,
                                        Addtime = DateTime.Now,
                                        Adduser = broker.Id,
                                        Upuser = broker.Id,
                                        Type =broaccount.Type.ToString(),
                                        BrokeAccount_Id =broaccount                                          
                                    };
                                    listBrokerWithDetail.Add(bwithdrawDetail);
                                }
                            }
                            //更新提现总金额
                            brokerWithdraw.WithdrawTotalNum = withdrawMoney;

                           brokerWithdraw= _brokerwithdrawService.Create(brokerWithdraw);//添加到提现主表

                            foreach(var browithdetail in listBrokerWithDetail)//添加到提现附表
                            {


                                browithdetail.BrokerWithdraw = brokerWithdraw;

                                _brokerwithdrawdetailService.Create(browithdetail);

                                //更改账户表中 状态
                                var brokeraccount = browithdetail.BrokeAccount_Id;
                                brokeraccount.State = -1;
                                _brokeaccountService.Update(brokeraccount);
                            }
                            //更新到经纪人表中 可用金额
                            broker.Amount = Convert.ToDecimal(GetBrokerAmount());
                            _brokerService.Update(broker);

                            return PageHelper.toJson(PageHelper.ReturnValue(true, "提现申请成功!"));
                        }
                        catch
                        {
                            return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误"));
                        }

                    }
                    else
                    {
                        return PageHelper.toJson(PageHelper.ReturnValue(false, "数据验证错误"));
                    }



                    #region 以前的逻辑
                    //decimal getMoney = Convert.ToDecimal(GetBrokerAmount());//计算得到的剩余总金额                   
                    //decimal syMoney = 0;//剩余金额
                    //// 提现金额逻辑判断(账户金额表 和提现表相减 跟经纪人表中‘提现金额’字段一致)

                    //if (Convert.ToDecimal(MoneyEntity.Money) > getMoney)
                    //{
                    //    return PageHelper.toJson(PageHelper.ReturnValue(false, "账户余额不足,不能提现"));
                    //}
                    //syMoney = getMoney - Convert.ToDecimal(MoneyEntity.Money);

                    ////将剩余金额更新到经纪人表中金额字段
                    ////broker.Amount = syMoney;
                    ////_brokerService.Update(broker);


                    ////更新到提现表中
                    //var entity = new BrokerWithdrawDetailEntity
                    //{
                    //    BankCard = _bankcardService.GetBankCardById(Convert.ToInt32(MoneyEntity.Bank)),
                    //    Withdrawnum = Convert.ToDecimal(MoneyEntity.Money),
                    //    Withdrawtime = DateTime.Now,
                    //    Broker = broker,
                    //    Uptime = DateTime.Now,
                    //    Addtime = DateTime.Now,
                    //    Adduser = broker.Id,
                    //    Upuser = broker.Id,
                    //    Type = "0"
                    //};

                    //try
                    //{
                    //    if (_brokerwithdrawdetailService.Create(entity) != null)
                    //    {
                    //        return PageHelper.toJson(PageHelper.ReturnValue(true, entity.Id.ToString()));
                    //    }
                    //}
                    //catch
                    //{
                    //    return PageHelper.toJson(PageHelper.ReturnValue(false, "数据添加失败!"));
                    //}

                    #endregion


                }
            }
            return PageHelper.toJson(PageHelper.ReturnValue(false, "获取用户失败,请检查是否登陆"));
        }