实现多商户履约平台基础
This commit is contained in:
@@ -10,19 +10,21 @@ import (
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
db *gorm.DB
|
||||
db *gorm.DB
|
||||
tenant *TenantService
|
||||
}
|
||||
|
||||
func NewUserService(db *gorm.DB) *UserService {
|
||||
return &UserService{db: db}
|
||||
func NewUserService(db *gorm.DB, tenant *TenantService) *UserService {
|
||||
return &UserService{db: db, tenant: tenant}
|
||||
}
|
||||
|
||||
type UserListQuery struct {
|
||||
Page int
|
||||
Size int
|
||||
Keyword string
|
||||
Role string
|
||||
Status *int
|
||||
MerchantID uint
|
||||
Page int
|
||||
Size int
|
||||
Keyword string
|
||||
Role string
|
||||
Status *int
|
||||
}
|
||||
|
||||
func (s *UserService) List(q UserListQuery) ([]model.User, int64, error) {
|
||||
@@ -33,26 +35,30 @@ func (s *UserService) List(q UserListQuery) ([]model.User, int64, error) {
|
||||
q.Size = 20
|
||||
}
|
||||
tx := s.db.Model(&model.User{})
|
||||
if q.MerchantID != 0 {
|
||||
tx = tx.Joins("JOIN merchant_members ON merchant_members.user_id = users.id").
|
||||
Where("merchant_members.merchant_id = ?", q.MerchantID)
|
||||
}
|
||||
if q.Keyword != "" {
|
||||
like := "%" + q.Keyword + "%"
|
||||
tx = tx.Where("username LIKE ? OR nickname LIKE ?", like, like)
|
||||
tx = tx.Where("users.username LIKE ? OR users.nickname LIKE ?", like, like)
|
||||
}
|
||||
if q.Role != "" {
|
||||
tx = tx.Where("role = ?", q.Role)
|
||||
tx = tx.Where("users.role = ?", q.Role)
|
||||
}
|
||||
if q.Status != nil {
|
||||
tx = tx.Where("status = ?", *q.Status)
|
||||
tx = tx.Where("users.status = ?", *q.Status)
|
||||
}
|
||||
var total int64
|
||||
if err := tx.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var list []model.User
|
||||
err := tx.Order("id DESC").Offset((q.Page - 1) * q.Size).Limit(q.Size).Find(&list).Error
|
||||
err := tx.Order("users.id DESC").Offset((q.Page - 1) * q.Size).Limit(q.Size).Find(&list).Error
|
||||
return list, total, err
|
||||
}
|
||||
|
||||
func (s *UserService) Create(username, password, nickname, role string, parentID *uint) (*model.User, error) {
|
||||
func (s *UserService) Create(username, password, nickname, role string, parentID *uint, merchantID uint) (*model.User, error) {
|
||||
var count int64
|
||||
s.db.Model(&model.User{}).Where("username = ?", username).Count(&count)
|
||||
if count > 0 {
|
||||
@@ -77,7 +83,29 @@ func (s *UserService) Create(username, password, nickname, role string, parentID
|
||||
if user.Nickname == "" {
|
||||
user.Nickname = username
|
||||
}
|
||||
if err := s.db.Create(user).Error; err != nil {
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if s.tenant != nil && merchantID != 0 {
|
||||
memberRole := model.MemberRoleOperator
|
||||
if role == model.RoleAdmin {
|
||||
memberRole = model.MemberRoleOwner
|
||||
}
|
||||
member := model.MerchantMember{
|
||||
MerchantID: merchantID,
|
||||
UserID: user.ID,
|
||||
Role: memberRole,
|
||||
Status: user.Status,
|
||||
IsDefault: role == model.RoleAdmin,
|
||||
}
|
||||
if err := tx.Create(&member).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
|
||||
Reference in New Issue
Block a user