feat: 完成尊享服务
This commit is contained in:
@@ -11,7 +11,7 @@ public class PriceAttribute : Attribute
|
||||
{
|
||||
public decimal Price { get; }
|
||||
public int ValidMonths { get; }
|
||||
|
||||
|
||||
public PriceAttribute(double price, int validMonths)
|
||||
{
|
||||
Price = (decimal)price;
|
||||
@@ -26,37 +26,114 @@ public class PriceAttribute : Attribute
|
||||
public class DisplayNameAttribute : Attribute
|
||||
{
|
||||
public string DisplayName { get; }
|
||||
|
||||
|
||||
public DisplayNameAttribute(string displayName)
|
||||
{
|
||||
DisplayName = displayName;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 商品类型特性
|
||||
/// 用于标识商品是VIP服务还是尊享包服务
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class GoodsCategoryAttribute : Attribute
|
||||
{
|
||||
public GoodsCategoryType Category { get; }
|
||||
|
||||
public GoodsCategoryAttribute(GoodsCategoryType category)
|
||||
{
|
||||
Category = category;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 商品类别类型
|
||||
/// </summary>
|
||||
public enum GoodsCategoryType
|
||||
{
|
||||
/// <summary>
|
||||
/// VIP服务
|
||||
/// </summary>
|
||||
VipService = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 尊享包服务
|
||||
/// </summary>
|
||||
PremiumPackage = 2
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Token数量特性
|
||||
/// 用于标识尊享包的token数量
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class TokenAmountAttribute : Attribute
|
||||
{
|
||||
public long TokenAmount { get; }
|
||||
|
||||
public TokenAmountAttribute(long tokenAmount)
|
||||
{
|
||||
TokenAmount = tokenAmount;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 商品枚举
|
||||
/// </summary>
|
||||
public enum GoodsTypeEnum
|
||||
{
|
||||
// VIP服务
|
||||
[Price(29.9, 1)]
|
||||
[DisplayName("YiXinVip 1 month")]
|
||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
||||
YiXinVip1 = 1,
|
||||
|
||||
|
||||
[Price(83.7, 3)]
|
||||
[DisplayName("YiXinVip 3 month")]
|
||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
||||
YiXinVip3 = 3,
|
||||
|
||||
|
||||
[Price(155.4, 6)]
|
||||
[DisplayName("YiXinVip 6 month")]
|
||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
||||
YiXinVip6 = 6,
|
||||
|
||||
|
||||
[Price(183.2, 8)]
|
||||
[DisplayName("YiXinVip 8 month")]
|
||||
YiXinVip8 = 8
|
||||
[GoodsCategory(GoodsCategoryType.VipService)]
|
||||
YiXinVip8 = 8,
|
||||
|
||||
// 尊享包服务 - 需要VIP资格才能购买
|
||||
[Price(49.9, 0)]
|
||||
[DisplayName("Premium Package 5M Tokens")]
|
||||
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
||||
[TokenAmount(5_000_000)]
|
||||
PremiumPackage5M = 101,
|
||||
|
||||
[Price(99.9, 0)]
|
||||
[DisplayName("Premium Package 10M Tokens")]
|
||||
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
||||
[TokenAmount(10_000_000)]
|
||||
PremiumPackage10M = 102,
|
||||
|
||||
[Price(199.9, 0)]
|
||||
[DisplayName("Premium Package 25M Tokens")]
|
||||
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
||||
[TokenAmount(25_000_000)]
|
||||
PremiumPackage25M = 103,
|
||||
|
||||
[Price(399.9, 0)]
|
||||
[DisplayName("Premium Package 50M Tokens")]
|
||||
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
|
||||
[TokenAmount(50_000_000)]
|
||||
PremiumPackage50M = 104
|
||||
|
||||
// [Price(197.1, 9)]
|
||||
// [DisplayName("YiXinVip 9 month")]
|
||||
// YiXinVip9 = 9
|
||||
|
||||
|
||||
// [Price(0.01, 1)]
|
||||
// [DisplayName("YiXinVip Test")]
|
||||
// YiXinVipTest = 0,
|
||||
@@ -91,7 +168,7 @@ public static class GoodsTypeEnumExtensions
|
||||
var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
|
||||
return priceAttribute?.Price ?? 0m;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取商品价格描述
|
||||
/// </summary>
|
||||
@@ -102,7 +179,7 @@ public static class GoodsTypeEnumExtensions
|
||||
var price = goodsType.GetTotalAmount();
|
||||
return $"¥{price:F1}";
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取商品名称
|
||||
/// </summary>
|
||||
@@ -114,7 +191,7 @@ public static class GoodsTypeEnumExtensions
|
||||
var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
|
||||
return displayNameAttribute?.DisplayName ?? goodsType.ToString();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取商品有效月份
|
||||
/// </summary>
|
||||
@@ -126,7 +203,7 @@ public static class GoodsTypeEnumExtensions
|
||||
var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
|
||||
return priceAttribute?.ValidMonths ?? 1;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取商品月均价格
|
||||
/// </summary>
|
||||
@@ -138,4 +215,48 @@ public static class GoodsTypeEnumExtensions
|
||||
var validMonths = goodsType.GetValidMonths();
|
||||
return validMonths > 0 ? totalPrice / validMonths : 0m;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取商品类别
|
||||
/// </summary>
|
||||
/// <param name="goodsType">商品类型</param>
|
||||
/// <returns>商品类别</returns>
|
||||
public static GoodsCategoryType GetGoodsCategory(this GoodsTypeEnum goodsType)
|
||||
{
|
||||
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
|
||||
var categoryAttribute = fieldInfo?.GetCustomAttribute<GoodsCategoryAttribute>();
|
||||
return categoryAttribute?.Category ?? GoodsCategoryType.VipService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为尊享包商品
|
||||
/// </summary>
|
||||
/// <param name="goodsType">商品类型</param>
|
||||
/// <returns>是否为尊享包</returns>
|
||||
public static bool IsPremiumPackage(this GoodsTypeEnum goodsType)
|
||||
{
|
||||
return goodsType.GetGoodsCategory() == GoodsCategoryType.PremiumPackage;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否为VIP服务商品
|
||||
/// </summary>
|
||||
/// <param name="goodsType">商品类型</param>
|
||||
/// <returns>是否为VIP服务</returns>
|
||||
public static bool IsVipService(this GoodsTypeEnum goodsType)
|
||||
{
|
||||
return goodsType.GetGoodsCategory() == GoodsCategoryType.VipService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取尊享包Token数量
|
||||
/// </summary>
|
||||
/// <param name="goodsType">商品类型</param>
|
||||
/// <returns>Token数量</returns>
|
||||
public static long GetTokenAmount(this GoodsTypeEnum goodsType)
|
||||
{
|
||||
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
|
||||
var tokenAttribute = fieldInfo?.GetCustomAttribute<TokenAmountAttribute>();
|
||||
return tokenAttribute?.TokenAmount ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user