Files
2026-11-14 23:06:12 +08:00

239 lines
13 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# s2-walkthrough.sh S2 用户人工走查脚本(D2/D3 2026-11-14;D4/D5+收官 2026-11-15)
#
# 用途:把 S2 各日验收从「AI 自证」补正为「用户本人走查」(七件套·本人走查项)。
# D2 消耗日汇总 / D3 充值审核流 / D4 出账+阶梯+结账+导出 / D5 利润+三方对账
# (D4/D5 用 2026-10 空月,与真实当月数据隔离;全程可还原,跑完自动清理)。
# 前置:① gateway 在跑(:8080,make gw-start,须含 D5 端点的二进制)
# ② sg docker 可用(dev MySQL 13306) ③ go 工具链(构建 settle-check)
# 用法:bash docs/acceptance/scripts/s2-walkthrough.sh 2>&1 | tee /tmp/s2-walk.log
# 走查后将 /tmp/s2-walk.log 全文交回,回填 s2-d2~d5.md/s2-closeout.md。
set -u
GW=http://127.0.0.1:8080/api/v1
ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"
step() { echo; echo "===== $1 ====="; }
pass() { echo "[PASS] $1"; }
fail() { echo "[FAIL] $1 (预期 ${2:-?},见上行输出)"; }
# mysql_pipe:stdin SQL → dev MySQL(多条语句稳态管道;-- 注释行自动忽略)。
mysql_pipe() { sg docker -c 'docker exec -i go-sms-mysql mysql -uroot -proot123 gosms' "$@"; }
# ---------- 步骤 0:签临时 JWT(dev secret,账号 1=admin) ----------
step "步骤0 签发临时 token(非侵入)"
TOK=$(python3 - <<'EOF'
import hmac, hashlib, base64, json, time, uuid
def b64(d): return base64.urlsafe_b64encode(d).rstrip(b'=')
now = int(time.time())
h = b64(json.dumps({'alg':'HS256','typ':'JWT'}).encode())
p = b64(json.dumps({'account_id':1,'username':'admin','roles':['admin'],
'permissions':[],'jti':uuid.uuid4().hex,'iss':'go-sms','sub':'1',
'exp':now+3600,'iat':now}).encode())
s = b64(hmac.new(b'dev-go-sms-secret', h+b'.'+p, hashlib.sha256).digest())
print((h+b'.'+p+b'.'+s).decode())
EOF
)
AUTH="Authorization: Bearer $TOK"
curl -s -m 3 -H "$AUTH" "$GW/billing/consume" | grep -q ISC:000 \
&& pass "token 有效(列表 200)" || fail "token 无效"
# ---------- D2 走查:消耗日汇总 ----------
step "D2-1 种两条测试明细(账号1 成功+未知,通道1)"
sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -e "\
INSERT INTO message_detail (id,task_id,account_id,channel_id,mobile,content,status,business,fee,created_at) VALUES \
(7302401110000000001,810001,1,1,\"13000000001\",\"s2-walk\",2,\"sms\",1.5,NOW()),\
(7302401110000000002,810002,1,1,\"13000000002\",\"s2-walk\",4,\"sms\",2.5,NOW());"' >/dev/null 2>&1 \
&& pass "明细 2 条已插入" || fail "明细插入"
step "D2-2 手动触发今日汇总(应 account_rows=1/channel_rows=1)"
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d "{\"date\":\"$(date +%F)\"}" "$GW/ops/settle/aggregate")
echo "$R"
echo "$R" | grep -q '"account_rows":1' && echo "$R" | grep -q '"channel_rows":1' \
&& pass "落账行数 1/1" || fail "落账行数"
step "D2-3 查询消耗(admin 应见账号1:submit2/succ1/unk1/fee4.0)"
R=$(curl -s -m 5 -H "$AUTH" "$GW/billing/consume?account_id=1")
echo "$R"
echo "$R" | grep -q '"success_count":1' && echo "$R" | grep -q '"unknown_count":1' \
&& echo "$R" | grep -q '"fee_total":4' && pass "计数口径正确" || fail "计数口径"
step "D2-4 customer 视角收窄(以账号1 冒充 customer 应仍只见自己)"
TOKC=$(python3 - <<'EOF'
import hmac, hashlib, base64, json, time, uuid
def b64(d): return base64.urlsafe_b64encode(d).rstrip(b'=')
now = int(time.time())
h = b64(json.dumps({'alg':'HS256','typ':'JWT'}).encode())
p = b64(json.dumps({'account_id':1,'username':'admin','roles':['customer'],
'permissions':[],'jti':uuid.uuid4().hex,'iss':'go-sms','sub':'1',
'exp':now+3600,'iat':now}).encode())
s = b64(hmac.new(b'dev-go-sms-secret', h+b'.'+p, hashlib.sha256).digest())
print((h+b'.'+p+b'.'+s).decode())
EOF
)
R=$(curl -s -m 5 -H "Authorization: Bearer $TOKC" "$GW/billing/consume?account_id=999")
echo "$R"
echo "$R" | grep -q '"total":1' && pass "customer 越权带参被覆盖收窄(total=1)" || fail "customer 收窄"
step "D2-5 状态迁移收敛(未知→成功,重刷后 unknown 0/succ 2)"
sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -e "\
UPDATE message_detail SET status=2 WHERE id=7302401110000000002;"' >/dev/null 2>&1
curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d "{\"date\":\"$(date +%F)\"}" "$GW/ops/settle/aggregate" >/dev/null
R=$(curl -s -m 5 -H "$AUTH" "$GW/billing/consume?account_id=1")
echo "$R"
echo "$R" | grep -q '"success_count":2' && echo "$R" | grep -q '"unknown_count":0' \
&& pass "滚动重刷收敛(72h 回执窗语义)" || fail "收敛"
# ---------- D3 走查:充值订单 ----------
step "D3-1 创建订单(count=300,不动余额)"
B0=$(sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -N -e "SELECT balance FROM account_user WHERE id=1;"' 2>/dev/null)
R=$(curl -s -m 5 -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"account_id":1,"count":300,"amount":30.0,"pay_type":"bank","remark":"s2-walk"}' "$GW/billing/orders")
echo "$R"
ON=$(echo "$R" | grep -o '"order_no":"[^"]*"' | cut -d'"' -f4)
OID=$(echo "$R" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
B1=$(sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -N -e "SELECT balance FROM account_user WHERE id=1;"' 2>/dev/null)
[ "$B0" = "$B1" ] && echo "$R" | grep -q '"pay_status":0' \
&& pass "创建后 pay_status=0 且余额未动($B0)" || fail "创建不动余额"
step "D3-2 confirm(余额应 +300)"
R=$(curl -s -m 5 -X POST -H "$AUTH" "$GW/billing/orders/$OID/confirm")
echo "$R"
echo "$R" | grep -q "\"balance\":$((B0+300))" && pass "confirm 余额 $B0→$((B0+300))" || fail "confirm 余额"
step "D3-3 重复 confirm(应 409 ISC:039)"
R=$(curl -s -w '\nHTTP=%{http_code}' -m 5 -X POST -H "$AUTH" "$GW/billing/orders/$OID/confirm")
echo "$R"
echo "$R" | grep -q 'HTTP=409' && echo "$R" | grep -q 'ISC:039' && pass "409+ISC:039" || fail "重复 confirm"
step "D3-4 真库核验流水(ref_id=order_no 双通道锚点)"
sg docker -c "docker exec go-sms-mysql mysql -uroot -proot123 gosms -e \
\"SELECT biz_type,amount,balance_after,ref_id FROM account_balance_log WHERE ref_id='$ON';\"" 2>/dev/null
step "D3-5 审计留痕(module=billing)"
sg docker -c 'docker exec go-sms-mysql mysql -uroot -proot123 gosms -e \
"SELECT module,action FROM system_operate_log WHERE module=\"billing\" ORDER BY id DESC LIMIT 3;"' 2>/dev/null
# ---------- D4 走查:出账+阶梯+结账+导出(2026-10 空月) ----------
step "D4-1 种出账链数据(通道/上家 9101,账号1 价0.10,通道价0.05,明细2条 2026-10-15)"
printf '%s\n' \
"INSERT INTO channel_upstream (id,name,contact,status,created_at,updated_at) VALUES (9101,'s2-walk-up','walk',1,NOW(),NOW());" \
"INSERT INTO channel_channel (id,name,code,protocol,upstream_id,status,billing_mode,created_at,updated_at) VALUES (9101,'s2-walk-ch','S2WALK','http',9101,1,'success',NOW(),NOW());" \
"INSERT INTO account_price (account_id,business,price,effective_at,created_at) VALUES (1,'sms',0.10,'2026-10-01 00:00:00',NOW());" \
"INSERT INTO channel_price (channel_id,price,effective_at,created_at) VALUES (9101,0.05,'2026-10-01 00:00:00',NOW());" \
"INSERT INTO message_detail (id,task_id,account_id,channel_id,mobile,content,status,business,fee,created_at) VALUES" \
"(7302411150000000001,820101,1,9101,'13000000001','s2walk',2,'sms',1,'2026-10-15 12:00:00')," \
"(7302411150000000002,820102,1,9101,'13000000002','s2walk',2,'sms',1,'2026-10-15 12:00:00');" \
| mysql_pipe >/dev/null 2>&1 && pass "种子数据已插入" || fail "种子插入"
step "D4-2 回补 2026-10-15 汇总(应 account_rows=1/channel_rows=1)"
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"date":"2026-10-15"}' "$GW/ops/settle/aggregate")
echo "$R"
echo "$R" | grep -q '"account_rows":1' && echo "$R" | grep -q '"channel_rows":1' \
&& pass "落账 1/1" || fail "落账行数"
step "D4-3 生成下家账单(时间线价 2×0.10=0.2)"
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"period":"2026-10"}' "$GW/settle/customer/generate")
echo "$R"
echo "$R" | grep -q '"generated":1' && pass "生成 1 张" || fail "生成"
R=$(curl -s -m 5 -H "$AUTH" "$GW/settle/customer?period=2026-10")
echo "$R"
BID=$(echo "$R" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
echo "$R" | grep -q '"amount":0.2' \
&& pass "时间线价金额 0.2(bill id=$BID)" || fail "金额 0.2"
step "D4-4 配阶梯(0 档 0.05)重算 → 整月覆写 0.1(Q2)"
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"owner_type":"account","owner_id":1,"tier_from":0,"price":0.05}' "$GW/settle/ladder")
echo "$R"
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"period":"2026-10"}' "$GW/settle/customer/generate")
echo "$R"
echo "$R" | grep -q '"recalculated":1' && pass "重算 1 张" || fail "重算"
curl -s -m 5 -H "$AUTH" "$GW/settle/customer?period=2026-10" | grep -q '"amount":0.1' \
&& pass "阶梯覆写金额 0.1" || fail "覆写金额"
step "D4-5 确认→结账(SettleRecord direction=1/amount=0.1)"
curl -s -m 5 -X POST -H "$AUTH" "$GW/settle/customer/$BID/confirm" >/dev/null
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"pay_type":"bank"}' "$GW/settle/customer/$BID/settle")
echo "$R"
echo "$R" | grep -q '"direction":1' && echo "$R" | grep -q '"amount":0.1' \
&& pass "结账 SettleRecord 0.1" || fail "结账"
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"period":"2026-10"}' "$GW/settle/customer/generate")
echo "$R" | grep -q '"skipped":1' && pass "已结拒重算 skipped=1" || fail "终态幂等"
step "D4-6 上家出账(US202610-9101,count=2,amount=2×0.05=0.1)"
R=$(curl -s -m 5 -X POST -H "$AUTH" -H 'Content-Type: application/json' \
-d '{"period":"2026-10"}' "$GW/settle/upstream/generate")
echo "$R"
R=$(curl -s -m 5 -H "$AUTH" "$GW/settle/upstream?period=2026-10")
echo "$R"
echo "$R" | grep -q '"success_count":2' && echo "$R" | grep -q '"amount":0.1' \
&& pass "上家账单 2/0.1" || fail "上家账单"
step "D4-7 导出下家账单 XLSX(PK 魔数,>1000 字节)"
curl -s -m 10 -H "$AUTH" -o /tmp/s2-walk-bill.xlsx "$GW/settle/customer/$BID/export"
SZ=$(wc -c < /tmp/s2-walk-bill.xlsx 2>/dev/null || echo 0)
head -c 2 /tmp/s2-walk-bill.xlsx | grep -q 'PK' && [ "$SZ" -gt 1000 ] \
&& pass "真 XLSX ${SZ}B" || fail "导出($SZ B)"
# ---------- D5 走查:利润分析+三方对账 ----------
step "D5-1 用户利润(阶梯不进分析:revenue=0.2 时间线价 vs 账单 0.1=口径差异)"
R=$(curl -s -m 5 -H "$AUTH" "$GW/stats/profit/users?date_from=2026-10-01&date_to=2026-10-31")
echo "$R"
echo "$R" | grep -q '"revenue":0.2' && echo "$R" | grep -q '"cost":0.1' \
&& echo "$R" | grep -q '"profit":0.1' && pass "rev0.2/cost0.1/profit0.1" || fail "用户利润"
step "D5-2 通道利润(cost=2×0.05;revenue 分摊=全平台收入×2/2)"
R=$(curl -s -m 5 -H "$AUTH" "$GW/stats/profit/channels?date_from=2026-10-01&date_to=2026-10-31")
echo "$R"
echo "$R" | grep -q '"channel_id":9101' && echo "$R" | grep -q '"profit":0.1' \
&& pass "通道利润 0.1" || fail "通道利润"
step "D5-3 customer 访问利润端点应 403"
R=$(curl -s -m 5 -H "Authorization: Bearer $TOKC" "$GW/stats/profit/users")
echo "$R"
echo "$R" | grep -q 'ISC:403' && pass "customer 403" || fail "利润权限门"
step "D5-4 settle-check 三方对账(all_zero=true,退出码 0)"
(cd "$ROOT" && go build -o /tmp/settle-check-walk ./cmd/reconcile/settle-check) \
&& pass "settle-check 已构建" || fail "构建 settle-check"
(cd "$ROOT" && /tmp/settle-check-walk -period 2026-10 2>/dev/null; echo "EXIT=$?") | tee /tmp/s2-walk-reconcile.log
grep -q 'all_zero=true' /tmp/s2-walk-reconcile.log && grep -q 'EXIT=0' /tmp/s2-walk-reconcile.log \
&& pass "三方对账 diff=0(S2 出口判据)" || fail "三方对账"
# ---------- 清理还原 ----------
step "清理(明细/汇总行/订单/流水还原;审计行保留为走查证据)"
# printf 双引号=外层展开 $ON/$B0(原 -e 单引号写法 $B0 内层展开为空致 SQL 语法错,
# 2026-11-15 D5 收官全脚本实跑暴露并修复);空值兜底防半程重跑。
printf '%s\n' \
"DELETE FROM message_detail WHERE id IN (7302401110000000001,7302401110000000002);" \
"DELETE FROM billing_consume_log WHERE stat_date=CURDATE();" \
"DELETE FROM stats_channel_daily WHERE stat_date=CURDATE();" \
"DELETE FROM billing_order WHERE order_no='${ON:-}';" \
"DELETE FROM account_balance_log WHERE ref_id='${ON:-}';" \
"UPDATE account_user SET balance=${B0:-0} WHERE id=1;" \
| mysql_pipe >/dev/null 2>&1 \
&& pass "数据已还原" || fail "清理"
step "清理 D4/D5(2026-10 走查数据全量还原)"
printf '%s\n' \
"DELETE FROM message_detail WHERE id IN (7302411150000000001,7302411150000000002);" \
"DELETE FROM billing_consume_log WHERE stat_date='2026-10-15';" \
"DELETE FROM stats_channel_daily WHERE stat_date='2026-10-15';" \
"DELETE FROM account_price WHERE account_id=1 AND effective_at='2026-10-01';" \
"DELETE FROM channel_price WHERE channel_id=9101;" \
"DELETE FROM settle_ladder WHERE owner_type='account' AND owner_id=1 AND tier_from=0;" \
"DELETE FROM settle_customer WHERE period='2026-10';" \
"DELETE FROM settle_upstream WHERE period='2026-10';" \
"DELETE FROM settle_record WHERE settle_no IN ('SC202610-1','US202610-9101');" \
"DELETE FROM channel_channel WHERE id=9101 AND code='S2WALK';" \
"DELETE FROM channel_upstream WHERE id=9101 AND name='s2-walk-up';" \
| mysql_pipe >/dev/null 2>&1 && pass "D4/D5 数据已还原" || fail "D4/D5 清理"
echo
echo "===== 走查完成:以上 [PASS] 全绿即 S2 全域(D2~D5)用户走查通过;任一 [FAIL] 请连同整份日志交回 ====="