功能:恢复商户账号分组查看
This commit is contained in:
@@ -27,6 +27,14 @@ type UserListQuery struct {
|
||||
Status *int
|
||||
}
|
||||
|
||||
// MerchantMemberGroup presents merchant accounts in their actual ownership
|
||||
// hierarchy: one merchant and all of its employee accounts.
|
||||
type MerchantMemberGroup struct {
|
||||
MerchantID uint `json:"merchant_id"`
|
||||
Merchant *model.Merchant `json:"merchant,omitempty"`
|
||||
Members []model.MerchantMember `json:"members"`
|
||||
}
|
||||
|
||||
func (s *UserService) List(q UserListQuery) ([]model.User, int64, error) {
|
||||
if q.Page < 1 {
|
||||
q.Page = 1
|
||||
@@ -123,6 +131,55 @@ func (s *UserService) ListPlatformAdmins(page, size int) ([]model.User, int64, e
|
||||
return s.List(UserListQuery{Page: page, Size: size, Role: model.RoleAdmin})
|
||||
}
|
||||
|
||||
// ListMerchantAccountGroups returns merchant employee accounts grouped by
|
||||
// merchant. Platform administrators are excluded from this independent view.
|
||||
func (s *UserService) ListMerchantAccountGroups(page, size int) ([]MerchantMemberGroup, int64, error) {
|
||||
page, size = normalizePage(page, size)
|
||||
tx := s.db.Model(&model.MerchantMember{}).
|
||||
Joins("JOIN users ON users.id = merchant_members.user_id AND users.deleted_at IS NULL").
|
||||
Where("users.role = ?", model.RoleMerchant)
|
||||
|
||||
var total int64
|
||||
if err := tx.Distinct("merchant_members.merchant_id").Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var merchantIDs []uint
|
||||
if err := tx.Distinct("merchant_members.merchant_id").
|
||||
Order("merchant_members.merchant_id ASC").
|
||||
Offset((page-1)*size).Limit(size).
|
||||
Pluck("merchant_members.merchant_id", &merchantIDs).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if len(merchantIDs) == 0 {
|
||||
return []MerchantMemberGroup{}, total, nil
|
||||
}
|
||||
|
||||
var members []model.MerchantMember
|
||||
if err := s.db.Model(&model.MerchantMember{}).
|
||||
Joins("JOIN users ON users.id = merchant_members.user_id AND users.deleted_at IS NULL").
|
||||
Where("users.role = ? AND merchant_members.merchant_id IN ?", model.RoleMerchant, merchantIDs).
|
||||
Preload("Merchant").Preload("User").
|
||||
Order("merchant_members.merchant_id ASC, merchant_members.id ASC").
|
||||
Find(&members).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
groups := make([]MerchantMemberGroup, 0, len(merchantIDs))
|
||||
byMerchantID := make(map[uint]*MerchantMemberGroup, len(merchantIDs))
|
||||
for _, merchantID := range merchantIDs {
|
||||
groups = append(groups, MerchantMemberGroup{MerchantID: merchantID, Members: []model.MerchantMember{}})
|
||||
byMerchantID[merchantID] = &groups[len(groups)-1]
|
||||
}
|
||||
for _, member := range members {
|
||||
group := byMerchantID[member.MerchantID]
|
||||
if group.Merchant == nil {
|
||||
group.Merchant = member.Merchant
|
||||
}
|
||||
group.Members = append(group.Members, member)
|
||||
}
|
||||
return groups, total, nil
|
||||
}
|
||||
|
||||
// CreatePlatformAdmin creates an account with platform-only privileges. It
|
||||
// deliberately does not assign the account to any merchant.
|
||||
func (s *UserService) CreatePlatformAdmin(username, password, nickname string) (*model.User, error) {
|
||||
|
||||
Reference in New Issue
Block a user