Example #1
0
 public void UpdateAdminUser(long id, string name, string phoneNum, string password, string email, long?cityId)
 {
     using (ZSZDbContext ctx = new ZSZDbContext())
     {
         BaseService <AdminUserEntity> bs
             = new BaseService <AdminUserEntity>(ctx);
         var user = bs.GetById(id);
         if (user == null)
         {
             throw new ArgumentException("找不到id=" + id + "的管理员");
         }
         user.Name     = name;
         user.PhoneNum = phoneNum;
         user.Email    = email;
         if (!string.IsNullOrEmpty(password))
         {
             user.PasswordHash =
                 CommonHelper.CalcMD5(user.PasswordSalt + password);
         }
         user.CityId = cityId;
         ctx.SaveChanges();
     }
 }
Example #2
0
 //2,3,4
 //3,4,5
 public void UpdatePermIds(long roleId, long[] permIds)
 {
     using (ZSZDbContext ctx = new ZSZDbContext())
     {
         BaseService <RoleEntity> roleBS
             = new BaseService <RoleEntity>(ctx);
         var role = roleBS.GetById(roleId);
         if (role == null)
         {
             throw new ArgumentException("roleId不存在" + roleId);
         }
         role.Permissions.Clear();
         BaseService <PermissionEntity> permBS
             = new BaseService <PermissionEntity>(ctx);
         var perms = permBS.GetAll()
                     .Where(p => permIds.Contains(p.Id)).ToList();
         foreach (var perm in perms)
         {
             role.Permissions.Add(perm);
         }
         ctx.SaveChanges();
     }
 }
Example #3
0
        public void AddRoleIds(long adminUserId, long[] roleIds)
        {
            using (ZSZDbContext ctx = new ZSZDbContext())
            {
                BaseService <AdminUserEntity> adminBS = new BaseService <AdminUserEntity>(ctx);
                AdminUserEntity user = adminBS.GetById(adminUserId);
                if (user == null)
                {
                    throw new ArgumentException("用户不存在" + adminUserId);
                }
                if (roleIds != null)
                {
                    BaseService <RoleEntity> roleBS = new BaseService <RoleEntity>(ctx);
                    var roles = roleBS.GetAll().Where(u => roleIds.Contains(u.Id)).ToArray();
                    foreach (var role in roles)
                    {
                        user.Roles.Add(role);
                    }
                }

                ctx.SaveChanges();
            }
        }
Example #4
0
        public long AddNew(string phoneNum, string password)
        {
            using (ZSZDbContext ctx = new ZSZDbContext())
            {
                //检查手机号不能重复

                BaseService <UserEntity> bs = new BaseService <UserEntity>(ctx);
                bool exists = bs.GetAll().Any(u => u.PhoneNum == phoneNum);
                if (exists)
                {
                    throw new ArgumentException("手机号已经存在");
                }
                UserEntity user = new UserEntity();
                user.PhoneNum = phoneNum;
                string salt    = CommonHelper.CreateVerifyCode(5);
                string pwdHash = CommonHelper.CalcMD5(salt + password);
                user.PasswordHash = pwdHash;
                user.PasswordSalt = salt;
                ctx.Users.Add(user);
                ctx.SaveChanges();
                return(user.Id);
            }
        }
Example #5
0
        /// <summary>
        /// 根据roleId更新角色名称
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="roleName"></param>
        public void Update(long roleId, string roleName)
        {
            using (ZSZDbContext ctx = new ZSZDbContext())
            {
                BaseService <RoleEntity> bs = new BaseService <RoleEntity>(ctx);
                var role = bs.GetAll().ToList().SingleOrDefault(r => r.Name == roleName && r.Id == roleId);
                //正常情况不应该执行这个异常,因为UI层应该把这些情况处理好
                //这里只是“把好最后一关”
                if (role == null)
                {
                    throw new ArgumentException("不存在" + roleId + "角色");
                }

                /*role.Name = roleName;
                 * ctx.Roles.Add(role);
                 * ctx.SaveChanges();*/
                RoleEntity roleentity = new Entities.RoleEntity();
                roleentity.Id         = roleId;
                ctx.Entry(role).State = EntityState.Unchanged;
                role.Name             = roleName;
                ctx.SaveChanges();
            }
        }
Example #6
0
        public void AddPermIds(long roleId, long[] permIds)
        {
            using (ZSZDbContext ctx = new ZSZDbContext())
            {
                BaseService <RoleEntity> roleBS = new BaseService <RoleEntity>(ctx);
                var role = roleBS.GetById(roleId);
                if (role == null)
                {
                    throw new ArgumentException("roleId不存在 " + roleId);
                }
                if (permIds != null)
                {
                    BaseService <PermissionEntity> permissionBS = new BaseService <PermissionEntity>(ctx);
                    var pers = permissionBS.GetAll().Where(u => permIds.Contains(u.Id)).ToArray();
                    foreach (var per in pers)
                    {
                        role.Permissions.Add(per);
                    }
                }

                ctx.SaveChanges();
            }
        }
Example #7
0
 public long AddNew(string cityName)
 {
     using (ZSZDbContext ctx = new ZSZDbContext())
     {
         CommonService <CityEntity> bs
             = new CommonService <CityEntity>(ctx);
         //判断是否存在任何一条数据满足 c.Name == cityName
         //即存在这样一个名字的城市
         //如果只是判断“是否存在”,那么用Any效率比Where().count()效率高
         //Where(c => c.Name == cityName).Count()>0
         var  datas  = bs.GetAll();
         bool exists = datas.Any(c => c.Name == cityName);
         if (exists)
         {
             throw new ArgumentException("城市已经存在");
         }
         CityEntity city = new CityEntity();
         city.Name = cityName;
         ctx.Cities.Add(city);
         ctx.SaveChanges();
         return(city.Id);
     }
 }
Example #8
0
        public long AddNew(HouseAddNewDTO house)
        {
            HouseEntity houseEntity = new HouseEntity();

            houseEntity.Address = house.Address;
            houseEntity.Area    = house.Area;
            using (ZSZDbContext ctx = new ZSZDbContext())
            {
                BaseService <AttachmentEntity> attbs
                    = new BaseService <AttachmentEntity>(ctx);
                //拿到house.AttachmentIds为主键的房屋配套设施
                var atts = attbs.GetAll().Where(a => house.AttachmentIds.Contains(a.Id));
                foreach (var att in atts)
                {
                    houseEntity.Attachments.Add(att);
                }
                houseEntity.CheckInDateTime  = house.CheckInDateTime;
                houseEntity.CommunityId      = house.CommunityId;
                houseEntity.DecorateStatusId = house.DecorateStatusId;
                houseEntity.Description      = house.Description;
                houseEntity.Direction        = house.Direction;
                houseEntity.FloorIndex       = house.FloorIndex;
                //houseEntity.HousePics 新增后再单独添加
                houseEntity.LookableDateTime = house.LookableDateTime;
                houseEntity.MonthRent        = house.MonthRent;
                houseEntity.OwnerName        = house.OwnerName;
                houseEntity.OwnerPhoneNum    = house.OwnerPhoneNum;
                houseEntity.RoomTypeId       = house.RoomTypeId;
                houseEntity.StatusId         = house.StatusId;
                houseEntity.TotalFloorCount  = house.TotalFloorCount;
                houseEntity.TypeId           = house.TypeId;
                ctx.Houses.Add(houseEntity);
                ctx.SaveChanges();
                return(houseEntity.Id);
            }
        }