pg_admin_permission.go
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package repository
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type AdminPermissionRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.AdminPermissionRepository = (*AdminPermissionRepository)(nil)
)
func NewAdminPermissionRepository(transactionContext *transaction.TransactionContext) (*AdminPermissionRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &AdminPermissionRepository{transactionContext: transactionContext}, nil
}
func (reponsitory AdminPermissionRepository) transformPgModelToDomainModel(permissionModel *models.AdminPermission) (domain.AdminPermission, error) {
result := domain.AdminPermission{
Id: permissionModel.Id,
ParentId: permissionModel.ParentId,
Name: permissionModel.Name,
Icon: permissionModel.Icon,
Code: permissionModel.Code,
}
return result, nil
}
func (reponsitory AdminPermissionRepository) Find(queryOptions domain.AdminPermissionFindQuery) ([]domain.AdminPermission, error) {
db := reponsitory.transactionContext.PgDd
permissionModels := make([]models.AdminPermission, 0)
query := db.Model(&permissionModels)
if queryOptions.ParentId != nil {
query = query.Where("parent_id=?", queryOptions.ParentId)
}
if len(queryOptions.NotCode) > 0 {
query = query.WhereIn("code not in (?) ", queryOptions.NotCode)
}
if len(queryOptions.IdsIn) > 0 {
query = query.WhereIn(" id in (?) ", queryOptions.IdsIn)
}
if err := query.Select(); err != nil {
return nil, err
}
var result []domain.AdminPermission
for i := range permissionModels {
v, _ := reponsitory.transformPgModelToDomainModel(&permissionModels[i])
result = append(result, v)
}
return result, nil
}