///<exception cref="Exception">出闸时抛出异常</exception> public static void Out(string cardId, string endStation) { try { lock (CardBalanceLocker) { decimal?balance = GetCardBalance(cardId, out Card card); if (card.CardStatus == CardStatusEnum.IN) { if (!MetroDataBuilderOperation.StationSet.Contains(new Station(endStation))) { throw new ROException("终点站不存在"); } card.EndStation = endStation; Dictionary <string, int> subWay = SubWayOperation.GetSubway(card.StartStation, card.EndStation); int stations = subWay.First().Value; decimal subBalance = Calculate(card, stations); balance -= subBalance; if (balance < 0) { throw new ROException("余额不足,无法出站,请先充值后出站"); } else { card.CardStatus = CardStatusEnum.OUT; card.CardBalance = balance.ToString(); MongoOperation.ReplaceDocument(MongoCollectionName.Cards, new Dictionary <string, string> { { "CardId", card.CardId } }, card); } } else if (card.CardStatus == CardStatusEnum.OUT) { balance -= 5m; if (balance < 0) { throw new ROException("余额不足,无法出站,请先充值后出站"); } else { card.CardBalance = balance.ToString(); MongoOperation.ReplaceDocument(MongoCollectionName.Cards, new Dictionary <string, string> { { "CardId", card.CardId } }, card); } } } } catch (Exception e) { Log.Error("出站失败", e); throw e; } }
static SubWayOperation() { foreach (Station start in MetroDataBuilderOperation.StationSet) { List <List <Station> > list = new List <List <Station> >(); foreach (Station end in MetroDataBuilderOperation.StationSet) { if (start.Equals(end)) { continue; } SubWayOperation sw = new SubWayOperation(); sw.Calculate(start, end); List <Station> tempList = new List <Station>(); tempList.AddRange(start.GetAllPassedStations(end)); for (int i = 0; i < tempList.Count; i++) { Station station = null; foreach (Station s in MetroDataBuilderOperation.StationSet) { if (tempList[i].Name.Equals(s.Name)) { station = s; break; } } tempList[i] = station; } list.Add(tempList); } SubWayDict.Add(start, list); } }