using System.Collections.Generic; using System.Linq; using PaySharp.Core.Exceptions; namespace PaySharp.Core { /// /// 网关集合类 /// public class Gateways : IGateways { #region 私有字段 private readonly ICollection _list; #endregion #region 属性 /// /// 网关数量 /// public int Count => _list.Count; #endregion #region 构造函数 /// /// 构造函数 /// public Gateways() { _list = new List(); } #endregion #region 方法 /// /// 添加网关 /// /// 网关 /// public bool Add(BaseGateway gateway) { if (gateway != null) { if (!Exist(gateway.Merchant.AppId)) { _list.Add(gateway); return true; } else { throw new GatewayException("该商户数据已存在"); } } return false; } /// /// 获取指定网关 /// /// 网关类型 /// public BaseGateway Get() { var gatewayList = _list .Where(a => a is T) .ToList(); return gatewayList.Count > 0 ? gatewayList[0] : throw new GatewayException("找不到指定网关"); } /// /// 通过AppId获取网关 /// /// 网关类型 /// AppId /// public BaseGateway Get(string appId) { var gatewayList = _list .Where(a => a is T && a.Merchant.AppId == appId) .ToList(); var gateway = gatewayList.Count > 0 ? gatewayList[0] : throw new GatewayException("找不到指定网关"); return gateway; } /// /// 指定AppId是否存在 /// /// appId /// private bool Exist(string appId) => _list.Any(a => a.Merchant.AppId == appId); /// /// 获取网关列表 /// /// public ICollection GetList() { return _list; } #endregion } }