///<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; } }
///<exception cref="Exception">入闸时抛出异常</exception> public static void In(string cardId, string startStation) { try { lock (CardBalanceLocker) { decimal?balance = GetCardBalance(cardId, out Card card); if (card.CardStatus == CardStatusEnum.OUT) { if (!MetroDataBuilderOperation.StationSet.Contains(new Station(startStation))) { throw new ROException("起始站不存在"); } if (balance < 1) { throw new ROException("卡内余额不足,无法入站"); } card.CardBalance = balance.ToString(); card.CardStatus = CardStatusEnum.IN; card.StartStation = startStation; MongoOperation.ReplaceDocument(MongoCollectionName.Cards, new Dictionary <string, string> { { "CardId", card.CardId } }, card); } else if (card.CardStatus == CardStatusEnum.IN) { balance -= 5m; if (balance < 1) { throw new ROException("卡内余额不足,无法入站"); } card.CardBalance = balance.ToString(); card.StartStation = startStation; MongoOperation.ReplaceDocument(MongoCollectionName.Cards, new Dictionary <string, string> { { "CardId", card.CardId } }, card); } } } catch (Exception e) { Log.Error("入站失败", e); throw e; } }