docs(constitution): 新增数据库设计原则(v2.4.0)

在项目宪章中新增第九条原则"数据库设计原则",明确禁止使用数据库外键约束和ORM关联标签。

主要变更:
- 新增原则IX:数据库设计原则(Database Design Principles)
- 强制要求:数据库表不得使用外键约束
- 强制要求:GORM模型不得使用ORM关联标签(foreignKey、hasMany等)
- 强制要求:表关系必须通过ID字段手动维护
- 强制要求:关联数据查询必须显式编写,避免ORM魔法
- 强制要求:时间字段由GORM处理,不使用数据库触发器

设计理念:
- 提升业务逻辑灵活性(无数据库约束限制)
- 优化高并发性能(无外键检查开销)
- 增强代码可读性(显式查询,无隐式预加载)
- 简化数据库架构和迁移流程
- 支持分布式和微服务场景

版本升级:2.3.0 → 2.4.0(MINOR)
This commit is contained in:
2025-11-13 13:40:19 +08:00
parent ea0c6a8b16
commit 984ccccc63
63 changed files with 12099 additions and 83 deletions

View File

@@ -0,0 +1,161 @@
package user
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
pkgErrors "github.com/break/junhong_cmp_fiber/pkg/errors"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
// Service 用户服务
type Service struct {
store *postgres.Store
logger *zap.Logger
}
// NewService 创建用户服务
func NewService(store *postgres.Store, logger *zap.Logger) *Service {
return &Service{
store: store,
logger: logger,
}
}
// CreateUser 创建用户
func (s *Service) CreateUser(ctx context.Context, req *model.CreateUserRequest) (*model.User, error) {
// 密码哈希
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
s.logger.Error("密码哈希失败", zap.Error(err))
return nil, pkgErrors.New(pkgErrors.CodeInternalError, "密码加密失败")
}
// 创建用户
user := &model.User{
Username: req.Username,
Email: req.Email,
Password: string(hashedPassword),
Status: constants.UserStatusActive,
}
if err := s.store.User.Create(ctx, user); err != nil {
s.logger.Error("创建用户失败",
zap.String("username", req.Username),
zap.Error(err))
return nil, pkgErrors.New(pkgErrors.CodeInternalError, "创建用户失败")
}
s.logger.Info("用户创建成功",
zap.Uint("user_id", user.ID),
zap.String("username", user.Username))
return user, nil
}
// GetUserByID 根据 ID 获取用户
func (s *Service) GetUserByID(ctx context.Context, id uint) (*model.User, error) {
user, err := s.store.User.GetByID(ctx, id)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, pkgErrors.New(pkgErrors.CodeNotFound, "用户不存在")
}
s.logger.Error("获取用户失败",
zap.Uint("user_id", id),
zap.Error(err))
return nil, pkgErrors.New(pkgErrors.CodeInternalError, "获取用户失败")
}
return user, nil
}
// UpdateUser 更新用户
func (s *Service) UpdateUser(ctx context.Context, id uint, req *model.UpdateUserRequest) (*model.User, error) {
// 查询用户
user, err := s.store.User.GetByID(ctx, id)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, pkgErrors.New(pkgErrors.CodeNotFound, "用户不存在")
}
s.logger.Error("查询用户失败",
zap.Uint("user_id", id),
zap.Error(err))
return nil, pkgErrors.New(pkgErrors.CodeInternalError, "查询用户失败")
}
// 更新字段
if req.Email != nil {
user.Email = *req.Email
}
if req.Status != nil {
user.Status = *req.Status
}
// 保存更新
if err := s.store.User.Update(ctx, user); err != nil {
s.logger.Error("更新用户失败",
zap.Uint("user_id", id),
zap.Error(err))
return nil, pkgErrors.New(pkgErrors.CodeInternalError, "更新用户失败")
}
s.logger.Info("用户更新成功",
zap.Uint("user_id", user.ID),
zap.String("username", user.Username))
return user, nil
}
// DeleteUser 删除用户(软删除)
func (s *Service) DeleteUser(ctx context.Context, id uint) error {
// 检查用户是否存在
_, err := s.store.User.GetByID(ctx, id)
if err != nil {
if err == gorm.ErrRecordNotFound {
return pkgErrors.New(pkgErrors.CodeNotFound, "用户不存在")
}
s.logger.Error("查询用户失败",
zap.Uint("user_id", id),
zap.Error(err))
return pkgErrors.New(pkgErrors.CodeInternalError, "查询用户失败")
}
// 软删除
if err := s.store.User.Delete(ctx, id); err != nil {
s.logger.Error("删除用户失败",
zap.Uint("user_id", id),
zap.Error(err))
return pkgErrors.New(pkgErrors.CodeInternalError, "删除用户失败")
}
s.logger.Info("用户删除成功", zap.Uint("user_id", id))
return nil
}
// ListUsers 分页获取用户列表
func (s *Service) ListUsers(ctx context.Context, page, pageSize int) ([]model.User, int64, error) {
// 参数验证
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = constants.DefaultPageSize
}
if pageSize > constants.MaxPageSize {
pageSize = constants.MaxPageSize
}
users, total, err := s.store.User.List(ctx, page, pageSize)
if err != nil {
s.logger.Error("获取用户列表失败",
zap.Int("page", page),
zap.Int("page_size", pageSize),
zap.Error(err))
return nil, 0, pkgErrors.New(pkgErrors.CodeInternalError, "获取用户列表失败")
}
return users, total, nil
}