add: 提交部分表设计
update: 1.所有model嵌入gorm公用model 2.所有model嵌入BaseModel
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/response"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
|
||||
@@ -1,26 +1,20 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Account 账号模型
|
||||
type Account struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
Username string `gorm:"uniqueIndex:idx_account_username,where:deleted_at IS NULL;not null;size:50" json:"username"`
|
||||
Phone string `gorm:"uniqueIndex:idx_account_phone,where:deleted_at IS NULL;not null;size:20" json:"phone"`
|
||||
Password string `gorm:"not null;size:255" json:"-"` // 不返回给客户端
|
||||
UserType int `gorm:"not null;index" json:"user_type"` // 1=root, 2=平台, 3=代理, 4=企业
|
||||
ShopID *uint `gorm:"index" json:"shop_id,omitempty"`
|
||||
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
|
||||
Status int `gorm:"not null;default:1" json:"status"` // 0=禁用, 1=启用
|
||||
Creator uint `gorm:"not null" json:"creator"`
|
||||
Updater uint `gorm:"not null" json:"updater"`
|
||||
CreatedAt time.Time `gorm:"not null" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"not null" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
gorm.Model
|
||||
BaseModel
|
||||
Username string `gorm:"uniqueIndex:idx_account_username,where:deleted_at IS NULL;not null;size:50" json:"username"`
|
||||
Phone string `gorm:"uniqueIndex:idx_account_phone,where:deleted_at IS NULL;not null;size:20" json:"phone"`
|
||||
Password string `gorm:"not null;size:255" json:"-"` // 不返回给客户端
|
||||
UserType int `gorm:"not null;index" json:"user_type"` // 1=root, 2=平台, 3=代理, 4=企业
|
||||
ShopID *uint `gorm:"index" json:"shop_id,omitempty"`
|
||||
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
|
||||
Status int `gorm:"not null;default:1" json:"status"` // 0=禁用, 1=启用
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// BaseModel 基础模型,包含通用字段
|
||||
type BaseModel struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` // 软删除
|
||||
Creator uint `gorm:"not null" json:"creator"`
|
||||
Updater uint `gorm:"not null" json:"updater"`
|
||||
}
|
||||
|
||||
func (b *BaseModel) BeforeCreate(tx *gorm.DB) error {
|
||||
if userID, ok := tx.Statement.Context.Value("current_user_id").(uint); ok {
|
||||
b.Creator = userID
|
||||
b.Updater = userID
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Permission 权限模型
|
||||
type Permission struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
PermName string `gorm:"not null;size:50" json:"perm_name"`
|
||||
PermCode string `gorm:"uniqueIndex:idx_permission_code,where:deleted_at IS NULL;not null;size:100" json:"perm_code"`
|
||||
PermType int `gorm:"not null;index" json:"perm_type"` // 1=菜单, 2=按钮
|
||||
URL string `gorm:"size:255" json:"url,omitempty"`
|
||||
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
|
||||
Sort int `gorm:"not null;default:0" json:"sort"`
|
||||
Status int `gorm:"not null;default:1" json:"status"`
|
||||
Creator uint `gorm:"not null" json:"creator"`
|
||||
Updater uint `gorm:"not null" json:"updater"`
|
||||
CreatedAt time.Time `gorm:"not null" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"not null" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
gorm.Model
|
||||
BaseModel
|
||||
|
||||
PermName string `gorm:"not null;size:50" json:"perm_name"`
|
||||
PermCode string `gorm:"uniqueIndex:idx_permission_code,where:deleted_at IS NULL;not null;size:100" json:"perm_code"`
|
||||
PermType int `gorm:"not null;index" json:"perm_type"` // 1=菜单, 2=按钮
|
||||
URL string `gorm:"size:255" json:"url,omitempty"`
|
||||
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
|
||||
Sort int `gorm:"not null;default:0" json:"sort"`
|
||||
Status int `gorm:"not null;default:1" json:"status"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Role 角色模型
|
||||
type Role struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
RoleName string `gorm:"not null;size:50" json:"role_name"`
|
||||
RoleDesc string `gorm:"size:255" json:"role_desc"`
|
||||
RoleType int `gorm:"not null;index" json:"role_type"` // 1=超级, 2=代理, 3=企业
|
||||
Status int `gorm:"not null;default:1" json:"status"`
|
||||
Creator uint `gorm:"not null" json:"creator"`
|
||||
Updater uint `gorm:"not null" json:"updater"`
|
||||
CreatedAt time.Time `gorm:"not null" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"not null" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
gorm.Model
|
||||
BaseModel
|
||||
|
||||
RoleName string `gorm:"not null;size:50" json:"role_name"`
|
||||
RoleDesc string `gorm:"size:255" json:"role_desc"`
|
||||
RoleType int `gorm:"not null;index" json:"role_type"` // 1=超级, 2=代理, 3=企业
|
||||
Status int `gorm:"not null;default:1" json:"status"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RolePermission 角色-权限关联模型
|
||||
type RolePermission struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
RoleID uint `gorm:"not null;index;uniqueIndex:idx_role_permission_unique,where:deleted_at IS NULL" json:"role_id"`
|
||||
PermID uint `gorm:"not null;index;uniqueIndex:idx_role_permission_unique,where:deleted_at IS NULL" json:"perm_id"`
|
||||
Status int `gorm:"not null;default:1" json:"status"`
|
||||
Creator uint `gorm:"not null" json:"creator"`
|
||||
Updater uint `gorm:"not null" json:"updater"`
|
||||
CreatedAt time.Time `gorm:"not null" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"not null" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
gorm.Model
|
||||
BaseModel
|
||||
|
||||
RoleID uint `gorm:"not null;index;uniqueIndex:idx_role_permission_unique,where:deleted_at IS NULL" json:"role_id"`
|
||||
PermID uint `gorm:"not null;index;uniqueIndex:idx_role_permission_unique,where:deleted_at IS NULL" json:"perm_id"`
|
||||
Status int `gorm:"not null;default:1" json:"status"`
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
|
||||
@@ -78,8 +78,6 @@ func (s *Service) Create(ctx context.Context, req *model.CreateAccountRequest) (
|
||||
ShopID: req.ShopID,
|
||||
ParentID: req.ParentID,
|
||||
Status: constants.StatusEnabled,
|
||||
Creator: currentUserID,
|
||||
Updater: currentUserID,
|
||||
}
|
||||
|
||||
if err := s.accountStore.Create(ctx, account); err != nil {
|
||||
|
||||
@@ -65,8 +65,6 @@ func (s *Service) Create(ctx context.Context, req *model.CreatePermissionRequest
|
||||
ParentID: req.ParentID,
|
||||
Sort: req.Sort,
|
||||
Status: constants.StatusEnabled,
|
||||
Creator: currentUserID,
|
||||
Updater: currentUserID,
|
||||
}
|
||||
|
||||
if err := s.permissionStore.Create(ctx, permission); err != nil {
|
||||
|
||||
@@ -43,8 +43,6 @@ func (s *Service) Create(ctx context.Context, req *model.CreateRoleRequest) (*mo
|
||||
RoleDesc: req.RoleDesc,
|
||||
RoleType: req.RoleType,
|
||||
Status: constants.StatusEnabled,
|
||||
Creator: currentUserID,
|
||||
Updater: currentUserID,
|
||||
}
|
||||
|
||||
if err := s.roleStore.Create(ctx, role); err != nil {
|
||||
@@ -187,11 +185,9 @@ func (s *Service) AssignPermissions(ctx context.Context, roleID uint, permIDs []
|
||||
}
|
||||
|
||||
rp := &model.RolePermission{
|
||||
RoleID: roleID,
|
||||
PermID: permID,
|
||||
Status: constants.StatusEnabled,
|
||||
Creator: currentUserID,
|
||||
Updater: currentUserID,
|
||||
RoleID: roleID,
|
||||
PermID: permID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
if err := s.rolePermissionStore.Create(ctx, rp); err != nil {
|
||||
return nil, fmt.Errorf("创建角色-权限关联失败: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user