feat: 商品枚举与支付服务优化,支持中文名称、参考价格及类别筛选

This commit is contained in:
ccnetcore
2025-10-12 21:04:08 +08:00
parent 9bcdaf6bd8
commit d32906702a
5 changed files with 125 additions and 65 deletions

View File

@@ -0,0 +1,16 @@
using Yi.Framework.AiHub.Domain.Shared.Enums;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos.Pay;
/// <summary>
/// 获取商品列表输入DTO
/// </summary>
public class GetGoodsListInput
{
/// <summary>
/// 商品类别(可选)
/// 如果不传,则返回所有商品
/// 如果传了则只返回指定类别的商品VIP服务或尊享包
/// </summary>
public GoodsCategoryType? GoodsCategoryType { get; set; }
}

View File

@@ -17,25 +17,32 @@ public class GoodsListOutput
/// </summary>
public decimal OriginalPrice { get; set; }
/// <summary>
/// 商品参考价格
/// </summary>
public decimal ReferencePrice { get; set; }
/// <summary>
/// 商品实际价格(折扣后的价格)
/// </summary>
public decimal GoodsPrice { get; set; }
/// <summary>
/// 商品类型
/// 折扣金额(仅尊享包)
/// </summary>
public GoodsTypeEnum GoodsType { get; set; }
public decimal? DiscountAmount { get; set; }
/// <summary>
/// 商品类别
/// </summary>
public string GoodsCategory { get; set; }
/// <summary>
/// 商品备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 折扣金额(仅尊享包)
/// </summary>
public decimal? DiscountAmount { get; set; }
/// <summary>
/// 折扣说明(仅尊享包)

View File

@@ -34,6 +34,7 @@ public interface IPayService : IApplicationService
/// <summary>
/// 获取商品列表
/// </summary>
/// <param name="input">获取商品列表输入</param>
/// <returns>商品列表</returns>
Task<List<GoodsListOutput>> GetGoodsListAsync();
Task<List<GoodsListOutput>> GetGoodsListAsync([FromQuery] GetGoodsListInput input);
}

View File

@@ -189,9 +189,10 @@ public class PayService : ApplicationService, IPayService
/// <summary>
/// 获取商品列表
/// </summary>
/// <param name="input">获取商品列表输入</param>
/// <returns>商品列表</returns>
[HttpGet("pay/GoodsList")]
public async Task<List<GoodsListOutput>> GetGoodsListAsync()
public async Task<List<GoodsListOutput>> GetGoodsListAsync([FromQuery] GetGoodsListInput input)
{
var goodsList = new List<GoodsListOutput>();
@@ -205,34 +206,46 @@ public class PayService : ApplicationService, IPayService
// 遍历所有商品枚举
foreach (GoodsTypeEnum goodsType in Enum.GetValues(typeof(GoodsTypeEnum)))
{
// 如果指定了商品类别,则过滤
if (input.GoodsCategoryType.HasValue)
{
var goodsCategory = goodsType.GetGoodsCategory();
if (goodsCategory != input.GoodsCategoryType.Value)
{
continue; // 跳过不匹配的商品
}
}
var originalPrice = goodsType.GetTotalAmount();
decimal actualPrice = originalPrice;
decimal? discountAmount = null;
string? discountDescription = null;
// 如果是尊享包商品,计算折扣
if (goodsType.IsPremiumPackage() && CurrentUser.IsAuthenticated)
if (goodsType.IsPremiumPackage())
{
discountAmount = goodsType.CalculateDiscount(totalRechargeAmount);
actualPrice = goodsType.GetDiscountedPrice(totalRechargeAmount);
discountDescription = "累计充值每10元可减2.5元最多减50元";
if ( CurrentUser.IsAuthenticated)
{
discountAmount = goodsType.CalculateDiscount(totalRechargeAmount);
actualPrice = goodsType.GetDiscountedPrice(totalRechargeAmount);
if (discountAmount > 0)
{
discountDescription = $"已优惠 ¥{discountAmount:F2}累计充值每10元减2.5元最多减50元";
}
else
{
discountDescription = "累计充值每10元可减2.5元最多减50元";
if (discountAmount > 0)
{
discountDescription = $"根据累积充值已优惠 ¥{discountAmount:F2}";
}
}
}
var goodsItem = new GoodsListOutput
{
GoodsName = goodsType.GetDisplayName(),
GoodsName = goodsType.GetChineseName(),
OriginalPrice = originalPrice,
ReferencePrice = goodsType.GetReferencePrice(),
GoodsPrice = actualPrice,
GoodsType = goodsType,
Remark = GetGoodsRemark(goodsType),
GoodsCategory = goodsType.GetGoodsCategory().ToString(),
Remark = goodsType.GetRemark(),
DiscountAmount = discountAmount,
DiscountDescription = discountDescription
};
@@ -242,28 +255,7 @@ public class PayService : ApplicationService, IPayService
return goodsList;
}
/// <summary>
/// 获取商品备注信息
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>商品备注</returns>
private string GetGoodsRemark(GoodsTypeEnum goodsType)
{
if (goodsType.IsPremiumPackage())
{
var tokenAmount = goodsType.GetTokenAmount();
return $"尊享包服务,提供 {tokenAmount:N0} Tokens需要VIP资格";
}
else if (goodsType.IsVipService())
{
var validMonths = goodsType.GetValidMonths();
var monthlyPrice = goodsType.GetMonthlyPrice();
return $"VIP服务有效期 {validMonths} 个月,月均价 ¥{monthlyPrice:F2}";
}
return "未知商品类型";
}
/// <summary>
/// 获取交易状态描述

View File

@@ -10,12 +10,16 @@ namespace Yi.Framework.AiHub.Domain.Shared.Enums;
public class PriceAttribute : Attribute
{
public decimal Price { get; }
public decimal ReferencePrice { get; }
public int ValidMonths { get; }
public PriceAttribute(double price, int validMonths)
public PriceAttribute(double price, int validMonths, double referencePrice)
{
Price = (decimal)price;
ValidMonths = validMonths;
ReferencePrice = (decimal)referencePrice;
}
}
@@ -26,10 +30,14 @@ public class PriceAttribute : Attribute
public class DisplayNameAttribute : Attribute
{
public string DisplayName { get; }
public string ChineseName { get; }
public string Remark { get; }
public DisplayNameAttribute(string displayName)
public DisplayNameAttribute(string displayName, string chineseName = "", string remark = "")
{
DisplayName = displayName;
ChineseName = chineseName;
Remark = remark;
}
}
@@ -56,7 +64,7 @@ public enum GoodsCategoryType
/// <summary>
/// VIP服务
/// </summary>
VipService = 1,
Vip = 1,
/// <summary>
/// 尊享包服务
@@ -85,35 +93,35 @@ public class TokenAmountAttribute : Attribute
public enum GoodsTypeEnum
{
// VIP服务
[Price(29.9, 1)]
[DisplayName("YiXinVip 1 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
[Price(29.9, 1,29.9)]
[DisplayName("YiXinVip 1 month", "1个月", "灵活选择")]
[GoodsCategory(GoodsCategoryType.Vip)]
YiXinVip1 = 1,
[Price(83.7, 3)]
[DisplayName("YiXinVip 3 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
[Price(83.7, 3,27.9)]
[DisplayName("YiXinVip 3 month", "3个月", "短期体验")]
[GoodsCategory(GoodsCategoryType.Vip)]
YiXinVip3 = 3,
[Price(155.4, 6)]
[DisplayName("YiXinVip 6 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
[Price(155.4, 6,25.9)]
[DisplayName("YiXinVip 6 month", "6个月", "年度热销")]
[GoodsCategory(GoodsCategoryType.Vip)]
YiXinVip6 = 6,
[Price(183.2, 8)]
[DisplayName("YiXinVip 8 month")]
[GoodsCategory(GoodsCategoryType.VipService)]
[Price(183.2, 8,22.9)]
[DisplayName("YiXinVip 8 month", "8个月推荐", "限时活动,超高性价比")]
[GoodsCategory(GoodsCategoryType.Vip)]
YiXinVip8 = 8,
// 尊享包服务 - 需要VIP资格才能购买
[Price(188.9, 0)]
[DisplayName("Premium Package 5000W Tokens")]
[Price(188.9, 0,1750)]
[DisplayName("YiXinPremiumPackage 5000W Tokens", "5000万Tokens", "简单尝试")]
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
[TokenAmount(5000)]
PremiumPackage5000W = 101,
[Price(248.9, 0)]
[DisplayName("Premium Package 10000W Tokens")]
[Price(248.9, 0,3500)]
[DisplayName("YiXinPremiumPackage 10000W Tokens", "1亿Tokens推荐", "极致性价比")]
[GoodsCategory(GoodsCategoryType.PremiumPackage)]
[TokenAmount(10000)]
PremiumPackage10000W = 102,
@@ -181,6 +189,18 @@ public static class GoodsTypeEnumExtensions
return validMonths > 0 ? totalPrice / validMonths : 0m;
}
/// <summary>
/// 获取商品参考价格
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>参考价格</returns>
public static decimal GetReferencePrice(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var priceAttribute = fieldInfo?.GetCustomAttribute<PriceAttribute>();
return priceAttribute?.ReferencePrice ?? 0m;
}
/// <summary>
/// 获取商品类别
/// </summary>
@@ -190,7 +210,7 @@ public static class GoodsTypeEnumExtensions
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var categoryAttribute = fieldInfo?.GetCustomAttribute<GoodsCategoryAttribute>();
return categoryAttribute?.Category ?? GoodsCategoryType.VipService;
return categoryAttribute?.Category ?? GoodsCategoryType.Vip;
}
/// <summary>
@@ -210,7 +230,7 @@ public static class GoodsTypeEnumExtensions
/// <returns>是否为VIP服务</returns>
public static bool IsVipService(this GoodsTypeEnum goodsType)
{
return goodsType.GetGoodsCategory() == GoodsCategoryType.VipService;
return goodsType.GetGoodsCategory() == GoodsCategoryType.Vip;
}
/// <summary>
@@ -225,6 +245,30 @@ public static class GoodsTypeEnumExtensions
return tokenAttribute?.TokenAmount ?? 0;
}
/// <summary>
/// 获取商品中文名称
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>中文名称</returns>
public static string GetChineseName(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
return displayNameAttribute?.ChineseName ?? goodsType.ToString();
}
/// <summary>
/// 获取商品备注
/// </summary>
/// <param name="goodsType">商品类型</param>
/// <returns>备注信息</returns>
public static string GetRemark(this GoodsTypeEnum goodsType)
{
var fieldInfo = goodsType.GetType().GetField(goodsType.ToString());
var displayNameAttribute = fieldInfo?.GetCustomAttribute<DisplayNameAttribute>();
return displayNameAttribute?.Remark ?? string.Empty;
}
/// <summary>
/// 计算折扣金额(仅用于尊享包)
/// 规则每累加充值10元减少2.5元最多减少50元