//新增用户 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 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 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 CheckUser(User entity, out UserViewResult result) { using (var db = new HouseMarketEntities()) { result = new UserViewResult(); if (string.IsNullOrEmpty(entity.UserName)) { return (int)Errors.UserErrors.NullParameter; } else { var user = db.Users.FirstOrDefault(u => u.UserName == entity.UserName); if (user == null) { return (int)Errors.UserErrors.NameNotExisted; } else { if (user.Password != entity.Password) { return (int)Errors.UserErrors.WrongPassword; } else { PropertyFunction.CopyEntity(user, result); 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 CheckCustomer(Customer entity, out CustomerViewResult result) { result = new CustomerViewResult(); using (var db = new HouseMarketEntities()) { var customer = db.Customers.FirstOrDefault(c => c.CustomerName == entity.CustomerName); if (customer == null) { result = null; return (int)Errors.UserErrors.NameNotExisted; } else { if (customer.Password != entity.Password) { result = null; return (int)Errors.UserErrors.WrongPassword; } else { PropertyFunction.CopyEntity(customer, result); return 0; } } } }
//新增房屋 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 QueryHousesCustomerInfo(HouseCustomerInfoQueryConditions queryConditions, out List<View_House_CustomerInfo> result, out int totalPage) { using (var db = new HouseMarketEntities()) { totalPage = 0; var entities = db.View_House_CustomerInfo; var query = SetQuery(entities, queryConditions, out totalPage); result = query.ToList(); return 0; } }
//查询小区当前均价 public int QueryNeighborhoodAvgPrice(NeighborhoodAvgPriceQueryConditions queryConditions, out List<View_NeighborhoodAvgPrice> result, out int totalPage) { using (var db = new HouseMarketEntities()) { totalPage = 0; var entities = db.View_NeighborhoodAvgPrice; var query = SetQuery(entities, queryConditions, out totalPage); result = query.ToList(); 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 string CheckUser(User entity) { using (var db = new HouseMarketEntities()) { var user = db.Users.FirstOrDefault(u => u.UserName == entity.UserName); if (user == null) { return ("2"); } else { if (user.Password != entity.Password) { return ("3"); } else { return ("1"); } } } }
//删除令牌 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(); } } } }
//使用令牌ID编辑令牌 public void EditTokenByToken(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 { PropertyFunction.CopyEntity(entity, token); db.SaveChanges(); } } } }
//新增客户 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 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 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; } } }
//编辑房屋 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 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 QueryNeighborhoodPriceTrend(NeighborhoodPriceTrendQueryConditions queryConditions, out List<NeighborhoodPriceTrend> result, out int totalPage) { using (var db = new HouseMarketEntities()) { totalPage = 0; var entities = db.NeighborhoodPriceTrends; var query = SetQuery(entities, queryConditions, out totalPage); result = query.ToList(); 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 QueryUsers(UserQueryConditions queryConditions, out List<UserViewResult> result, out int totalPage) { result = new List<UserViewResult>(); using (var db = new HouseMarketEntities()) { int currentPermission = (int)AuthenticationFunction.GetPermission(); int totalCount = 0; totalPage = 0; var queryProperties = queryConditions.GetType().GetProperties(); string orderByName = queryConditions.orderBy; string orderByDescendingName = queryConditions.orderByDescending; if (!AuthenticationFunction.IsAuthenticated || !AuthenticationFunction.HasPermission(Permissions.User)) { return (int)Errors.UserErrors.NoPermission; } else if (AuthenticationFunction.IsUser && AuthenticationFunction.CurrentUserID != queryConditions.UserID) { return (int)Errors.UserErrors.NoPermission; } var query = db.Users.AsQueryable(); query = SetQueryOnly(query, queryConditions, out totalCount); int removeCount = query.Count(u => u.Permission < currentPermission); totalCount -= removeCount; query = query.Where(u => u.Permission >= currentPermission); if (!string.IsNullOrEmpty(orderByName)) { var orderPropertyName = queryProperties.FirstOrDefault(p => p.Name.ToLower() == orderByName.ToLower()).Name; var orderPropertyType = queryProperties.FirstOrDefault(p => p.Name == orderPropertyName).PropertyType; query = SetOrder(query, orderPropertyName, orderPropertyType, false); } else if (!string.IsNullOrEmpty(orderByDescendingName)) { var orderPropertyName = queryProperties.FirstOrDefault(p => p.Name.ToLower() == orderByDescendingName.ToLower()).Name; var orderPropertyType = queryProperties.FirstOrDefault(p => p.Name == orderPropertyName).PropertyType; query = SetOrder(query, orderPropertyName, orderPropertyType, true); } if (!string.IsNullOrEmpty(orderByName) || !string.IsNullOrEmpty(orderByDescendingName)) { int? pageIndex = (int?)queryProperties.FirstOrDefault(p => p.Name == "pageIndex").GetValue(queryConditions); int? pageSize = (int?)queryProperties.FirstOrDefault(p => p.Name == "pageSize").GetValue(queryConditions); query = SetPagination(query, pageIndex, pageSize, out totalPage); totalPage = (totalCount + queryConditions.pageSize.Value - 1) / queryConditions.pageSize.Value; } List<User> entityResult = query.ToList(); PropertyFunction.CopyEntities(entityResult, result); 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; } } } }
//使用ID查询客户 public int QueryCustomers(CustomerQueryConditions queryConditions, out List<CustomerViewResult> result, out int totalPage) { result = new List<CustomerViewResult>(); using (var db = new HouseMarketEntities()) { totalPage = 0; var entities = db.Customers; var query = SetQuery(entities, queryConditions, out totalPage); List<Customer> entityResult = query.ToList(); PropertyFunction.CopyEntities(entityResult, result); return 0; } }