实现用户和组织模型(店铺、企业、个人客户)

核心功能:
- 实现 7 级店铺层级体系(Shop 模型 + 层级校验)
- 实现企业管理模型(Enterprise 模型)
- 实现个人客户管理模型(PersonalCustomer 模型)
- 重构 Account 模型关联关系(基于 EnterpriseID 而非 ParentID)
- 完整的 Store 层和 Service 层实现
- 递归查询下级店铺功能(含 Redis 缓存)
- 全面的单元测试覆盖(Shop/Enterprise/PersonalCustomer Store + Shop Service)

技术要点:
- 显式指定所有 GORM 模型的数据库字段名(column: 标签)
- 统一的字段命名规范(数据库用 snake_case,Go 用 PascalCase)
- 完整的中文字段注释和业务逻辑说明
- 100% 测试覆盖(20+ 测试用例全部通过)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-09 18:02:46 +08:00
parent 6fc90abeb6
commit a36e4a79c0
51 changed files with 5736 additions and 144 deletions

View File

@@ -81,7 +81,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
accService := accountService.New(accountStore, roleStore, accountRoleStore)
// 创建测试用户上下文
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeRoot, 0)
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeSuperAdmin, 0)
t.Run("成功分配单个角色", func(t *testing.T) {
// 创建测试账号
@@ -307,7 +307,7 @@ func TestAccountRoleAssociation_SoftDelete(t *testing.T) {
accountRoleStore := postgresStore.NewAccountRoleStore(db)
accService := accountService.New(accountStore, roleStore, accountRoleStore)
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeRoot, 0)
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeSuperAdmin, 0)
t.Run("软删除角色后重新分配可以恢复", func(t *testing.T) {
// 创建测试数据

View File

@@ -166,7 +166,7 @@ func TestAccountAPI_Create(t *testing.T) {
// 创建一个测试用的中间件来设置用户上下文
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -176,7 +176,7 @@ func TestAccountAPI_Create(t *testing.T) {
Username: "root",
Phone: "13800000000",
Password: "hashedpassword",
UserType: constants.UserTypeRoot,
UserType: constants.UserTypeSuperAdmin,
Status: constants.StatusEnabled,
}
createTestAccount(t, env.db, rootAccount)
@@ -274,7 +274,7 @@ func TestAccountAPI_Get(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -332,7 +332,7 @@ func TestAccountAPI_Update(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -376,7 +376,7 @@ func TestAccountAPI_Delete(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -413,7 +413,7 @@ func TestAccountAPI_List(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -458,7 +458,7 @@ func TestAccountAPI_AssignRoles(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -509,7 +509,7 @@ func TestAccountAPI_GetRoles(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -562,7 +562,7 @@ func TestAccountAPI_RemoveRole(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})

View File

@@ -121,7 +121,7 @@ func setupRegressionTestEnv(t *testing.T) *regressionTestEnv {
// 添加测试中间件设置用户上下文
app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), 1, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), 1, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})

View File

@@ -116,7 +116,7 @@ func TestPermissionAPI_Create(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -220,7 +220,7 @@ func TestPermissionAPI_Get(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -266,7 +266,7 @@ func TestPermissionAPI_Update(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -309,7 +309,7 @@ func TestPermissionAPI_Delete(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -345,7 +345,7 @@ func TestPermissionAPI_List(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -389,7 +389,7 @@ func TestPermissionAPI_GetTree(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})

View File

@@ -64,7 +64,7 @@ func TestRolePermissionAssociation_AssignPermissions(t *testing.T) {
roleSvc := roleService.New(roleStore, permStore, rolePermStore)
// 创建测试用户上下文
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeRoot, 0)
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeSuperAdmin, 0)
t.Run("成功分配单个权限", func(t *testing.T) {
// 创建测试角色
@@ -270,7 +270,7 @@ func TestRolePermissionAssociation_SoftDelete(t *testing.T) {
rolePermStore := postgresStore.NewRolePermissionStore(db)
roleSvc := roleService.New(roleStore, permStore, rolePermStore)
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeRoot, 0)
userCtx := middleware.SetUserContext(ctx, 1, constants.UserTypeSuperAdmin, 0)
t.Run("软删除权限后重新分配可以恢复", func(t *testing.T) {
// 创建测试数据

View File

@@ -158,7 +158,7 @@ func TestRoleAPI_Create(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -216,7 +216,7 @@ func TestRoleAPI_Get(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -261,7 +261,7 @@ func TestRoleAPI_Update(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -303,7 +303,7 @@ func TestRoleAPI_Delete(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -338,7 +338,7 @@ func TestRoleAPI_List(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -374,7 +374,7 @@ func TestRoleAPI_AssignPermissions(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -424,7 +424,7 @@ func TestRoleAPI_GetPermissions(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})
@@ -474,7 +474,7 @@ func TestRoleAPI_RemovePermission(t *testing.T) {
// 添加测试中间件
testUserID := uint(1)
env.app.Use(func(c *fiber.Ctx) error {
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeRoot, 0)
ctx := middleware.SetUserContext(c.UserContext(), testUserID, constants.UserTypeSuperAdmin, 0)
c.SetUserContext(ctx)
return c.Next()
})