/// <summary> /// 登录 /// </summary> /// <param name="db"></param> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public async static Task <LoginOrRegisterResult> LoginAsync(PublicBicyclesContext db, string username, string password) { //判断参数是否为空 if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) { return(new LoginOrRegisterResult() { Type = LoginOrRegisterResultType.Empty }); } //寻找用户名与密码都匹配的用户 User user = await db.Users .FirstOrDefaultAsync(p => p.Username == username && p.Password == CreateMD5(username + password)); if (user == null) { //返回用户名或密码错误 return(new LoginOrRegisterResult() { Type = LoginOrRegisterResultType.Wrong }); } db.Entry(user).State = EntityState.Modified; //修改并保存用户信息 await db.SaveChangesAsync(); return(new LoginOrRegisterResult() { User = user }); }
/// <summary> /// 新增租赁点 /// </summary> /// <param name="db"></param> /// <param name="name"></param> /// <param name="address"></param> /// <param name="lng"></param> /// <param name="lat"></param> /// <param name="count"></param> /// <returns></returns> public async static Task AddStationAsync(PublicBicyclesContext db, string name, string address, double lng, double lat, int count) { Station station = new Station() { Name = name, Address = address, Lng = lng, Lat = lat, Count = count }; db.Stations.Add(station); await db.SaveChangesAsync(); }
/// <summary> /// 删除租赁点 /// </summary> /// <param name="db"></param> /// <param name="stationID"></param> /// <returns></returns> public async static Task <bool> DeleteStationAsync(PublicBicyclesContext db, int stationID) { Station station = await db.Stations.FindAsync(stationID); if (station == null) { return(false); } station.Deleted = true; db.Update(station); await db.SaveChangesAsync(); return(true); }
public async static Task <bool> ModifyBicycleAsync(PublicBicyclesContext db, int id, int bicycleID, bool canHire) { Bicycle bicycle = await db.Bicycles.FindAsync(id); if (bicycle == null) { return(false); } bicycle.CanHire = canHire; bicycle.BicycleID = bicycleID; db.Bicycles.Update(bicycle); await db.SaveChangesAsync(); return(true); }
/// <summary> /// 删除自行车 /// </summary> /// <param name="db"></param> /// <param name="bicycleID"></param> /// <returns></returns> public async static Task <bool> DeleteBicycleAsync(PublicBicyclesContext db, int bicycleID) { Bicycle bicycle = await db.Bicycles.Where(p => p.ID == bicycleID).Include(p => p.Station).FirstOrDefaultAsync(); if (bicycle == null) { return(false); } bicycle.Deleted = true; db.Update(bicycle); bicycle.Station.BicycleCount--; db.Update(bicycle.Station); await db.SaveChangesAsync(); return(true); }
/// <summary> /// 还车 /// </summary> /// <param name="db"></param> /// <param name="userID"></param> /// <param name="bicycleID"></param> /// <param name="stationID"></param> /// <returns></returns> public static async Task <ReturnResult> ReturnAsync(PublicBicyclesContext db, int userID, int bicycleID, int stationID) { User user = db.Users.Find(userID); Station station = db.Stations.Find(stationID); Bicycle bicycle = db.Bicycles.Find(bicycleID); Hire hire = await GetHiringAsync(db, userID); //if (hire == null && Hires != null && Hires.Any(p => p.Hirer.ID == userID)) //{ // hire = Hires.First(p => p.Hirer.ID == userID); //} if (user == null || station == null || bicycle == null || hire == null) { return(new ReturnResult(null, ReturnResultType.DatabaseError)); } if (hire == null) { //找不到借车记录,联系客服 return(new ReturnResult(null, ReturnResultType.RecordNotFound)); } if (!station.CanReturn || !station.Online) { //表示租赁点不可还车或没有在线 return(new ReturnResult(hire, ReturnResultType.StationCannotReturn)); } if (station.BicycleCount >= station.Count) { //表示租赁点已满 return(new ReturnResult(hire, ReturnResultType.StationIsFull)); } hire.ReturnStation = station; hire.ReturnTime = Now(); db.Hires.Update(hire); //更新信息,包括车站自行车+1,设置自行车的被借状态,设置自行车的新租赁点为 station.BicycleCount++; db.Stations.Update(station); bicycle.Hiring = false; bicycle.Station = station; db.Update(bicycle); if (SaveChanges) { await db.SaveChangesAsync(); } return(new ReturnResult(hire, ReturnResultType.Succeed)); }
/// <summary> /// 新增自行车 /// </summary> /// <param name="db"></param> /// <param name="bicycleID"></param> /// <param name="stationID"></param> /// <returns></returns> public async static Task <bool> AddBicycleAsync(PublicBicyclesContext db, int bicycleID, int stationID) { var station = await db.Stations.FindAsync(stationID); if (station == null || station.BicycleCount >= station.Count)//无法停放更多车辆 { return(false); } Bicycle bicycle = new Bicycle() { BicycleID = bicycleID, Station = station }; db.Bicycles.Add(bicycle); station.BicycleCount++; db.Update(station); await db.SaveChangesAsync(); return(true); }
/// <summary> /// 修改租赁点信息 /// </summary> /// <param name="db"></param> /// <param name="id"></param> /// <param name="name"></param> /// <param name="address"></param> /// <param name="lng"></param> /// <param name="lat"></param> /// <param name="count"></param> /// <returns></returns> public async static Task <bool> ModifyStationAsync(PublicBicyclesContext db, int id, string name, string address, double lng, double lat, int count) { Station station = await db.Stations.FindAsync(id); if (station == null) { return(false); } if (count < station.BicycleCount)//租赁点新的容量小于当前车辆数 { return(false); } station.Name = name; station.Address = address; station.Lng = lng; station.Lat = lat; station.Count = count; db.Stations.Update(station); await db.SaveChangesAsync(); return(true); }
//internal static System.Collections.Generic.List<Hire> Hires { get; set; } public static async Task <HireResult> HireAsync(PublicBicyclesContext db, int userID, int bicycleID, int stationID) { User user = db.Users.Find(userID); Station station = db.Stations.Find(stationID); Bicycle bicycle = db.Bicycles.Find(bicycleID); if (user == null || station == null || bicycle == null) { return(new HireResult(null, HireResultType.DatabaseError)); } ///获取最后一条没有完成的借车记录 Hire hire = await db.Hires.LastOrDefaultRecordAsync(p => p.HireTime.Value, p => p.Hirer.ID == userID && p.ReturnStation == null); if (hire != null) { //如果发现用户还没有还车,那么不允许借车 return(new HireResult(null, HireResultType.AnotherIsHired)); } hire = new Hire() { HireStation = station, HireTime = Now(), Bicycle = bicycle, Hirer = user, }; db.Hires.Add(hire); station.BicycleCount--; db.Stations.Update(station); //更新信息,包括车站自行车-1,设置自行车的被借状态,设置自行车的租赁点为null bicycle.Hiring = true; bicycle.Station = null; db.Update(bicycle); if (SaveChanges) { await db.SaveChangesAsync(); } return(new HireResult(hire, HireResultType.Succeed)); }
/// <summary> /// 注册 /// </summary> /// <param name="db"></param> /// <param name="username"></param> /// <param name="password"></param> /// <returns>如果用户名已存在,返回null,否则返回注册后的用户对象</returns> public async static Task <LoginOrRegisterResult> RegistAsync(PublicBicyclesContext db, string username, string password) { if (await db.Users.AnyAsync(p => p.Username == username)) { return(new LoginOrRegisterResult() { Type = LoginOrRegisterResultType.Existed }); } User user = new User() { Username = username, Password = CreateMD5(username + password), }; db.Add(user); await db.SaveChangesAsync(); return(new LoginOrRegisterResult() { User = user }); }
/// <summary> /// 设置密码 /// </summary> /// <param name="db"></param> /// <param name="user">车主</param> /// <param name="password">新密码</param> /// <returns></returns> public async static Task SetPasswordAsync(PublicBicyclesContext db, User user, string password) { user.Password = CreateMD5(user.Username + password); db.Entry(user).State = EntityState.Modified; await db.SaveChangesAsync(); }