/// <summary> /// 添加一个后台用户 /// </summary> /// <param name="name"></param> /// <param name="phoneNum"></param> /// <param name="password"></param> /// <param name="email"></param> /// <param name="cityId"></param> /// <returns></returns> public long AddAdminUser(string name, string phoneNum, string password, string email, long?cityId) { AdminUserEntity user = new AdminUserEntity(); user.CityId = cityId; user.Email = email; user.Name = name; user.PhoneNum = phoneNum; string salt = CommonHelper.CreateVerifyCode(5);//盐 user.PasswordSalt = salt; //Md5(盐+用户密码) string pwdHash = CommonHelper.CalcMD5(salt + password); user.PasswordHash = pwdHash; using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); bool exists = bs.GetAll().Any(u => u.PhoneNum == phoneNum); if (exists) { throw new ArgumentException("手机号已经存在" + phoneNum); } ctx.AdminUsers.Add(user); ctx.SaveChanges(); return(user.Id); } }
public void MarkDeleted(long id) { var data = GetById(id); data.IsDeleted = true; ctx.SaveChanges(); }
/// <summary> /// 更新一个房屋 /// </summary> /// <param name="house"></param> public void Update(HouseDTO house) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <HouseEntity> bs = new BaseService <HouseEntity>(ctx); HouseEntity entity = bs.GetById(house.Id); entity.Address = house.Address; entity.Area = house.Area; //2,3,4 entity.Attachments.Clear();//先删再加 var atts = ctx.Attachments.Where(a => a.IsDeleted == false && house.AttachmentIds.Contains(a.Id)); foreach (AttachmentEntity att in atts) { entity.Attachments.Add(att); } //3,4,5 entity.CheckInDateTime = house.CheckInDateTime; entity.CommunityId = house.CommunityId; entity.DecorateStatusId = house.DecorateStatusId; entity.Description = house.Description; entity.Direction = house.Direction; entity.FloorIndex = house.FloorIndex; entity.LookableDateTime = house.LookableDateTime; entity.MonthRent = house.MonthRent; entity.OwnerName = house.OwnerName; entity.OwnerPhoneNum = house.OwnerPhoneNum; entity.RoomTypeId = house.RoomTypeId; entity.StatusId = house.StatusId; entity.TotalFloorCount = house.TotalFloorCount; entity.TypeId = house.TypeId; ctx.SaveChanges(); } }
public void SetUserCityId(long userId, long cityId) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <UserEntity> bs = new BaseService <UserEntity>(ctx); var user = bs.GetById(userId); if (user == null) { throw new ArgumentException("用户id不存在" + userId); } user.CityId = cityId; ctx.SaveChanges(); } }
/// <summary> /// 为一个房屋添加照片 /// </summary> /// <param name="housePic"></param> /// <returns></returns> public long AddNewHousePic(HousePicDTO housePic) { HousePicEntity entity = new HousePicEntity(); entity.HouseId = housePic.HouseId; entity.ThumbUrl = housePic.ThumbUrl; entity.Url = housePic.Url; using (PalmRentDbContext ctx = new PalmRentDbContext()) { ctx.HousePics.Add(entity); ctx.SaveChanges(); return(entity.Id); } }
public long AddNew(long adminUserId, string message) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { AdminLogEntity log = new AdminLogEntity() { AdminUserId = adminUserId, Message = message }; ctx.AdminUserLogs .Add(log); ctx.SaveChanges(); return(log.Id); } }
public long AddNew(string typeName, string name) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { IdNameEntity idName = new IdNameEntity { Name = name, TypeName = typeName }; //todo:检查重复性 ctx.IdNames.Add(idName); ctx.SaveChanges(); return(idName.Id); } }
/// <summary> /// 更新一个权限 /// </summary> /// <param name="id"></param> /// <param name="permName"></param> /// <param name="description"></param> public void UpdatePermission(long id, string permName, string description) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <PermissionEntity> bs = new BaseService <PermissionEntity>(ctx); var perm = bs.GetById(id); if (perm == null) { throw new ArgumentException("id不存在" + id); } perm.Name = permName; perm.Description = description; ctx.SaveChanges(); } }
public void IncrLoginError(long id) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { //检查手机号不能重复 BaseService <UserEntity> bs = new BaseService <UserEntity>(ctx); var user = bs.GetById(id); if (user == null) { throw new ArgumentException("用户不存在 " + id); } user.LoginErrorTimes++; user.LastLoginErrorDateTime = DateTime.Now; ctx.SaveChanges(); } }
/// <summary> /// 添加预约看房 /// </summary> /// <param name="userId"></param> /// <param name="name"></param> /// <param name="phoneNum"></param> /// <param name="houseId"></param> /// <param name="visitDate"></param> /// <returns></returns> public long AddNew(long?userId, string name, string phoneNum, long houseId, DateTime visitDate) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { HouseAppointmentEntity houseApp = new HouseAppointmentEntity(); houseApp.HouseId = houseId; houseApp.Name = name; houseApp.PhoneNum = phoneNum; houseApp.Status = "未处理"; houseApp.UserId = userId; houseApp.VisitDate = visitDate; ctx.HouseAppointments.Add(houseApp); ctx.SaveChanges(); return(houseApp.Id); } }
/// <summary> /// 添加一个权限 /// </summary> /// <param name="permName"></param> /// <param name="description"></param> /// <returns></returns> public long AddPermission(string permName, string description) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <PermissionEntity> permBS = new BaseService <PermissionEntity>(ctx); bool exists = permBS.GetAll().Any(p => p.Name == permName); if (exists) { throw new ArgumentException("权限项已经存在"); } PermissionEntity perm = new PermissionEntity(); perm.Description = description; perm.Name = permName; ctx.Permissions.Add(perm); ctx.SaveChanges(); return(perm.Id); } }
public void UpdatePwd(long userId, string newPassword) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { //检查手机号不能重复 BaseService <UserEntity> bs = new BaseService <UserEntity>(ctx); var user = bs.GetById(userId); if (user == null) { throw new ArgumentException("用户不存在 " + userId); } string salt = user.PasswordSalt;// CommonHelper.CreateVerifyCode(5); string pwdHash = CommonHelper.CalcMD5(salt + newPassword); user.PasswordHash = pwdHash; user.PasswordSalt = salt; ctx.SaveChanges(); } }
public void SetValue(string name, string value) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <SettingEntity> bs = new BaseService <SettingEntity>(ctx); var setting = bs.GetAll().SingleOrDefault(s => s.Name == name); if (setting == null)//没有,则新增 { ctx.Settings.Add(new SettingEntity { Name = name, Value = value }); } else { setting.Value = value; } ctx.SaveChanges(); } }
/// <summary> /// 更新角色 /// </summary> /// <param name="roleId"></param> /// <param name="roleName"></param> public void Update(long roleId, string roleName) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <RoleEntity> roleBS = new BaseService <RoleEntity>(ctx); bool exists = roleBS.GetAll().Any(r => r.Name == roleName && r.Id != roleId); //正常情况不应该执行这个异常,因为UI层应该把这些情况处理好 //这里只是“把好最后一关” if (exists) { throw new ArgumentException(""); } RoleEntity role = new RoleEntity(); role.Id = roleId; ctx.Entry(role).State = System.Data.Entity.EntityState.Unchanged; role.Name = roleName; ctx.SaveChanges(); } }
/// <summary> /// 为一个后台用户添加角色 /// </summary> /// <param name="adminUserId"></param> /// <param name="roleIds"></param> public void AddRoleIds(long adminUserId, long[] roleIds) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <AdminUserEntity> userBS = new BaseService <AdminUserEntity>(ctx); var user = userBS.GetById(adminUserId); if (user == null) { throw new ArgumentException("用户不存在" + adminUserId); } BaseService <RoleEntity> roleBS = new BaseService <RoleEntity>(ctx); var roles = roleBS.GetAll().Where(r => roleIds.Contains(r.Id)).ToArray(); foreach (var role in roles) { user.Roles.Add(role); } ctx.SaveChanges(); } }
/// <summary> /// 添加一个角色 /// </summary> /// <param name="roleName"></param> /// <returns></returns> public long AddNew(string roleName) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <RoleEntity> roleBS = new BaseService <RoleEntity>(ctx); bool exists = roleBS.GetAll().Any(r => r.Name == roleName); //正常情况不应该执行这个异常,因为UI层应该把这些情况处理好 //这里只是“把好最后一关” if (exists) { throw new ArgumentException("角色名字已经存在" + roleName); } RoleEntity role = new RoleEntity(); role.Name = roleName; ctx.Roles.Add(role); ctx.SaveChanges(); return(role.Id); } }
/// <summary> /// 删除房屋照片 /// </summary> /// <param name="housePicId"></param> public void DeleteHousePic(long housePicId) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { //复习EF状态转换 /* * HousePicEntity entity = new HousePicEntity(); * entity.Id = housePicId; * ctx.Entry(entity).State = EntityState.Deleted; * ctx.SaveChanges();*/ var entity = ctx.HousePics .SingleOrDefault(p => p.IsDeleted == false && p.Id == housePicId); if (entity != null) { ctx.HousePics.Remove(entity); ctx.SaveChanges(); } } }
public bool Follow(long adminUserId, long houseAppointmentId) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <HouseAppointmentEntity> bs = new BaseService <HouseAppointmentEntity>(ctx); var app = bs.GetById(houseAppointmentId); if (app == null) { throw new ArgumentException("不存在的订单id"); } //FollowAdminUserId不为null,说明要么是自己已经抢过,要么是已经早早的 //被别人抢了 if (app.FollowAdminUserId != null) { return(app.FollowAdminUserId == adminUserId); /* * if(app.FollowAdminUserId==adminUserId) * { * return true; * } * else * { * return false; * }*/ } //如果/FollowAdminUserId为null,说明有抢的机会 app.FollowAdminUserId = adminUserId; try { ctx.SaveChanges(); return(true); }//如果抛出DbUpdateConcurrencyException说明抢单失败(乐观锁) catch (DbUpdateConcurrencyException) { return(false); } } }
/// <summary> /// 为一个角色添加权限 /// </summary> /// <param name="roleId"></param> /// <param name="permIds"></param> public void AddPermIds(long roleId, long[] permIds) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <RoleEntity> roleBS = new BaseService <RoleEntity>(ctx); var role = roleBS.GetById(roleId); if (role == null) { throw new ArgumentException("roleId不存在" + roleId); } BaseService <PermissionEntity> permBS = new BaseService <PermissionEntity>(ctx); var perms = permBS.GetAll() .Where(p => permIds.Contains(p.Id)).ToArray(); foreach (var perm in perms) { role.Permissions.Add(perm); } ctx.SaveChanges(); } }
/// <summary> /// 添加一个城市 /// </summary> /// <param name="cityName"></param> /// <returns></returns> public long AddNew(string cityName) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <CityEntity> bs = new BaseService <CityEntity>(ctx); //判断是否存在任何一条数据满足 c.Name == cityName //即存在这样一个名字的城市 //如果只是判断“是否存在”,那么用Any效率比Where().count()效率高 //Where(c => c.Name == cityName).Count()>0 bool exists = bs.GetAll().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); } }
/// <summary> /// 新增房屋 /// </summary> /// <param name="house"></param> /// <returns></returns> public long AddNew(HouseAddNewDTO house) { HouseEntity houseEntity = new HouseEntity(); houseEntity.Address = house.Address; houseEntity.Area = house.Area; using (PalmRentDbContext ctx = new PalmRentDbContext()) { BaseService <AttachmentEntity> attBS = new BaseService <AttachmentEntity>(ctx); //拿到house.AttachmentIds为主键的房屋配套设施 var atts = attBS.GetAll().Where(a => house.AttachmentIds.Contains(a.Id)); //houseEntity.Attachments = new List<AttachmentEntity>(); 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); } }
/// <summary> /// 更新用户信息 /// </summary> /// <param name="id"></param> /// <param name="name"></param> /// <param name="phoneNum"></param> /// <param name="password"></param> /// <param name="email"></param> /// <param name="cityId"></param> public void UpdateAdminUser(long id, string name, string phoneNum, string password, string email, long?cityId) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { 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(); } }
/// <summary> /// 添加一个前台用户 /// </summary> /// <param name="phoneNum"></param> /// <param name="password"></param> /// <returns></returns> public long AddNew(string phoneNum, string password) { using (PalmRentDbContext ctx = new PalmRentDbContext()) { //检查手机号不能重复 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); } }