public static int CountUserByQuery(UserQuery query) { using (var context = new XiaoluEntities()) { var repository = new UserRepository(context); int count = repository.GetPageCount(item => _isMatch(item, query)); return count; } }
// GET api/users //跨域请求精确控制 //[EnableCors(origins: "*", headers: "*", methods: "*")] public Object Get(UserQuery query) { string msg; try { if (query == null) query = new UserQuery(); List<User> dbList = BusinessService.GetUserListByQuery(query); List<UserApiModel> vmList = UserApiModel.FromUserList(dbList); msg = string.Format(XiaoluResources.MSG_SINGLE_ACTION_SUCCESS, "获取", "用户列表"); return new { IsSuccess = true, Message = msg, Obj = vmList }; } catch (Exception e) { msg = string.Format(XiaoluResources.MSG_SINGLE_ACTION_FAIL, "获取", "用户列表") + string.Format(XiaoluResources.STR_FAIL_RESAON, ExceptionHelper.GetInnerExceptionInfo(e)); return new { IsSuccess = false, Message = msg }; } }
private static dynamic _orderByKey(User obj, UserQuery query) { if (string.IsNullOrEmpty(query.OrderByKey)) return obj.Id; return obj.GetType().GetProperty(query.OrderByKey).GetValue(obj); }
public static bool _isMatch(User obj, UserQuery query) { if (!string.IsNullOrEmpty(query.IdEqual) && !string.Equals(obj.Id, query.IdEqual)) return false; if (!string.IsNullOrEmpty(query.IdNotEqual) && string.Equals(obj.Id, query.IdNotEqual)) return false; if (!string.IsNullOrEmpty(query.NameEqual) && !string.Equals(obj.Name, query.NameEqual)) return false; if (!string.IsNullOrEmpty(query.NameNotEqual) && string.Equals(obj.Name, query.NameNotEqual)) return false; if (!string.IsNullOrEmpty(query.NameLike) && !obj.Name.Contains(query.NameLike)) return false; if (!string.IsNullOrEmpty(query.StatusEqual) && !string.Equals(obj.Status, query.StatusEqual)) return false; if (!string.IsNullOrEmpty(query.StatusNotEqual) && string.Equals(obj.Status, query.StatusNotEqual)) return false; if ((query.StatusIn != null) && (query.StatusIn.Select(item => string.Equals(item, obj.Status)) == null)) return false; if ((query.StatusNotIn != null) && (query.StatusNotIn.Select(item => string.Equals(item, obj.Status)) != null)) return false; if (!string.IsNullOrEmpty(query.MobileEqual) && !string.Equals(obj.Mobile, query.MobileEqual)) return false; return true; }
public static List<User> GetUserListByQuery(UserQuery query) { using (var context = new XiaoluEntities()) { var repository = new UserRepository(context); List<User> users = repository.GetPageList(item => _isMatch(item, query), item => _orderByKey(item, query), query.OrderByValue, query.Offset, query.Limit); return users; } }
public static User GetUserByName(string name4query) { UserQuery query = new UserQuery() { NameEqual = name4query }; User userInDb = GetUserListByQuery(query).FirstOrDefault(); return userInDb; }
public static User GetUserByMobile(string mobile4query) { UserQuery query = new UserQuery() { MobileEqual = mobile4query }; User userInDb = GetUserListByQuery(query).FirstOrDefault(); return userInDb; }
public static User GetUserById(string id4query) { UserQuery query = new UserQuery() { IdEqual = id4query }; User userInDb = GetUserListByQuery(query).FirstOrDefault(); return userInDb; }