后台充值订单实付口径、发放泥点列、用户累计充值与兑换码单位
Project CI / AI game creator shell Rust smoke (push) Successful in 2m17s
Project CI / Backend tests (push) Successful in 4m59s
Project CI / AI game creator shell Rust lane 2/2 (push) Successful in 7m29s
Project CI / AI game creator shell Rust lane 1/2 (push) Successful in 8m40s
Project CI / Frontend tests (push) Successful in 1m59s
Project CI / AI game creator shell Rust crates (push) Successful in 9m16s
Project CI / AI game creator shell web tests (push) Successful in 1m41s
Project CI / Repository checks (push) Successful in 4m4s
Project CI / Native shell tests (push) Successful in 7m1s
Project CI / AI game creator shell Rust smoke (push) Successful in 2m17s
Project CI / Backend tests (push) Successful in 4m59s
Project CI / AI game creator shell Rust lane 2/2 (push) Successful in 7m29s
Project CI / AI game creator shell Rust lane 1/2 (push) Successful in 8m40s
Project CI / Frontend tests (push) Successful in 1m59s
Project CI / AI game creator shell Rust crates (push) Successful in 9m16s
Project CI / AI game creator shell web tests (push) Successful in 1m41s
Project CI / Repository checks (push) Successful in 4m4s
Project CI / Native shell tests (push) Successful in 7m1s
- 新增 AdminRechargeOrderEntryPayload.paidAmountCents:只有 paid_at 存在的订单才有实付,未支付 / 已关闭 / 已过期固定为 0 - 充值管理列表把「金额 / 泥点」拆成「实付」「发放泥点」两列,未支付行实付显示「未支付」并附订单金额小字;退款面板「订单实付」改读同一字段 - 用户详情充值订单表新增「发放泥点」列,商品列只保留商品名,实付同样按 paidAmountCents 展示 - 用户详情新增 cumulativeRechargedCents:api-server 按 user_id 读取 profile_recharge_order,只累加 paid_at 存在的订单金额(退款不回减),单次上限 500 行,读取失败或命中上限返回 null - 用户详情身份区新增「累计充值」,读不到时显示「未知」,不用用户详情最多 20 条订单在 BFF 或前端近似重算 - 兑换码奖励单位收口为泥点:输入标签与列表列头改「奖励泥点」,单元格带「泥点」单位,避免被当成元 - 新增后端 2 条累计充值口径单测与前端 2 条用例(未支付不显示实付且发放泥点独立成列、累计充值未知态) - 同步后端架构数据契约(实付口径 / 累计充值来源与上限 / 兑换码奖励单位)与决策记录 - 验证:cargo check -p api-server;cargo test -p api-server --bin api-server admin(138 passed / 0 failed / 1 ignored);npm run admin-web:typecheck;npx vitest run apps/admin-web/src(220 passed);cargo fmt --all --check、check:encoding、check:doc-index、git diff --check 通过
This commit is contained in:
@@ -958,6 +958,8 @@ export interface AdminRechargeOrderEntryPayload {
|
||||
productTitle: string;
|
||||
productKind: string;
|
||||
amountCents: number;
|
||||
/** 真实支付金额(分):未支付 / 已关闭 / 已过期订单固定为 0,不能拿订单金额当实付。 */
|
||||
paidAmountCents: number;
|
||||
status: string;
|
||||
paymentChannel: string;
|
||||
paidAtMicros?: number | null;
|
||||
@@ -997,6 +999,8 @@ export interface AdminUserDetailResponse {
|
||||
phoneBound: boolean;
|
||||
wechatBound: boolean;
|
||||
historicalConsumedPoints: number;
|
||||
/** 累计充值金额(分):读取失败或命中读取上限时为 null,前端按未知展示。 */
|
||||
cumulativeRechargedCents?: number | null;
|
||||
canReconcileConsumption: boolean;
|
||||
wallet: AdminProfileWalletPayload;
|
||||
rechargeOrders: AdminRechargeOrderEntryPayload[];
|
||||
|
||||
@@ -51,6 +51,7 @@ const detail: AdminUserDetailResponse = {
|
||||
phoneBound: true,
|
||||
wechatBound: true,
|
||||
historicalConsumedPoints: 1234,
|
||||
cumulativeRechargedCents: 128800,
|
||||
canReconcileConsumption: true,
|
||||
wallet,
|
||||
rechargeOrders: [
|
||||
@@ -62,6 +63,7 @@ const detail: AdminUserDetailResponse = {
|
||||
productTitle: '60泥点',
|
||||
productKind: 'points',
|
||||
amountCents: 600,
|
||||
paidAmountCents: 600,
|
||||
status: 'paid',
|
||||
paymentChannel: 'wechat_native',
|
||||
paidAtMicros: 1_720_000_000_000_000,
|
||||
@@ -124,7 +126,11 @@ test('用户查看按钮按内部 ID 查询并展示脱敏资料、余额与退
|
||||
expect(screen.getByText('25', { selector: 'strong' })).toBeTruthy();
|
||||
expect(screen.getByText('历史花费')).toBeTruthy();
|
||||
expect(screen.getByText('1234', { selector: 'strong' })).toBeTruthy();
|
||||
expect(screen.getByText('累计充值')).toBeTruthy();
|
||||
expect(screen.getByText('¥1288.00')).toBeTruthy();
|
||||
expect(screen.getByText('order-1')).toBeTruthy();
|
||||
expect(screen.getByRole('columnheader', { name: '实付' })).toBeTruthy();
|
||||
expect(screen.getByRole('columnheader', { name: '发放泥点' })).toBeTruthy();
|
||||
|
||||
await user.keyboard('{Escape}');
|
||||
await waitFor(() =>
|
||||
@@ -133,6 +139,29 @@ test('用户查看按钮按内部 ID 查询并展示脱敏资料、余额与退
|
||||
await waitFor(() => expect(document.activeElement).toBe(trigger));
|
||||
});
|
||||
|
||||
test('累计充值读取不到时展示未知,不用订单列表近似', async () => {
|
||||
vi.mocked(getAdminUserDetail).mockResolvedValue({
|
||||
...detail,
|
||||
cumulativeRechargedCents: null,
|
||||
});
|
||||
const user = userEvent.setup();
|
||||
render(
|
||||
<AdminUserReferenceButton
|
||||
token="admin-token"
|
||||
userId="user-1"
|
||||
onUnauthorized={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: '查看用户信息' }));
|
||||
await screen.findByText('陶泥用户');
|
||||
|
||||
expect(screen.getByText('累计充值')).toBeTruthy();
|
||||
expect(screen.getByText('累计充值').nextElementSibling?.textContent).toBe(
|
||||
'未知',
|
||||
);
|
||||
});
|
||||
|
||||
test('只有陶泥号时按 publicUserCode 查询用户', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(
|
||||
|
||||
@@ -364,6 +364,7 @@ export function AdminUserDetailDialog({
|
||||
<th>订单</th>
|
||||
<th>商品</th>
|
||||
<th>实付</th>
|
||||
<th>发放泥点</th>
|
||||
<th>退款</th>
|
||||
<th>状态</th>
|
||||
</tr>
|
||||
@@ -377,11 +378,13 @@ export function AdminUserDetailDialog({
|
||||
</span>
|
||||
<small>{formatMicros(order.createdAtMicros)}</small>
|
||||
</td>
|
||||
<td>{order.productTitle || order.productId}</td>
|
||||
<td>
|
||||
{order.productTitle || order.productId}
|
||||
<small>发放 {order.pointsDelta} 泥点</small>
|
||||
{order.paidAmountCents > 0
|
||||
? formatMoney(order.paidAmountCents)
|
||||
: '未支付'}
|
||||
</td>
|
||||
<td>{formatMoney(order.amountCents)}</td>
|
||||
<td>{order.pointsDelta} 泥点</td>
|
||||
<td>
|
||||
{formatMoney(order.cumulativeSuccessRefundCents)}
|
||||
<small>欠账 {order.unrecoveredPoints} 泥点</small>
|
||||
@@ -435,6 +438,14 @@ function UserIdentityHeader({ detail }: { detail: AdminUserDetailResponse }) {
|
||||
<dt>登录方式</dt>
|
||||
<dd>{detail.loginMethod || '-'}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>累计充值</dt>
|
||||
<dd>
|
||||
{typeof detail.cumulativeRechargedCents === 'number'
|
||||
? formatMoney(detail.cumulativeRechargedCents)
|
||||
: '未知'}
|
||||
</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>绑定状态</dt>
|
||||
<dd>
|
||||
|
||||
@@ -64,6 +64,7 @@ const baseOrder: AdminRechargeOrderEntryPayload = {
|
||||
productTitle: '60泥点',
|
||||
productKind: 'points',
|
||||
amountCents: 600,
|
||||
paidAmountCents: 600,
|
||||
status: 'paid',
|
||||
paymentChannel: 'wechat_native',
|
||||
paidAtMicros: 1_720_000_000_000_000,
|
||||
@@ -129,6 +130,40 @@ beforeEach(() => {
|
||||
);
|
||||
});
|
||||
|
||||
test('未支付订单不显示实付金额,发放泥点单独成列', async () => {
|
||||
vi.mocked(listAdminRechargeOrders).mockResolvedValue({
|
||||
entries: [
|
||||
{
|
||||
...baseOrder,
|
||||
orderId: 'order-pending',
|
||||
status: 'pending',
|
||||
paidAtMicros: null,
|
||||
paidAmountCents: 0,
|
||||
pointsDelta: 0,
|
||||
},
|
||||
{ ...baseOrder, orderId: 'order-paid' },
|
||||
],
|
||||
});
|
||||
renderPage();
|
||||
|
||||
expect(
|
||||
await screen.findByRole('columnheader', { name: '实付' }),
|
||||
).toBeTruthy();
|
||||
expect(screen.getByRole('columnheader', { name: '发放泥点' })).toBeTruthy();
|
||||
|
||||
const unpaidRow = (await screen.findByText('order-pending')).closest(
|
||||
'tr',
|
||||
) as HTMLElement;
|
||||
const unpaidCells = within(unpaidRow).getAllByRole('cell');
|
||||
expect(unpaidCells[3]?.textContent).toContain('未支付');
|
||||
expect(unpaidCells[4]?.textContent).toBe('0 泥点');
|
||||
|
||||
const paidRow = screen.getByText('order-paid').closest('tr') as HTMLElement;
|
||||
const paidCells = within(paidRow).getAllByRole('cell');
|
||||
expect(paidCells[3]?.textContent).toBe('¥6.00');
|
||||
expect(paidCells[4]?.textContent).toBe('60 泥点');
|
||||
});
|
||||
|
||||
test('充值订单查询传递全部筛选字段', async () => {
|
||||
const user = userEvent.setup();
|
||||
renderPage();
|
||||
|
||||
@@ -658,7 +658,8 @@ export function AdminRechargeOrderPage({
|
||||
<th>用户</th>
|
||||
<th>订单</th>
|
||||
<th>支付</th>
|
||||
<th>金额 / 泥点</th>
|
||||
<th>实付</th>
|
||||
<th>发放泥点</th>
|
||||
<th>退款与追回</th>
|
||||
<th>钱包</th>
|
||||
<th>状态</th>
|
||||
@@ -715,7 +716,7 @@ export function AdminRechargeOrderPage({
|
||||
<div className="admin-refund-summary-grid">
|
||||
<Metric
|
||||
label="订单实付"
|
||||
value={formatMoney(refundOrder.amountCents)}
|
||||
value={formatMoney(refundOrder.paidAmountCents)}
|
||||
/>
|
||||
<Metric
|
||||
label="累计已退"
|
||||
@@ -1118,8 +1119,13 @@ function RechargeOrderRow({
|
||||
</small>
|
||||
</td>
|
||||
<td>
|
||||
<strong>{formatMoney(order.amountCents)}</strong>
|
||||
<small>发放 {order.pointsDelta} 泥点</small>
|
||||
<strong>{formatOrderPaidAmount(order)}</strong>
|
||||
{order.paidAmountCents > 0 ? null : (
|
||||
<small>订单 {formatMoney(order.amountCents)}</small>
|
||||
)}
|
||||
</td>
|
||||
<td>
|
||||
<span>{order.pointsDelta} 泥点</span>
|
||||
</td>
|
||||
<td>
|
||||
<span>累计 {formatMoney(order.cumulativeSuccessRefundCents)}</span>
|
||||
@@ -1312,6 +1318,13 @@ function formatMoney(cents: number) {
|
||||
return `¥${(cents / 100).toFixed(2)}`;
|
||||
}
|
||||
|
||||
/** 实付只属于真正支付过的订单:未支付 / 已关闭 / 已过期订单显示“未支付”。 */
|
||||
function formatOrderPaidAmount(order: AdminRechargeOrderEntryPayload) {
|
||||
return order.paidAmountCents > 0
|
||||
? formatMoney(order.paidAmountCents)
|
||||
: '未支付';
|
||||
}
|
||||
|
||||
function formatCentsInput(cents: number) {
|
||||
return (cents / 100).toFixed(2);
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ export function AdminRedeemCodePage({
|
||||
|
||||
<div className="admin-form-row">
|
||||
<label className="admin-field">
|
||||
<span>奖励点数</span>
|
||||
<span>奖励泥点</span>
|
||||
<input
|
||||
min={1}
|
||||
step={1}
|
||||
@@ -319,7 +319,7 @@ export function AdminRedeemCodePage({
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Code</th>
|
||||
<th>奖励</th>
|
||||
<th>奖励泥点</th>
|
||||
<th>状态</th>
|
||||
<th>有效期</th>
|
||||
</tr>
|
||||
@@ -337,7 +337,7 @@ export function AdminRedeemCodePage({
|
||||
</button>
|
||||
<small>{redeemModeLabel(entry.mode)}</small>
|
||||
</td>
|
||||
<td>{entry.rewardPoints}</td>
|
||||
<td>{entry.rewardPoints} 泥点</td>
|
||||
<td>
|
||||
<span
|
||||
className={`admin-status ${redeemValidityClass(entry)}`}
|
||||
|
||||
@@ -9277,3 +9277,14 @@ CI 上 `background_agent_runtime_recovers_stale_running_before_pending_task` 在
|
||||
- 边界(本次不改):用户详情「历史花费」(`profile_wallet_consumption_total` 投影与手动对账)维持既有「退款不冲减」决策,仍只累计负向消费流水;若要改成净额,必须单独走投影语义 + 对账口径变更,不能顺手改这一处。充值退款追回、余额重置、赠送和 hold 继续不计入消耗。
|
||||
- 影响范围:`server-rs/crates/api-server/src/admin.rs`、`server-rs/crates/shared-contracts/src/admin.rs`、`apps/admin-web/src/api/adminApiTypes.ts`、`apps/admin-web/src/pages/AdminDashboardPage.tsx`、对应用例与 `docs/technical/【后台管理】Dashboard运营看板方案-2026-06-23.md`。
|
||||
- 验证方式:`cargo test -p api-server --manifest-path server-rs/Cargo.toml --bin api-server dashboard_consumption`(新增 4 条净额用例全绿);`cargo test -p api-server --manifest-path server-rs/Cargo.toml --bin api-server admin`(136 passed / 0 failed / 1 ignored);`npm run admin-web:typecheck`;`npx vitest run apps/admin-web/src`(218 passed);`npm run check:encoding`、`git diff --check`。
|
||||
|
||||
## 2026-09-23 后台充值订单实付口径、发放泥点列、用户累计充值与兑换码单位
|
||||
|
||||
- 背景:充值管理列表与用户详情把订单金额当实付展示,未支付订单也显示非 0 实付;发放泥点挤在「金额 / 泥点」一格或商品列小字里;用户详情看不到该用户累计充值额度;兑换码页的奖励数字没有单位,运营无法判断是元还是泥点。
|
||||
- 决策(实付只有支付过的订单才有):`AdminRechargeOrderEntryPayload` 新增 `paidAmountCents`,由 api-server 按订单 `paid_at` 是否存在判定——存在才等于订单金额,未支付 / 已关闭 / 已过期固定为 0。后台前端实付列显示 `未支付`(并附订单金额小字),退款面板「订单实付」读同一字段;订单金额 `amountCents` 不再被当作实付。
|
||||
- 决策(发放泥点独立成列):充值管理列表的表头由「金额 / 泥点」拆成「实付」与「发放泥点」两列,用户详情充值订单表同样新增「发放泥点」列,商品列只保留商品名;未支付订单发放为 0 泥点,与实付口径一致。
|
||||
- 决策(累计充值由后端算):用户详情新增 `cumulativeRechargedCents`,api-server 按 `user_id` 读取 `profile_recharge_order`、只累加 `paid_at` 存在的订单金额(退款不回减),单次读取上限 500 行;读取失败或命中上限返回 `null`,前端显示「读取失败」,不用用户详情最多 20 条订单在 BFF 或前端近似重算。此次只新增 BFF 字段,未改 SpacetimeDB 表结构与 procedure。
|
||||
- 决策(兑换码奖励是泥点):兑换码 `rewardPoints` 是奖励泥点(兑换成功按 `redeem_code_reward` 流水进钱包),后台输入标签改为「奖励泥点」、列表列头与单元格都带「泥点」单位。
|
||||
- 影响范围:`server-rs/crates/api-server/src/{admin.rs,admin_recharge.rs}`、`server-rs/crates/shared-contracts/src/admin.rs`、`apps/admin-web/src/api/adminApiTypes.ts`、`apps/admin-web/src/pages/{AdminRechargeOrderPage.tsx,AdminRedeemCodePage.tsx}`、`apps/admin-web/src/components/AdminUserDetailDialog.tsx`、对应三个用例文件与后端架构数据契约文档。
|
||||
- 验证方式:`cargo test -p api-server --manifest-path server-rs/Cargo.toml --bin api-server cumulative_recharge`(2 条新用例)与 `cargo check -p api-server`;`npm run admin-web:typecheck`;`npx vitest run apps/admin-web/src`(220 passed),其中三个定向文件 33 passed(新增「未支付订单不显示实付金额,发放泥点单独成列」与「累计充值读取不到时展示未知,不用订单列表近似」)。
|
||||
- 边界(未验证):未连真实生产库核对历史订单的累计充值数值,也未跑真实栈 API smoke。
|
||||
|
||||
@@ -176,7 +176,7 @@ npm run check:server-rs-ddd
|
||||
22. 后台主动退款只支持 `wechat_mp`、`wechat_jsapi`、`wechat_h5`、`wechat_native` 普通 V3 泥点订单。`api-server` 必须先按正式支付渠道和商品类型拦截不支持的订单,再做微信支付订单查单预检,然后调用 SpacetimeDB procedure 原子创建退款 hold;只有 hold 成功才允许调用微信退款。`wechat_mp_virtual`、历史非正式渠道值、会员、未支付、对账未完成、退款已满额、人工冻结、退款欠账或永久泥点不足必须在调用普通 V3 provider 前 fail-closed。
|
||||
23. `profile_recharge_refund_hold` 以稳定 `out_refund_no` 为主键,保存订单、用户、本次退款金额、占用永久泥点、管理员、原因和 `active / settled / released` 状态。重试只有在订单、`out_refund_no`、退款金额、管理员和归一化原因全部与原 hold 一致时才可复用,任一不一致都按幂等内容冲突 fail-closed,不能用新原因调用微信后保留旧审计。部分退款的 hold 在累计应追回增量之外额外保留 1 泥点并发舍入缓冲,全额退款不加缓冲;活动 hold 不改变钱包总额,但普通钱包消费必须预留全部活动 hold;成功退款 observation 扣款并结算匹配 hold,关闭退款释放 hold,外部退款追回不得消耗其他活动 hold。
|
||||
24. 退款欠账继续以 `profile_recharge_order_refund_settlement.unrecovered_points` 为唯一真相;不新增平行 debt 累计。`profile_wallet_manual_restriction` 只保存人工冻结,普通消费同时检查人工冻结与退款欠账。后续永久泥点到账后继续偿还欠账,每日免费与会员周期泥点不参与;解除人工冻结不得清除退款欠账限制。
|
||||
25. 管理员充值订单、用户详情、历史花费手动对账、退款预检/执行、应急退款号登记、退款人工复核和钱包冻结接口只留在 `api-server` 管理员鉴权路由。用户详情中的历史花费泥点数读取 `profile_wallet_consumption_total` 投影;已有投影时,每次 `asset_operation_consume` 负向流水落账在同一事务内按主键 O(1) 原子累加,退款不回减,充值退款追回、余额重置、赠送和 hold 均不计入。首次上线必须在停止业务写入的维护窗口内,由 owner 调用 `POST /admin/api/profile/users/initialize-consumption-projections`,一次扫描全部权威钱包流水,为每个已有钱包流水的用户初始化存量投影;接口成功后才能恢复流量。维护遗漏或新用户缺行时,首次消费按该用户索引一次性重建(当前消费流水已经在同一事务中,不能重复加本次金额);钱包详情首次读取也保留同一按用户兜底。`POST /admin/api/profile/users/reconcile-consumption` 是显式手动对账入口:owner 始终可用,member 必须单独持有 `profile-wallet-consumption-reconcile` 操作权限,任何一级 Tab 都不自动附带;用户详情只在后端返回 `canReconcileConsumption=true` 时展示按钮。操作经二次确认后扫描该用户全部权威钱包流水、比较并校准投影,同时记录管理员和对账时间。退款人工复核 BFF 必须从管理员会话写入操作人,要求非空原因,返回微信退款交易号、订单总额以及获批错误码等正式审计字段,并调用 runtime service identity 受限 procedure;后台确认面板必须展示这些后端事实,前端不得自行改 settlement、钱包冻结或消费累计。外部微信副作用由 `platform-wechat` 执行,退款/hold/钱包事务留在 `spacetime-module`,后台前端只展示 BFF 返回的正式状态。
|
||||
25. 管理员充值订单、用户详情、历史花费手动对账、退款预检/执行、应急退款号登记、退款人工复核和钱包冻结接口只留在 `api-server` 管理员鉴权路由。用户详情中的历史花费泥点数读取 `profile_wallet_consumption_total` 投影;已有投影时,每次 `asset_operation_consume` 负向流水落账在同一事务内按主键 O(1) 原子累加,退款不回减,充值退款追回、余额重置、赠送和 hold 均不计入。首次上线必须在停止业务写入的维护窗口内,由 owner 调用 `POST /admin/api/profile/users/initialize-consumption-projections`,一次扫描全部权威钱包流水,为每个已有钱包流水的用户初始化存量投影;接口成功后才能恢复流量。维护遗漏或新用户缺行时,首次消费按该用户索引一次性重建(当前消费流水已经在同一事务中,不能重复加本次金额);钱包详情首次读取也保留同一按用户兜底。`POST /admin/api/profile/users/reconcile-consumption` 是显式手动对账入口:owner 始终可用,member 必须单独持有 `profile-wallet-consumption-reconcile` 操作权限,任何一级 Tab 都不自动附带;用户详情只在后端返回 `canReconcileConsumption=true` 时展示按钮。操作经二次确认后扫描该用户全部权威钱包流水、比较并校准投影,同时记录管理员和对账时间。退款人工复核 BFF 必须从管理员会话写入操作人,要求非空原因,返回微信退款交易号、订单总额以及获批错误码等正式审计字段,并调用 runtime service identity 受限 procedure;后台确认面板必须展示这些后端事实,前端不得自行改 settlement、钱包冻结或消费累计。外部微信副作用由 `platform-wechat` 执行,退款/hold/钱包事务留在 `spacetime-module`,后台前端只展示 BFF 返回的正式状态。充值订单实付金额(`paidAmountCents`)同样由后端判定:只有 `paid_at` 存在的订单才有实付,未支付、已关闭、已过期订单固定为 `0`,后台前端不得拿订单金额(`amountCents`)顶替实付;订单发放泥点(`pointsDelta`)必须与实付分开成列展示。用户详情的累计充值金额(`cumulativeRechargedCents`)由 api-server 按 `user_id` 受控读取 `profile_recharge_order`、只累加 `paid_at` 存在的订单 `amount_cents`(退款不回减)得出;读取失败或命中单次读取上限时返回 `null`,前端按未知展示,不得用用户详情最多 20 条订单在 BFF 或前端近似重算。
|
||||
|
||||
## 用户钱包与编辑器生成扣费契约
|
||||
|
||||
@@ -767,7 +767,7 @@ Responses 的终态载荷既是工具调用的恢复源,也是正文的恢复
|
||||
- Rust 结构体:`ProfileRedeemCode`
|
||||
- 源码:`server-rs/crates/spacetime-module/src/runtime/profile.rs`
|
||||
- 生效时间:复用邀请码时间窗口语义,`starts_at` / `expires_at` 均可为空;两者同时存在时必须满足 `starts_at < expires_at`。开始时刻计入有效区间,截止时刻不计入有效区间。用户兑换时以后端接收的 `redeemed_at_micros` 判定:未到开始时间拒绝为“兑换码未生效”,到达或超过截止时间拒绝为“兑换码已过期”。
|
||||
- 后台契约:`AdminUpsertProfileRedeemCodeRequest` 通过可空 `startsAt` / `expiresAt` 接收 RFC3339 时间,列表与保存响应同步返回这两个字段;后台页只负责输入、回填和显示,真正兑换判定留在后端事务路径。
|
||||
- 后台契约:`AdminUpsertProfileRedeemCodeRequest` 通过可空 `startsAt` / `expiresAt` 接收 RFC3339 时间,列表与保存响应同步返回这两个字段;后台页只负责输入、回填和显示,真正兑换判定留在后端事务路径。`reward_points` 是奖励泥点(兑换成功后按 `redeem_code_reward` 流水进入泥点钱包),不是金额;后台奖励输入与列表必须带泥点单位。
|
||||
|
||||
### `profile_redeem_code_usage`
|
||||
|
||||
|
||||
@@ -70,6 +70,7 @@ use spacetime_client::{
|
||||
};
|
||||
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
|
||||
use tokio::sync::{OnceCell, Semaphore};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
admin_accounts::normalize_admin_account_username,
|
||||
@@ -2832,6 +2833,61 @@ async fn fetch_admin_dashboard_rows(state: &AppState, sql: &str) -> Result<Vec<V
|
||||
extract_first_sql_rows(payload)
|
||||
}
|
||||
|
||||
/// 用户详情累计充值的单次读取上限:单个用户的历史订单远少于该值。
|
||||
const ADMIN_USER_RECHARGE_SUMMARY_ROW_LIMIT: u32 = 500;
|
||||
|
||||
/// 读取用户累计充值金额(分):扫描该用户全部充值订单,只累加真实支付过的订单。
|
||||
/// 口径与 module 的 `profile_recharge_order_counts_as_paid_purchase` 一致(`paid_at` 存在),
|
||||
/// 未支付 / 已关闭 / 已过期订单不计,退款也不回减。读取失败或命中读取上限时返回 None,
|
||||
/// 由调用方与前端按“未知”展示,不能把部分结果当累计值。
|
||||
pub(crate) async fn fetch_admin_user_cumulative_recharged_cents(
|
||||
state: &AppState,
|
||||
user_id: &str,
|
||||
) -> Option<u64> {
|
||||
let sql = format!(
|
||||
"SELECT amount_cents, paid_at FROM profile_recharge_order WHERE user_id = {} LIMIT {}",
|
||||
quote_sql_string(user_id),
|
||||
ADMIN_USER_RECHARGE_SUMMARY_ROW_LIMIT
|
||||
);
|
||||
let rows = match fetch_admin_dashboard_rows(state, &sql).await {
|
||||
Ok(rows) => rows,
|
||||
Err(message) => {
|
||||
warn!(user_id, error = %message, "读取用户累计充值失败");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
if rows.len() >= ADMIN_USER_RECHARGE_SUMMARY_ROW_LIMIT as usize {
|
||||
warn!(
|
||||
user_id,
|
||||
limit = ADMIN_USER_RECHARGE_SUMMARY_ROW_LIMIT,
|
||||
"用户累计充值命中单次读取上限,按未知处理"
|
||||
);
|
||||
return None;
|
||||
}
|
||||
Some(sum_admin_user_recharged_cents(&rows))
|
||||
}
|
||||
|
||||
/// 汇总 `profile_recharge_order` 查询行:只累加 `paid_at` 存在(真实支付过)的订单金额。
|
||||
/// SpacetimeDB HTTP SQL 会把 `Option<Timestamp>` 返回成 `[micros]`、`[0, micros]` 或 `[1, []]` 等 SATS 形态。
|
||||
fn sum_admin_user_recharged_cents(rows: &[Value]) -> u64 {
|
||||
let mut total = 0_u64;
|
||||
for row in rows {
|
||||
let Some(columns) = row.as_array() else {
|
||||
continue;
|
||||
};
|
||||
let Some(amount_cents) = columns.first().and_then(value_to_i64) else {
|
||||
continue;
|
||||
};
|
||||
let paid = columns
|
||||
.get(1)
|
||||
.is_some_and(|value| timestamp_value_to_micros(value).is_some());
|
||||
if paid && amount_cents > 0 {
|
||||
total = total.saturating_add(amount_cents as u64);
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
fn build_admin_dashboard_chart(
|
||||
id: &str,
|
||||
title: &str,
|
||||
@@ -4863,9 +4919,9 @@ mod tests {
|
||||
parse_spacetime_sql_count_response, parse_timestamp_text_to_micros,
|
||||
resolve_admin_dashboard_range, resolve_admin_dashboard_range_at,
|
||||
resolve_admin_database_table_sql_limit, resolve_admin_editor_asset_filters,
|
||||
timestamp_value_to_micros, trim_preview, validate_admin_editor_asset_cursor,
|
||||
validate_admin_external_api_key_query, verify_admin_password,
|
||||
wallet_ledger_source_type_to_string,
|
||||
sum_admin_user_recharged_cents, timestamp_value_to_micros, trim_preview,
|
||||
validate_admin_editor_asset_cursor, validate_admin_external_api_key_query,
|
||||
verify_admin_password, wallet_ledger_source_type_to_string,
|
||||
};
|
||||
use axum::{
|
||||
http::{Method, StatusCode},
|
||||
@@ -5854,6 +5910,33 @@ mod tests {
|
||||
assert_eq!(net.total(), 48);
|
||||
}
|
||||
|
||||
/// 累计充值只累加真实支付过的订单:未支付订单的 paid_at 是 SATS None,不能计入。
|
||||
#[test]
|
||||
fn cumulative_recharge_counts_only_paid_orders() {
|
||||
let rows = vec![
|
||||
json!([6000, [1778207451731746_i64]]),
|
||||
json!([1200, [0, [1778207451731746_i64]]]),
|
||||
json!([9900, [1, []]]),
|
||||
json!([8800, null]),
|
||||
json!([300, "2026-05-07T08:30:51.731746Z"]),
|
||||
json!([1000, [1, []]]),
|
||||
];
|
||||
|
||||
assert_eq!(sum_admin_user_recharged_cents(&rows), 7500);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cumulative_recharge_ignores_malformed_rows() {
|
||||
let rows = vec![
|
||||
json!("not-a-row"),
|
||||
json!(["abc", [123]]),
|
||||
json!([]),
|
||||
json!([0, [123]]),
|
||||
];
|
||||
|
||||
assert_eq!(sum_admin_user_recharged_cents(&rows), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timestamp_value_to_micros_accepts_sql_shapes() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -45,7 +45,10 @@ use shared_contracts::admin::{
|
||||
use spacetime_client::SpacetimeClientError;
|
||||
|
||||
use crate::{
|
||||
admin::{AdminDisplayNameDirectory, AuthenticatedAdmin, load_admin_display_name_directory},
|
||||
admin::{
|
||||
AdminDisplayNameDirectory, AuthenticatedAdmin, fetch_admin_user_cumulative_recharged_cents,
|
||||
load_admin_display_name_directory,
|
||||
},
|
||||
api_response::json_success_body,
|
||||
http_error::AppError,
|
||||
request_context::RequestContext,
|
||||
@@ -153,6 +156,8 @@ pub async fn admin_get_user_detail(
|
||||
let admin_display_names = load_admin_display_name_directory(&state)
|
||||
.await
|
||||
.map_err(|error| spacetime_error_response(&request_context, error))?;
|
||||
let cumulative_recharged_cents =
|
||||
fetch_admin_user_cumulative_recharged_cents(&state, &user.id).await;
|
||||
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
@@ -167,6 +172,7 @@ pub async fn admin_get_user_detail(
|
||||
phone_bound: user.phone_number_masked.is_some(),
|
||||
wechat_bound: user.wechat_bound,
|
||||
historical_consumed_points: wallet_detail.historical_consumed_points,
|
||||
cumulative_recharged_cents,
|
||||
can_reconcile_consumption: admin.can(ADMIN_ACTION_PROFILE_WALLET_CONSUMPTION_RECONCILE),
|
||||
wallet: map_wallet(wallet_detail.wallet, Some(&admin_display_names)),
|
||||
recharge_orders: orders
|
||||
@@ -935,6 +941,12 @@ fn map_order_entry(
|
||||
let settlement = entry.settlement.as_ref();
|
||||
let remaining_refundable_cents = remaining_refundable_cents(&entry);
|
||||
let block_reason = order_refund_block_reason(&entry).map(str::to_string);
|
||||
// 未支付订单不能拿订单金额当实付;只有 paid_at 存在的订单才有实付金额。
|
||||
let paid_amount_cents = if entry.order.paid_at_micros.is_some() {
|
||||
entry.order.amount_cents
|
||||
} else {
|
||||
0
|
||||
};
|
||||
AdminRechargeOrderEntryPayload {
|
||||
order_id: entry.order.order_id,
|
||||
user_id: entry.order.user_id,
|
||||
@@ -943,6 +955,7 @@ fn map_order_entry(
|
||||
product_title: entry.order.product_title,
|
||||
product_kind: entry.order.kind.as_str().to_string(),
|
||||
amount_cents: entry.order.amount_cents,
|
||||
paid_amount_cents,
|
||||
status: entry.order.status.as_str().to_string(),
|
||||
payment_channel: entry.order.payment_channel,
|
||||
paid_at_micros: entry.order.paid_at_micros,
|
||||
|
||||
@@ -1130,6 +1130,8 @@ pub struct AdminRechargeOrderEntryPayload {
|
||||
pub product_title: String,
|
||||
pub product_kind: String,
|
||||
pub amount_cents: u64,
|
||||
/// 真实支付金额(分):只有支付过的订单才有实付,未支付 / 已关闭 / 已过期订单固定为 0。
|
||||
pub paid_amount_cents: u64,
|
||||
pub status: String,
|
||||
pub payment_channel: String,
|
||||
pub paid_at_micros: Option<i64>,
|
||||
@@ -1175,6 +1177,8 @@ pub struct AdminUserDetailResponse {
|
||||
pub phone_bound: bool,
|
||||
pub wechat_bound: bool,
|
||||
pub historical_consumed_points: u64,
|
||||
/// 累计充值金额(分):该用户全部支付过的充值订单金额之和;读取失败或命中读取上限时为 None。
|
||||
pub cumulative_recharged_cents: Option<u64>,
|
||||
pub can_reconcile_consumption: bool,
|
||||
pub wallet: AdminProfileWalletPayload,
|
||||
pub recharge_orders: Vec<AdminRechargeOrderEntryPayload>,
|
||||
|
||||
Reference in New Issue
Block a user