public void AddPermIds(long roleId, long[] permIds) { using (RhDbContext ctx = new RhDbContext()) { CommonService <RoleEntity> roleCs = new CommonService <RoleEntity>(ctx); RoleEntity role = roleCs.GetById(roleId); if (role == null) { throw new ArgumentException("roleId不存在 " + roleId); } CommonService <PermissionEntity> permCs = new CommonService <PermissionEntity>(ctx); var perms = permCs.GetAll().Where(a => permIds.Contains(a.Id)).ToArray(); foreach (var perm in perms) { role.Permissions.Add(perm); } ctx.SaveChanges(); } }
public void UpdateRoleIds(long adminUserId, long[] roleIds) { using (RhDbContext ctx = new RhDbContext()) { CommonService <AdminUserEntity> adminUserCs = new CommonService <AdminUserEntity>(ctx); var adminUser = adminUserCs.GetById(adminUserId); if (adminUser == null) { throw new ArgumentException("管理员用户Id不存在 " + adminUserId); } CommonService <RoleEntity> roleCs = new CommonService <RoleEntity>(ctx); var roles = roleCs.GetAll().Where(a => roleIds.Contains(a.Id)).ToList().ToArray(); adminUser.Roles.Clear(); foreach (var role in roles) { adminUser.Roles.Add(role); } ctx.SaveChanges(); } }
//新增或更改配置信息 public void SetValue(string name, string value) { using (RhDbContext ctx = new RhDbContext()) { CommonService <SettingEntity> cs = new CommonService <SettingEntity>(ctx); var settingEntity = cs.GetAll().SingleOrDefault(a => a.Name == name); if (settingEntity == null) //没有则新增 { ctx.Settings.Add(new SettingEntity() { Name = name, Value = value }); } else { settingEntity.Value = value; } ctx.SaveChanges(); } }
public long AddNew(string cityName) { using (RhDbContext ctx = new RhDbContext()) { CommonService <CityEntity> baseService = new CommonService <CityEntity>(ctx); //判断是否存在任何一条数据满足 c.Name == cityName //即存在这样一个名字的城市 //如果只是判断“是否存在”,那么用Any效率比Where().count()效率 if (baseService.GetAll().Any(c => c.Name == cityName)) { throw new ArgumentException("城市已存在"); } CityEntity city = new CityEntity { Name = cityName }; ctx.Cities.Add(city); ctx.SaveChanges(); return(city.Id); } }
public long AddNew(string phoneNum, string password) { using (RhDbContext ctx = new RhDbContext()) { //检查手机号不能重复 CommonService <UserEntity> cs = new CommonService <UserEntity>(ctx); bool exists = cs.GetAll().Any(a => a.PhoneNum == phoneNum); if (exists) { throw new ArgumentException("手机号已存在"); } string salt = CommonHelper.GenerateCaptchaCode(5); string pwdHash = CommonHelper.CalcMd5(salt + password); UserEntity user = new UserEntity(); user.PhoneNum = phoneNum; user.PasswordSalt = salt; user.PasswordHash = pwdHash; ctx.Users.Add(user); ctx.SaveChanges(); return(user.Id); } }
//检查用户名密码是否正确 public bool CheckLogin(string phoneNum, string password) { using (RhDbContext ctx = new RhDbContext()) { CommonService <AdminUserEntity> commonService = new CommonService <AdminUserEntity>(ctx); AdminUserEntity adminUser = commonService.GetAll().SingleOrDefault(a => a.PhoneNum == phoneNum); //手机号不存在 if (adminUser == null) { return(false); } //密码盐来转换密码 if (adminUser.PasswordHash == CommonHelper.CalcMd5(password + adminUser.PasswordSalt)) { return(true); } else { return(false); } } }
public HouseSearchResult Search(HouseSearchOptions options) { using (RhDbContext ctx = new RhDbContext()) { CommonService <HouseEntity> cs = new CommonService <HouseEntity>(ctx); //判断所有的searchoptions var items = cs.GetAll().Where(t => t.Community.Region.CityId == options.CityId); items = items.Where(t => t.TypeId == options.TypeId); if (options.RegionId != null) { items = items.Where(t => t.Community.RegionId == options.RegionId); } if (options.StartMonthRent != null) { items = items.Where(t => t.MonthRent >= options.StartMonthRent); } if (options.EndMonthRent != null) { items = items.Where(t => t.MonthRent <= options.EndMonthRent); } if (!string.IsNullOrEmpty(options.Keywords)) { items = items.Where(t => t.Address.Contains(options.Keywords) || t.Description.Contains(options.Keywords) || t.Community.Name.Contains(options.Keywords) || t.Community.Location.Contains(options.Keywords) || t.Community.Traffic.Contains(options.Keywords)); } //使用include连接查询,是立即把外键表都查询出来,正常是用到外键表才去查的,但是这里已知肯定要用到外键表,所以用include,避免延迟加载,不然查询的时候一直去查外键表肯定不好 items = items.Include(h => h.Attachments).Include(h => h.Community) .Include(h => h.Community.Region).Include(h => h.Community.Region.City).Include(h => h.Type); //搜索结果总条数 long totalCount = items.LongCount(); switch (options.OrderByType) { case HouseSearchOrderByType.AreaAsc: items = items.OrderBy(t => t.Area); break; case HouseSearchOrderByType.AreaDesc: items = items.OrderByDescending(t => t.Area); break; case HouseSearchOrderByType.MonthRentAsc: items = items.OrderBy(t => t.MonthRent); break; case HouseSearchOrderByType.MonthRentDesc: items = items.OrderByDescending(t => t.MonthRent); break; case HouseSearchOrderByType.CreateDateDesc: items = items.OrderBy(t => t.CreateDateTime); break; } //一定不要items.Where //而要items=items.Where(); //OrderBy要在Skip和Take之前 //给用户看的页码从1开始,程序中是从0开始 items = items.Skip((options.CurrentIndex - 1) * options.PageSize).Take(options.PageSize); HouseSearchResult searchResult = new HouseSearchResult(); searchResult.totalCount = totalCount; HouseEntity[] lastItems = items.ToArray(); List <HouseDTO> houses = new List <HouseDTO>(); foreach (var houseEntity in lastItems) { houses.Add(Entity2DTO(houseEntity)); } searchResult.result = houses.ToArray(); return(searchResult); } }