修复主账号问题
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m6s

This commit is contained in:
2026-04-17 18:07:29 +08:00
parent 0d2baabcf1
commit 1033d7666b
3 changed files with 28 additions and 6 deletions

View File

@@ -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

View File

@@ -0,0 +1,6 @@
-- 回滚:将所有代理账号的 is_primary 重置为 false
-- 注意:无法精确还原哪些记录是迁移设置的,此操作会清除所有代理账号的主账号标记
UPDATE tb_account
SET is_primary = false
WHERE user_type = 3
AND deleted_at IS NULL;

View File

@@ -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;