From 1033d7666b91b4586fd6e9cd167709131ca4e936 Mon Sep 17 00:00:00 2001 From: huang Date: Fri, 17 Apr 2026 18:07:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=BB=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/shop/service.go | 13 +++++++------ migrations/000127_fix_primary_account.down.sql | 6 ++++++ migrations/000127_fix_primary_account.up.sql | 15 +++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 migrations/000127_fix_primary_account.down.sql create mode 100644 migrations/000127_fix_primary_account.up.sql diff --git a/internal/service/shop/service.go b/internal/service/shop/service.go index b97b8a8..7e2da4e 100644 --- a/internal/service/shop/service.go +++ b/internal/service/shop/service.go @@ -112,12 +112,13 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto. } account := &model.Account{ - Username: req.InitUsername, - Phone: req.InitPhone, - Password: string(hashedPassword), - UserType: constants.UserTypeAgent, - ShopID: &shop.ID, - Status: constants.StatusEnabled, + Username: req.InitUsername, + Phone: req.InitPhone, + Password: string(hashedPassword), + UserType: constants.UserTypeAgent, + ShopID: &shop.ID, + Status: constants.StatusEnabled, + IsPrimary: true, } account.Creator = currentUserID account.Updater = currentUserID diff --git a/migrations/000127_fix_primary_account.down.sql b/migrations/000127_fix_primary_account.down.sql new file mode 100644 index 0000000..a070ec7 --- /dev/null +++ b/migrations/000127_fix_primary_account.down.sql @@ -0,0 +1,6 @@ +-- 回滚:将所有代理账号的 is_primary 重置为 false +-- 注意:无法精确还原哪些记录是迁移设置的,此操作会清除所有代理账号的主账号标记 +UPDATE tb_account +SET is_primary = false +WHERE user_type = 3 + AND deleted_at IS NULL; diff --git a/migrations/000127_fix_primary_account.up.sql b/migrations/000127_fix_primary_account.up.sql new file mode 100644 index 0000000..0c8ab2f --- /dev/null +++ b/migrations/000127_fix_primary_account.up.sql @@ -0,0 +1,15 @@ +-- 修复历史数据:对每个店铺取 created_at 最早的代理账号(user_type = 3)设为主账号(is_primary = true) +-- 背景:shop/service.go 创建初始账号时未设置 is_primary = true,导致 fund-summary 接口查不到主账号信息 +-- 策略:DISTINCT ON (shop_id) 按 created_at ASC 取最早账号;AND is_primary = false 避免覆盖已正确的数据 +UPDATE tb_account a +SET is_primary = true +FROM ( + SELECT DISTINCT ON (shop_id) id + FROM tb_account + WHERE shop_id IS NOT NULL + AND user_type = 3 + AND deleted_at IS NULL + ORDER BY shop_id, created_at ASC +) earliest +WHERE a.id = earliest.id + AND a.is_primary = false;