pg_role_access_dao.go
1.7 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
59
60
61
62
63
64
65
66
67
package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"github.com/tiptok/gocomm/common"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/infrastructure/pg/transaction"
)
type RoleAccessDao struct {
transactionContext *transaction.TransactionContext
}
func (dao *RoleAccessDao) DeleteRoleAccess(roleId int64) error {
tx := dao.transactionContext.PgTx
q := tx.Model(new(models.RoleAccess))
q.Where("role_id=?", roleId)
_, err := q.Delete()
return err
}
func (dao *RoleAccessDao) GetRoleAccess(roleId ...int64) ([]int64, error) {
if len(roleId) == 0 {
return []int64{}, nil
}
tx := dao.transactionContext.PgDd
q := tx.Model(new(models.RoleAccess))
q.Column("access_id")
if len(roleId) == 1 {
q.Where("role_id=?", roleId[0])
} else {
q.Where("role_id in (?)", pg.In(roleId))
}
var accessIds []int64
err := q.Distinct().Select(&accessIds)
return accessIds, err
}
func (dao *RoleAccessDao) SaveRoleAccess(roleAccess []*domain.RoleAccess) error {
if len(roleAccess) == 0 {
return nil
}
tx := dao.transactionContext.PgTx
var modelsRoleAccess []*models.RoleAccess
for i := range roleAccess {
var item *models.RoleAccess
common.GobModelTransform(&item, roleAccess[i])
if item == nil {
continue
}
modelsRoleAccess = append(modelsRoleAccess, item)
}
_, err := tx.Model(&modelsRoleAccess).Insert()
return err
}
func NewRoleAccessDao(transactionContext *transaction.TransactionContext) (*RoleAccessDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &RoleAccessDao{
transactionContext: transactionContext,
}, nil
}
}