OEM 分级

佣金价格设置 数据库存放修改
This commit is contained in:
chfei committed 2026-11-17 19:10:29 +08:00
1 parent ea0cbd6b92
commit 2de3d0dc92
24 files changed
+1224 -10

No files matched your search

@@ -0,0 +1,83 @@
using KJD_Web.Services.Users;
using Store.Contracts.User.Entitys;
using Store.Contracts.User.Enums;
using System.Reflection;
namespace WebServiceTest;
[TestFixture]
public class BatchMarkupPriceTest
{
private static readonly Dictionary<UserPaymentTypes, int> Floors = new()
{
[UserPaymentTypes.中转仓扣费支出] = 220,
[UserPaymentTypes.退货包裹扣费支出] = 250,
[UserPaymentTypes.礼物单支出] = 160,
[UserPaymentTypes.同步物流] = 190
};
private static Dictionary<string, Dictionary<UserPaymentTypes, int>> Calculate(List<UserEntity> users, List<OEMMarkupRuleEntity> rules)
{
try
{
return (Dictionary<string, Dictionary<UserPaymentTypes, int>>)typeof(UserService)
.GetMethod("CalculateUserPrices", BindingFlags.NonPublic | BindingFlags.Static)!
.Invoke(null, new object[] { users, rules, Floors })!;
}
catch (TargetInvocationException ex) { throw ex.InnerException!; }
}
[Test]
public void FourPricesMatchSingleCalculationAndDoNotMixUsersOrOems()
{
var users = new List<UserEntity>
{
new() { UserName = "user1", OemId = "B" },
new() { UserName = "user2", OemId = "C" },
new() { UserName = "plain" }
};
var rules = new List<OEMMarkupRuleEntity>();
foreach (var type in Floors.Keys)
{
rules.Add(new() { TargetType = OEMMarkupTargetType.OEM, TargetId = "B", SetterOemId = "A", PaymentType = type, Markup = 10 });
rules.Add(new() { TargetType = OEMMarkupTargetType.User, TargetId = "user1", SetterOemId = "B", PaymentType = type, Markup = 20 });
rules.Add(new() { TargetType = OEMMarkupTargetType.User, TargetId = "user1", SetterOemId = "C", PaymentType = type, Markup = 99 });
}
var result = Calculate(users, rules);
Assert.That(result.Count, Is.EqualTo(3));
foreach (var user in users)
{
Assert.That(result[user.UserName].Count, Is.EqualTo(4));
foreach (var floor in Floors)
{
Assert.That(result[user.UserName][floor.Key], Is.EqualTo(floor.Value + (user.UserName == "user1" ? 30 : 0)));
var single = typeof(UserService).GetMethod("CalculateUserPrice", BindingFlags.NonPublic | BindingFlags.Static)!
.Invoke(null, new object[] { rules, user, floor.Key, floor.Value });
Assert.That(result[user.UserName][floor.Key], Is.EqualTo(single));
}
}
}
[TestCase(-1, 1, typeof(InvalidOperationException))]
[TestCase(10, 2, typeof(InvalidOperationException))]
[TestCase(int.MaxValue, 1, typeof(OverflowException))]
public void InvalidRuleDataStillFails(int markup, int copies, Type exceptionType)
{
var rule = new OEMMarkupRuleEntity { TargetType = OEMMarkupTargetType.User, TargetId = "user",
SetterOemId = "B", PaymentType = UserPaymentTypes.中转仓扣费支出, Markup = markup };
Assert.Throws(exceptionType, () => Calculate(new() { new() { UserName = "user", OemId = "B" } },
Enumerable.Repeat(rule, copies).ToList()));
}
[Test]
public void EmptyAndInvalidUserListsDoNotAccessDatabase()
{
using var sql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Server=127.0.0.1;Database=unused;User Id=unused;Password=unused;").Build();
var service = new UserService(sql, null!);
Assert.That(service.GetUserFinalPrices(Array.Empty<UserEntity>()), Is.Empty);
Assert.Throws<ArgumentNullException>(() => service.GetUserFinalPrices(null!));
Assert.Throws<ArgumentException>(() => service.GetUserFinalPrices(new[] { new UserEntity() }));
Assert.Throws<ArgumentException>(() => service.GetUserFinalPrices(new[] { new UserEntity { UserName = "same" }, new UserEntity { UserName = "same" } }));
}
}