//新增令牌 public void AddToken(Token entity) { using (var db = new HouseMarketEntities()) { if (entity == null) { return; } else { var token = db.Tokens.SingleOrDefault(t => t.TokenID == entity.TokenID); var token2 = db.Tokens.SingleOrDefault(t => t.UserID == entity.UserID); if (token != null) { EditTokenByToken(entity); } else if (token2 != null) { DeleteToken(token2); db.Tokens.Add(entity); db.SaveChanges(); } else { db.Tokens.Add(entity); db.SaveChanges(); } } } }
//新增房屋 public int AddHouse(View_HouseAddress entity) { using (var db = new HouseMarketEntities()) { if (entity.AddressID != null || string.IsNullOrEmpty(entity.Road) || string.IsNullOrEmpty(entity.Neighborhood) || string.IsNullOrEmpty(entity.Area) || string.IsNullOrEmpty(entity.City)) { return (int)Errors.HouseAddressErrors.WrongParameter; } else { Address address = new Address(); PropertyFunction.CopyEntity(entity, address); db.Addresses.Add(address); db.SaveChanges(); House house = new House(); PropertyFunction.CopyEntity(entity, house); house.AddressID = db.Addresses.FirstOrDefault(a => a.Road == address.Road).AddressID; db.Houses.Add(house); db.SaveChanges(); return 0; } } }
//新增用户 public int AddUser(User entity) { using (var db = new HouseMarketEntities()) { if (!AuthenticationFunction.IsAuthenticated || !AuthenticationFunction.HasPermission(Permissions.SeniorUser)) { return (int)Errors.UserErrors.NoPermission; } else if (!AuthenticationFunction.IsAdmin && entity.Permission <= (int)AuthenticationFunction.GetPermission()) { return (int)Errors.UserErrors.NoPermission; } if (string.IsNullOrEmpty(entity.UserName) || string.IsNullOrEmpty(entity.Password) || entity.Permission == null) { return (int)Errors.UserErrors.WrongParameter; } else if (db.Users.Any(u => u.UserName == entity.UserName)) { return (int)Errors.UserErrors.NameExisted; } else { db.Users.Add(entity); db.SaveChanges(); return 0; } } }
//新增用户 public int AddUser(User entity) { using (var db = new HouseMarketEntities()) { if (db.Users.Any(u => u.UserName == entity.UserName)) { return (int)Errors.NameExisted; } else { db.Users.Add(entity); db.SaveChanges(); return 1; } } }
//删除客户 public int DeleteCustomer(Customer entity) { using (var db = new HouseMarketEntities()) { var customer = db.Customers.FirstOrDefault(c => c.CustomerID == entity.CustomerID); if (customer == null) { return (int)Errors.UserErrors.IDNotExisted; } else { db.Customers.Remove(customer); db.SaveChanges(); return 0; } } }
//删除房子 public int DeleteHouse(View_HouseAddress entity) { using (var db = new HouseMarketEntities()) { if (entity == null || entity.HouseID == 0) { return (int)Errors.HouseAddressErrors.NullParameter; } else { var house = db.Houses.FirstOrDefault(h => h.HouseID == entity.HouseID); var collections = db.Collections.Where(c => c.HouseID == entity.HouseID); if (house == null) { return (int)Errors.HouseAddressErrors.HouseIDNotExisted; } else { db.Houses.Remove(house); if (collections != null) { foreach (var v in collections) { db.Collections.Remove(v); } } if (house.AddressID != null) { int addressID = Convert.ToInt32(house.AddressID); var address = db.Addresses.FirstOrDefault(a => a.AddressID == addressID); if (address != null) { db.Addresses.Remove(address); } } db.SaveChanges(); return 0; } } } }
//新增收藏 public int AddCollection(Collection entity) { using (var db = new HouseMarketEntities()) { if (db.Collections.Any(c => c.CustomerID == entity.CustomerID && c.HouseID == entity.HouseID)) { return (int)Errors.CollectionErrors.RecordExisted; } else if (entity.HouseID == null || entity.CustomerID == null || entity.HouseID == 0 || entity.CustomerID == 0) { return (int)Errors.CollectionErrors.WrongParameter; } else { db.Collections.Add(entity); db.SaveChanges(); return 0; } } }
//新增小区价格趋势 public int AddNeighborhoodPriceTrend(NeighborhoodPriceTrend entity) { using (var db = new HouseMarketEntities()) { if (db.NeighborhoodPriceTrends.Any(c => c.Neighborhood == entity.Neighborhood && c.Date == entity.Date)) { return (int)Errors.PriceTrendErrors.RecordExisted; } else if (entity.ID != 0 || entity.Neighborhood == null || entity.NeighborhoodAvgPrice == null || entity.Date == null) { return (int)Errors.PriceTrendErrors.WrongParameter; } else { db.NeighborhoodPriceTrends.Add(entity); db.SaveChanges(); return 0; } } }
//新增客户 public int AddCustomer(Customer entity, out CustomerViewResult result) { result = new CustomerViewResult(); using (var db = new HouseMarketEntities()) { if (string.IsNullOrEmpty(entity.CustomerName) || string.IsNullOrEmpty(entity.Password)) { return (int)Errors.UserErrors.WrongParameter; } else if (db.Customers.Any(c => c.CustomerName == entity.CustomerName)) { return (int)Errors.UserErrors.NameExisted; } else { var entityResult = db.Customers.Add(entity); db.SaveChanges(); PropertyFunction.CopyEntity(entityResult, result); return 0; } } }
//删除令牌 public void DeleteToken(Token entity) { using (var db = new HouseMarketEntities()) { if (entity == null) { return; } else { var token = db.Tokens.SingleOrDefault(t => t.TokenID == entity.TokenID); if (token == null) { return; } else { db.Tokens.Remove(token); db.SaveChanges(); } } } }
//批量新增小区价格趋势 public int AddNeighborhoodPriceTrends(List<NeighborhoodPriceTrend> entities) { using (var db = new HouseMarketEntities()) { if (entities == null) { return (int)Errors.PriceTrendErrors.NullParameter; } else { foreach (var entity in entities) { int result = AddNeighborhoodPriceTrend(entity); if (result != 0) { return result; } } db.SaveChanges(); return 0; } } }
//删除收藏 public int DeleteCollection(Collection entity) { using (var db = new HouseMarketEntities()) { if (entity.CollectionID == 0) { return (int)Errors.CollectionErrors.NullParameter; } else { var collection = db.Collections.FirstOrDefault(c => c.CollectionID == entity.CollectionID); if (collection == null) { return (int)Errors.CollectionErrors.CollectionIDNotExisted; } else { db.Collections.Remove(collection); db.SaveChanges(); return 0; } } } }
//编辑小区价格趋势 public int EditNeighborhoodPriceTrend(NeighborhoodPriceTrend entity) { using (var db = new HouseMarketEntities()) { if (db.NeighborhoodPriceTrends.Any(c => c.Neighborhood == entity.Neighborhood && c.Date == entity.Date)) { return (int)Errors.PriceTrendErrors.RecordExisted; } else if (entity.ID == 0) { return (int)Errors.PriceTrendErrors.NullParameter; } else if (entity.Neighborhood == null || entity.NeighborhoodAvgPrice == null || entity.Date == null) { return (int)Errors.PriceTrendErrors.WrongParameter; } else { var neighborhoodPriceTrend = db.NeighborhoodPriceTrends.FirstOrDefault(c => c.ID == entity.ID); if (neighborhoodPriceTrend == null) { return (int)Errors.PriceTrendErrors.PriceTrendIDNotExisted; } else { PropertyFunction.CopyEntity(entity, neighborhoodPriceTrend); db.SaveChanges(); return 0; } } } }
//删除用户 public int DeleteUser(User entity) { using (var db = new HouseMarketEntities()) { var user = db.Users.FirstOrDefault(u => u.UserID == entity.UserID); if (!AuthenticationFunction.IsAuthenticated || !AuthenticationFunction.HasPermission(Permissions.SeniorUser)) { return (int)Errors.UserErrors.NoPermission; } else if (!AuthenticationFunction.IsAdmin && user.Permission <= (int)AuthenticationFunction.GetPermission()) { return (int)Errors.UserErrors.NoPermission; } if (user == null) { return (int)Errors.UserErrors.IDNotExisted; } else { db.Users.Remove(user); db.SaveChanges(); return 0; } } }
//批量编辑用户 public int EditUsers(List<User> entities) { using (var db = new HouseMarketEntities()) { if (entities == null) { return (int)Errors.UserErrors.NullParameter; } else { foreach (var v in entities) { var q = EditUser(v); if (q != 0) { return q; } } db.SaveChanges(); return 0; } } }
//编辑用户 public int EditUser(User entity) { using (var db = new HouseMarketEntities()) { var user = db.Users.FirstOrDefault(u => u.UserID == entity.UserID); if (user == null) { return (int)Errors.IDNotExisted; } else { if (db.Users.Any(u => u.UserName == entity.UserName)) { return (int)Errors.NameExisted; } else { user.UserName = entity.UserName; user.Password = entity.Password; db.SaveChanges(); return 1; } } } }
//编辑用户 public int EditUser(User entity) { using (var db = new HouseMarketEntities()) { var user = db.Users.FirstOrDefault(u => u.UserID == entity.UserID); if (!AuthenticationFunction.IsAuthenticated || !AuthenticationFunction.HasPermission(Permissions.User)) { return (int)Errors.UserErrors.NoPermission; } else if (!AuthenticationFunction.IsAdmin && AuthenticationFunction.CurrentUserID != entity.UserID && entity.Permission <= (int)AuthenticationFunction.GetPermission()) { return (int)Errors.UserErrors.NoPermission; } else if (!AuthenticationFunction.IsAdmin && AuthenticationFunction.CurrentUserID != entity.UserID && user.Permission <= (int)AuthenticationFunction.GetPermission()) { return (int)Errors.UserErrors.NoPermission; } else if (AuthenticationFunction.IsUser && Convert.ToInt32(HttpContext.Current.User.Identity.Name) != entity.UserID) { return (int)Errors.UserErrors.NoPermission; } if (user == null) { return (int)Errors.UserErrors.IDNotExisted; } else { if (string.IsNullOrEmpty(entity.Password)) { user.UserName = entity.UserName; user.Permission = entity.Permission; if (string.IsNullOrEmpty(user.UserName)) { return (int)Errors.UserErrors.WrongParameter; } if (db.Users.Count(u => u.UserName == user.UserName) > 1) { return (int)Errors.UserErrors.NameExisted; } } else { user.Password = entity.Password; } db.SaveChanges(); return 0; } } }
//使用用户ID编辑令牌 public void EditTokenByUser(Token entity) { using (var db = new HouseMarketEntities()) { if (entity == null) { return; } else { var token = db.Tokens.SingleOrDefault(t => t.UserID == entity.UserID); if (token == null) { return; } else { PropertyFunction.CopyEntity(entity, token); db.SaveChanges(); } } } }
//编辑房屋 public int EditHouse(View_HouseAddress entity) { using (var db = new HouseMarketEntities()) { if (entity.HouseID == 0) { return (int)Errors.HouseAddressErrors.NullParameter; } else { var house = db.Houses.FirstOrDefault(h => h.HouseID == entity.HouseID); if (house == null) { return (int)Errors.HouseAddressErrors.HouseIDNotExisted; } else { var address = db.Addresses.FirstOrDefault(a => a.AddressID == house.AddressID); var houseProperties = house.GetType().GetProperties(); var addressProperties = address.GetType().GetProperties(); var entityProperties = entity.GetType().GetProperties(); foreach (var houseProperty in houseProperties) { var entityProperty = entityProperties.FirstOrDefault(p => p.Name == houseProperty.Name); if (entityProperty.GetValue(entity) != null) { houseProperty.SetValue(house, entityProperty.GetValue(entity)); } } foreach (var addressProperty in addressProperties) { var entityProperty = entityProperties.FirstOrDefault(p => p.Name == addressProperty.Name); if (entityProperty.GetValue(entity) != null) { addressProperty.SetValue(address, entityProperty.GetValue(entity)); } } if (string.IsNullOrEmpty(address.Road)) { return (int)Errors.HouseAddressErrors.WrongParameter; } if (db.Houses.Count(h => h.AddressID == entity.AddressID) > 1) { return (int)Errors.HouseAddressErrors.AddressIDExisted; } else { db.SaveChanges(); return 0; } } } } }
//批量删除用户 public int RemoveUsers(List<User> entities) { using (var db = new HouseMarketEntities()) { if (entities.Count() == 0) { return (int)Errors.NullParameter; } else { foreach (var v in entities) { var q = RemoveUser(v); if (q != 1) { return q; } } db.SaveChanges(); return 1; } } }
//编辑客户 public int EditCustomer(Customer entity) { using (var db = new HouseMarketEntities()) { var customer = db.Customers.FirstOrDefault(c => c.CustomerID == entity.CustomerID); if (customer == null) { return (int)Errors.UserErrors.IDNotExisted; } else { if (string.IsNullOrEmpty(entity.Password)) { if (string.IsNullOrEmpty(entity.CustomerName)) { return (int)Errors.UserErrors.WrongParameter; } else { customer.CustomerName = entity.CustomerName; customer.Tele = entity.Tele; } } else { customer.Password = entity.Password; } if (db.Customers.Count(c=>c.CustomerName == customer.CustomerName) > 1) { return (int)Errors.UserErrors.NameExisted; } else { db.SaveChanges(); return 0; } } } }
//编辑收藏 public int EditCollection(Collection entity) { using (var db = new HouseMarketEntities()) { if (db.Collections.Any(c => c.CustomerID == entity.CustomerID && c.HouseID == entity.HouseID)) { return (int)Errors.CollectionErrors.RecordExisted; } else if (entity.CollectionID == 0) { return (int)Errors.CollectionErrors.NullParameter; } else if (entity.HouseID == 0 || entity.CustomerID == 0) { return (int)Errors.CollectionErrors.WrongParameter; } else { var collection = db.Collections.FirstOrDefault(c => c.CollectionID == entity.CollectionID); if (collection == null) { return (int)Errors.CollectionErrors.CollectionIDNotExisted; } else { PropertyFunction.CopyEntity(entity, collection); db.SaveChanges(); return 0; } } } }
//删除小区价格趋势 public int DeleteNeighborhoodPriceTrend(NeighborhoodPriceTrend entity) { using (var db = new HouseMarketEntities()) { if (entity.ID == 0) { return (int)Errors.PriceTrendErrors.NullParameter; } else { var neighborhoodPriceTrend = db.NeighborhoodPriceTrends.FirstOrDefault(c => c.ID == entity.ID); if (neighborhoodPriceTrend == null) { return (int)Errors.PriceTrendErrors.PriceTrendIDNotExisted; } else { db.NeighborhoodPriceTrends.Remove(neighborhoodPriceTrend); db.SaveChanges(); return 0; } } } }
//删除用户 public int RemoveUser(User entity) { using (var db = new HouseMarketEntities()) { var user = db.Users.FirstOrDefault(u => u.UserID == entity.UserID); if (user == null) { return (int)Errors.IDNotExisted; } else { db.Users.Remove(entity); db.SaveChanges(); return 1; } } }