public EventOrderEntity Update(EventOrderEntity entity)
 {
     try
     {
         _eventorderRepository.Update(entity);
         return entity;
     }
     catch (Exception e)
     {
         _log.Error(e, "数据库操作出错");
         return null;
     }
 }
 public bool Delete(EventOrderEntity entity)
 {
     try
     {
         _eventorderRepository.Delete(entity);
         return true;
     }
     catch (Exception e)
     {
         _log.Error(e, "数据库操作出错");
         return false;
     }
 }
        public HttpResponseMessage UpdateBroker([FromBody] BrokerModel broker)
        {
            if (broker != null && !string.IsNullOrEmpty(broker.Id.ToString()) && PageHelper.ValidateNumber(broker.Id.ToString()))
            {
                var brokerModel = _brokerService.GetBrokerById(broker.Id);
                brokerModel.Headphoto = broker.Headphoto;
                brokerModel.Nickname = broker.Nickname;
                brokerModel.Phone = broker.Phone;
                brokerModel.Sfz = broker.Sfz;
                brokerModel.Email = broker.Email;
                brokerModel.Realname = broker.Realname;
                brokerModel.Sexy = broker.Sexy;
                brokerModel.WeiXinNumber = broker.WeiXinNumber;//by  yangyue  2015/7/16

                #region 转职经纪人 杨定鹏 2015年6月11日17:29:58
                //填写身份证,邮箱,和真实姓名后就能转职经纪人
                if (!string.IsNullOrEmpty(broker.Email) && !string.IsNullOrEmpty(broker.Sfz) &&
                    !string.IsNullOrEmpty(broker.Realname))
                {
                    //权限变更
                    var brokerRole = _roleService.GetRoleByName("broker");
                    //User权限缺少时自动添加
                    if (brokerRole == null)
                    {
                        brokerRole = new Role
                        {
                            RoleName = "broker",
                            RolePermissions = null,
                            Status = RoleStatus.Normal,
                            Description = "user用户转职为broker"
                        };
                    }

                    var user = _userService.FindUser(brokerModel.UserId);
                    user.UserRoles.First().Role = brokerRole;

                    //更新用户权限
                    if (_userService.ModifyUser(user))
                    {
                        //更新broker表记录
                        brokerModel.Usertype = EnumUserType.经纪人;
                        _brokerService.Update(brokerModel);
                        //return PageHelper.toJson(PageHelper.ReturnValue(true, "数据更新成功!"));
                    }


                }
                #endregion


                #region 邀请码逻辑 by yangyue  2015/7/16
                
                var even = new EventSearchCondition //判断该活动是否开启
                {
                    EventContent ="完善经纪人资料活动",
                    State = true
                };
                if (_eventService.GetEventCount(even) > 0)
                {
                    #region  邀请码活动 by yangyue  2015/7/16

                    InviteCodeSearchCondition icodeseCon = new InviteCodeSearchCondition
                    {
                        NumUser = brokerModel.Id,
                        State = 1
                    };
                    //判断有无使用过邀请码
                    if (_inviteCodeService.GetInviteCodeByCount(icodeseCon) <= 0)//没使用过邀请码
                    {
                        //邀请码不为空
                        if (!string.IsNullOrEmpty(broker.code))
                        {
                            var levelCon = new LevelSearchCondition
                            {
                                Name = "白银"
                            };
                            var level = _levelService.GetLevelsByCondition(levelCon).FirstOrDefault();

                            #region  白银逻辑

                            BrokerSearchCondition bsearchModel = new BrokerSearchCondition
                            {
                                Levels = level

                            };
                            //1判断白银等级人数是否《=3000 
                            if (_brokerService.GetBrokerCount(bsearchModel) <= 3000)
                            {
                                var invite = new InviteCodeSearchCondition
                                {

                                    Number = broker.code,
                                    State = 0
                                };
                                var con = _inviteCodeService.GetInviteCodeByCondition(invite).FirstOrDefault();//查询邀请码是否存在并且未使用
                                var eventcon = new EventOrderSearchCondition //判断该经济人有无参与活动 等于0是没参与 等于1是参与过

                                {
                                    Brokers = _brokerService.GetBrokerById(brokerModel.Id)
                                };
                                var num = _eventOrderService.GetEventOrderCount(eventcon);//查询活动订单表有无该经纪人

                                if (con != null && num == 0) //存在 未使用  并且该经纪人未参与过活动
                                {
                                    //using (TransactionScope tsCope = new TransactionScope(TransactionScopeOption.RequiresNew))
                                    //{
                                    #region 添加到活动订单 经纪人账户表 AgentBill表 修改经纪人等级 生成3个邀请码  并发送到手机

                                    var eve = new EventSearchCondition
                                    {
                                        EventContent = "完善经纪人资料活动"
                                    };
                                   var coneve= _eventService.GetEventByCondition(eve).FirstOrDefault();
                                    //添加活动订单信息
                                    EventOrderEntity emodel = new EventOrderEntity();
                                    emodel.AcDetail = "完整经济人资料奖励30元";
                                    emodel.Addtime = DateTime.Now;
                                    emodel.MoneyCount = 30;
                                    emodel.Broker = brokerModel;
                                    emodel.Event = coneve;
                                    _eventOrderService.Create(emodel);

                                    //添加到经纪人账户表brokeraccount
                                    BrokeAccountEntity brokeraccountmodel = new BrokeAccountEntity();
                                    brokeraccountmodel.MoneyDesc = "完整经济人资料奖励30元";
                                    brokeraccountmodel.Balancenum = 30;
                                    brokeraccountmodel.Adduser = brokerModel.Id;
                                    brokeraccountmodel.Addtime = DateTime.Now;
                                    brokeraccountmodel.Upuser = brokerModel.Id;
                                    brokeraccountmodel.Uptime = DateTime.Now;
                                    brokeraccountmodel.Broker = brokerModel;
                                    brokeraccountmodel.Type = 2;
                                    brokeraccountmodel.State = 0;
                                    _brokerAccountService.Create(brokeraccountmodel);



                                    //添加记录到AgentBill表

                                    AgentBillEntity abmmodel = new AgentBillEntity();
                                    abmmodel.AgentId = brokerModel.Id;
                                    abmmodel.Agentname = brokerModel.Brokername;
                                    abmmodel.LandagentId = 1;
                                    abmmodel.Amount = 30;
                                    abmmodel.Isinvoice = false;
                                    abmmodel.Checkoutdate = DateTime.Now;
                                    abmmodel.Addtime = DateTime.Now;
                                    abmmodel.Updtime = DateTime.Now;
                                    abmmodel.EventOrderId = emodel.Id;
                                    _agentBillService.Create(abmmodel);

                                    //    tsCope.Complete();
                                    //}

                                    //      using (TransactionScope tsCope = new TransactionScope(TransactionScopeOption.RequiresNew))
                                    //{

                                    //修改邀请码表信息
                                    con.NumUser = brokerModel.Id;
                                    con.UseTime = DateTime.Now;
                                    con.State = 1;
                                    _inviteCodeService.Update(con);

                                    //更新等级
                                    brokerModel.Level = level;


                                    brokerModel.Amount += 30;
                                    _brokerService.Update(brokerModel);
                                    //    tsCope.Complete();
                                    //}

                                    //并且生成3个邀请码发送到手机端口 并插入库中
                                    string randmNums = string.Empty;
                                    for (int i = 0; i < 3; i++)
                                    {
                                        string rans = GenerateRandomNumber(6);
                                        randmNums += rans + ",";

                                        InviteCodeEntity inviteCode = new InviteCodeEntity();
                                        inviteCode.CreatTime = DateTime.Now;
                                        inviteCode.Number = rans;
                                        inviteCode.UseTime = DateTime.Now;
                                        inviteCode.State = 0;
                                        inviteCode.Broker = brokerModel;
                                        _inviteCodeService.Create(inviteCode);
                                    }
                                    SMSHelper.Sending(brokerModel.Phone, "恭喜您完善个人信息,奖励您三个邀请码:" + randmNums + "赶快邀请小伙伴们,惊喜等你哟!" + "【优客惠】");
                                    #endregion


                                }
                                else //不存在 或已被使用
                                {
                                    #region 邀请码不存在 或已被使用 就转为青铜逻辑


                                    #region 青铜逻辑

                                    var levelConn = new LevelSearchCondition
                                    {
                                        Name = "青铜"
                                    };
                                    var qlevel = _levelService.GetLevelsByCondition(levelConn).FirstOrDefault();

                                    BrokerSearchCondition qbsearchModel = new BrokerSearchCondition
                                    {
                                        Levels = qlevel

                                    };

                                    // 1判断青铜是否《=1000                             
                                    if (_brokerService.GetBrokerCount(qbsearchModel) <= 1000)
                                    {
                                        var qinvite = new InviteCodeSearchCondition
                                        {

                                            Number = broker.code,
                                            State = 0
                                        };
                                        var qcon = _inviteCodeService.GetInviteCodeByCondition(qinvite).FirstOrDefault();
                                        //查询邀请码是否存在并且未使用
                                        var eventcon1 = new EventOrderSearchCondition //判断该经济人有无参与活动 等于0是没参与 等于1是参与过

                                        {
                                            Brokers = _brokerService.GetBrokerById(brokerModel.Id)
                                        };
                                        var num1 = _eventOrderService.GetEventOrderCount(eventcon1); //查询活动订单表有无该经纪人
                                        if (qcon != null && num1 == 0)
                                        {
                                            var eve = new EventSearchCondition
                                            {
                                                EventContent = "完善经纪人资料活动"
                                            };
                                            var coneve = _eventService.GetEventByCondition(eve).FirstOrDefault();
                                            //using (TransactionScope tsCope = new TransactionScope(TransactionScopeOption.RequiresNew))
                                            //{
                                            EventOrderEntity emodel = new EventOrderEntity();
                                            emodel.AcDetail = "完整经济人资料无邀请码奖励20元";
                                            emodel.Addtime = DateTime.Now;
                                            emodel.MoneyCount = 20;
                                            emodel.Broker = brokerModel;
                                            emodel.Event = coneve;
                                            _eventOrderService.Create(emodel);

                                            //添加到经纪人账户表
                                            BrokeAccountEntity brokeraccountmodel = new BrokeAccountEntity();
                                            brokeraccountmodel.MoneyDesc = "完整经济人资料奖励20元";
                                            brokeraccountmodel.Balancenum = 20;
                                            brokeraccountmodel.Adduser = brokerModel.Id;
                                            brokeraccountmodel.Addtime = DateTime.Now;
                                            brokeraccountmodel.Upuser = brokerModel.Id;
                                            brokeraccountmodel.Uptime = DateTime.Now;
                                            brokeraccountmodel.Broker = brokerModel;
                                            brokeraccountmodel.Type = 2;
                                            brokeraccountmodel.State = 0;
                                            _brokerAccountService.Create(brokeraccountmodel);


                                            //添加记录到AgentBill表

                                            AgentBillEntity abmmodel = new AgentBillEntity();
                                            abmmodel.AgentId = brokerModel.Id;
                                            abmmodel.Agentname = brokerModel.Brokername;
                                            abmmodel.LandagentId = 1;
                                            abmmodel.Amount = 20;
                                            abmmodel.Isinvoice = false;
                                            abmmodel.Checkoutdate = DateTime.Now;
                                            abmmodel.Addtime = DateTime.Now;
                                            abmmodel.Updtime = DateTime.Now;
                                            abmmodel.EventOrderId = emodel.Id;
                                            _agentBillService.Create(abmmodel);

                                            brokerModel.Level = qlevel;
                                            brokerModel.Amount += 20;
                                            _brokerService.Update(brokerModel);
                                            //给20元钱 等级设为青铜 
                                            //    tsCope.Complete();
                                            //}
                                        }
                                        else
                                        {
                                            if (_brokerService.Update(brokerModel) != null)
                                            {
                                                //等级设为青铜
                                                brokerModel.Level = qlevel;
                                                _brokerService.Update(brokerModel);
                                                return PageHelper.toJson(PageHelper.ReturnValue(true, "邀请码输入错误!"));
                                            }
                                        }

                                    #endregion

                                    }

                                    #endregion
                                }
                            }
                            else
                            {
                                if (_brokerService.Update(brokerModel) != null)
                                {
                                    //白银人数超过3000 等级设为白银
                                    brokerModel.Level = level;
                                    _brokerService.Update(brokerModel);
                                }
                            }

                            #endregion
                        }
                        else//邀请码没有填写  没有参与过活动 给20元钱 等级设为青铜
                        {
                            #region 没有填写邀请码 给20元钱 等级设为青铜
                            var levelConn = new LevelSearchCondition
                            {
                                Name = "青铜"
                            };
                            var qlevel = _levelService.GetLevelsByCondition(levelConn).FirstOrDefault(); //等级为青铜
                            //判断青铜是否《=1000 
                            BrokerSearchCondition qbsearchModel = new BrokerSearchCondition
                            {
                                Levels = qlevel

                            };
                            var eventcon = new EventOrderSearchCondition //判断该经济人有无参与活动 等于0是没参与 等于1是参与过

                            {
                                Brokers = _brokerService.GetBrokerById(brokerModel.Id)
                            };
                            var num = _eventOrderService.GetEventOrderCount(eventcon);
                            //Brokers
                            if (_brokerService.GetBrokerCount(qbsearchModel) <= 1000 && num == 0) //判断青铜是否《=1000  
                            {
                                //青铜等级人数《=1000 给20元钱 等级设为青铜
                                //using (TransactionScope tsCope = new TransactionScope(TransactionScopeOption.RequiresNew))
                                //{
                                //添加到活动订单表

                                var eve = new EventSearchCondition
                                {
                                    EventContent = "完善经纪人资料活动"
                                };
                                var coneve = _eventService.GetEventByCondition(eve).FirstOrDefault();
                                EventOrderEntity emodel = new EventOrderEntity();
                                emodel.AcDetail = "完整经济人资料无邀请码奖励20元";
                                emodel.Addtime = DateTime.Now;
                                emodel.MoneyCount = 20;
                                emodel.Event = coneve;
                                emodel.Broker = brokerModel;
                                _eventOrderService.Create(emodel);

                                //添加到经纪人账户表
                                BrokeAccountEntity brokeraccountmodel = new BrokeAccountEntity();
                                brokeraccountmodel.MoneyDesc = "完整经济人资料奖励20元";
                                brokeraccountmodel.Balancenum = 20;
                                brokeraccountmodel.Adduser = brokerModel.Id;
                                brokeraccountmodel.Addtime = DateTime.Now;
                                brokeraccountmodel.Upuser = brokerModel.Id;
                                brokeraccountmodel.Uptime = DateTime.Now;
                                brokeraccountmodel.Broker = brokerModel;
                                brokeraccountmodel.Type = 2;
                                brokeraccountmodel.State = 0;
                                _brokerAccountService.Create(brokeraccountmodel);


                                //添加记录到AgentBill表

                                AgentBillEntity abmmodel = new AgentBillEntity();
                                abmmodel.AgentId = brokerModel.Id;
                                abmmodel.Agentname = brokerModel.Brokername;
                                abmmodel.LandagentId = 1;
                                abmmodel.Amount = 20;
                                abmmodel.Isinvoice = false;
                                abmmodel.Checkoutdate = DateTime.Now;
                                abmmodel.Addtime = DateTime.Now;
                                abmmodel.Updtime = DateTime.Now;
                                abmmodel.EventOrderId = emodel.Id;
                                _agentBillService.Create(abmmodel);


                                brokerModel.Level = qlevel;
                                brokerModel.Amount += 20;
                                _brokerService.Update(brokerModel);
                                //    tsCope.Complete();
                                //}

                            }
                            else
                            {
                                if (_brokerService.Update(brokerModel) != null)
                                {
                                    //青铜人数已经超过1000人 则等级直接设为青铜
                                    brokerModel.Level = qlevel;
                                    _brokerService.Update(brokerModel);
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion
                }
                else
                {
                    return PageHelper.toJson(PageHelper.ReturnValue(true, "该活动已经下架!"));
                }
                

                #endregion
               

                try
                {
                    if (_brokerService.Update(brokerModel) != null)
                    {
                        return PageHelper.toJson(PageHelper.ReturnValue(true, "数据更新成功!"));
                    }
                }
                catch
                {
                    return PageHelper.toJson(PageHelper.ReturnValue(false, "数据更新失败!"));
                }


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

        }