|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role/adapter"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type RoleUserRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func NewRoleUserRepository(transactionContext *pgTransaction.TransactionContext) *RoleUserRepository {
|
|
|
return &RoleUserRepository{transactionContext: transactionContext}
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) TransformToDomain(m *models.RoleUser) domain.RoleUser {
|
|
|
return domain.RoleUser{
|
|
|
Id: m.Id,
|
|
|
RoleId: m.RoleId,
|
|
|
UserId: m.UserId,
|
|
|
CompanyId: m.CompanyId,
|
|
|
CreatedAt: m.CreatedAt,
|
|
|
UpdatedAt: m.UpdatedAt,
|
|
|
DeletedAt: m.DeletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) TransformToModel(d *domain.RoleUser) models.RoleUser {
|
|
|
return models.RoleUser{
|
|
|
Id: d.Id,
|
|
|
RoleId: d.RoleId,
|
|
|
UserId: d.UserId,
|
|
|
CompanyId: d.CompanyId,
|
|
|
CreatedAt: d.CreatedAt,
|
|
|
UpdatedAt: d.UpdatedAt,
|
|
|
DeletedAt: d.DeletedAt,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) nextIdentify() (int64, error) {
|
|
|
return utils.NewSnowflakeId()
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) Insert(d *domain.RoleUser) (*domain.RoleUser, error) {
|
|
|
var isCreate = d.Id == 0
|
|
|
if isCreate {
|
|
|
id, err := repo.nextIdentify()
|
|
|
if err != nil {
|
|
|
return d, err
|
|
|
}
|
|
|
d.Id = id
|
|
|
d.CreatedAt = time.Now()
|
|
|
d.UpdatedAt = d.CreatedAt
|
|
|
} else {
|
|
|
d.UpdatedAt = time.Now()
|
|
|
}
|
|
|
m := repo.TransformToModel(d)
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var err error
|
|
|
if isCreate {
|
|
|
_, err = tx.Model(&m).Returning("id").Insert()
|
|
|
} else {
|
|
|
_, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
|
|
|
}
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
d.Id = m.Id
|
|
|
return d, nil
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) Remove(d *domain.RoleUser) (*domain.RoleUser, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
nowTime := time.Now()
|
|
|
m := repo.TransformToModel(d)
|
|
|
m.DeletedAt = &nowTime
|
|
|
// 真删
|
|
|
if _, err := tx.Model(&m).WherePK().Delete(); err != nil {
|
|
|
return d, err
|
|
|
}
|
|
|
return d, nil
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) FindOne(queryOptions map[string]interface{}) (*domain.RoleUser, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.RoleUser)
|
|
|
query := tx.Model(m)
|
|
|
query.Where("deleted_at isnull")
|
|
|
if id, ok := queryOptions["id"]; ok {
|
|
|
query.Where("id=?", id)
|
|
|
}
|
|
|
if err := query.First(); err != nil {
|
|
|
if errors.Is(err, pg.ErrNoRows) {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
u := repo.TransformToDomain(m)
|
|
|
return &u, nil
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.RoleUser, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var m []*models.RoleUser
|
|
|
query := tx.Model(&m).Where("deleted_at isnull").Limit(20)
|
|
|
|
|
|
if roleId, ok := queryOptions["roleId"]; ok {
|
|
|
query.Where("role_id = ?", roleId)
|
|
|
}
|
|
|
|
|
|
if companyId, ok := queryOptions["companyId"]; ok {
|
|
|
query.Where("company_id = ?", companyId)
|
|
|
}
|
|
|
|
|
|
if userId, ok := queryOptions["userId"]; ok {
|
|
|
query.Where("user_id = ?", userId)
|
|
|
}
|
|
|
|
|
|
if userIds, ok := queryOptions["userIds"]; ok {
|
|
|
query.Where("user_id in (?)", userIds)
|
|
|
}
|
|
|
|
|
|
if v, ok := queryOptions["limit"].(int); ok {
|
|
|
query.Limit(v)
|
|
|
}
|
|
|
if v, ok := queryOptions["offset"].(int); ok {
|
|
|
query.Offset(v)
|
|
|
}
|
|
|
|
|
|
count, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
var arrays []*domain.RoleUser
|
|
|
for _, v := range m {
|
|
|
d := repo.TransformToDomain(v)
|
|
|
arrays = append(arrays, &d)
|
|
|
}
|
|
|
return int64(count), arrays, nil
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) Count(queryOptions map[string]interface{}) (int64, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
m := new(models.RoleUser)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
|
|
|
query.Where("deleted_at isnull")
|
|
|
|
|
|
if id, ok := queryOptions["id"]; ok {
|
|
|
query.Where("id = ?", id)
|
|
|
}
|
|
|
|
|
|
if notId, ok := queryOptions["notId"]; ok {
|
|
|
query.Where("id != ?", notId)
|
|
|
}
|
|
|
|
|
|
if name, ok := queryOptions["name"]; ok {
|
|
|
query.Where("name = ?", name)
|
|
|
}
|
|
|
|
|
|
if companyId, ok := queryOptions["companyId"]; ok {
|
|
|
query.Where("company_id = ?", companyId)
|
|
|
}
|
|
|
|
|
|
count, err := query.Count()
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
return int64(count), nil
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) BatchDeleteById(ids []int64) error {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
_, err := tx.Model(&models.RoleUser{}).Where("id IN (?)", pg.In(ids)).Delete()
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
func (repo *RoleUserRepository) FindAllContainUser(pageSize int, pageNumber int, companyId int64, roleId int64) ([]*adapter.RoleContainUser, error) {
|
|
|
limit := pageSize
|
|
|
offset := limit * (pageNumber - 1)
|
|
|
if offset < 0 {
|
|
|
offset = 0
|
|
|
}
|
|
|
dataSql := ` SELECT
|
|
|
"role_user".role_id,
|
|
|
"role_user".user_id,
|
|
|
"user".name as user_name,
|
|
|
"user".email as user_email,
|
|
|
`
|
|
|
whereFrom := `
|
|
|
FROM "role_user"
|
|
|
LEFT JOIN "user" ON "user".id = "role_user".user_id
|
|
|
WHERE 1=1 AND "role_user".deleted_at ISNULL `
|
|
|
|
|
|
var param []interface{}
|
|
|
if companyId > 0 {
|
|
|
param = append(param, companyId)
|
|
|
whereFrom += ` AND role_user.company_id=? `
|
|
|
}
|
|
|
if roleId >= 0 {
|
|
|
param = append(param, roleId)
|
|
|
whereFrom += ` AND role_user.role_id =? `
|
|
|
}
|
|
|
dataSql += whereFrom
|
|
|
dataSql = fmt.Sprintf("%s limit %d offset %d", dataSql, limit, offset)
|
|
|
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
var dataList = make([]*adapter.RoleContainUser, 0)
|
|
|
_, err := tx.Query(&dataList, dataSql, param...)
|
|
|
return dataList, err
|
|
|
} |
...
|
...
|
|