/// <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="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);
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 5
0
        //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));
        }