All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m46s
- 创建角色时检查角色名是否已存在 - 更新角色时检查角色名是否与其他角色重复 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
120 lines
3.1 KiB
Go
120 lines
3.1 KiB
Go
package postgres
|
||
|
||
import (
|
||
"context"
|
||
|
||
"gorm.io/gorm"
|
||
|
||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||
)
|
||
|
||
// RoleStore 角色数据访问层
|
||
type RoleStore struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
// NewRoleStore 创建角色 Store
|
||
func NewRoleStore(db *gorm.DB) *RoleStore {
|
||
return &RoleStore{db: db}
|
||
}
|
||
|
||
// Create 创建角色
|
||
func (s *RoleStore) Create(ctx context.Context, role *model.Role) error {
|
||
return s.db.WithContext(ctx).Create(role).Error
|
||
}
|
||
|
||
// GetByID 根据 ID 获取角色
|
||
func (s *RoleStore) GetByID(ctx context.Context, id uint) (*model.Role, error) {
|
||
var role model.Role
|
||
if err := s.db.WithContext(ctx).First(&role, id).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return &role, nil
|
||
}
|
||
|
||
// GetByName 根据名称获取角色
|
||
func (s *RoleStore) GetByName(ctx context.Context, name string) (*model.Role, error) {
|
||
var role model.Role
|
||
if err := s.db.WithContext(ctx).Where("role_name = ?", name).First(&role).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return &role, nil
|
||
}
|
||
|
||
// ExistsByName 检查角色名是否已存在
|
||
// excludeID: 排除的角色 ID(用于更新时排除自身),传 0 表示不排除
|
||
func (s *RoleStore) ExistsByName(ctx context.Context, name string, excludeID uint) (bool, error) {
|
||
var count int64
|
||
query := s.db.WithContext(ctx).Model(&model.Role{}).Where("role_name = ?", name)
|
||
if excludeID > 0 {
|
||
query = query.Where("id != ?", excludeID)
|
||
}
|
||
if err := query.Count(&count).Error; err != nil {
|
||
return false, err
|
||
}
|
||
return count > 0, nil
|
||
}
|
||
|
||
// Update 更新角色
|
||
func (s *RoleStore) Update(ctx context.Context, role *model.Role) error {
|
||
return s.db.WithContext(ctx).Save(role).Error
|
||
}
|
||
|
||
// Delete 软删除角色
|
||
func (s *RoleStore) Delete(ctx context.Context, id uint) error {
|
||
return s.db.WithContext(ctx).Delete(&model.Role{}, id).Error
|
||
}
|
||
|
||
// List 查询角色列表
|
||
func (s *RoleStore) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Role, int64, error) {
|
||
var roles []*model.Role
|
||
var total int64
|
||
|
||
query := s.db.WithContext(ctx).Model(&model.Role{})
|
||
|
||
// 应用过滤条件
|
||
if name, ok := filters["role_name"].(string); ok && name != "" {
|
||
query = query.Where("role_name LIKE ?", "%"+name+"%")
|
||
}
|
||
if roleType, ok := filters["role_type"].(int); ok {
|
||
query = query.Where("role_type = ?", roleType)
|
||
}
|
||
if status, ok := filters["status"].(int); ok {
|
||
query = query.Where("status = ?", status)
|
||
}
|
||
|
||
// 计算总数
|
||
if err := query.Count(&total).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
// 分页
|
||
if opts == nil {
|
||
opts = store.DefaultQueryOptions()
|
||
}
|
||
offset := (opts.Page - 1) * opts.PageSize
|
||
query = query.Offset(offset).Limit(opts.PageSize)
|
||
|
||
// 排序
|
||
if opts.OrderBy != "" {
|
||
query = query.Order(opts.OrderBy)
|
||
}
|
||
|
||
// 执行查询
|
||
if err := query.Find(&roles).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
|
||
return roles, total, nil
|
||
}
|
||
|
||
// GetByIDs 根据 ID 列表获取角色
|
||
func (s *RoleStore) GetByIDs(ctx context.Context, ids []uint) ([]*model.Role, error) {
|
||
var roles []*model.Role
|
||
if err := s.db.WithContext(ctx).Where("id IN ?", ids).Find(&roles).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return roles, nil
|
||
}
|