AML/docker-compose-local-dev/seed-agent-plans.sql

76 lines
4.2 KiB
Transact-SQL
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- 本地测试数据给「可选推薦人」配可售方案AMLPortal_AgentUserPlans
--
-- 背景 —— 恢复的 AML-local-dev.bak 里存在一处数据断层:
-- · AMLPortal_AgentUserSettings 决定下单页「推薦人」下拉里出现谁(本地为 agent_A/U7D191·HKG、
-- agentTest3/C87654·HKG、agentTest2/D6Y6U0·JPN
-- · AMLPortal_AgentUserPlans 决定该推薦人能卖哪些方案(后端 GetPlanList 按 AgentCode 裁剪)。
-- 两张表零交集:有关联的只有 SalesAdmin / XiaoJunlong且它们关联的方案全部 IsDeleted=1
-- (上一代旧方案,无 PlanDetail。结果是**任选一个推薦人,任何行業都买不到东西**
-- BFF 严格模式返回 agentNoPlans前端红字阻断下单
--
-- 本脚本按「不同推薦人可售范围不同」的业务语义补齐关联,供本地验证推薦人过滤链路:
-- agent_A (U7D191, HKG) → Standard 12/24HKG+ Add Login + jQuota 50/200
-- agentTest3(C87654, HKG) → Starter 12 / NGO 12 / CPA 24HKG+ Add Login + jQuota 50
-- agentTest2(D6Y6U0, JPN) → Standard 12JPN+ Add Login + jQuota 50
--
-- 幂等:先软删该 agent 已有关联,再插入。仅用于本地开发库,勿用于 dev/stag/prod。
--
-- 执行:
-- docker exec -i aml-mssql /opt/mssql-tools18/bin/sqlcmd \
-- -S localhost -U sa -P "Aml@Local2026" -C -d AbpAML -i /dev/stdin < seed-agent-plans.sql
SET NOCOUNT ON;
DECLARE @agentA uniqueidentifier = (SELECT Id FROM AbpUsers WHERE UserCode = 'U7D191');
DECLARE @agentT3 uniqueidentifier = (SELECT Id FROM AbpUsers WHERE UserCode = 'C87654');
DECLARE @agentT2 uniqueidentifier = (SELECT Id FROM AbpUsers WHERE UserCode = 'D6Y6U0');
IF @agentA IS NULL OR @agentT3 IS NULL OR @agentT2 IS NULL
BEGIN
RAISERROR('找不到预期的推薦人U7D191 / C87654 / D6Y6U0请确认已恢复 AML-local-dev.bak', 16, 1);
RETURN;
END
-- 目标关联:(AgentUserId, PlanId)。PlanDetailId 取该方案当前有效的第一条明细。
DECLARE @want TABLE (AgentUserId uniqueidentifier, PlanId uniqueidentifier);
INSERT INTO @want (AgentUserId, PlanId) VALUES
-- agent_A · HKG只卖 Standard 系
(@agentA, '00000000-0000-0000-0051-000000000001'), -- Standard 12 Months (HKG)
(@agentA, '00000000-0000-0000-0051-000000000002'), -- Standard 24 Months (HKG)
(@agentA, '00000000-0000-0000-0051-000000000013'), -- Add Login (HKG)
(@agentA, '00000000-0000-0000-0051-000000000007'), -- jQuota 50 (HKG)
(@agentA, '00000000-0000-0000-0051-000000000008'), -- jQuota 200 (HKG)
-- agentTest3 · HKG只卖入门 / NGO / CPA 系(与 agent_A 完全不重叠的基础方案)
(@agentT3, '00000000-0000-0000-0051-000000000003'), -- Starter 12 Months (HKG)
(@agentT3, '00000000-0000-0000-0051-000000000005'), -- NGO 12 Months (HKG)
(@agentT3, '00000000-0000-0000-0051-000000000006'), -- CPA 24 Months (HKG)
(@agentT3, '00000000-0000-0000-0051-000000000013'), -- Add Login (HKG)
(@agentT3, '00000000-0000-0000-0051-000000000007'), -- jQuota 50 (HKG)
-- agentTest2 · JPN只卖 Standard 12
(@agentT2, '00000000-0000-0000-0051-000000000017'), -- Standard 12 Months (JPN)
(@agentT2, '00000000-0000-0000-0051-000000000026'), -- Add Login (JPN)
(@agentT2, '00000000-0000-0000-0051-000000000021'); -- jQuota 50 (JPN)
-- 幂等:先清掉这三个 agent 的现有关联(软删,保留审计痕迹)
UPDATE AMLPortal_AgentUserPlans
SET IsDeleted = 1, DeletionTime = SYSUTCDATETIME()
WHERE IsDeleted = 0 AND AgentUserId IN (@agentA, @agentT3, @agentT2);
INSERT INTO AMLPortal_AgentUserPlans (Id, AgentUserId, PlanId, PlanDetailId, CreationTime, IsDeleted)
SELECT NEWID(), w.AgentUserId, w.PlanId, d.Id, SYSUTCDATETIME(), 0
FROM @want w
CROSS APPLY (
SELECT TOP 1 pd.Id
FROM AMLPortal_PlanDetails pd
WHERE pd.PlanId = w.PlanId AND pd.IsDeleted = 0
ORDER BY pd.CreationTime
) d;
SELECT u.UserCode, u.UserName, p.NameEN, p.Tag1Code, p.CountryCode
FROM AMLPortal_AgentUserPlans a
JOIN AbpUsers u ON u.Id = a.AgentUserId
JOIN AMLPortal_Plans p ON p.Id = a.PlanId
WHERE a.IsDeleted = 0 AND a.AgentUserId IN (@agentA, @agentT3, @agentT2)
ORDER BY u.UserCode, p.Tag1Code, p.NameEN;